mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-24 21:07:26 +08:00
added math methods to QMath
added subquery and union support to querydsl-sql moved SubQuery to querydsl-core
This commit is contained in:
parent
e2fee815a8
commit
0c531271fe
@ -17,7 +17,6 @@ import com.mysema.query.grammar.HqlOps.OpHql;
|
||||
import com.mysema.query.grammar.HqlOps.OpQuant;
|
||||
import com.mysema.query.grammar.types.*;
|
||||
import com.mysema.query.grammar.types.HqlTypes.DistinctPath;
|
||||
import com.mysema.query.grammar.types.HqlTypes.SubQuery;
|
||||
|
||||
/**
|
||||
* HqlGrammar extends the Query DSL base grammar to provide HQL specific syntax elements.
|
||||
@ -71,8 +70,8 @@ public class HqlGrammar extends GrammarWithAlias{
|
||||
return new Quant.Boolean<D>(OpQuant.EXISTS, col);
|
||||
}
|
||||
|
||||
public static <A> SubQuery<A> from(Expr.EEntity<A> select){
|
||||
return new SubQuery<A>(select).from(select);
|
||||
public static <A> SubQuery<HqlJoinMeta,A> from(Expr.EEntity<A> select){
|
||||
return new SubQuery<HqlJoinMeta,A>(select).from(select);
|
||||
}
|
||||
|
||||
public static Expr.EComparable<Date> hour(Expr<Date> date){
|
||||
@ -155,8 +154,8 @@ public class HqlGrammar extends GrammarWithAlias{
|
||||
return createComparable(OpHql.SECOND, date);
|
||||
}
|
||||
|
||||
public static <A> SubQuery<A> select(Expr<A> select){
|
||||
return new SubQuery<A>(select);
|
||||
public static <A> SubQuery<HqlJoinMeta,A> select(Expr<A> select){
|
||||
return new SubQuery<HqlJoinMeta,A>(select);
|
||||
}
|
||||
|
||||
public static <D> Expr<D> some(CollectionType<D> col){
|
||||
|
||||
@ -11,6 +11,6 @@ package com.mysema.query.grammar;
|
||||
* @author tiwe
|
||||
* @version $Id$
|
||||
*/
|
||||
public enum JoinMeta {
|
||||
public enum HqlJoinMeta {
|
||||
FETCH
|
||||
}
|
||||
@ -44,7 +44,7 @@ public class HqlOps extends OperationPatterns {
|
||||
add(Ops.NOTBETWEEN, "%s not between %s and %s",30);
|
||||
|
||||
// numeric
|
||||
add(Ops.SQRT, "sqrt(%s)");
|
||||
add(Ops.OpMath.SQRT, "sqrt(%s)");
|
||||
|
||||
// various
|
||||
add(Ops.IN, "%s in %s");
|
||||
|
||||
@ -27,7 +27,7 @@ import com.mysema.query.hql.QueryModifiers;
|
||||
* @author tiwe
|
||||
* @version $Id$
|
||||
*/
|
||||
public abstract class HqlQueryBase<A extends HqlQueryBase<A>> extends QueryBase<JoinMeta,A>{
|
||||
public abstract class HqlQueryBase<A extends HqlQueryBase<A>> extends QueryBase<HqlJoinMeta,A>{
|
||||
|
||||
private static final Logger logger = LoggerFactory
|
||||
.getLogger(HqlQueryBase.class);
|
||||
@ -46,7 +46,7 @@ public abstract class HqlQueryBase<A extends HqlQueryBase<A>> extends QueryBase<
|
||||
|
||||
private String buildQueryString(boolean forCountRow) {
|
||||
if (joins.isEmpty()){
|
||||
throw new IllegalArgumentException("No where clause given");
|
||||
throw new IllegalArgumentException("No joins given");
|
||||
}
|
||||
HqlSerializer serializer = new HqlSerializer(ops);
|
||||
serializer.serialize(select, joins, where.self(), groupBy, having.self(), orderBy, forCountRow);
|
||||
@ -94,14 +94,14 @@ public abstract class HqlQueryBase<A extends HqlQueryBase<A>> extends QueryBase<
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public A innerJoin(JoinMeta meta, EEntity<?> o) {
|
||||
joins.add(new JoinExpression<JoinMeta>(JoinType.INNERJOIN, o, meta));
|
||||
public A innerJoin(HqlJoinMeta meta, EEntity<?> o) {
|
||||
joins.add(new JoinExpression<HqlJoinMeta>(JoinType.INNERJOIN, o, meta));
|
||||
return (A) this;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public A leftJoin(JoinMeta meta, EEntity<?> o) {
|
||||
joins.add(new JoinExpression<JoinMeta>(JoinType.LEFTJOIN, o, meta));
|
||||
public A leftJoin(HqlJoinMeta meta, EEntity<?> o) {
|
||||
joins.add(new JoinExpression<HqlJoinMeta>(JoinType.LEFTJOIN, o, meta));
|
||||
return (A) this;
|
||||
}
|
||||
|
||||
|
||||
@ -14,7 +14,6 @@ import com.mysema.query.QueryBase;
|
||||
import com.mysema.query.grammar.Ops.Op;
|
||||
import com.mysema.query.grammar.types.*;
|
||||
import com.mysema.query.grammar.types.HqlTypes.DistinctPath;
|
||||
import com.mysema.query.grammar.types.HqlTypes.SubQuery;
|
||||
import com.mysema.query.serialization.BaseSerializer;
|
||||
|
||||
|
||||
@ -32,7 +31,7 @@ public class HqlSerializer extends BaseSerializer<HqlSerializer>{
|
||||
super(ops);
|
||||
}
|
||||
|
||||
public void serialize(List<Expr<?>> select, List<JoinExpression<JoinMeta>> joins,
|
||||
public void serialize(List<Expr<?>> select, List<JoinExpression<HqlJoinMeta>> joins,
|
||||
Expr.EBoolean where, List<Expr<?>> groupBy, Expr.EBoolean having,
|
||||
List<OrderSpecifier<?>> orderBy, boolean forCountRow){
|
||||
if (forCountRow){
|
||||
@ -42,7 +41,7 @@ public class HqlSerializer extends BaseSerializer<HqlSerializer>{
|
||||
}
|
||||
_append("from ");
|
||||
for (int i=0; i < joins.size(); i++){
|
||||
JoinExpression<JoinMeta> je = joins.get(i);
|
||||
JoinExpression<HqlJoinMeta> je = joins.get(i);
|
||||
if (i > 0){
|
||||
String sep = ", ";
|
||||
switch(je.getType()){
|
||||
@ -179,8 +178,8 @@ public class HqlSerializer extends BaseSerializer<HqlSerializer>{
|
||||
visit((Quant)q);
|
||||
}
|
||||
|
||||
protected void visit(SubQuery<?> query) {
|
||||
QueryBase<JoinMeta,?>.Metadata md = query.getQuery().getMetadata();
|
||||
protected void visit(SubQuery<HqlJoinMeta,?> query) {
|
||||
QueryBase<HqlJoinMeta,?>.Metadata md = query.getQuery().getMetadata();
|
||||
_append("(");
|
||||
serialize(md.getSelect(), md.getJoins(),
|
||||
md.getWhere(), md.getGroupBy(), md.getHaving(),
|
||||
|
||||
@ -5,10 +5,6 @@
|
||||
*/
|
||||
package com.mysema.query.grammar.types;
|
||||
|
||||
import com.mysema.query.Query;
|
||||
import com.mysema.query.QueryBase;
|
||||
import com.mysema.query.grammar.JoinMeta;
|
||||
import com.mysema.query.grammar.OrderSpecifier;
|
||||
|
||||
/**
|
||||
* HqlTypes provides general HQL specific types
|
||||
@ -33,35 +29,4 @@ public class HqlTypes {
|
||||
public Path<T> getPath(){ return path; }
|
||||
}
|
||||
|
||||
/**
|
||||
* The Class SubQuery.
|
||||
*/
|
||||
public static class SubQuery<A> extends Expr<A> implements Query<SubQuery<A>>, CollectionType<A>{
|
||||
@SuppressWarnings("unchecked")
|
||||
private QueryWithPublicSelect query = new QueryWithPublicSelect();
|
||||
public SubQuery(Expr<A> select) {
|
||||
super(null);
|
||||
query.s(select);
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
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(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(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>{
|
||||
public void s(Expr<?>... expr){
|
||||
select(expr);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -22,7 +22,8 @@ import antlr.TokenStreamException;
|
||||
import antlr.collections.AST;
|
||||
|
||||
import com.mysema.query.grammar.HqlGrammar;
|
||||
import com.mysema.query.grammar.JoinMeta;
|
||||
import com.mysema.query.grammar.HqlJoinMeta;
|
||||
import com.mysema.query.grammar.QMath;
|
||||
import com.mysema.query.hql.HqlDomain.*;
|
||||
|
||||
|
||||
@ -66,7 +67,7 @@ public class HqlParserTest extends QueryBaseWithDomain<HqlParserTest> {
|
||||
// parse( "from eg.Cat as cat join cat.mate as mate left join cat.kittens as kitten" );
|
||||
from(cat).join(cat.mate.as(mate)).leftJoin(cat.kittens.as(kitten)).parse();
|
||||
// parse( "from eg.Cat as cat\ninner join fetch cat.mate\nleft join fetch cat.kittens" );
|
||||
from(cat).innerJoin(cat.mate).leftJoin(JoinMeta.FETCH, cat.kittens).parse();
|
||||
from(cat).innerJoin(cat.mate).leftJoin(HqlJoinMeta.FETCH, cat.kittens).parse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -89,8 +90,8 @@ public class HqlParserTest extends QueryBaseWithDomain<HqlParserTest> {
|
||||
from($(f)).innerJoin($(c.getMate()).as($(m)))
|
||||
.leftJoin($(c.getKittens()).as($(k))).parse();
|
||||
// parse( "from eg.Cat as cat\ninner join fetch cat.mate\nleft join fetch cat.kittens" );
|
||||
from($(f)).innerJoin(JoinMeta.FETCH, $(c.getMate()).as($(m)))
|
||||
.leftJoin(JoinMeta.FETCH, $(c.getKittens()).as($(k))).parse();
|
||||
from($(f)).innerJoin(HqlJoinMeta.FETCH, $(c.getMate()).as($(m)))
|
||||
.leftJoin(HqlJoinMeta.FETCH, $(c.getKittens()).as($(k))).parse();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -478,7 +479,7 @@ public class HqlParserTest extends QueryBaseWithDomain<HqlParserTest> {
|
||||
// parse( "FROM eg.mypackage.Cat qat where qat.name not in ('crater','bean','fluffy')" );
|
||||
from(qat).where(qat.name.notIn("crater","bean","fluffy")).parse();
|
||||
// parse( "from Animal an where sqrt(an.bodyWeight)/2 > 10" );
|
||||
from(an).where(div(sqrt(an.bodyWeight),2).gt(10)).parse();
|
||||
from(an).where(div(QMath.sqrt(an.bodyWeight),2).gt(10)).parse();
|
||||
// parse( "from Animal an where (an.bodyWeight > 10 and an.bodyWeight < 100) or an.bodyWeight is null" );
|
||||
from(an).where(an.bodyWeight.gt(10).and(an.bodyWeight.lt(100).or(an.bodyWeight.isnull()))).parse();
|
||||
}
|
||||
@ -496,7 +497,7 @@ public class HqlParserTest extends QueryBaseWithDomain<HqlParserTest> {
|
||||
// parse( "FROM eg.mypackage.Cat qat order by avg(qat.toes)" );
|
||||
from(qat).orderBy(avg(qat.toes).asc()).parse();
|
||||
// parse( "from Animal an order by sqrt(an.bodyWeight)/2" );
|
||||
from(qat).orderBy(sqrt(div(an.bodyWeight,2)).asc()).parse();
|
||||
from(qat).orderBy(QMath.sqrt(div(an.bodyWeight,2)).asc()).parse();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Loading…
Reference in New Issue
Block a user