#667362 : improved Collection.any() support

This commit is contained in:
Timo Westkämper 2010-10-28 14:59:07 +00:00
parent 049de7f05f
commit dcaed0c1f8
3 changed files with 31 additions and 13 deletions

View File

@ -29,15 +29,13 @@ import com.mysema.codegen.model.Types;
import com.mysema.query.JoinExpression;
import com.mysema.query.JoinType;
import com.mysema.query.QueryMetadata;
import com.mysema.query.support.CollectionAnyVisitor;
import com.mysema.query.types.Expression;
import com.mysema.query.types.FactoryExpression;
import com.mysema.query.types.Operation;
import com.mysema.query.types.ParamExpression;
import com.mysema.query.types.ParamNotSetException;
import com.mysema.query.types.PathType;
import com.mysema.query.types.Predicate;
import com.mysema.query.types.Templates;
import com.mysema.query.types.ToStringVisitor;
/**
* DefaultEvaluatorFactory extends the EvaluatorFactory class to provide Java source
@ -170,13 +168,20 @@ public class DefaultEvaluatorFactory {
}else if (join.getType() == JoinType.INNERJOIN){
Operation alias = (Operation)join.getTarget();
if (join.getCondition() != null && join.getCondition().toString().equals("any")){
boolean colAnyJoin = join.getCondition() != null && join.getCondition().toString().equals("any");
if (colAnyJoin){
String matcher = alias.getArg(1).toString() + "_matched";
ser.append("boolean " + matcher + " = false;\n");
anyJoinMatchers.add(matcher);
}
ser.append("for ( " + typeName + " " + alias.getArg(1) + " : ");
ser.handle(alias.getArg(0));
if (colAnyJoin){
CollectionAnyVisitor.Context context = new CollectionAnyVisitor.Context();
Expression<?> replacement = (Expression<?>) alias.getArg(0).accept(CollectionAnyVisitor.DEFAULT, context);
ser.handle(replacement);
}else{
ser.handle(alias.getArg(0));
}
if (alias.getArg(0).getType().equals(Map.class)){
ser.append(".values()");
}

View File

@ -6,7 +6,6 @@ import java.util.Arrays;
import java.util.List;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
public class CollectionTest {
@ -37,20 +36,34 @@ public class CollectionTest {
}
@Test
public void Any(){
public void Join_From_Two_Sources(){
QCat cat_kittens = new QCat("cat_kittens");
QCat other_kittens = new QCat("other_kittens");
assertEquals(30, MiniApi
.from(cat, cats).from(other, cats)
.innerJoin(cat.kittens, cat_kittens)
.innerJoin(other.kittens, other_kittens)
.where(cat_kittens.eq(other_kittens)).count());
}
@Test
public void Any_UniqueResult(){
assertEquals("4", MiniApi.from(cat, cats).where(cat.kittens.any().name.eq("4")).uniqueResult(cat.name));
}
@Test
public void Any2(){
public void Any_Count(){
assertEquals(4, MiniApi.from(cat, cats).where(cat.kittens.any().name.isNotNull()).count());
}
@Test
@Ignore
public void Any3(){
// TODO : support multiple levels of any usage
public void Any_Two_Levels(){
assertEquals(4, MiniApi.from(cat, cats).where(cat.kittens.any().name.isNotNull(), cat.kittens.any().kittens.any().isNotNull()).count());
}
@Test
public void Any_From_Two_Sources(){
assertEquals(16, MiniApi.from(cat, cats).from(other, cats).where(cat.kittens.any().name.eq(other.kittens.any().name)).count());
}
}

View File

@ -15,7 +15,7 @@ public class CollectionAnyVisitor implements Visitor<Expression<?>,CollectionAny
public static final CollectionAnyVisitor DEFAULT = new CollectionAnyVisitor();
private static final Templates COLLECTION_ANY_TEMPLATE = new Templates(){
public static final Templates TEMPLATE = new Templates(){
{
add(PathType.PROPERTY, "{0}_{1}");
add(PathType.COLLECTION_ANY, "{0}");
@ -116,7 +116,7 @@ public class CollectionAnyVisitor implements Visitor<Expression<?>,CollectionAny
@Override
public Expression<?> visit(Path<?> expr, Context context) {
if (expr.getMetadata().getPathType() == PathType.COLLECTION_ANY){
String variable = expr.accept(ToStringVisitor.DEFAULT, COLLECTION_ANY_TEMPLATE).replace('.', '_');
String variable = expr.accept(ToStringVisitor.DEFAULT, TEMPLATE).replace('.', '_');
EntityPath<?> replacement = new EntityPathBase(expr.getType(), variable);
context.add(expr, replacement);
return replacement;