mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-13 21:01:01 +08:00
added QueryBaseWithProjection for Query implementations with projection
modified ColQuery, HqlQuery and SqlQuery to extend QueryBaseWithProjection modified Serializer implementations to user QueryMetadata instead of individual query fields
This commit is contained in:
parent
30a5f18560
commit
e7c2b2c778
@ -14,7 +14,7 @@ import org.slf4j.LoggerFactory;
|
||||
import com.mysema.query.CascadingBoolean;
|
||||
import com.mysema.query.JoinExpression;
|
||||
import com.mysema.query.JoinType;
|
||||
import com.mysema.query.QueryBase;
|
||||
import com.mysema.query.QueryBaseWithProjection;
|
||||
import com.mysema.query.grammar.types.Expr;
|
||||
import com.mysema.query.grammar.types.Path;
|
||||
import com.mysema.query.grammar.types.PathMetadata;
|
||||
@ -27,7 +27,7 @@ import com.mysema.query.hql.QueryModifiers;
|
||||
* @author tiwe
|
||||
* @version $Id$
|
||||
*/
|
||||
public abstract class HqlQueryBase<SubType extends HqlQueryBase<SubType>> extends QueryBase<HqlJoinMeta,SubType>{
|
||||
public abstract class HqlQueryBase<SubType extends HqlQueryBase<SubType>> extends QueryBaseWithProjection<HqlJoinMeta,SubType>{
|
||||
|
||||
private static final Logger logger = LoggerFactory
|
||||
.getLogger(HqlQueryBase.class);
|
||||
@ -48,11 +48,11 @@ public abstract class HqlQueryBase<SubType extends HqlQueryBase<SubType>> extend
|
||||
}
|
||||
|
||||
private String buildQueryString(boolean forCountRow) {
|
||||
if (joins.isEmpty()){
|
||||
if (getMetadata().getJoins().isEmpty()){
|
||||
throw new IllegalArgumentException("No joins given");
|
||||
}
|
||||
HqlSerializer serializer = new HqlSerializer(ops);
|
||||
serializer.serialize(select, joins, where.create(), groupBy, having.create(), orderBy, forCountRow);
|
||||
serializer.serialize(getMetadata(), forCountRow);
|
||||
constants = serializer.getConstants();
|
||||
return serializer.toString();
|
||||
}
|
||||
@ -80,7 +80,7 @@ public abstract class HqlQueryBase<SubType extends HqlQueryBase<SubType>> extend
|
||||
}
|
||||
|
||||
public SubType forExample(Path.PEntity<?> entity, Map<String, Object> map) {
|
||||
select(entity).from(entity);
|
||||
addToProjection(entity).from(entity);
|
||||
try {
|
||||
where(createQBECondition(entity,map));
|
||||
return _this;
|
||||
@ -96,17 +96,17 @@ public abstract class HqlQueryBase<SubType extends HqlQueryBase<SubType>> extend
|
||||
}
|
||||
|
||||
public SubType innerJoin(HqlJoinMeta meta, EEntity<?> o) {
|
||||
joins.add(new JoinExpression<HqlJoinMeta>(JoinType.INNERJOIN, o, meta));
|
||||
getMetadata().getJoins().add(new JoinExpression<HqlJoinMeta>(JoinType.INNERJOIN, o, meta));
|
||||
return _this;
|
||||
}
|
||||
|
||||
public SubType fullJoin(HqlJoinMeta meta, EEntity<?> o) {
|
||||
joins.add(new JoinExpression<HqlJoinMeta>(JoinType.FULLJOIN, o, meta));
|
||||
getMetadata().getJoins().add(new JoinExpression<HqlJoinMeta>(JoinType.FULLJOIN, o, meta));
|
||||
return _this;
|
||||
}
|
||||
|
||||
public SubType leftJoin(HqlJoinMeta meta, EEntity<?> o) {
|
||||
joins.add(new JoinExpression<HqlJoinMeta>(JoinType.LEFTJOIN, o, meta));
|
||||
getMetadata().getJoins().add(new JoinExpression<HqlJoinMeta>(JoinType.LEFTJOIN, o, meta));
|
||||
return _this;
|
||||
}
|
||||
|
||||
|
||||
@ -38,9 +38,14 @@ public class HqlSerializer extends BaseSerializer<HqlSerializer>{
|
||||
super(ops);
|
||||
}
|
||||
|
||||
public void serialize(List<? extends Expr<?>> select, List<JoinExpression<HqlJoinMeta>> joins,
|
||||
Expr.EBoolean where, List<? extends Expr<?>> groupBy, Expr.EBoolean having,
|
||||
List<OrderSpecifier<?>> orderBy, boolean forCountRow){
|
||||
public void serialize(QueryMetadata<HqlJoinMeta> metadata, boolean forCountRow){
|
||||
List<? extends Expr<?>> select = metadata.getSelect();
|
||||
List<JoinExpression<HqlJoinMeta>> joins = metadata.getJoins();
|
||||
Expr.EBoolean where = metadata.getWhere();
|
||||
List<? extends Expr<?>> groupBy = metadata.getGroupBy();
|
||||
Expr.EBoolean having = metadata.getHaving();
|
||||
List<OrderSpecifier<?>> orderBy = metadata.getOrderBy();
|
||||
|
||||
if (forCountRow){
|
||||
append("select count(*)\n");
|
||||
}else if (!select.isEmpty()){
|
||||
@ -175,11 +180,8 @@ public class HqlSerializer extends BaseSerializer<HqlSerializer>{
|
||||
}
|
||||
|
||||
protected void visit(SubQuery<HqlJoinMeta,?> query) {
|
||||
QueryMetadata<HqlJoinMeta> md = query.getQuery().getMetadata();
|
||||
append("(");
|
||||
serialize(md.getSelect(), md.getJoins(),
|
||||
md.getWhere(), md.getGroupBy(), md.getHaving(),
|
||||
md.getOrderBy(), false);
|
||||
serialize(query.getQuery().getMetadata(), false);
|
||||
append(")");
|
||||
}
|
||||
|
||||
|
||||
@ -69,7 +69,7 @@ public class AbstractHqlQuery<A extends AbstractHqlQuery<A>> extends HqlQueryBas
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <RT> List<RT> list(Expr<RT> expr){
|
||||
select(expr);
|
||||
addToProjection(expr);
|
||||
String queryString = toString();
|
||||
logger.debug("query : {}", queryString);
|
||||
Query query = createQuery(queryString, limit, offset);
|
||||
@ -78,8 +78,8 @@ public class AbstractHqlQuery<A extends AbstractHqlQuery<A>> extends HqlQueryBas
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Object[]> list(Expr<?> expr1, Expr<?> expr2, Expr<?>...rest){
|
||||
select(expr1, expr2);
|
||||
select(rest);
|
||||
addToProjection(expr1, expr2);
|
||||
addToProjection(rest);
|
||||
String queryString = toString();
|
||||
logger.debug("query : {}", queryString);
|
||||
Query query = createQuery(queryString, limit, offset);
|
||||
@ -87,7 +87,7 @@ public class AbstractHqlQuery<A extends AbstractHqlQuery<A>> extends HqlQueryBas
|
||||
}
|
||||
|
||||
public <RT> SearchResults<RT> listResults(Expr<RT> expr) {
|
||||
select(expr);
|
||||
addToProjection(expr);
|
||||
Query query = createQuery(toCountRowsString(), null, null);
|
||||
long total = (Long) query.uniqueResult();
|
||||
if (total > 0) {
|
||||
@ -114,7 +114,7 @@ public class AbstractHqlQuery<A extends AbstractHqlQuery<A>> extends HqlQueryBas
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <RT> RT uniqueResult(Expr<RT> expr) {
|
||||
select(expr);
|
||||
addToProjection(expr);
|
||||
String queryString = toString();
|
||||
logger.debug("query : {}", queryString);
|
||||
Query query = createQuery(queryString, 1, null);
|
||||
@ -122,10 +122,12 @@ public class AbstractHqlQuery<A extends AbstractHqlQuery<A>> extends HqlQueryBas
|
||||
}
|
||||
|
||||
public Iterator<Object[]> iterate(Expr<?> e1, Expr<?> e2, Expr<?>... rest) {
|
||||
// TODO : optimize
|
||||
return list(e1, e2, rest).iterator();
|
||||
}
|
||||
|
||||
public <RT> Iterator<RT> iterate(Expr<RT> projection) {
|
||||
// TODO : optimize
|
||||
return list(projection).iterator();
|
||||
}
|
||||
|
||||
|
||||
@ -5,6 +5,7 @@
|
||||
*/
|
||||
package com.mysema.query.hql;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
@ -13,6 +14,7 @@ import javax.persistence.Query;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.mysema.query.grammar.HqlGrammar;
|
||||
import com.mysema.query.grammar.HqlOps;
|
||||
import com.mysema.query.grammar.HqlQueryBase;
|
||||
import com.mysema.query.grammar.types.Expr;
|
||||
@ -56,7 +58,7 @@ public class JpaqlQuery<A extends JpaqlQuery<A>> extends HqlQueryBase<A>{
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <RT> List<RT> list(Expr<RT> expr){
|
||||
select(expr);
|
||||
addToProjection(expr);
|
||||
String queryString = toString();
|
||||
logger.debug("query : {}", queryString);
|
||||
Query query = createQuery(queryString, limit, offset);
|
||||
@ -65,13 +67,22 @@ public class JpaqlQuery<A extends JpaqlQuery<A>> extends HqlQueryBase<A>{
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Object[]> list(Expr<?> expr1, Expr<?> expr2, Expr<?>...rest){
|
||||
select(expr1, expr2);
|
||||
select(rest);
|
||||
addToProjection(expr1, expr2);
|
||||
addToProjection(rest);
|
||||
String queryString = toString();
|
||||
logger.debug("query : {}", queryString);
|
||||
Query query = createQuery(queryString, limit, offset);
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
public long count() {
|
||||
return uniqueResult(HqlGrammar.count());
|
||||
}
|
||||
|
||||
public <RT> Iterator<RT> iterate(Expr<RT> projection) {
|
||||
// TODO Auto-generated method stub
|
||||
return list(projection).iterator();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
*/
|
||||
package com.mysema.query.hql;
|
||||
|
||||
import static com.mysema.query.grammar.Grammar.count;
|
||||
import com.mysema.query.grammar.Grammar;
|
||||
import static com.mysema.query.grammar.Grammar.not;
|
||||
import static com.mysema.query.grammar.GrammarWithAlias.$;
|
||||
import static com.mysema.query.grammar.HqlGrammar.*;
|
||||
@ -20,6 +20,7 @@ import static org.junit.Assert.assertNull;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
@ -335,7 +336,7 @@ public class FeaturesTest extends HqlQueryBase<FeaturesTest>{
|
||||
@Test
|
||||
public void testSimpleAliasForNonEntityPaths(){
|
||||
toString("cat.bodyWeight as catbodyWeight", cat.bodyWeight.as("catbodyWeight"));
|
||||
toString("count(*) as numPosts", count().as("numPosts"));
|
||||
toString("count(*) as numPosts", Grammar.count().as("numPosts"));
|
||||
toString("cat.bodyWeight + kitten.bodyWeight as abc", add(cat.bodyWeight,kitten.bodyWeight).as("abc"));
|
||||
}
|
||||
|
||||
@ -387,9 +388,9 @@ public class FeaturesTest extends HqlQueryBase<FeaturesTest>{
|
||||
|
||||
toString("distinct cat.bodyWeight", distinct(cat.bodyWeight));
|
||||
|
||||
toString("count(*)", count());
|
||||
toString("count(distinct cat.bodyWeight)", count(distinct(cat.bodyWeight)));
|
||||
toString("count(cat)", count(cat));
|
||||
toString("count(*)", Grammar.count());
|
||||
toString("count(distinct cat.bodyWeight)", Grammar.count(distinct(cat.bodyWeight)));
|
||||
toString("count(cat)", Grammar.count(cat));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -462,5 +463,14 @@ public class FeaturesTest extends HqlQueryBase<FeaturesTest>{
|
||||
public static final class BookmarkDTO{
|
||||
|
||||
}
|
||||
|
||||
|
||||
public long count() {
|
||||
// TODO Auto-generated method stub
|
||||
return 0;
|
||||
}
|
||||
|
||||
public <RT> Iterator<RT> iterate(Expr<RT> projection) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,9 +5,8 @@
|
||||
*/
|
||||
package com.mysema.query.hql;
|
||||
|
||||
//import static com.mysema.query.grammar.Grammar.avg;
|
||||
import static com.mysema.query.grammar.Grammar.avg;
|
||||
import static com.mysema.query.grammar.Grammar.count;
|
||||
import com.mysema.query.grammar.Grammar;
|
||||
import static com.mysema.query.grammar.Grammar.in;
|
||||
import static com.mysema.query.grammar.Grammar.not;
|
||||
import static com.mysema.query.grammar.GrammarWithAlias.$;
|
||||
@ -18,6 +17,7 @@ import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.hibernate.hql.ast.HqlParser;
|
||||
import org.junit.Test;
|
||||
@ -29,6 +29,7 @@ import antlr.collections.AST;
|
||||
import com.mysema.query.grammar.HqlGrammar;
|
||||
import com.mysema.query.grammar.HqlJoinMeta;
|
||||
import com.mysema.query.grammar.QMath;
|
||||
import com.mysema.query.grammar.types.Expr;
|
||||
import com.mysema.query.grammar.types.Expr.EComparable;
|
||||
import com.mysema.query.grammar.types.Expr.ENumber;
|
||||
import com.mysema.query.hql.HqlDomain.*;
|
||||
@ -43,6 +44,10 @@ import com.mysema.query.hql.HqlDomain.*;
|
||||
*/
|
||||
public class HqlParserTest extends QueryBaseWithDomain<HqlParserTest> {
|
||||
|
||||
protected HqlParserTest select(Expr<?>... o) {
|
||||
return addToProjection(o);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBeforeAndAfter() throws RecognitionException, TokenStreamException{
|
||||
EComparable<java.util.Date> ed = catalog.effectiveDate;
|
||||
@ -184,19 +189,19 @@ public class HqlParserTest extends QueryBaseWithDomain<HqlParserTest> {
|
||||
public void testDocoExamples95() throws Exception {
|
||||
// parse( "select avg(cat.weight), sum(cat.weight), max(cat.weight), count(cat)\n"
|
||||
// + "from eg.Cat cat" );
|
||||
select(avg(cat.weight), sum(cat.weight), QMath.max(cat.weight), count(cat)).from(cat).parse();
|
||||
select(avg(cat.weight), sum(cat.weight), QMath.max(cat.weight), Grammar.count(cat)).from(cat).parse();
|
||||
|
||||
// parse( "select cat, count( elements(cat.kittens) )\n"
|
||||
// + " from eg.Cat cat group by cat" );
|
||||
// NOTE : groupBy don't work properly in HSQLDB
|
||||
select(cat, count(cat.kittens)).from(cat).groupBy(cat);
|
||||
select(cat, Grammar.count(cat.kittens)).from(cat).groupBy(cat);
|
||||
clear();
|
||||
|
||||
// parse( "select distinct cat.name from eg.Cat cat" );
|
||||
select(distinct(cat.name)).from(cat).parse();
|
||||
|
||||
// parse( "select count(distinct cat.name), count(cat) from eg.Cat cat" );
|
||||
select(count(distinct(cat.name)), count(cat)).from(cat).parse();
|
||||
select(Grammar.count(distinct(cat.name)), Grammar.count(cat)).from(cat).parse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -371,7 +376,7 @@ public class HqlParserTest extends QueryBaseWithDomain<HqlParserTest> {
|
||||
public void testDocoExamples910() throws Exception {
|
||||
// parse( "select cat.color, sum(cat.weight), count(cat)\n"
|
||||
// + "from eg.Cat cat group by cat.color" );
|
||||
select(cat.color, sum(cat.weight), count(cat)).from(cat).groupBy(cat.color).parse();
|
||||
select(cat.color, sum(cat.weight), Grammar.count(cat)).from(cat).groupBy(cat.color).parse();
|
||||
|
||||
// parse( "select foo.id, avg( elements(foo.names) ), max( indices(foo.names) )\n"
|
||||
// + "from eg.Foo foo group by foo.id" );
|
||||
@ -381,7 +386,7 @@ public class HqlParserTest extends QueryBaseWithDomain<HqlParserTest> {
|
||||
// parse( "select cat.color, sum(cat.weight), count(cat)\n"
|
||||
// + "from eg.Cat cat group by cat.color\n"
|
||||
// + "having cat.color in (eg.Color.TABBY, eg.Color.BLACK)" );
|
||||
select(cat.color, sum(cat.weight), count(cat)).from(cat).groupBy(cat.color)
|
||||
select(cat.color, sum(cat.weight), Grammar.count(cat)).from(cat).groupBy(cat.color)
|
||||
.having(cat.color.in(Color.TABBY, Color.BLACK)).parse();
|
||||
|
||||
// parse( "select cat from eg.Cat cat join cat.kittens kitten\n"
|
||||
@ -389,7 +394,7 @@ public class HqlParserTest extends QueryBaseWithDomain<HqlParserTest> {
|
||||
// + "order by count(kitten) asc, sum(kitten.weight) desc" );
|
||||
select(cat).from(cat).join(cat.kittens.as(kitten))
|
||||
.groupBy(cat).having(avg(kitten.weight).gt(100.0))
|
||||
.orderBy(count(kitten).asc(), sum(kitten.weight).desc()).parse();
|
||||
.orderBy(Grammar.count(kitten).asc(), sum(kitten.weight).desc()).parse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -429,7 +434,7 @@ public class HqlParserTest extends QueryBaseWithDomain<HqlParserTest> {
|
||||
// + "having sum(price.amount) > :minAmount\n"
|
||||
// + "order by sum(price.amount) desc" );
|
||||
// QCatalog cat = new QCatalog("cat");
|
||||
select(ord.id, sum(price.amount), count(item))
|
||||
select(ord.id, sum(price.amount), Grammar.count(item))
|
||||
.from(ord).join(ord.lineItems.as(item))
|
||||
.join(item.product.as(product)).from(catalog)
|
||||
.join(catalog.prices.as(price))
|
||||
@ -456,7 +461,7 @@ public class HqlParserTest extends QueryBaseWithDomain<HqlParserTest> {
|
||||
HqlDomain.Customer c1 = new HqlDomain.Customer();
|
||||
HqlDomain.Catalog c2 = new HqlDomain.Catalog();
|
||||
|
||||
select(ord.id, sum(price.amount), count(item))
|
||||
select(ord.id, sum(price.amount), Grammar.count(item))
|
||||
.from(ord).join(ord.lineItems.as(item)).join(item.product.as(product))
|
||||
.from(catalog).join(catalog.prices.as(price))
|
||||
.where(not(ord.paid).and(ord.customer.eq(c1))
|
||||
@ -551,7 +556,7 @@ public class HqlParserTest extends QueryBaseWithDomain<HqlParserTest> {
|
||||
// parse( "SELECT f FROM eg.mypackage.Cat qat, com.toadstool.Foo f join net.sf.blurb.Blurb" );
|
||||
// parse( "SELECT DISTINCT bar FROM eg.mypackage.Cat qat left join com.multijoin.JoinORama as bar, com.toadstool.Foo f join net.sf.blurb.Blurb" );
|
||||
// parse( "SELECT count(*) FROM eg.mypackage.Cat qat" );
|
||||
select(count()).from(qat).parse();
|
||||
select(Grammar.count()).from(qat).parse();
|
||||
|
||||
// parse( "SELECT avg(qat.weight) FROM eg.mypackage.Cat qat" );
|
||||
select(avg(qat.weight)).from(qat).parse();
|
||||
@ -612,10 +617,10 @@ public class HqlParserTest extends QueryBaseWithDomain<HqlParserTest> {
|
||||
@Test
|
||||
public void testComplexConstructor() throws Exception {
|
||||
// parse( "select new Foo(count(bar)) from bar" );
|
||||
select(new QFooDTO(count(bar))).from(bar).parse();
|
||||
select(new QFooDTO(Grammar.count(bar))).from(bar).parse();
|
||||
|
||||
// parse( "select new Foo(count(bar),(select count(*) from doofus d where d.gob = 'fat' )) from bar" );
|
||||
select(new QFooDTO(count(bar), HqlGrammar.select(count()).from(d).where(d.gob.eq("fat")))).from(bar).parse();
|
||||
select(new QFooDTO(Grammar.count(bar), HqlGrammar.select(Grammar.count()).from(d).where(d.gob.eq("fat")))).from(bar).parse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -757,6 +762,16 @@ public class HqlParserTest extends QueryBaseWithDomain<HqlParserTest> {
|
||||
clear();
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
public long count() {
|
||||
// TODO Auto-generated method stub
|
||||
return 0;
|
||||
}
|
||||
|
||||
public <RT> Iterator<RT> iterate(Expr<RT> projection) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -14,8 +14,7 @@ import com.mysema.query.grammar.HqlQueryBase;
|
||||
* @author tiwe
|
||||
* @version $Id$
|
||||
*/
|
||||
public abstract class QueryBaseWithDomain<A extends QueryBaseWithDomain<A>>
|
||||
extends HqlQueryBase<A>{
|
||||
public abstract class QueryBaseWithDomain<A extends QueryBaseWithDomain<A>> extends HqlQueryBase<A>{
|
||||
|
||||
public QueryBaseWithDomain(){super(new HqlOps());}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user