mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-13 21:01:01 +08:00
extended SQLQuery interface
added MSSQL paging tests
This commit is contained in:
parent
12d1a97d70
commit
8d9beece5c
@ -54,7 +54,7 @@ public class QueryMixin<T>{
|
||||
return metadata;
|
||||
}
|
||||
|
||||
public T from(PEntity<?>... args) {
|
||||
public T from(Expr<?>... args) {
|
||||
metadata.addFrom(args);
|
||||
return self;
|
||||
}
|
||||
|
||||
@ -31,7 +31,10 @@ import com.mysema.query.types.OrderSpecifier;
|
||||
import com.mysema.query.types.expr.EBoolean;
|
||||
import com.mysema.query.types.expr.EConstructor;
|
||||
import com.mysema.query.types.expr.Expr;
|
||||
import com.mysema.query.types.operation.OSimple;
|
||||
import com.mysema.query.types.operation.Ops;
|
||||
import com.mysema.query.types.path.PEntity;
|
||||
import com.mysema.query.types.path.Path;
|
||||
import com.mysema.query.types.query.ListSubQuery;
|
||||
import com.mysema.query.types.query.SubQuery;
|
||||
import com.mysema.util.JDBCUtil;
|
||||
@ -116,13 +119,26 @@ public abstract class AbstractSQLQuery<Q extends AbstractSQLQuery<Q>> extends
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <D> Expr<D> createAlias(Expr<?> target, Expr<D> alias){
|
||||
return OSimple.create((Class<D>)alias.getType(), Ops.ALIAS, target.asExpr(), alias.asExpr());
|
||||
}
|
||||
|
||||
protected SQLSerializer createSerializer() {
|
||||
return new SQLSerializer(templates);
|
||||
}
|
||||
|
||||
public Q from(PEntity<?>... args) {
|
||||
public Q from(Expr<?>... args) {
|
||||
return queryMixin.from(args);
|
||||
}
|
||||
|
||||
public <T> Q from(SubQuery<T> source, Expr<T> alias) {
|
||||
return queryMixin.from(createAlias(source.asExpr(), alias));
|
||||
}
|
||||
|
||||
public <T> Q from(ListSubQuery<T> source, Expr<T> alias) {
|
||||
return queryMixin.from(createAlias(source.asExpr(), alias));
|
||||
}
|
||||
|
||||
public Q fullJoin(PEntity<?> target) {
|
||||
return queryMixin.fullJoin(target);
|
||||
@ -152,57 +168,6 @@ public abstract class AbstractSQLQuery<Q extends AbstractSQLQuery<Q>> extends
|
||||
return queryMixin.getMetadata();
|
||||
}
|
||||
|
||||
protected SQLTemplates getTemplates() {
|
||||
return templates;
|
||||
}
|
||||
|
||||
public Q innerJoin(PEntity<?> target) {
|
||||
return queryMixin.innerJoin(target);
|
||||
}
|
||||
|
||||
private <RT> UnionBuilder<RT> innerUnion(SubQuery<?>... sq) {
|
||||
if (!queryMixin.getMetadata().getJoins().isEmpty()) {
|
||||
throw new IllegalArgumentException("Don't mix union and from");
|
||||
}
|
||||
this.sq = sq;
|
||||
return new UnionBuilder<RT>();
|
||||
}
|
||||
|
||||
public Q join(PEntity<?> target) {
|
||||
return queryMixin.join(target);
|
||||
}
|
||||
|
||||
public Q leftJoin(PEntity<?> target) {
|
||||
return queryMixin.leftJoin(target);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Object[]> list(Expr<?>[] args) {
|
||||
return IteratorAdapter.asList(iterate(args));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <RT> List<RT> list(Expr<RT> expr) {
|
||||
return IteratorAdapter.asList(iterate(expr));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CloseableIterator<Object[]> iterate(Expr<?>[] args) {
|
||||
queryMixin.addToProjection(args);
|
||||
return iterateMultiple();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <RT> CloseableIterator<RT> iterate(Expr<RT> expr) {
|
||||
queryMixin.addToProjection(expr);
|
||||
if (expr.getType().isArray()) {
|
||||
return (CloseableIterator<RT>) iterateMultiple();
|
||||
} else {
|
||||
return iterateSingle(expr);
|
||||
}
|
||||
}
|
||||
|
||||
public ResultSet getResults(Expr<?>... exprs) {
|
||||
queryMixin.addToProjection(exprs);
|
||||
String queryString = buildQueryString(false);
|
||||
@ -230,6 +195,39 @@ public abstract class AbstractSQLQuery<Q extends AbstractSQLQuery<Q>> extends
|
||||
}
|
||||
}
|
||||
|
||||
protected SQLTemplates getTemplates() {
|
||||
return templates;
|
||||
}
|
||||
|
||||
public Q innerJoin(PEntity<?> target) {
|
||||
return queryMixin.innerJoin(target);
|
||||
}
|
||||
|
||||
private <RT> UnionBuilder<RT> innerUnion(SubQuery<?>... sq) {
|
||||
if (!queryMixin.getMetadata().getJoins().isEmpty()) {
|
||||
throw new IllegalArgumentException("Don't mix union and from");
|
||||
}
|
||||
this.sq = sq;
|
||||
return new UnionBuilder<RT>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CloseableIterator<Object[]> iterate(Expr<?>[] args) {
|
||||
queryMixin.addToProjection(args);
|
||||
return iterateMultiple();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <RT> CloseableIterator<RT> iterate(Expr<RT> expr) {
|
||||
queryMixin.addToProjection(expr);
|
||||
if (expr.getType().isArray()) {
|
||||
return (CloseableIterator<RT>) iterateMultiple();
|
||||
} else {
|
||||
return iterateSingle(expr);
|
||||
}
|
||||
}
|
||||
|
||||
private CloseableIterator<Object[]> iterateMultiple() {
|
||||
String queryString = buildQueryString(false);
|
||||
logger.debug("query : {}", queryString);
|
||||
@ -265,23 +263,6 @@ public abstract class AbstractSQLQuery<Q extends AbstractSQLQuery<Q>> extends
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public <RT> SearchResults<RT> listResults(Expr<RT> expr) {
|
||||
queryMixin.addToProjection(expr);
|
||||
long total = count();
|
||||
try {
|
||||
if (total > 0) {
|
||||
QueryModifiers modifiers = queryMixin.getMetadata().getModifiers();
|
||||
return new SearchResults<RT>(list(expr), modifiers, total);
|
||||
} else {
|
||||
return SearchResults.emptyResults();
|
||||
}
|
||||
|
||||
} finally {
|
||||
reset();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <RT> CloseableIterator<RT> iterateSingle(@Nullable final Expr<RT> expr) {
|
||||
String queryString = buildQueryString(false);
|
||||
@ -337,6 +318,41 @@ public abstract class AbstractSQLQuery<Q extends AbstractSQLQuery<Q>> extends
|
||||
}
|
||||
}
|
||||
|
||||
public Q join(PEntity<?> target) {
|
||||
return queryMixin.join(target);
|
||||
}
|
||||
|
||||
public Q leftJoin(PEntity<?> target) {
|
||||
return queryMixin.leftJoin(target);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Object[]> list(Expr<?>[] args) {
|
||||
return IteratorAdapter.asList(iterate(args));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <RT> List<RT> list(Expr<RT> expr) {
|
||||
return IteratorAdapter.asList(iterate(expr));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <RT> SearchResults<RT> listResults(Expr<RT> expr) {
|
||||
queryMixin.addToProjection(expr);
|
||||
long total = count();
|
||||
try {
|
||||
if (total > 0) {
|
||||
QueryModifiers modifiers = queryMixin.getMetadata().getModifiers();
|
||||
return new SearchResults<RT>(list(expr), modifiers, total);
|
||||
} else {
|
||||
return SearchResults.emptyResults();
|
||||
}
|
||||
|
||||
} finally {
|
||||
reset();
|
||||
}
|
||||
}
|
||||
|
||||
public Q on(EBoolean... conditions) {
|
||||
return queryMixin.on(conditions);
|
||||
}
|
||||
|
||||
@ -30,7 +30,23 @@ public interface SQLQuery extends Query<SQLQuery>, Projectable {
|
||||
* @param o
|
||||
* @return
|
||||
*/
|
||||
SQLQuery from(PEntity<?>... o);
|
||||
SQLQuery from(Expr<?>... o);
|
||||
|
||||
/**
|
||||
* @param <T>
|
||||
* @param source
|
||||
* @param alias
|
||||
* @return
|
||||
*/
|
||||
<T> SQLQuery from(ListSubQuery<T> source, Expr<T> alias);
|
||||
|
||||
/**
|
||||
* @param <T>
|
||||
* @param source
|
||||
* @param alias
|
||||
* @return
|
||||
*/
|
||||
<T> SQLQuery from(SubQuery<T> source, Expr<T> alias);
|
||||
|
||||
/**
|
||||
* Adds a full join to the given target
|
||||
|
||||
@ -9,6 +9,8 @@ import java.sql.Connection;
|
||||
|
||||
import com.mysema.query.DefaultQueryMetadata;
|
||||
import com.mysema.query.QueryMetadata;
|
||||
import com.mysema.query.types.expr.Expr;
|
||||
import com.mysema.query.types.query.SubQuery;
|
||||
|
||||
/**
|
||||
* SQLQueryImpl is a JDBC based implementation of the Querydsl SQLQuery interface
|
||||
@ -59,5 +61,6 @@ public class SQLQueryImpl extends AbstractSQLQuery<SQLQueryImpl> implements SQLQ
|
||||
public SQLQueryImpl clone(Connection conn){
|
||||
return new SQLQueryImpl(conn, getTemplates(), getMetadata().clone());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -19,7 +19,7 @@ import com.mysema.query.types.path.PEntity;
|
||||
*
|
||||
*/
|
||||
public class SQLSubQuery extends DetachableQuery<SQLSubQuery>{
|
||||
|
||||
|
||||
public SQLSubQuery() {
|
||||
this(new DefaultQueryMetadata());
|
||||
}
|
||||
|
||||
@ -5,14 +5,27 @@
|
||||
*/
|
||||
package com.mysema.query.mssql;
|
||||
|
||||
import static com.mysema.query.Constants.employee;
|
||||
import static com.mysema.query.sql.mssql.SQLServerGrammar.rn;
|
||||
import static com.mysema.query.sql.mssql.SQLServerGrammar.rowNumber;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import com.mysema.query.Connections;
|
||||
import com.mysema.query.SelectBaseTest;
|
||||
import com.mysema.query.Target;
|
||||
import com.mysema.query.sql.SQLServerTemplates;
|
||||
import com.mysema.query.sql.mssql.RowNumber;
|
||||
import com.mysema.query.types.custom.CSimple;
|
||||
import com.mysema.query.types.expr.Expr;
|
||||
import com.mysema.query.types.path.PSimple;
|
||||
import com.mysema.query.types.query.ListSubQuery;
|
||||
import com.mysema.query.types.query.SubQuery;
|
||||
import com.mysema.testutil.FilteringTestRunner;
|
||||
import com.mysema.testutil.Label;
|
||||
import com.mysema.testutil.ResourceCheck;
|
||||
@ -32,16 +45,30 @@ public class SelectMSSQLTest extends SelectBaseTest {
|
||||
dialect = new SQLServerTemplates().newLineToSingleSpace();
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
SELECT <original projection>
|
||||
FROM (
|
||||
SELECT <original projection>, ROW_NUMBER() OVER ([PARTITION BY ... ] ORDER BY ...) AS rowNum
|
||||
FROM table1
|
||||
) AS table2
|
||||
WHERE table2.rowNum BETWEEN ? and ?
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
@Test
|
||||
public void testPaging(){
|
||||
RowNumber rowNumber = rowNumber().orderBy(employee.lastname.asc()).as(rn);
|
||||
// TODO : create a short cut for wild card
|
||||
Expr<Object[]> all = CSimple.create(Object[].class, "*");
|
||||
|
||||
// simple
|
||||
for (Object[] row : query().from(employee).list(employee.firstname, employee.lastname, rowNumber)){
|
||||
System.out.println(Arrays.asList(row));
|
||||
}
|
||||
|
||||
// with subquery
|
||||
ListSubQuery<Object[]> sub = s().from(employee).list(employee.firstname, employee.lastname, rowNumber);
|
||||
PSimple<Object[]> subAlias = new PSimple<Object[]>(Object[].class, "s");
|
||||
for (Object[] row : query().from(sub, subAlias).list(all)){
|
||||
System.out.println(Arrays.asList(row));
|
||||
}
|
||||
|
||||
// with subquery, only row number
|
||||
SubQuery<Long> sub2 = s().from(employee).unique(rowNumber);
|
||||
PSimple<Long> subAlias2 = new PSimple<Long>(Long.class, "s");
|
||||
for (Object[] row : query().from(sub2, subAlias2).list(all)){
|
||||
System.out.println(Arrays.asList(row));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user