improved MiniApi with factory methods for paths

This commit is contained in:
Timo Westkämper 2009-01-04 14:08:53 +00:00
parent 5f18e19019
commit 08bc0107ed
13 changed files with 120 additions and 112 deletions

View File

@ -4,9 +4,4 @@ $ mvn clean generate-test-sources install -Dtest
* Or when creating Eclipse project files:
$ mvn clean generate-test-sources eclipse:clean eclipse:eclipse -DdownloadSources=true install -Dtest
* For tests, configure your JDBC connection in (see default.properties.sample for an example)
/querydsl-hql/src/test/resources/com/mysema/query/hql/default.properties
$ mvn clean generate-test-sources eclipse:clean eclipse:eclipse -DdownloadSources=true install -Dtest

View File

@ -39,11 +39,10 @@
<!-- test -->
<!-- TODO : replace MySQL dependency with embedded database -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.2</version>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.7</version>
<scope>test</scope>
<!-- license : TODO -->
</dependency>

View File

@ -32,42 +32,42 @@ public class HqlGrammar extends Grammar{
public static <D> Expr<D> all(CollectionType<D> col){
return new Quant.Simple<D>(OpQuant.ALL, col);
}
public static <D extends Comparable<D>> Expr.Comparable<D> all(CollectionType<D> col){
public static <D extends Comparable<D>> Expr.EComparable<D> all(CollectionType<D> col){
return new Quant.Comparable<D>(OpQuant.ALL, col);
}
public static <D> Expr.Simple<D> any(CollectionType<D> col){
public static <D> Expr.ESimple<D> any(CollectionType<D> col){
return new Quant.Simple<D>(OpQuant.ANY, col);
}
public static <D extends Comparable<D>> Expr.Comparable<D> any(CollectionType<D> col){
public static <D extends Comparable<D>> Expr.EComparable<D> any(CollectionType<D> col){
return new Quant.Comparable<D>(OpQuant.ANY, col);
}
public static <A extends Comparable<A>> Expr.Comparable<A> avg(Expr<A> left){
public static <A extends Comparable<A>> Expr.EComparable<A> avg(Expr<A> left){
return createNumber(OpNumberAgg.AVG, left);
}
public static <A extends Comparable<A>> Expr.Comparable<A> avg(Path.Collection<A> left){
public static <A extends Comparable<A>> Expr.EComparable<A> avg(Path.PCollection<A> left){
return new Quant.Comparable<A>(OpQuant.AVG_IN_COL, left);
}
public static Expr.Comparable<Long> count(){
public static Expr.EComparable<Long> count(){
return new CountExpression(null);
}
public static Expr.Comparable<Long> count(Expr<?> expr){
public static Expr.EComparable<Long> count(Expr<?> expr){
return new CountExpression(expr);
}
public static Expr.Comparable<Date> current_date(){
public static Expr.EComparable<Date> current_date(){
return createComparable(OpHql.CURRENT_DATE);
}
public static Expr.Comparable<Date> current_time(){
public static Expr.EComparable<Date> current_time(){
return createComparable(OpHql.CURRENT_TIME);
}
public static Expr.Comparable<Date> current_timestamp(){
public static Expr.EComparable<Date> current_timestamp(){
return createComparable(OpHql.CURRENT_TIMESTAMP);
}
public static Expr.Comparable<Date> day(Expr<Date> date){
public static Expr.EComparable<Date> day(Expr<Date> date){
return createComparable(OpHql.DAY, date);
}
@ -75,87 +75,87 @@ public class HqlGrammar extends Grammar{
return new DistinctPath<T>(left);
}
public static <D> Expr.Boolean exists(CollectionType<D> col){
public static <D> Expr.EBoolean exists(CollectionType<D> col){
return new Quant.Boolean<D>(OpQuant.EXISTS, col);
}
public static <A> SubQuery<A> from(Expr.Entity<A> select){
public static <A> SubQuery<A> from(Expr.EEntity<A> select){
return new SubQuery<A>(select).from(select);
}
public static Expr.Comparable<Date> hour(Expr<Date> date){
public static Expr.EComparable<Date> hour(Expr<Date> date){
return createComparable(OpHql.HOUR, date);
}
public static Path.ComponentCollection<Integer> indices(Path.Collection<?> col){
return new Path.ComponentCollection<Integer>(Integer.class, new PathMetadata<Collection<Integer>>(col, null, HqlPathType.LISTINDICES));
public static Path.PComponentCollection<Integer> indices(Path.PCollection<?> col){
return new Path.PComponentCollection<Integer>(Integer.class, new PathMetadata<Collection<Integer>>(col, null, HqlPathType.LISTINDICES));
}
public static <K,V> Path.ComponentCollection<K> indices(Path.Map<K,V> col){
return new Path.ComponentCollection<K>(col.getKeyType(), new PathMetadata<Collection<Integer>>(col, null, HqlPathType.LISTINDICES));
public static <K,V> Path.PComponentCollection<K> indices(Path.PMap<K,V> col){
return new Path.PComponentCollection<K>(col.getKeyType(), new PathMetadata<Collection<Integer>>(col, null, HqlPathType.LISTINDICES));
}
public static Expr.Boolean isempty(Path.ComponentCollection<?> collection) {
public static Expr.EBoolean isempty(Path.PComponentCollection<?> collection) {
return createBoolean(OpHql.ISEMPTY, collection);
}
public static Expr.Boolean isempty(Path.EntityCollection<?> collection) {
public static Expr.EBoolean isempty(Path.PEntityCollection<?> collection) {
return createBoolean(OpHql.ISEMPTY, collection);
}
public static Expr.Boolean isnotempty(Path.ComponentCollection<?> collection) {
public static Expr.EBoolean isnotempty(Path.PComponentCollection<?> collection) {
return createBoolean(OpHql.ISNOTEMPTY, collection);
}
public static Expr.Boolean isnotempty(Path.EntityCollection<?> collection) {
public static Expr.EBoolean isnotempty(Path.PEntityCollection<?> collection) {
return createBoolean(OpHql.ISNOTEMPTY, collection);
}
public static <A extends Comparable<A>> Expr.Comparable<A> max(Expr<A> left){
public static <A extends Comparable<A>> Expr.EComparable<A> max(Expr<A> left){
return createNumber(OpNumberAgg.MAX, left);
}
public static <A extends Comparable<A>> Expr.Comparable<A> max(Path.Collection<A> left){
public static <A extends Comparable<A>> Expr.EComparable<A> max(Path.PCollection<A> left){
return new Quant.Comparable<A>(OpQuant.MAX_IN_COL, left);
}
public static <A> Path.Entity<A> maxelement(Path.EntityCollection<A> col) {
return new Path.Entity<A>(col.getElementType(), new PathMetadata<A>(col, null, HqlPathType.MINELEMENT));
public static <A> Path.PEntity<A> maxelement(Path.PEntityCollection<A> col) {
return new Path.PEntity<A>(col.getElementType(), new PathMetadata<A>(col, null, HqlPathType.MINELEMENT));
}
public static <A> Path.Comparable<Integer> maxindex(Path.ComponentCollection<A> col) {
return new Path.Comparable<Integer>(Integer.class, new PathMetadata<Integer>(col, null, HqlPathType.MAXINDEX));
public static <A> Path.PComparable<Integer> maxindex(Path.PComponentCollection<A> col) {
return new Path.PComparable<Integer>(Integer.class, new PathMetadata<Integer>(col, null, HqlPathType.MAXINDEX));
}
public static <A> Path.Comparable<Integer> maxindex(Path.EntityCollection<A> col) {
return new Path.Comparable<Integer>(Integer.class, new PathMetadata<Integer>(col, null, HqlPathType.MAXINDEX));
public static <A> Path.PComparable<Integer> maxindex(Path.PEntityCollection<A> col) {
return new Path.PComparable<Integer>(Integer.class, new PathMetadata<Integer>(col, null, HqlPathType.MAXINDEX));
}
public static <A extends Comparable<A>> Expr.Comparable<A> min(Expr<A> left){
public static <A extends Comparable<A>> Expr.EComparable<A> min(Expr<A> left){
return createNumber(OpNumberAgg.MIN, left);
}
public static <A extends Comparable<A>> Expr.Comparable<A> min(Path.Collection<A> left){
public static <A extends Comparable<A>> Expr.EComparable<A> min(Path.PCollection<A> left){
return new Quant.Comparable<A>(OpQuant.MIN_IN_COL, left);
}
public static <A> Path.Entity<A> minelement(Path.EntityCollection<A> col) {
return new Path.Entity<A>(col.getElementType(), new PathMetadata<A>(col, null, HqlPathType.MINELEMENT));
public static <A> Path.PEntity<A> minelement(Path.PEntityCollection<A> col) {
return new Path.PEntity<A>(col.getElementType(), new PathMetadata<A>(col, null, HqlPathType.MINELEMENT));
}
public static <A> Path.Comparable<Integer> minindex(Path.ComponentCollection<A> col) {
return new Path.Comparable<Integer>(Integer.class, new PathMetadata<Integer>(col, null, HqlPathType.MININDEX));
public static <A> Path.PComparable<Integer> minindex(Path.PComponentCollection<A> col) {
return new Path.PComparable<Integer>(Integer.class, new PathMetadata<Integer>(col, null, HqlPathType.MININDEX));
}
public static <A> Path.Comparable<Integer> minindex(Path.EntityCollection<A> col) {
return new Path.Comparable<Integer>(Integer.class, new PathMetadata<Integer>(col, null, HqlPathType.MININDEX));
public static <A> Path.PComparable<Integer> minindex(Path.PEntityCollection<A> col) {
return new Path.PComparable<Integer>(Integer.class, new PathMetadata<Integer>(col, null, HqlPathType.MININDEX));
}
public static Expr.Comparable<Date> minute(Expr<Date> date){
public static Expr.EComparable<Date> minute(Expr<Date> date){
return createComparable(OpHql.MINUTE, date);
}
public static Expr.Comparable<Date> month(Expr<Date> date){
public static Expr.EComparable<Date> month(Expr<Date> date){
return createComparable(OpHql.MONTH, date);
}
@ -163,11 +163,11 @@ public class HqlGrammar extends Grammar{
return new Constructor<A>(a,args);
}
public static <D> Expr.Boolean notExists(CollectionType<D> col){
public static <D> Expr.EBoolean notExists(CollectionType<D> col){
return new Quant.Boolean<D>(OpQuant.NOTEXISTS, col);
}
public static Expr.Comparable<Date> second(Expr<Date> date){
public static Expr.EComparable<Date> second(Expr<Date> date){
return createComparable(OpHql.SECOND, date);
}
@ -179,15 +179,15 @@ public class HqlGrammar extends Grammar{
return any(col);
}
public static <D extends Comparable<D>> Expr.Comparable<D> sum(Expr<D> left){
public static <D extends Comparable<D>> Expr.EComparable<D> sum(Expr<D> left){
return createNumber(OpHql.SUM, left);
}
public static Expr.Comparable<Date> sysdate(){
public static Expr.EComparable<Date> sysdate(){
return createComparable(OpHql.SYSDATE);
}
public static Expr.Comparable<Date> year(Expr<Date> date){
public static Expr.EComparable<Date> year(Expr<Date> date){
return createComparable(OpHql.YEAR, date);
}

View File

@ -5,9 +5,9 @@
*/
package com.mysema.query.grammar;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.List;
import com.mysema.query.grammar.Ops.Op;
import com.mysema.query.grammar.types.PathMetadata;
@ -22,17 +22,13 @@ import com.mysema.query.serialization.OperationPatterns;
*/
public class HqlOps extends OperationPatterns {
public static final Set<Op<?>> wrapCollectionsForOp;
public static final List<Op<?>> wrapCollectionsForOp;
static{
Set<Op<?>> ops = new HashSet<Op<?>>();
ops.add(Ops.IN);
ops.add(Ops.NOTIN);
ops.add(OpQuant.ALL);
ops.add(OpQuant.ANY);
ops.add(OpQuant.EXISTS);
ops.add(OpQuant.NOTEXISTS);
wrapCollectionsForOp = Collections.unmodifiableSet(ops);
wrapCollectionsForOp = Collections.<Op<?>>unmodifiableList(Arrays.<Op<?>>asList(
Ops.IN, Ops.NOTIN,
OpQuant.ALL, OpQuant.ANY,
OpQuant.EXISTS, OpQuant.NOTEXISTS));
}
public HqlOps(){

View File

@ -18,7 +18,7 @@ import com.mysema.query.QueryBase;
import com.mysema.query.grammar.types.Expr;
import com.mysema.query.grammar.types.Path;
import com.mysema.query.grammar.types.PathMetadata;
import com.mysema.query.grammar.types.Expr.Entity;
import com.mysema.query.grammar.types.Expr.EEntity;
import com.mysema.query.hql.QueryModifiers;
/**
@ -61,12 +61,12 @@ public abstract class HqlQueryBase<A extends HqlQueryBase<A>> extends QueryBase<
countRowsString = null;
}
protected Expr.Boolean createQBECondition(Path.Entity<?> entity,
protected Expr.EBoolean createQBECondition(Path.PEntity<?> entity,
Map<String, Object> map) {
CascadingBoolean expr = new CascadingBoolean();
for (Map.Entry<String, Object> entry : map.entrySet()){
PathMetadata<String> md = PathMetadata.forProperty(entity, entry.getKey());
Path.Simple<Object> path = new Path.Simple<Object>(Object.class, md);
Path.PSimple<Object> path = new Path.PSimple<Object>(Object.class, md);
if (entry.getValue() != null){
expr.and(path.eq(entry.getValue()));
}else{
@ -76,7 +76,7 @@ public abstract class HqlQueryBase<A extends HqlQueryBase<A>> extends QueryBase<
return expr.self();
}
public A forExample(Path.Entity<?> entity, Map<String, Object> map) {
public A forExample(Path.PEntity<?> entity, Map<String, Object> map) {
select(entity).from(entity);
try {
where(createQBECondition(entity,map));
@ -93,13 +93,13 @@ public abstract class HqlQueryBase<A extends HqlQueryBase<A>> extends QueryBase<
}
@SuppressWarnings("unchecked")
public A innerJoin(JoinMeta meta, Entity<?> o) {
public A innerJoin(JoinMeta meta, EEntity<?> o) {
joins.add(new JoinExpression<JoinMeta>(JoinType.INNERJOIN, o, meta));
return (A) this;
}
@SuppressWarnings("unchecked")
public A leftJoin(JoinMeta meta, Entity<?> o) {
public A leftJoin(JoinMeta meta, EEntity<?> o) {
joins.add(new JoinExpression<JoinMeta>(JoinType.LEFTJOIN, o, meta));
return (A) this;
}

View File

@ -41,7 +41,7 @@ public class HqlSerializer extends BaseSerializer<HqlSerializer>{
}
public void serialize(List<Expr<?>> select, List<JoinExpression<JoinMeta>> joins,
Expr.Boolean where, List<Expr<?>> groupBy, Expr.Boolean having,
Expr.EBoolean where, List<Expr<?>> groupBy, Expr.EBoolean having,
List<OrderSpecifier<?>> orderBy, boolean forCountRow){
if (forCountRow){
_append("select count(*)\n");
@ -68,8 +68,8 @@ public class HqlSerializer extends BaseSerializer<HqlSerializer>{
}
// type specifier
if (je.getTarget() instanceof Path.Entity){
Path.Entity<?> pe = (Path.Entity<?>)je.getTarget();
if (je.getTarget() instanceof Path.PEntity){
Path.PEntity<?> pe = (Path.PEntity<?>)je.getTarget();
if (pe.getMetadata().getParent() == null){
String pn = pe.getType().getPackage().getName();
String typeName = pe.getType().getName().substring(pn.length()+1);
@ -107,12 +107,12 @@ public class HqlSerializer extends BaseSerializer<HqlSerializer>{
}
@Override
protected void visit(Alias.Simple<?> expr) {
protected void visit(Alias.ASimple<?> expr) {
handle(expr.getFrom())._append(" as ")._append(expr.getTo());
}
@Override
protected void visit(Alias.ToPath expr) {
protected void visit(Alias.AToPath expr) {
handle(expr.getFrom())._append(" as ").visit(expr.getTo());
}
@ -149,7 +149,7 @@ public class HqlSerializer extends BaseSerializer<HqlSerializer>{
}
@Override
protected void visit(Expr.Constant<?> expr) {
protected void visit(Expr.EConstant<?> expr) {
boolean wrap = expr.getConstant().getClass().isArray();
if (wrap) _append("(");
_append(":a");
@ -163,7 +163,7 @@ public class HqlSerializer extends BaseSerializer<HqlSerializer>{
}
@Override
protected void visit(Path.Collection<?> expr){
protected void visit(Path.PCollection<?> expr){
// only wrap a PathCollection, if it the pathType is PROPERTY
boolean wrap = wrapElements && expr.getMetadata().getPathType().equals(PROPERTY);
if (wrap) _append("elements(");

View File

@ -19,13 +19,13 @@ public interface Custom<T> {
/**
* The Class Boolean.
*/
public static abstract class Boolean extends Expr.Boolean implements Custom<java.lang.Boolean>{ }
public static abstract class Boolean extends Expr.EBoolean implements Custom<java.lang.Boolean>{ }
/**
* The Class Comparable.
*/
public static abstract class Comparable<T extends java.lang.Comparable<T>>
extends Expr.Comparable<T> implements Custom<T>{
extends Expr.EComparable<T> implements Custom<T>{
public Comparable(Class<T> type) {
super(type);
}
@ -34,6 +34,6 @@ public interface Custom<T> {
/**
* The Class String.
*/
public static abstract class String extends Expr.String implements Custom<java.lang.String>{ }
public static abstract class String extends Expr.EString implements Custom<java.lang.String>{ }
}

View File

@ -18,10 +18,12 @@ import com.mysema.query.grammar.OrderSpecifier;
*/
public class HqlTypes {
private HqlTypes(){}
/**
* The Class CountExpression.
*/
public static class CountExpression extends Expr.Comparable<Long>{
public static class CountExpression extends Expr.EComparable<Long>{
private final Expr<?> target;
public CountExpression(Expr<?> expr) {
super(Long.class);
@ -54,18 +56,18 @@ public class HqlTypes {
query.s(select);
}
@SuppressWarnings("unchecked")
public SubQuery<A> from(Entity... o) {query.from(o); return this;}
public SubQuery<A> fullJoin(Entity<?> o) {query.fullJoin(o); return this;}
public SubQuery<A> from(EEntity... o) {query.from(o); return this;}
public SubQuery<A> fullJoin(EEntity<?> o) {query.fullJoin(o); return this;}
public QueryBase<JoinMeta,?> getQuery(){ return query;}
public SubQuery<A> groupBy(Expr<?>... o) {query.groupBy(o); return this;}
public SubQuery<A> having(Boolean o) {query.having(o); return this;}
public SubQuery<A> innerJoin(Entity<?> o) {query.innerJoin(o); return this;}
public SubQuery<A> join(Entity<?> o) {query.join(o); return this;}
public SubQuery<A> leftJoin(Entity<?> o) {query.leftJoin(o); return this;}
public SubQuery<A> having(EBoolean o) {query.having(o); return this;}
public SubQuery<A> innerJoin(EEntity<?> o) {query.innerJoin(o); return this;}
public SubQuery<A> join(EEntity<?> o) {query.join(o); return this;}
public SubQuery<A> leftJoin(EEntity<?> o) {query.leftJoin(o); return this;}
public SubQuery<A> orderBy(OrderSpecifier<?>... o) {query.orderBy(o); return this;}
public SubQuery<A> select(Expr<?>... o) {query.s(o); return this;}
public SubQuery<A> where(Boolean o) {query.where(o); return this;}
public SubQuery<A> with(Boolean o) {query.with(o); return this;}
public SubQuery<A> where(EBoolean o) {query.where(o); return this;}
public SubQuery<A> with(EBoolean o) {query.with(o); return this;}
}
private static class QueryWithPublicSelect extends QueryBase<JoinMeta,QueryWithPublicSelect>{

View File

@ -21,7 +21,7 @@ public interface Quant {
/**
* The Class Boolean.
*/
public static class Boolean<Q> extends Expr.Boolean implements Quant{
public static class Boolean<Q> extends Expr.EBoolean implements Quant{
private final Expr<?> col;
private final Op<?> op;
public Boolean(Op<?> op, CollectionType<Q> col) {
@ -35,7 +35,7 @@ public interface Quant {
/**
* The Class Comparable.
*/
public static class Comparable<Q extends java.lang.Comparable<Q>> extends Expr.Comparable<Q> implements Quant{
public static class Comparable<Q extends java.lang.Comparable<Q>> extends Expr.EComparable<Q> implements Quant{
private final Expr<?> col;
private final Op<?> op;
public Comparable(Op<?> op, CollectionType<Q> col) {
@ -50,7 +50,7 @@ public interface Quant {
/**
* The Class Simple.
*/
public static class Simple<Q> extends Expr.Simple<Q> implements Quant{
public static class Simple<Q> extends Expr.ESimple<Q> implements Quant{
private final Expr<?> col;
private final Op<?> op;
public Simple(Op<?> op, CollectionType<Q> col) {

View File

@ -5,8 +5,13 @@
*/
package com.mysema.query.hql;
import static com.mysema.query.grammar.Grammar.div;
import static com.mysema.query.grammar.Grammar.sqrt;
import static com.mysema.query.grammar.HqlGrammar.avg;
import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.Test;
import org.junit.runner.RunWith;
import antlr.RecognitionException;
@ -43,6 +48,16 @@ public class HqlIntegrationTest extends HqlParserTest{
throw new RuntimeException(e);
}
}
@Test
public void testGroupBy() throws Exception {
// do nothing
}
@Test
public void testOrderBy() throws Exception {
// do nothing
}
public void setSession(Session session) {
this.session = session;

View File

@ -62,6 +62,7 @@ public class HibernateTestRunner extends JUnit4ClassRunner{
Hibernate config = getTestClass().getJavaClass().getAnnotation(Hibernate.class);
cfg.setNamingStrategy(config.namingStrategy().newInstance());
Properties props = new Properties();
// TODO : null check and error message
props.load(HqlIntegrationTest.class.getResourceAsStream(config.properties()));
cfg.setProperties(props);
sessionFactory = cfg.buildSessionFactory();

View File

@ -0,0 +1,18 @@
## HSQL
hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.connection.url=jdbc:hsqldb:file:target/testdb
hibernate.connection.driver_class=org.hsqldb.jdbcDriver
hibernate.connection.username=sa
hibernate.connection.password=
## MySQL
#hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.connection.driver_class=com.mysql.jdbc.Driver
#hibernate.connection.url=jdbc:mysql://localhost:3306/querydsl
#hibernate.connection.username=root
#hibernate.connection.password=
## Common properties
hibernate.show_sql=true
hibernate.flushMode=FLUSH_AUTO
hibernate.hbm2ddl.auto=update

View File

@ -1,18 +0,0 @@
## HSQL
#hibernate.dialect=org.hibernate.dialect.HSQLDialect
#hibernate.connection.url=jdbc:hsqldb:file:target/testdb
#hibernate.connection.driver_class=org.hsqldb.jdbcDriver
#hibernate.connection.username=sa
#hibernate.connection.password=
## MySQL
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/querydsl
hibernate.connection.username=root
hibernate.connection.password=
## Common properties
hibernate.show_sql=true
hibernate.flushMode=FLUSH_AUTO
hibernate.hbm2ddl.auto=update