fixed ToStringVisitor delegation

improved CollectionAnyVisitor behaviour
This commit is contained in:
Timo Westkämper 2010-10-28 14:10:11 +00:00
parent b7259e9d60
commit 80d96fcdc1
5 changed files with 64 additions and 10 deletions

View File

@ -11,7 +11,16 @@ import com.mysema.query.types.template.BooleanTemplate;
* @author tiwe
*
*/
public abstract class CollectionAnyVisitor implements Visitor<Expression<?>,CollectionAnyVisitor.Context>{
public class CollectionAnyVisitor implements Visitor<Expression<?>,CollectionAnyVisitor.Context>{
public static final CollectionAnyVisitor DEFAULT = new CollectionAnyVisitor();
private static final Templates COLLECTION_ANY_TEMPLATE = new Templates(){
{
add(PathType.PROPERTY, "{0}_{1}");
add(PathType.COLLECTION_ANY, "{0}");
}};
public static class Context {
@ -99,13 +108,15 @@ public abstract class CollectionAnyVisitor implements Visitor<Expression<?>,Coll
}
}
protected abstract Predicate exists(Context c, Predicate condition);
protected Predicate exists(Context c, Predicate condition){
return condition;
}
@SuppressWarnings("unchecked")
@Override
public Expression<?> visit(Path<?> expr, Context context) {
if (expr.getMetadata().getPathType() == PathType.COLLECTION_ANY){
String variable = expr.getMetadata().getParent().toString().replace('.', '_');
String variable = expr.accept(ToStringVisitor.DEFAULT, COLLECTION_ANY_TEMPLATE).replace('.', '_');
EntityPath<?> replacement = new EntityPathBase(expr.getType(), variable);
context.add(expr, replacement);
return replacement;

View File

@ -146,7 +146,7 @@ public class QueryMixin<T>{
}
public T having(Predicate... o) {
metadata.addHaving(o);
metadata.addHaving(normalize(o,false));
return self;
}
@ -351,8 +351,12 @@ public class QueryMixin<T>{
}
public T where(Predicate... o) {
metadata.addWhere(o);
metadata.addWhere(normalize(o, true));
return self;
}
protected Predicate[] normalize(Predicate[] conditions, boolean where) {
return conditions;
}
}

View File

@ -24,7 +24,7 @@ public final class ToStringVisitor implements Visitor<String,Templates>{
if (element.getStaticText() != null){
builder.append(element.getStaticText());
}else{
builder.append(expr.getArg(element.getIndex()));
builder.append(expr.getArg(element.getIndex()).accept(this, templates));
}
}
return builder.toString();
@ -44,7 +44,7 @@ public final class ToStringVisitor implements Visitor<String,Templates>{
if (!first){
builder.append(", ");
}
builder.append(arg);
builder.append(arg.accept(this, templates));
first = false;
}
builder.append(")");
@ -60,7 +60,7 @@ public final class ToStringVisitor implements Visitor<String,Templates>{
if (element.getStaticText() != null){
builder.append(element.getStaticText());
}else{
builder.append(o.getArg(element.getIndex()));
builder.append(o.getArg(element.getIndex()).accept(this, templates));
}
}
return builder.toString();
@ -81,9 +81,9 @@ public final class ToStringVisitor implements Visitor<String,Templates>{
if (element.getStaticText() != null){
builder.append(element.getStaticText());
}else if (element.getIndex() == 0){
builder.append(parent);
builder.append(parent.accept(this, templates));
}else if (element.getIndex() == 1){
builder.append(expr);
builder.append(expr.accept(this, templates));
}
}
return builder.toString();

View File

@ -25,6 +25,11 @@ public class CollectionAnyVisitorTest {
assertEquals("cat_kittens.name", serialize(cat.kittens.any().name));
}
@Test
public void Very_Long_Path(){
assertEquals("cat_kittens_kittens.name", serialize(cat.kittens.any().kittens.any().name));
}
@Test
public void Simple_BooleanOperation(){
Predicate predicate = cat.kittens.any().name.eq("Ruth123");

View File

@ -0,0 +1,34 @@
package com.mysema.query.types;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.mysema.query.domain.QCat;
public class ToStringVisitorTest {
private Templates templates = new Templates(){
{
add(PathType.PROPERTY, "{0}_{1}");
add(PathType.COLLECTION_ANY, "{0}");
}};
@Test
public void Operation(){
assertEquals("cat_name is not null", QCat.cat.name.isNotNull().accept(ToStringVisitor.DEFAULT, templates));
}
@Test
public void Template(){
Expression<Boolean> template = TemplateExpressionImpl.create(Boolean.class, "{0} is not null", QCat.cat.name);
assertEquals("cat_name is not null", template.accept(ToStringVisitor.DEFAULT, templates));
}
@Test
public void Path(){
assertEquals("cat_kittens_kittens_name", QCat.cat.kittens.any().kittens.any().name.accept(ToStringVisitor.DEFAULT, templates));
}
}