From cc793dda8ab54ba81704286e4b8a61efee3dfd96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Westk=C3=A4mper?= Date: Sun, 24 Feb 2008 12:42:01 +0000 Subject: [PATCH] made the grammar more fluent --- .../com/mysema/query/grammar/Grammar.java | 46 ++++-- .../java/com/mysema/query/grammar/Types.java | 150 ++++++++++++------ .../com/mysema/query/grammar/Visitor.java | 16 +- .../mysema/query/grammar/VisitorAdapter.java | 28 ++-- 4 files changed, 166 insertions(+), 74 deletions(-) diff --git a/src/main/java/com/mysema/query/grammar/Grammar.java b/src/main/java/com/mysema/query/grammar/Grammar.java index e35783bb4..1c306231a 100644 --- a/src/main/java/com/mysema/query/grammar/Grammar.java +++ b/src/main/java/com/mysema/query/grammar/Grammar.java @@ -118,12 +118,21 @@ public class Grammar { return _binOp(BoOp.AND, left, right); } - public static EntityExpr as(DomainType from, DomainType to) { - return new Alias(from, to); + public static AliasForCollection as(RefCollection from, RefDomainType to) { + return new AliasForCollection(from, to); } - public static EntityExpr as(CollectionReference from, DomainType to) { - return new CollectionAlias(from, to); + public static AliasForEntity as(RefDomainType from, RefDomainType to) { + return new AliasForEntity(from, to); + } + + public static AliasForEntity as(RefDomainType from, String to) { + return new AliasForEntity(from, to); + } + + public static AliasForAnything as(Reference from, String to) { + // NOTE : maybe this needs to be possible for all expressions + return new AliasForAnything(from, to); } public static > OrderSpecifier asc(Expr target) { @@ -145,25 +154,38 @@ public class Grammar { // NOTE : basically same as lt return _binOp(DateOp.BEFORE, left, right); } - + public static > BooleanExpr between(Expr left, A start, A end) { return _terOp(CompOp.BETWEEN, left, _const(start), _const(end)); } + public static > BooleanExpr between(Expr left, + Expr start, Expr end) { + return _terOp(CompOp.BETWEEN, left, start, end); + } + public static Expr concat(Expr left, Expr right) { return _binOp(StrOp.CONCAT, left, right); } + public static Expr count(){ + return new CountExpr(); + } + public static > OrderSpecifier desc( Expr target) { return _orderDesc(target); } + public static Expr div(Expr left, A right) { + return _binOp(NumOp.DIV, left, _const(right)); + } + public static Expr div(Expr left, Expr right) { return _binOp(NumOp.DIV, left, right); } - + public static BooleanExpr eq(Expr left, B right) { return _binOp(Op.EQ, left, _const(right)); } @@ -231,6 +253,10 @@ public class Grammar { return _binOp(NumOp.LT, left, right); } + public static Expr mult(Expr left, A right) { + return _binOp(NumOp.MULT, left, _const(right)); + } + public static Expr mult(Expr left, Expr right) { return _binOp(NumOp.MULT, left, right); } @@ -275,12 +301,4 @@ public class Grammar { public static Expr upper(Expr left) { return _unOp(StrOp.UPPER, left); } - - public static Expr div(Expr left, A right) { - return _binOp(NumOp.DIV, left, _const(right)); - } - - public static Expr mult(Expr left, A right) { - return _binOp(NumOp.MULT, left, _const(right)); - } } diff --git a/src/main/java/com/mysema/query/grammar/Types.java b/src/main/java/com/mysema/query/grammar/Types.java index 6a7354cde..122b5c290 100644 --- a/src/main/java/com/mysema/query/grammar/Types.java +++ b/src/main/java/com/mysema/query/grammar/Types.java @@ -16,15 +16,36 @@ import java.util.Collection; */ public class Types { - public static class Alias extends Reference implements EntityExpr{ - public final Reference from, to; - Alias(Reference from, Reference to) { - super(to.toString()); + public static class Alias implements Expr{ + public final Expr from; + public final String to; + Alias(Expr from, String to) { this.from = from; this.to = to; + } + } + + public static class AliasForAnything extends Alias{ + AliasForAnything(Expr from, String to) { + super(from,to); } } + public static class AliasForCollection extends Alias implements EntityExpr{ + AliasForCollection(RefCollection from, Reference to) { + super(from,to.toString()); + } + } + + public static class AliasForEntity extends Alias implements EntityExpr{ + AliasForEntity(RefDomainType from, RefDomainType to) { + super(from,to.toString()); + } + AliasForEntity(RefDomainType from, String to) { + super(from,to.toString()); + } + } + public static class BinaryBooleanOperation extends BinaryOperation implements BooleanOperation { @@ -47,10 +68,6 @@ public class Types { public interface BooleanOperation extends Operation, BooleanExpr {} - public static class BooleanProperty extends Reference implements BooleanExpr{ - public BooleanProperty(String path) {super(path);} - } - /** * Boolean operators (operators used with boolean operands) */ @@ -64,23 +81,6 @@ public class Types { static class BoOpImpl implements BoOp{} - public static class CollectionAlias extends Reference implements EntityExpr{ - public final CollectionReference from; - public final Reference to; - CollectionAlias(CollectionReference from, Reference to) { - super(to.toString()); - this.from = from; - this.to = to; - } - } - - public static class CollectionReference extends Reference> implements - EntityExpr>{ - public CollectionReference(String p) { - super(p); - } - } - /** * Operators for Comparable objects */ @@ -90,12 +90,16 @@ public class Types { CompOp GT = new CompOpImpl(); CompOp LOE = new CompOpImpl(); CompOp LT = new CompOpImpl(); - } - + } + static class CompOpImpl implements CompOp {} public static class ConstantExpr implements Expr{ public A constant; + } + + public static class CountExpr implements Expr{ + // TODO : add count selection etc } /** @@ -108,29 +112,13 @@ public class Types { static class DateOpImpl implements DateOp{} - public static class DomainType extends Reference implements EntityExpr{ - protected DomainType(DomainType type, String path) { - super(type+"."+path); - } - protected DomainType(String path) {super(path);} - protected BooleanProperty _boolean(String path){ - return new BooleanProperty(this+"."+path); - } - protected CollectionReference _collection(String path,Class type) { - return new CollectionReference(this+"."+path); - } - protected Reference _prop(String path,Class type) { - return new Reference(this+"."+path); - } - } - /** * Reference to an entity */ public static interface EntityExpr extends Expr{} public interface Expr { } - + /** * Numeric Operators (operators used with numeric operands) */ @@ -155,24 +143,94 @@ public class Types { Op ISTYPEOF = new OpImpl(); Op NE = new OpImpl(); } - + public interface Operation extends Expr {} static class OpImpl implements Op {} - public enum Order{ ASC,DESC } + public enum Order{ ASC,DESC } public static class OrderSpecifier>{ public Order order; public Expr target; } + public static class RefBoolean extends Reference implements BooleanExpr{ + RefBoolean(String path) {super(path);} + } + + public static class RefCollection extends Reference> implements + EntityExpr>{ + RefCollection(String p) { + super(p); + } + // convenience + public AliasForCollection as(RefDomainType to) { + return Grammar.as(this, to); + } + } + + public static class RefComparable> extends Reference{ + RefComparable(String p) { + super(p); + } + + // convenience methods + public BooleanExpr between(A start, A end){ + return Grammar.between(this,start, end);} + public BooleanExpr between(Expr start, Expr end){ + return Grammar.between(this,start, end);} + public BooleanExpr goe(A right){ return Grammar.goe(this, right);} + public BooleanExpr goe(Expr right){ return Grammar.goe(this, right);} + public BooleanExpr gt(A right){ return Grammar.gt(this, right);} + public BooleanExpr gt(Expr right){ return Grammar.gt(this, right);} + public BooleanExpr loe(A right){ return Grammar.loe(this, right);} + public BooleanExpr loe(Expr right){ return Grammar.loe(this, right);} + public BooleanExpr lt(A right){ return Grammar.lt(this, right);} + public BooleanExpr lt(Expr right){ return Grammar.lt(this, right);} + + // asc + desc + } + + public static class RefDomainType extends Reference implements EntityExpr{ + protected RefDomainType(RefDomainType type, String path) { + super(type+"."+path); + } + protected RefDomainType(String path) {super(path);} + protected RefBoolean _boolean(String path){ + return new RefBoolean(this+"."+path); + } + protected RefCollection _collection(String path,Class type) { + return new RefCollection(this+"."+path); + } + protected Reference _prop(String path,Class type) { + return new Reference(this+"."+path); + } + + // convenience + public AliasForEntity as(RefDomainType to) {return Grammar.as(this, to);} + public AliasForEntity as(String to) {return Grammar.as(this, to);} + } + public static class Reference implements Expr{ // _path is hidden to not pollute the namespace of the domain types private final String path; public Reference(String p) { path = p; } + // convenience (these can be applied to all expressions) + public Alias as(String to) {return Grammar.as(this, to);} + public BooleanExpr eq(Expr right){return Grammar.eq(this, right);} + public BooleanExpr eq(T right){return Grammar.eq(this, right);} + public BooleanExpr ne(Expr right){return Grammar.ne(this, right);} + public BooleanExpr ne(T right){return Grammar.ne(this, right);} + + // these should only be applied to paths + public BooleanExpr isnull(){return Grammar.isnull(this);} + public BooleanExpr isnotnull(){return Grammar.isnotnull(this);} +// Op IN = new OpImpl(); +// Op ISTYPEOF = new OpImpl(); + @Override public final String toString(){ return path; } } diff --git a/src/main/java/com/mysema/query/grammar/Visitor.java b/src/main/java/com/mysema/query/grammar/Visitor.java index 62d7f806b..916e31704 100644 --- a/src/main/java/com/mysema/query/grammar/Visitor.java +++ b/src/main/java/com/mysema/query/grammar/Visitor.java @@ -58,19 +58,25 @@ public abstract class Visitor> { protected abstract void visit(Alias expr); + protected abstract void visit(AliasForAnything expr); + + protected abstract void visit(AliasForCollection expr); + + protected abstract void visit(AliasForEntity expr); + protected abstract void visit(BinaryBooleanOperation expr); protected abstract void visit(BinaryOperation expr); - protected abstract void visit(BooleanProperty expr); + protected abstract void visit(RefBoolean expr); - protected abstract void visit(CollectionAlias expr); - - protected abstract void visit(CollectionReference expr); + protected abstract void visit(RefCollection expr); protected abstract void visit(ConstantExpr expr); - protected abstract void visit(DomainType expr); + protected abstract void visit(CountExpr expr); + + protected abstract void visit(RefDomainType expr); protected abstract void visit(Reference expr); diff --git a/src/main/java/com/mysema/query/grammar/VisitorAdapter.java b/src/main/java/com/mysema/query/grammar/VisitorAdapter.java index 796bb9f8a..c890be1d1 100644 --- a/src/main/java/com/mysema/query/grammar/VisitorAdapter.java +++ b/src/main/java/com/mysema/query/grammar/VisitorAdapter.java @@ -15,28 +15,38 @@ import com.mysema.query.grammar.Types.*; */ public abstract class VisitorAdapter> extends Visitor{ + @Override + protected void visit(AliasForAnything expr) { + visit((Alias)expr); + } + + @Override + protected void visit(AliasForCollection expr){ + visit((Alias)expr); + } + + @Override + protected void visit(AliasForEntity expr) { + visit((Alias)expr); + } + @Override protected void visit(BinaryBooleanOperation expr) { visit((BinaryOperation)expr); } - + @Override - protected void visit(BooleanProperty expr) { + protected void visit(RefBoolean expr) { visit((Reference)expr); } @Override - protected void visit(CollectionAlias expr){ + protected void visit(RefCollection expr){ visit((Reference)expr); } @Override - protected void visit(CollectionReference expr){ - visit((Reference)expr); - } - - @Override - protected void visit(DomainType expr) { + protected void visit(RefDomainType expr) { visit((Reference)expr); }