#767500 : fixed IN handling

This commit is contained in:
Timo Westkämper 2011-04-20 19:17:33 +00:00
parent faab5f2617
commit 56e5f0351c
3 changed files with 58 additions and 6 deletions

View File

@ -6,6 +6,7 @@
package com.mysema.query.jpa;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import com.mysema.query.sql.SQLSerializer;
@ -30,16 +31,33 @@ public final class HibernateSQLSerializer extends SQLSerializer{
@Override
public Void visit(Constant<?> expr, Void context) {
if (!getConstantToLabel().containsKey(expr.getConstant())) {
String constLabel = getConstantPrefix() + (getConstantToLabel().size() + 1);
getConstantToLabel().put(expr.getConstant(), constLabel);
append(":"+constLabel);
} else {
append(":"+getConstantToLabel().get(expr.getConstant()));
if (expr.getConstant() instanceof Collection<?>) {
append("(");
boolean first = true;
for (Object element : ((Collection<?>)expr.getConstant())) {
if (!first){
append(", ");
}
visitConstant(element);
first = false;
}
append(")");
}else {
visitConstant(expr.getConstant());
}
return null;
}
private void visitConstant(Object constant){
if (!getConstantToLabel().containsKey(constant)) {
String constLabel = getConstantPrefix() + (getConstantToLabel().size() + 1);
getConstantToLabel().put(constant, constLabel);
append(":"+constLabel);
} else {
append(":"+getConstantToLabel().get(constant));
}
}
@Override
public Void visit(Path<?> path, Void context) {
if (path.getMetadata().getParent() == null && !path.getType().equals(path.getClass())){

View File

@ -0,0 +1,29 @@
package com.mysema.query.jpa;
import static org.junit.Assert.assertEquals;
import org.junit.Ignore;
import org.junit.Test;
import com.mysema.query.DefaultQueryMetadata;
import com.mysema.query.JoinType;
import com.mysema.query.jpa.domain.sql.SAnimal;
import com.mysema.query.sql.MySQLTemplates;
public class HibernateSQLSerializerTest {
@Test
@Ignore
public void In() {
HibernateSQLSerializer serializer = new HibernateSQLSerializer(new MySQLTemplates());
DefaultQueryMetadata md = new DefaultQueryMetadata();
SAnimal cat = SAnimal.animal;
md.addJoin(JoinType.DEFAULT, cat);
md.addWhere(cat.name.in("X", "Y"));
md.addProjection(cat.id);
serializer.serialize(md, false);
assertEquals("cat.*.id in :a1", serializer.toString());
}
}

View File

@ -55,6 +55,11 @@ public class DerbySQLTest {
session.save(new Cat("Tim",6));
session.flush();
}
@Test
public void In(){
assertEquals(6l, query().from(cat).where(cat.dtype.in("C", "CX")).count());
}
@Test
public void Count(){