mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-30 21:08:30 +08:00
tests for paged results
This commit is contained in:
parent
220dcd7c36
commit
84ee503526
@ -1,5 +1,3 @@
|
||||
* make sure queries and results are properly closed
|
||||
* closing results and queries
|
||||
|
||||
* make the following things work
|
||||
- tabular results in projection
|
||||
- distinct results
|
||||
* distinct results
|
||||
@ -29,47 +29,47 @@ import com.mysema.query.types.path.PEntity;
|
||||
* Default implementation of the JDOQLQuery interface
|
||||
*
|
||||
* @author tiwe
|
||||
*
|
||||
*
|
||||
* @param <A>
|
||||
*/
|
||||
class JDOQLQueryImpl extends QueryBaseWithProjection<Object, JDOQLQueryImpl> implements Projectable, JDOQLQuery {
|
||||
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(JDOQLQueryImpl.class);
|
||||
|
||||
|
||||
private List<Object> constants;
|
||||
|
||||
|
||||
private List<PEntity<?>> sources = new ArrayList<PEntity<?>>();
|
||||
|
||||
|
||||
private String filter;
|
||||
|
||||
|
||||
private final JDOQLOps ops;
|
||||
|
||||
|
||||
private final PersistenceManager pm;
|
||||
|
||||
|
||||
public JDOQLQueryImpl(PersistenceManager pm) {
|
||||
this(pm, JDOQLOps.DEFAULT);
|
||||
}
|
||||
|
||||
|
||||
public JDOQLQueryImpl(PersistenceManager pm, JDOQLOps ops) {
|
||||
this.ops = ops;
|
||||
this.pm = pm;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected JDOQLQueryImpl addToProjection(Expr<?>... o) {
|
||||
for (Expr<?> expr : o) {
|
||||
if (expr instanceof EConstructor){
|
||||
EConstructor<?> constructor = (EConstructor<?>)expr;
|
||||
for (Expr<?> arg : constructor.getArgs()){
|
||||
if (expr instanceof EConstructor) {
|
||||
EConstructor<?> constructor = (EConstructor<?>) expr;
|
||||
for (Expr<?> arg : constructor.getArgs()) {
|
||||
super.addToProjection(arg);
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
super.addToProjection(expr);
|
||||
}
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public JDOQLQueryImpl from(PEntity<?>... o) {
|
||||
for (PEntity<?> expr : o) {
|
||||
@ -79,167 +79,167 @@ class JDOQLQueryImpl extends QueryBaseWithProjection<Object, JDOQLQueryImpl> imp
|
||||
}
|
||||
|
||||
private String buildFilterString(boolean forCountRow) {
|
||||
if (getMetadata().getWhere() == null){
|
||||
return null;
|
||||
}
|
||||
if (getMetadata().getWhere() == null) {
|
||||
return null;
|
||||
}
|
||||
JDOQLSerializer serializer = new JDOQLSerializer(ops, sources.get(0));
|
||||
serializer.handle(getMetadata().getWhere());
|
||||
constants = serializer.getConstants();
|
||||
serializer.handle(getMetadata().getWhere());
|
||||
constants = serializer.getConstants();
|
||||
System.err.println("SERIALIZED : " + serializer.toString());
|
||||
return serializer.toString();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void clear(){
|
||||
protected void clear() {
|
||||
super.clear();
|
||||
filter = null;
|
||||
}
|
||||
|
||||
public long count(){
|
||||
String filterString = getFilterString();
|
||||
|
||||
public long count() {
|
||||
String filterString = getFilterString();
|
||||
logger.debug("query : {}", filterString);
|
||||
Query query = createQuery(filterString, null, true);
|
||||
query.setUnique(true);
|
||||
query.setResult("count(this)");
|
||||
return (Long) execute(query);
|
||||
}
|
||||
|
||||
|
||||
private Query createQuery(String filterString, QueryModifiers modifiers, boolean forCount) {
|
||||
Query query = pm.newQuery(sources.get(0).getType());
|
||||
if (filterString != null){
|
||||
query.setFilter(filterString);
|
||||
}
|
||||
|
||||
// variables
|
||||
if (sources.size() > 1){
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
for (int i = 1; i < sources.size(); i++){
|
||||
if (buffer.length() > 0){
|
||||
buffer.append(", ");
|
||||
}
|
||||
PEntity<?> source = sources.get(i);
|
||||
buffer.append(source.getType().getName()).append(" ").append(source.toString());
|
||||
}
|
||||
}
|
||||
|
||||
// explicit parameters
|
||||
if (constants != null){
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (int i=0; i < constants.size(); i++){
|
||||
if (builder.length() > 0){
|
||||
builder.append(", ");
|
||||
Query query = pm.newQuery(sources.get(0).getType());
|
||||
if (filterString != null) {
|
||||
query.setFilter(filterString);
|
||||
}
|
||||
|
||||
// variables
|
||||
if (sources.size() > 1) {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
for (int i = 1; i < sources.size(); i++) {
|
||||
if (buffer.length() > 0) {
|
||||
buffer.append(", ");
|
||||
}
|
||||
PEntity<?> source = sources.get(i);
|
||||
buffer.append(source.getType().getName()).append(" ").append(source.toString());
|
||||
}
|
||||
}
|
||||
|
||||
// explicit parameters
|
||||
if (constants != null) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (int i = 0; i < constants.size(); i++) {
|
||||
if (builder.length() > 0) {
|
||||
builder.append(", ");
|
||||
}
|
||||
Object val = constants.get(i);
|
||||
builder.append(val.getClass().getName()).append(" ").append("a"+(i+1));
|
||||
builder.append(val.getClass().getName()).append(" ").append("a" + (i + 1));
|
||||
}
|
||||
query.declareParameters(builder.toString());
|
||||
}
|
||||
|
||||
query.declareParameters(builder.toString());
|
||||
}
|
||||
|
||||
// range (not for count)
|
||||
if (modifiers != null && modifiers.isRestricting() && !forCount){
|
||||
long fromIncl = 0;
|
||||
long toExcl = Long.MAX_VALUE;
|
||||
if (modifiers.getOffset() != null){
|
||||
fromIncl = modifiers.getOffset().longValue();
|
||||
if (modifiers != null && modifiers.isRestricting() && !forCount) {
|
||||
long fromIncl = 0;
|
||||
long toExcl = Long.MAX_VALUE;
|
||||
if (modifiers.getOffset() != null) {
|
||||
fromIncl = modifiers.getOffset().longValue();
|
||||
}
|
||||
if (modifiers.getLimit() != null) {
|
||||
toExcl = fromIncl + modifiers.getLimit().longValue();
|
||||
}
|
||||
if (modifiers.getLimit() != null){
|
||||
toExcl = fromIncl + modifiers.getLimit().longValue();
|
||||
}
|
||||
query.setRange(fromIncl, toExcl);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// projection (not for count)
|
||||
if (!getMetadata().getProjection().isEmpty() && !forCount){
|
||||
List<? extends Expr<?>> projection = getMetadata().getProjection();
|
||||
if (!projection.get(0).equals(sources.get(0))){
|
||||
JDOQLSerializer serializer = new JDOQLSerializer(ops, sources.get(0));
|
||||
serializer.handle(", ", projection);
|
||||
query.setResult(serializer.toString());
|
||||
}
|
||||
if (!getMetadata().getProjection().isEmpty() && !forCount) {
|
||||
List<? extends Expr<?>> projection = getMetadata().getProjection();
|
||||
if (!projection.get(0).equals(sources.get(0))) {
|
||||
JDOQLSerializer serializer = new JDOQLSerializer(ops, sources.get(0));
|
||||
serializer.handle(", ", projection);
|
||||
query.setResult(serializer.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// order (not for count)
|
||||
if (!getMetadata().getOrderBy().isEmpty() && !forCount){
|
||||
List<OrderSpecifier<?>> order = getMetadata().getOrderBy();
|
||||
JDOQLSerializer serializer = new JDOQLSerializer(ops, sources.get(0));
|
||||
boolean first = true;
|
||||
for (OrderSpecifier<?> os : order){
|
||||
if (!first){
|
||||
serializer.append(", ");
|
||||
}
|
||||
serializer.handle(os.getTarget());
|
||||
serializer.append(os.getOrder() == Order.ASC ? " ascending" : "descending");
|
||||
first = false;
|
||||
}
|
||||
query.setOrdering(serializer.toString());
|
||||
if (!getMetadata().getOrderBy().isEmpty() && !forCount) {
|
||||
List<OrderSpecifier<?>> order = getMetadata().getOrderBy();
|
||||
JDOQLSerializer serializer = new JDOQLSerializer(ops, sources.get(0));
|
||||
boolean first = true;
|
||||
for (OrderSpecifier<?> os : order) {
|
||||
if (!first) {
|
||||
serializer.append(", ");
|
||||
}
|
||||
serializer.handle(os.getTarget());
|
||||
serializer.append(os.getOrder() == Order.ASC ? " ascending" : "descending");
|
||||
first = false;
|
||||
}
|
||||
query.setOrdering(serializer.toString());
|
||||
}
|
||||
|
||||
|
||||
return query;
|
||||
}
|
||||
|
||||
|
||||
protected List<Object> getConstants() {
|
||||
return constants;
|
||||
}
|
||||
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Object[]> list(Expr<?> expr1, Expr<?> expr2, Expr<?>...rest){
|
||||
public List<Object[]> list(Expr<?> expr1, Expr<?> expr2, Expr<?>... rest) {
|
||||
addToProjection(expr1, expr2);
|
||||
addToProjection(rest);
|
||||
String filterString = getFilterString();
|
||||
return (List<Object[]>) execute(createQuery(filterString, getMetadata().getModifiers(), false));
|
||||
return (List<Object[]>) execute(createQuery(filterString, getMetadata().getModifiers(), false));
|
||||
}
|
||||
|
||||
private Object execute(Query query){
|
||||
Object rv;
|
||||
if (constants != null){
|
||||
rv = query.executeWithArray(constants.toArray());
|
||||
}else{
|
||||
rv = query.execute();
|
||||
}
|
||||
// query.closeAll();
|
||||
return rv;
|
||||
|
||||
private Object execute(Query query) {
|
||||
Object rv;
|
||||
if (constants != null) {
|
||||
rv = query.executeWithArray(constants.toArray());
|
||||
} else {
|
||||
rv = query.execute();
|
||||
}
|
||||
// query.closeAll();
|
||||
return rv;
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <RT> List<RT> list(Expr<RT> expr){
|
||||
public <RT> List<RT> list(Expr<RT> expr) {
|
||||
addToProjection(expr);
|
||||
String filterString = getFilterString();
|
||||
logger.debug("query : {}", filterString);
|
||||
return (List<RT>) execute(createQuery(filterString, getMetadata().getModifiers(), false));
|
||||
return (List<RT>) execute(createQuery(filterString, getMetadata().getModifiers(), false));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <RT> SearchResults<RT> listResults(Expr<RT> expr) {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <RT> SearchResults<RT> listResults(Expr<RT> expr) {
|
||||
addToProjection(expr);
|
||||
Query countQuery = createQuery(getFilterString(), null, true);
|
||||
countQuery.setUnique(true);
|
||||
countQuery.setResult("count(this)");
|
||||
long total = (Long) execute(countQuery);
|
||||
if (total > 0) {
|
||||
QueryModifiers modifiers = getMetadata().getModifiers();
|
||||
QueryModifiers modifiers = getMetadata().getModifiers();
|
||||
String filterString = getFilterString();
|
||||
logger.debug("query : {}", filterString);
|
||||
Query query = createQuery(filterString, modifiers,false);
|
||||
List<RT> list = (List<RT>) execute(query);
|
||||
return new SearchResults<RT>(list, modifiers, total);
|
||||
Query query = createQuery(filterString, modifiers, false);
|
||||
return new SearchResults<RT>((List<RT>)execute(query), modifiers, total);
|
||||
} else {
|
||||
return SearchResults.emptyResults();
|
||||
}
|
||||
}
|
||||
|
||||
private String getFilterString(){
|
||||
if (filter == null){
|
||||
filter = buildFilterString(false);
|
||||
}
|
||||
|
||||
private String getFilterString() {
|
||||
if (filter == null) {
|
||||
filter = buildFilterString(false);
|
||||
}
|
||||
return filter;
|
||||
}
|
||||
|
||||
@ -248,7 +248,7 @@ class JDOQLQueryImpl extends QueryBaseWithProjection<Object, JDOQLQueryImpl> imp
|
||||
addToProjection(expr);
|
||||
String filterString = getFilterString();
|
||||
logger.debug("query : {}", filterString);
|
||||
Query query = createQuery(filterString, QueryModifiers.limit(1),false);
|
||||
Query query = createQuery(filterString, QueryModifiers.limit(1), false);
|
||||
query.setUnique(true);
|
||||
return (RT) execute(query);
|
||||
}
|
||||
|
||||
@ -26,7 +26,7 @@ public class QueryBasicsTest extends AbstractJDOTest{
|
||||
// FIXME
|
||||
assertEquals("count", 2, query().from(product).count()); // returns 1, why?
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void projectionTests(){
|
||||
assertEquals("Sony Discman", query().from(product)
|
||||
|
||||
@ -3,6 +3,7 @@ package com.mysema.query.jdoql;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import javax.jdo.PersistenceManager;
|
||||
@ -12,76 +13,106 @@ import org.junit.BeforeClass;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mysema.query.SearchResults;
|
||||
import com.mysema.query.jdoql.testdomain.Product;
|
||||
import com.mysema.query.jdoql.testdomain.QProduct;
|
||||
|
||||
public class QueryOrderingTest extends AbstractJDOTest{
|
||||
|
||||
private QProduct product = QProduct.product;
|
||||
|
||||
@Test
|
||||
public void testOrderAsc(){
|
||||
List<String> namesAsc = query().from(product)
|
||||
.orderBy(product.name.asc(), product.description.desc())
|
||||
.list(product.name);
|
||||
assertEquals(30, namesAsc.size());
|
||||
String prev = null;
|
||||
for (String name : namesAsc){
|
||||
if (prev != null){
|
||||
assertTrue(prev.compareTo(name) < 0);
|
||||
}
|
||||
prev = name;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOrderDesc(){
|
||||
List<String> namesDesc = query().from(product)
|
||||
.orderBy(product.name.desc())
|
||||
.list(product.name);
|
||||
assertEquals(30, namesDesc.size());
|
||||
String prev = null;
|
||||
for (String name : namesDesc){
|
||||
if (prev != null){
|
||||
assertTrue(prev.compareTo(name) > 0);
|
||||
}
|
||||
prev = name;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void searchResults(){
|
||||
// TODO
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void testDistinct(){
|
||||
// TODO
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void doPersist() {
|
||||
// Persistence of a Product and a Book.
|
||||
PersistenceManager pm = pmf.getPersistenceManager();
|
||||
Transaction tx = pm.currentTransaction();
|
||||
try {
|
||||
tx.begin();
|
||||
for (int i=0; i < 10; i++){
|
||||
pm.makePersistent(new Product("C"+i,"F"+i, 200.00));
|
||||
pm.makePersistent(new Product("B"+i,"E"+i, 200.00));
|
||||
pm.makePersistent(new Product("A"+i,"D"+i, 200.00));
|
||||
}
|
||||
tx.commit();
|
||||
} finally {
|
||||
if (tx.isActive()) {
|
||||
tx.rollback();
|
||||
}
|
||||
pm.close();
|
||||
}
|
||||
System.out.println("");
|
||||
public class QueryOrderingTest extends AbstractJDOTest {
|
||||
|
||||
}
|
||||
private QProduct product = QProduct.product;
|
||||
|
||||
@Test
|
||||
public void testOrderAsc() {
|
||||
List<String> namesAsc = query().from(product).orderBy(
|
||||
product.name.asc(), product.description.desc()).list(
|
||||
product.name);
|
||||
assertEquals(30, namesAsc.size());
|
||||
String prev = null;
|
||||
for (String name : namesAsc) {
|
||||
if (prev != null) {
|
||||
assertTrue(prev.compareTo(name) < 0);
|
||||
}
|
||||
prev = name;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOrderDesc() {
|
||||
List<String> namesDesc = query().from(product).orderBy(
|
||||
product.name.desc()).list(product.name);
|
||||
assertEquals(30, namesDesc.size());
|
||||
String prev = null;
|
||||
for (String name : namesDesc) {
|
||||
if (prev != null) {
|
||||
assertTrue(prev.compareTo(name) > 0);
|
||||
}
|
||||
prev = name;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTabularResults() {
|
||||
List<Object[]> rows = query().from(product).orderBy(product.name.asc())
|
||||
.list(product.name, product.description);
|
||||
assertEquals(30, rows.size());
|
||||
for (Object[] row : rows) {
|
||||
assertEquals(row[0].toString().substring(1), row[1].toString()
|
||||
.substring(1));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void paging(){
|
||||
assertEquals(
|
||||
Arrays.asList("A0", "A1"),
|
||||
query().from(product).orderBy(product.name.asc()).limit(2).list(product.name));
|
||||
assertEquals(
|
||||
Arrays.asList("A2", "A3", "A4"),
|
||||
query().from(product).orderBy(product.name.asc()).offset(2).limit(3).list(product.name));
|
||||
assertEquals(
|
||||
Arrays.asList("C9", "C8"),
|
||||
query().from(product).orderBy(product.name.desc()).limit(2).list(product.name));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void searchResults() {
|
||||
SearchResults<String> results = query()
|
||||
.from(product)
|
||||
.orderBy(product.name.asc())
|
||||
.limit(2)
|
||||
.listResults(product.name);
|
||||
assertEquals(Arrays.asList("A0","A1"), results.getResults());
|
||||
assertEquals(30, results.getTotal());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void testDistinct() {
|
||||
// TODO
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void doPersist() {
|
||||
// Persistence of a Product and a Book.
|
||||
PersistenceManager pm = pmf.getPersistenceManager();
|
||||
Transaction tx = pm.currentTransaction();
|
||||
try {
|
||||
tx.begin();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
pm.makePersistent(new Product("C" + i, "F" + i, 200.00));
|
||||
pm.makePersistent(new Product("B" + i, "E" + i, 200.00));
|
||||
pm.makePersistent(new Product("A" + i, "D" + i, 200.00));
|
||||
}
|
||||
tx.commit();
|
||||
} finally {
|
||||
if (tx.isActive()) {
|
||||
tx.rollback();
|
||||
}
|
||||
pm.close();
|
||||
}
|
||||
System.out.println("");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user