Moved remotely

This commit is contained in:
Timo Westkämper 2008-03-06 17:26:33 +00:00
parent 01a2da1475
commit d89a89d776
15 changed files with 1079 additions and 0 deletions

10
trunk/.classpath Normal file
View File

@ -0,0 +1,10 @@
<classpath>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="src" path="src/test/java" output="target/test-classes"/>
<classpathentry kind="output" path="target/classes"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="var" path="M2_REPO/com/mysema/commons/mysema-core/0.3.3/mysema-core-0.3.3.jar" sourcepath="M2_REPO/com/mysema/commons/mysema-core/0.3.3/mysema-core-0.3.3-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/commons-io/commons-io/1.4-SNAPSHOT/commons-io-1.4-SNAPSHOT.jar"/>
<classpathentry kind="var" path="M2_REPO/commons-lang/commons-lang/2.3/commons-lang-2.3.jar" sourcepath="M2_REPO/commons-lang/commons-lang/2.3/commons-lang-2.3-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/junit/junit/4.4/junit-4.4.jar" sourcepath="M2_REPO/junit/junit/4.4/junit-4.4-sources.jar"/>
</classpath>

13
trunk/.project Normal file
View File

@ -0,0 +1,13 @@
<projectDescription>
<name>querydsl</name>
<comment>Default Mysema project template</comment>
<projects/>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@ -0,0 +1,5 @@
#Mon Mar 03 20:26:17 EET 2008
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.source=1.5
org.eclipse.jdt.core.compiler.compliance=1.5

BIN
trunk/etc/overview.cayra Normal file

Binary file not shown.

38
trunk/pom.xml Normal file
View File

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.mysema</groupId>
<artifactId>maven-parent</artifactId>
<version>2.0.0-SNAPSHOT</version>
</parent>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl</artifactId>
<version>0.1.1</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.mysema.commons</groupId>
<artifactId>mysema-core</artifactId>
<version>0.3.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>

14
trunk/readme.txt Normal file
View File

@ -0,0 +1,14 @@
* more testing
* possibly Elmo evaluation
* Query DSL is promoted on website
+ open Maven repo
+
refactorings
Operation, Alias and Path as interfaces

View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2008 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query;
import com.mysema.query.grammar.Types.ExprBoolean;
import com.mysema.query.grammar.Types.ExprEntity;
import com.mysema.query.grammar.Types.Expr;
import com.mysema.query.grammar.Types.OrderSpecifier;
/**
* Query provides a the query interface of the fluent query DSL
*
* @author tiwe
* @version $Id$
*/
public interface Query<A extends Query<A>>{
A select(Expr<?>... o);
A from(ExprEntity<?>... o);
A innerJoin(ExprEntity<?> o);
A join(ExprEntity<?> o);
A leftJoin(ExprEntity<?> o);
A with(ExprBoolean... o);
A where(ExprBoolean... o);
A groupBy(Expr<?>... o);
A having(ExprBoolean... o);
A orderBy(OrderSpecifier<?>... o);
}

View File

@ -0,0 +1,109 @@
/*
* Copyright (c) 2008 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.mysema.query.grammar.Types.Expr;
import com.mysema.query.grammar.Types.ExprBoolean;
import com.mysema.query.grammar.Types.ExprEntity;
import com.mysema.query.grammar.Types.OrderSpecifier;
/**
* QueryBase provides a basic implementation of the Query interface
*
* @author tiwe
* @version $Id$
*/
@SuppressWarnings("unchecked")
public class QueryBase<A extends QueryBase<A>> implements Query<A> {
protected final List<Expr<?>> groupBy = new ArrayList<Expr<?>>();
protected final List<ExprBoolean> having = new ArrayList<ExprBoolean>();
protected final List<JoinExpression> joins = new ArrayList<JoinExpression>();
protected final List<OrderSpecifier<?>> orderBy = new ArrayList<OrderSpecifier<?>>();
protected final List<Expr<?>> select = new ArrayList<Expr<?>>();
protected final List<ExprBoolean> where = new ArrayList<ExprBoolean>();
protected void clear(){
joins.clear();
groupBy.clear();
having.clear();
orderBy.clear();
select.clear();
where.clear();
}
public A from(ExprEntity<?>... o) {
for (ExprEntity<?> expr : o){
joins.add(new JoinExpression(JoinType.DEFAULT,expr));
}
return (A) this;
}
public A groupBy(Expr<?>... o) {
groupBy.addAll(Arrays.asList(o));
return (A) this;
}
public A having(ExprBoolean... o) {
having.addAll(Arrays.asList(o));
return (A) this;
}
public A innerJoin(ExprEntity<?> o) {
joins.add(new JoinExpression(JoinType.IJ,o));
return (A) this;
}
public A join(ExprEntity<?> o) {
joins.add(new JoinExpression(JoinType.J,o));
return (A) this;
}
public A leftJoin(ExprEntity<?> o) {
joins.add(new JoinExpression(JoinType.LJ,o));
return (A) this;
}
public A orderBy(OrderSpecifier<?>... o) {
orderBy.addAll(Arrays.asList(o));
return (A) this;
}
public A select(Expr<?>... o) {
select.addAll(Arrays.asList(o));
return (A) this;
}
public A where(ExprBoolean... o) {
where.addAll(Arrays.asList(o));
return (A) this;
}
public A with(ExprBoolean... o) {
if (!joins.isEmpty()){
joins.get(joins.size()-1).conditions = o;
}
return (A) this;
}
public static class JoinExpression{
public ExprBoolean[] conditions;
public final ExprEntity<?> target;
public final JoinType type;
JoinExpression(JoinType type, ExprEntity<?> target){
this.type = type;
this.target = target;
}
}
public enum JoinType{
DEFAULT,IJ,J,LJ
}
}

View File

@ -0,0 +1,18 @@
/*
* Copyright (c) 2008 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query.dto;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Target(TYPE)
@Retention(RUNTIME)
public @interface DTO {
Class<?> domainType();
}

View File

@ -0,0 +1,266 @@
/*
* Copyright (c) 2008 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query.grammar;
import java.util.Collection;
import com.mysema.query.grammar.Ops.*;
import com.mysema.query.grammar.Types.*;
/**
* Grammar provides the factory methods for the fluent grammar
*
* @author tiwe
* @version $Id$
*/
public class Grammar {
static final ExprBoolean _boolean(Op<Boolean> operator, Expr<?>... args) {
return new OperationBoolean(operator, args);
}
@SuppressWarnings("unchecked")
static final <A> Expr<A> _const(A obj) {
if (obj instanceof Expr)
return (Expr<A>) obj;
return new ConstantExpr<A>(obj);
}
static final <D extends Comparable<D>> ExprComparable<D> _number(Op<Number> operator, Expr<?>... args) {
return new OperationNumber<D>(operator, args);
}
static final <OP, RT extends Comparable<RT>> ExprComparable<RT> _comparable(Op<OP> operator, Expr<?>... args) {
return new OperationComparable<OP,RT>(operator, args);
}
static final <A extends Comparable<A>> OrderSpecifier<A> _orderAsc(Expr<A> target) {
OrderSpecifier<A> os = new OrderSpecifier<A>();
os.order = Order.ASC;
os.target = target;
return os;
}
static final <A extends Comparable<A>> OrderSpecifier<A> _orderDesc(Expr<A> target) {
OrderSpecifier<A> os = new OrderSpecifier<A>();
os.order = Order.DESC;
os.target = target;
return os;
}
static final ExprString _string(Op<String> operator, Expr<?>... args) {
return new OperationString(operator, args);
}
public static <A extends Comparable<A>> ExprComparable<A> add(Expr<A> left, A right) {
return _number(OpNumber.ADD, left, _const(right));
}
public static <A extends Comparable<A>> ExprComparable<A> add(Expr<A> left, Expr<A> right) {
return _number(OpNumber.ADD, left, right);
}
static <A extends Comparable<A>> ExprBoolean after(Expr<A> left,
A right) {
// NOTE : signature is for Comparables to support other than Java's date types
return _boolean(OpDate.AFTER, left, _const(right));
}
static <A extends Comparable<A>> ExprBoolean after(Expr<A> left,
Expr<A> right) {
// NOTE : signature is for Comparables to support other than Java's date types
return _boolean(OpDate.AFTER, left, right);
}
static ExprBoolean and(ExprBoolean left, ExprBoolean right) {
return _boolean(OpBoolean.AND, left, right);
}
static <D> AliasNoEntity<D> as(ExprNoEntity<D> from, String to) {
return new AliasNoEntity<D>(from, to);
}
static <D> AliasEntity<D> as(PathEntity<D> from, PathEntity<D> to) {
return new AliasEntity<D>(from, to);
}
static <D> AliasCollection<D> as(PathEntityCollection<D> from, PathEntity<D> to) {
return new AliasCollection<D>(from, to);
}
static <A extends Comparable<A>> OrderSpecifier<A> asc(Expr<A> target) {
return _orderAsc(target);
}
static <A extends Comparable<A>> ExprBoolean before(Expr<A> left,
A right) {
// NOTE : signature is for Comparables to support other than Java's date types
// NOTE : basically same as lt
return _boolean(OpDate.BEFORE, left, _const(right));
}
static <A extends Comparable<A>> ExprBoolean before(Expr<A> left,
Expr<A> right) {
// NOTE : signature is for Comparables to support other than Java's date types
// NOTE : basically same as lt
return _boolean(OpDate.BEFORE, left, right);
}
static <A extends Comparable<A>> ExprBoolean between(Expr<A> left,
A start, A end) {
return _boolean(OpComparable.BETWEEN, left, _const(start), _const(end));
}
static <A extends Comparable<A>> ExprBoolean between(Expr<A> left,
Expr<A> start, Expr<A> end) {
return _boolean(OpComparable.BETWEEN, left, start, end);
}
public static ExprNoEntity<String> concat(Expr<String> left, Expr<String> right) {
return _string(OpString.CONCAT, left, right);
}
static <A extends Comparable<A>> OrderSpecifier<A> desc(
Expr<A> target) {
return _orderDesc(target);
}
public static <A extends Comparable<A>> ExprComparable<A> div(Expr<A> left, A right) {
return _number(OpNumber.DIV, left, _const(right));
}
public static <A extends Comparable<A>> ExprComparable<A> div(Expr<A> left, Expr<A> right) {
return _number(OpNumber.DIV, left, right);
}
static <A, B extends A> ExprBoolean eq(Expr<A> left, B right) {
return _boolean(Op.EQ, left, _const(right));
}
static <A, B extends A> ExprBoolean eq(Expr<A> left, Expr<B> right) {
return _boolean(Op.EQ, left, right);
}
public static <A extends Comparable<A>> ExprBoolean goe(Expr<A> left,
A right) {
return _boolean(OpComparable.GOE, left, _const(right));
}
public static <A extends Comparable<A>> ExprBoolean goe(Expr<A> left,
Expr<A> right) {
return _boolean(OpComparable.GOE, left, right);
}
public static <A extends Comparable<A>> ExprBoolean gt(Expr<A> left, A right) {
return _boolean(OpComparable.GT, left, _const(right));
}
public static <A extends Comparable<A>> ExprBoolean gt(Expr<A> left,
Expr<A> right) {
return _boolean(OpComparable.GT, left, right);
}
public static <A> ExprBoolean in(A left, ExprEntity<Collection<A>> right){
return _boolean(Op.INELEMENTS, _const(left), right);
}
static <A extends Comparable<A>> ExprBoolean in(Expr<A> left,
A... rest) {
return _boolean(Op.INARRAY, left, _const(rest));
}
static <A> ExprBoolean in(ExprEntity<A> left, ExprEntity<Collection<A>> right){
return _boolean(Op.INELEMENTS, left, right);
}
static <A> ExprBoolean isnotnull(Expr<A> left) {
return _boolean(Op.ISNOTNULL, left);
}
static <A> ExprBoolean isnull(Expr<A> left) {
return _boolean(Op.ISNULL, left);
}
static ExprBoolean like(Expr<String> left, String right) {
return _boolean(OpString.LIKE, left, _const(right));
}
static <A extends Comparable<A>> ExprBoolean loe(Expr<A> left,
A right) {
return _boolean(OpComparable.LOE, left, _const(right));
}
static <A extends Comparable<A>> ExprBoolean loe(Expr<A> left,
Expr<A> right) {
return _boolean(OpComparable.LOE, left, right);
}
static ExprString lower(Expr<String> left) {
return _string(OpString.LOWER, left);
}
static <A extends Comparable<A>> ExprBoolean lt(Expr<A> left, A right) {
return _boolean(OpComparable.LT, left, _const(right));
}
static <A extends Comparable<A>> ExprBoolean lt(Expr<A> left,
Expr<A> right) {
return _boolean(OpComparable.LT, left, right);
}
public static <A extends Comparable<A>> ExprComparable<A> mult(Expr<A> left, A right) {
return _number(OpNumber.MULT, left, _const(right));
}
public static <A extends Comparable<A>> ExprComparable<A> mult(Expr<A> left, Expr<A> right) {
return _number(OpNumber.MULT, left, right);
}
static <A, B extends A> ExprBoolean ne(Expr<A> left, B right) {
return _boolean(Op.NE, left, _const(right));
}
static <A, B extends A> ExprBoolean ne(Expr<A> left, Expr<B> right) {
return _boolean(Op.NE, left, right);
}
public static ExprBoolean not(ExprBoolean left) {
return _boolean(OpBoolean.NOT, left);
}
static ExprBoolean or(ExprBoolean left, ExprBoolean right) {
return _boolean(OpBoolean.OR, left, right);
}
public static <A extends Comparable<A>> ExprComparable<A> sub(Expr<A> left, A right) {
return _number(OpNumber.SUB, left, _const(right));
}
public static <A extends Comparable<A>> ExprComparable<A> sub(Expr<A> left, Expr<A> right) {
return _number(OpNumber.SUB, left, right);
}
static ExprString substring(Expr<String> left, int beginIndex) {
return _string(OpString.SUBSTR1ARG, left, _const(beginIndex));
}
static ExprString substring(Expr<String> left, int beginIndex, int endIndex) {
return _string(OpString.SUBSTR2ARGS, left, _const(beginIndex), _const(endIndex));
}
static <A, B extends A> ExprBoolean typeOf(Expr<A> left, Class<B> right) {
return _boolean(Op.ISTYPEOF, left, _const(right));
}
static ExprString trim(Expr<String> left) {
return _string(OpString.TRIM, left);
}
static ExprString upper(Expr<String> left) {
return _string(OpString.UPPER, left);
}
}

View File

@ -0,0 +1,85 @@
/*
* Copyright (c) 2008 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query.grammar;
/**
* Ops provides the operators for the fluent query grammar
*
* @author tiwe
* @version $Id$
*/
public class Ops {
/**
* Operators (the return type is encoded in the 1st generic parameter)
*/
public interface Op<RT> {
Op<Boolean> EQ = new OpImpl<Boolean>();
Op<Boolean> INARRAY = new OpImpl<Boolean>();
Op<Boolean> INELEMENTS = new OpImpl<Boolean>();
Op<Boolean> ISNOTNULL = new OpImpl<Boolean>();
Op<Boolean> ISNULL = new OpImpl<Boolean>();
Op<Boolean> ISTYPEOF = new OpImpl<Boolean>();
Op<Boolean> NE = new OpImpl<Boolean>();
}
/**
* Boolean operators (operators used with boolean operands)
*/
public interface OpBoolean{
Op<Boolean> AND = new OpImpl<Boolean>();
Op<Boolean> NOT = new OpImpl<Boolean>();
Op<Boolean> OR = new OpImpl<Boolean>();
Op<Boolean> XNOR = new OpImpl<Boolean>();
Op<Boolean> XOR = new OpImpl<Boolean>();
}
/**
* Operators for Comparable objects
*/
public interface OpComparable{
Op<Boolean> BETWEEN = new OpImpl<Boolean>();
Op<Boolean> GOE = new OpImpl<Boolean>();
Op<Boolean> GT = new OpImpl<Boolean>();
Op<Boolean> LOE = new OpImpl<Boolean>();
Op<Boolean> LT = new OpImpl<Boolean>();
}
/**
* Date Operators (operators used with Date operands)
*/
public interface OpDate{
Op<Boolean> AFTER = new OpImpl<Boolean>();
Op<Boolean> BEFORE = new OpImpl<Boolean>();
}
public static final class OpImpl<RT> implements Op<RT>{}
/**
* Numeric Operators (operators used with numeric operands)
*/
public interface OpNumber{
Op<Number> ADD = new OpImpl<Number>();
Op<Number> DIV = new OpImpl<Number>();
Op<Number> MOD = new OpImpl<Number>();
Op<Number> MULT = new OpImpl<Number>();
Op<Number> SUB = new OpImpl<Number>();
}
/**
* String Operators (operators used with String operands)
*/
public interface OpString{
Op<String> CONCAT = new OpImpl<String>();
Op<Boolean> LIKE = new OpImpl<Boolean>();
Op<String> LOWER = new OpImpl<String>();
Op<String> SUBSTR1ARG = new OpImpl<String>();
Op<String> SUBSTR2ARGS = new OpImpl<String>();
Op<String> TRIM = new OpImpl<String>();
Op<String> UPPER = new OpImpl<String>();
}
}

View File

@ -0,0 +1,284 @@
/*
* Copyright (c) 2008 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query.grammar;
import java.util.Collection;
import com.mysema.query.grammar.Ops.Op;
/**
* Types provides the types of the fluent grammar
*
* @author tiwe
* @version $Id$
*/
public class Types {
public interface Alias<D>{
Expr<?> getFrom();
String getTo();
}
public static class AliasCollection<D> extends ExprEntity<D> implements Alias<D>{
private final Expr<?> from;
private final String to;
AliasCollection(PathEntityCollection<D> from, Path<D> to) {
super(null);
this.from = from;
this.to = to.toString();
}
public Expr<?> getFrom() {return from;}
public String getTo() {return to;}
}
public static class AliasEntity<D> extends ExprEntity<D> implements Alias<D>{
private final Expr<?> from;
private final String to;
AliasEntity(PathEntity<D> from, PathEntity<D> to) {
super(from.type);
this.from = from;
this.to = to.toString();
}
AliasEntity(PathEntity<D> from, String to) {
super(from.type);
this.from = from;
this.to = to;
}
public Expr<?> getFrom() {return from;}
public String getTo() {return to;}
}
public static class AliasNoEntity<D> extends ExprNoEntity<D> implements Alias<D>{
private final Expr<?> from;
private final String to;
AliasNoEntity(Expr<D> from, String to) {
super(from.type);
this.from = from;
this.to = to;
}
public Expr<D> as(String to) {
return Grammar.as(this, to);
}
public Expr<?> getFrom() {return from;}
public String getTo() {return to;}
}
public static class ConstantExpr<D> extends Expr<D>{
private final D constant;
@SuppressWarnings("unchecked")
ConstantExpr(D constant) {
super((Class<D>) constant.getClass());
this.constant = constant;
}
public D getConstant(){ return constant;}
}
public static abstract class Expr<D>{
protected final Class<D> type;
Expr(Class<D> type){this.type = type;}
public <B extends D> ExprBoolean eq(B right){return Grammar.eq(this, right);}
public <B extends D> ExprBoolean eq(Expr<B> right){return Grammar.eq(this, right);}
public <B extends D> ExprBoolean ne(B right){return Grammar.ne(this, right);}
public <B extends D> ExprBoolean ne(Expr<B> right){return Grammar.ne(this, right);}
}
public static abstract class ExprBoolean extends ExprNoEntity<Boolean>{
ExprBoolean() {super(Boolean.class);}
public ExprBoolean and(ExprBoolean right) {return Grammar.and(this, right);}
public ExprBoolean or(ExprBoolean right) {return Grammar.or(this, right);}
}
public static abstract class ExprComparable<D extends Comparable<D>> extends ExprNoEntity<D>{
ExprComparable(Class<D> type) {super(type);}
public ExprBoolean after(Expr<D> right) {return Grammar.after(this,right);}
public ExprBoolean after(D right) {return Grammar.after(this,right);}
public OrderSpecifier<D> asc() {return Grammar.asc(this);}
public ExprBoolean before(Expr<D> right) {return Grammar.before(this,right);}
public ExprBoolean before(D right) {return Grammar.before(this,right);}
public ExprBoolean between(Expr<D> first, Expr<D> second) {return Grammar.between(this,first,second);}
public ExprBoolean between(D first, D second) {return Grammar.between(this,first,second);}
public OrderSpecifier<D> desc() {return Grammar.desc(this);}
public ExprBoolean goe(D right) {return Grammar.goe(this,right);}
public ExprBoolean goe(Expr<D> right) {return Grammar.goe(this,right);}
public ExprBoolean gt(D right) {return Grammar.gt(this,right);}
public ExprBoolean gt(Expr<D> right) {return Grammar.gt(this,right);}
public ExprBoolean loe(D right) {return Grammar.loe(this,right);}
public ExprBoolean loe(Expr<D> right) {return Grammar.loe(this,right);}
public ExprBoolean lt(D right) {return Grammar.lt(this,right);}
public ExprBoolean lt(Expr<D> right) {return Grammar.lt(this,right);}
public ExprBoolean in(D... args) {return Grammar.in(this,args);}
}
public static abstract class ExprEntity<D> extends Expr<D>{
ExprEntity(Class<D> type) {super(type);}
}
public static abstract class ExprNoEntity<D> extends Expr<D>{
ExprNoEntity(Class<D> type) {super(type);}
public Expr<D> as(String to){return Grammar.as(this, to);}
}
public static abstract class ExprString extends ExprComparable<String>{
ExprString() {super(String.class);}
public ExprBoolean like(String str) { return Grammar.like(this, str); }
public ExprString lower() { return Grammar.lower(this); }
public ExprString substring(int beginIndex) { return Grammar.substring(this, beginIndex);}
public ExprString substring(int beginIndex, int endIndex) { return Grammar.substring(this, beginIndex, endIndex);}
public ExprString trim() { return Grammar.trim(this); }
public ExprString upper() { return Grammar.upper(this); }
}
public interface Operation<OP,RT>{
Expr<?>[] getArgs();
Op<OP> getOperator();
}
public static class OperationBoolean extends ExprBoolean implements Operation<Boolean,Boolean>{
private final Expr<?>[] args;
private final Op<Boolean> op;
OperationBoolean(Op<Boolean> op, Expr<?>... args){
this.op = op;
this.args = args;
}
public Expr<?>[] getArgs() {return args;}
public Op<Boolean> getOperator() {return op;}
}
public static class OperationComparable<OpType,D extends Comparable<D>> extends ExprComparable<D> implements Operation<OpType,D> {
private final Expr<?>[] args;
private final Op<OpType> op;
OperationComparable(Op<OpType> op, Expr<?>... args){
super(null);
this.op = op;
this.args = args;
}
public Expr<?>[] getArgs() {return args;}
public Op<OpType> getOperator() {return op;}
}
public static class OperationNumber<D extends Comparable<D>> extends OperationComparable<Number,D>{
OperationNumber(Op<Number> op, Expr<?>[] args) {
super(op, args);
}
}
public static class OperationString extends ExprString implements Operation<String,String>{
private final Expr<?>[] args;
private final Op<String> op;
OperationString(Op<String> op, Expr<?>... args){
this.op = op;
this.args = args;
}
public Expr<?>[] getArgs() {return args;}
public Op<String> getOperator() {return op;}
}
public enum Order{ ASC,DESC }
public static class OrderSpecifier<A extends Comparable<A>>{
public Order order;
public Expr<A> target;
}
public interface Path<D>{
ExprBoolean isnotnull();
ExprBoolean isnull();
}
public static class PathBoolean extends ExprBoolean implements PathNoEntity<Boolean>{
private final String path;
PathBoolean(String path) {this.path = path;}
public ExprBoolean isnotnull() {return Grammar.isnotnull(this);}
public ExprBoolean isnull() {return Grammar.isnull(this);}
public String toString(){ return path;}
}
public static class PathComparable<D extends Comparable<D>> extends ExprComparable<D> implements PathNoEntity<D>{
private final String path;
public PathComparable(Class<D> type, String path) {
super(type);
this.path = path;
}
public ExprBoolean isnotnull() {return Grammar.isnotnull(this);}
public ExprBoolean isnull() {return Grammar.isnull(this);}
public String toString() {return path;}
}
public static class PathEntity<D> extends ExprEntity<D> implements Path<D>{
private final String path;
protected PathEntity(Class<D> type, String path) {
super(type);
this.path = path;
}
protected PathBoolean _boolean(String path){
return new PathBoolean(this+"."+path);
}
protected <A>PathEntityCollection<A> _collection(String path,Class<A> type) {
return new PathEntityCollection<A>(type, this+"."+path);
}
protected <A extends Comparable<A>> PathComparable<A> _comparable(String path,Class<A> type) {
return new PathComparable<A>(type, this+"."+path);
}
protected <A> PathEntityRenamable<A> _entity(String path, Class<A> type){
return new PathEntityRenamable<A>(type, this+"."+path);
}
protected <A> PathNoEntitySimple<A> _simple(String path, Class<A> type){
return new PathNoEntitySimple<A>(type, this+"."+path);
}
protected PathString _string(String path){
return new PathString(this+"."+path);
}
public ExprBoolean in(ExprEntity<Collection<D>> right){return Grammar.in(this, right);}
public ExprBoolean isnotnull() {return Grammar.isnotnull(this);}
public ExprBoolean isnull() {return Grammar.isnull(this);}
public String toString() {return path;}
public <B extends D> ExprBoolean typeOf(Class<B> type) {return Grammar.typeOf(this, type);}
}
public static class PathEntityCollection<D> extends ExprEntity<Collection<D>> implements Path<Collection<D>>{
private final String path;
PathEntityCollection(Class<D> type, String path) {
super(null);
this.path = path;
}
public AliasCollection<D> as(PathEntity<D> to) {return Grammar.as(this, to);}
public ExprBoolean isnotnull() {return Grammar.isnotnull(this);}
public ExprBoolean isnull() {return Grammar.isnull(this);}
public String toString() {return path;}
}
public static class PathEntityRenamable<D> extends PathEntity<D>{
protected PathEntityRenamable(Class<D> type, String path) {super(type,path);}
public AliasEntity<D> as(PathEntity<D> to) {return Grammar.as(this, to);}
}
public interface PathNoEntity<D> extends Path<D>{
Expr<D> as(String to);
}
public static class PathNoEntitySimple<D> extends ExprNoEntity<D> implements PathNoEntity<D>{
private final String path;
public PathNoEntitySimple(Class<D> type, String path) {
super(type);
this.path = path;
}
public ExprBoolean isnotnull() {return Grammar.isnotnull(this);}
public ExprBoolean isnull() {return Grammar.isnull(this);}
public String toString() {return path;}
}
public static class PathString extends ExprString implements PathNoEntity<String>{
private final String path;
public PathString(String path) {
this.path = path;
}
public ExprBoolean isnotnull() {return Grammar.isnotnull(this);}
public ExprBoolean isnull() {return Grammar.isnull(this);}
public String toString() {return path;}
}
}

View File

@ -0,0 +1,100 @@
/*
* Copyright (c) 2008 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query.grammar;
import java.lang.reflect.Method;
import com.mysema.core.collection.FactoryMap;
import com.mysema.query.grammar.Types.*;
/**
* Visitor provides a dispatching Visitor for Expr instances
*
* @author tiwe
* @version $Id$
*/
public abstract class Visitor<T extends Visitor<T>> {
private final FactoryMap<Class<?>, Method> methodMap = new FactoryMap<Class<?>, Method>() {
@Override
protected Method create(Class<?> cl) {
try {
if (PathEntity.class.isAssignableFrom(cl)) {
cl = PathEntity.class;
}
Method method = null;
Class<?> sigClass = Visitor.this.getClass();
while (method == null && !sigClass.equals(Visitor.class)) {
try {
method = sigClass.getDeclaredMethod("visit", cl);
} catch (NoSuchMethodException nsme) {
sigClass = sigClass.getSuperclass();
}
}
if (method != null) {
method.setAccessible(true);
} else {
throw new IllegalArgumentException("No method found for "
+ cl.getSimpleName());
}
return method;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
};
@SuppressWarnings("unchecked")
public final T handle(Expr<?> expr) {
try {
methodMap.get(expr.getClass()).invoke(this, expr);
} catch (Exception e) {
throw new RuntimeException(e);
}
return (T) this;
}
protected abstract void visit(Alias<?> expr);
protected abstract void visit(AliasCollection<?> expr);
protected abstract void visit(AliasEntity<?> expr);
protected abstract void visit(AliasNoEntity<?> expr);
protected abstract void visit(ConstantExpr<?> expr);
protected abstract void visit(Operation<?, ?> expr);
protected abstract void visit(OperationBoolean expr);
protected abstract void visit(OperationComparable<?,?> expr);
protected abstract void visit(OperationNumber<?> expr);
protected abstract void visit(OperationString expr);
protected abstract void visit(Path<?> expr);
protected abstract void visit(PathBoolean expr);
protected abstract void visit(PathComparable<?> expr);
protected abstract void visit(PathEntity<?> expr);
protected abstract void visit(PathEntityCollection<?> expr);
protected abstract void visit(PathEntityRenamable<?> expr);
protected abstract void visit(PathNoEntity<?> expr);
protected abstract void visit(PathNoEntitySimple<?> expr);
protected abstract void visit(PathString expr);
}

View File

@ -0,0 +1,80 @@
/*
* Copyright (c) 2008 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query.grammar;
import com.mysema.query.grammar.Types.*;
/**
* VisitorAdapter provides a base implementation where invocations are
* dispatched to supertypes when available and visible
*
* @author tiwe
* @version $Id$
*/
public abstract class VisitorAdapter<V extends VisitorAdapter<V>> extends
Visitor<V> {
@Override
protected void visit(AliasCollection<?> expr) {
visit((Alias<?>) expr);
}
@Override
protected void visit(AliasEntity<?> expr) {
visit((Alias<?>) expr);
}
@Override
protected void visit(AliasNoEntity<?> expr) {
visit((Alias<?>) expr);
}
@Override
protected void visit(OperationBoolean expr) {
visit((Operation<?, ?>) expr);
}
@Override
protected void visit(OperationComparable<?,?> expr) {
visit((Operation<?, ?>) expr);
}
@Override
protected void visit(OperationNumber<?> expr) {
visit((Operation<?, ?>) expr);
}
@Override
protected void visit(OperationString expr) {
visit((Operation<?, ?>) expr);
}
@Override
protected void visit(PathBoolean expr) {
visit((Path<?>) expr);
}
@Override
protected void visit(PathComparable<?> expr) {
visit((Path<?>) expr);
}
@Override
protected void visit(PathString expr) {
visit((Path<?>) expr);
}
@Override
protected void visit(PathEntity<?> expr) {
visit((Path<?>) expr);
}
@Override
protected void visit(PathEntityRenamable<?> expr) {
visit((PathEntity<?>) expr);
}
@Override
protected void visit(PathEntityCollection<?> expr) {
visit((Path<?>) expr);
}
@Override
protected void visit(PathNoEntity<?> expr) {
visit((Path<?>) expr);
}
@Override
protected void visit(PathNoEntitySimple<?> expr) {
visit((Path<?>) expr);
}
}

View File

@ -0,0 +1,27 @@
package com.mysema.query.grammar;
import org.junit.Test;
import com.mysema.query.grammar.Types.Expr;
/**
* VisitorTest provides
*
* @author tiwe
* @version $Id$
*/
public class VisitorTest {
@Test
public void testIteration() throws SecurityException, NoSuchMethodException{
for (Class<?> innerType : Types.class.getClasses()){
if (!innerType.isInterface()
&& Expr.class.isAssignableFrom(innerType)
&& !innerType.getSimpleName().startsWith("Expr")){
Visitor.class.getDeclaredMethod("visit", innerType);
}
}
}
}