This commit is contained in:
Timo Westkämper 2009-05-19 18:22:12 +00:00
parent 6ace0bf0d8
commit 27100ebf64
10 changed files with 92 additions and 69 deletions

View File

@ -5,9 +5,12 @@
*/
package com.mysema.query;
import java.util.*;
import org.apache.commons.lang.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.mysema.query.grammar.OrderSpecifier;
import com.mysema.query.grammar.types.Expr;
@ -37,7 +40,7 @@ public class DefaultQueryMetadata<JoinMeta> implements QueryMetadata<JoinMeta>{
private boolean distinct;
private QueryModifiers modifiers = QueryModifiers.getDefault();
private QueryModifiers modifiers = new QueryModifiers();
public List<? extends Expr<?>> getGroupBy() {
return Collections.unmodifiableList(groupBy);

View File

@ -37,6 +37,15 @@ public interface Projectable {
*/
Iterator<Object[]> iterate(Expr<?> first, Expr<?> second, Expr<?>... rest);
/**
* iterate over the results with the given projection
*
* @param <RT> generic type of the Iteratpr
* @param projection
* @return an Iterator over the projection
*/
<RT> Iterator<RT> iterate(Expr<RT> projection);
/**
* iterate over the distinct results with the given projection
*
@ -46,15 +55,6 @@ public interface Projectable {
* @return an Iterator over the projection
*/
Iterator<Object[]> iterateDistinct(Expr<?> first, Expr<?> second, Expr<?>... rest);
/**
* iterate over the results with the given projection
*
* @param <RT> generic type of the Iteratpr
* @param projection
* @return an Iterator over the projection
*/
<RT> Iterator<RT> iterate(Expr<RT> projection);
/**
* iterate over the distinct results with the given projection
@ -75,6 +75,15 @@ public interface Projectable {
*/
List<Object[]> list(Expr<?> first, Expr<?> second, Expr<?>... rest);
/**
* list the results with the given projection
*
* @param <RT> generic type of the List
* @param projection
* @return a List over the projection
*/
<RT> List<RT> list(Expr<RT> projection);
/**
* list the distinct results with the given projection
*
@ -84,15 +93,6 @@ public interface Projectable {
* @return a List over the projection
*/
List<Object[]> listDistinct(Expr<?> first, Expr<?> second, Expr<?>... rest);
/**
* list the results with the given projection
*
* @param <RT> generic type of the List
* @param projection
* @return a List over the projection
*/
<RT> List<RT> list(Expr<RT> projection);
/**
* list the distinct results with the given projection
@ -104,13 +104,12 @@ public interface Projectable {
<RT> List<RT> listDistinct(Expr<RT> projection);
/**
* return a unique result for the given projection
*
* @param <RT> return type
* @param projection
* @return the result or null for an empty result
* @param <RT>
* @param expr
* @return
*/
<RT> RT uniqueResult(Expr<RT> projection);
<RT> SearchResults<RT> listResults(Expr<RT> expr);
/**
*
@ -122,12 +121,13 @@ public interface Projectable {
Object[] uniqueResult(Expr<?> first, Expr<?> second, Expr<?>... rest);
/**
* return a unique result for the given projection
*
* @param <RT>
* @param expr
* @return
* @param <RT> return type
* @param projection
* @return the result or null for an empty result
*/
<RT> SearchResults<RT> listResults(Expr<RT> expr);
<RT> RT uniqueResult(Expr<RT> projection);
}

View File

@ -68,7 +68,6 @@ public abstract class ProjectableAdapter implements Projectable{
return projectable.listResults(expr);
}
public <RT> List<RT> listDistinct(Expr<RT> projection) {
return projectable.listDistinct(projection);
}

View File

@ -18,7 +18,7 @@ import com.mysema.query.grammar.types.Expr.EBoolean;
*/
public class QueryBase<JoinMeta,SubType extends QueryBase<JoinMeta,SubType>> implements Query<SubType> {
@SuppressWarnings("unchecked")
private SubType _this = (SubType)this;
protected final SubType _this = (SubType)this;
private QueryMetadata<JoinMeta> metadata;

View File

@ -40,6 +40,21 @@ public abstract class QueryBaseWithProjection<JoinMeta,SubType
return count();
}
public SubType limit(long limit){
getMetadata().getModifiers().setLimit(limit);
return _this;
}
public SubType offset(long offset){
getMetadata().getModifiers().setOffset(offset);
return _this;
}
public SubType restrict(QueryModifiers modifiers){
getMetadata().setModifiers(modifiers);
return _this;
}
public final Iterator<Object[]> iterateDistinct(Expr<?> first, Expr<?> second, Expr<?>... rest) {
getMetadata().setDistinct(true);
return iterate(first, second, rest);
@ -58,18 +73,6 @@ public abstract class QueryBaseWithProjection<JoinMeta,SubType
return IteratorUtils.toList(iterate(projection));
}
public <RT> SearchResults<RT> listResults(Expr<RT> projection){
QueryModifiers modifiers = getMetadata().getModifiers();
List<RT> list = list(projection);
if (list.isEmpty()){
return SearchResults.emptyResults();
}else{
int start = Math.min(modifiers.getOffset(), list.size());
int end = Math.min(modifiers.getOffset() + modifiers.getLimit(), list.size());
return new SearchResults<RT>(list.subList(start, end), modifiers, list.size());
}
}
public final List<Object[]> listDistinct(Expr<?> first, Expr<?> second, Expr<?>... rest) {
getMetadata().setDistinct(true);
return list(first, second, rest);
@ -80,13 +83,15 @@ public abstract class QueryBaseWithProjection<JoinMeta,SubType
return list(projection);
}
public <RT> RT uniqueResult(Expr<RT> expr) {
Iterator<RT> it = iterate(expr);
return it.hasNext() ? it.next() : null;
}
public Object[] uniqueResult(Expr<?> first, Expr<?> second, Expr<?>... rest) {
getMetadata().getModifiers().setLimit(1l);
Iterator<Object[]> it = iterate(first, second, rest);
return it.hasNext() ? it.next() : null;
}
public <RT> RT uniqueResult(Expr<RT> expr) {
getMetadata().getModifiers().setLimit(1l);
Iterator<RT> it = iterate(expr);
return it.hasNext() ? it.next() : null;
}
}

View File

@ -1,8 +1,6 @@
/*
* Copyright (c) 2007 Mysema Ltd.
* All rights reserved.
*
* originally developed in Bookmarks project
*/
package com.mysema.query;
@ -14,25 +12,43 @@ package com.mysema.query;
*/
public final class QueryModifiers {
private static final QueryModifiers DEFAULT = new QueryModifiers(Integer.MAX_VALUE,0);
public static QueryModifiers limit(long limit) {
return new QueryModifiers(Long.valueOf(limit), null);
}
private final int limit, offset;
public static QueryModifiers offset(long offset) {
return new QueryModifiers(null, Long.valueOf(offset));
}
public QueryModifiers(int limit, int offset){
private Long limit, offset;
public QueryModifiers(){}
public QueryModifiers(Long limit, Long offset){
this.limit = limit;
this.offset = offset;
}
public static QueryModifiers getDefault(){
return DEFAULT;
}
public int getLimit() {
public Long getLimit() {
return limit;
}
public int getOffset() {
public Long getOffset() {
return offset;
}
public void setLimit(Long limit) {
this.limit = limit;
}
public void setOffset(Long offset) {
this.offset = offset;
}
public boolean isRestricting() {
return limit != null || offset != null;
}
}

View File

@ -25,9 +25,9 @@ public final class SearchResults<T> {
private final List<T> results;
public SearchResults(List<T> results, long limit, long offset, long total){
this.limit = limit;
this.offset = offset;
public SearchResults(List<T> results, Long limit, Long offset, long total){
this.limit = limit != null ? limit : Long.MAX_VALUE;
this.offset = offset != null ? offset : 0l;
this.total = total;
this.results = results;
}

View File

@ -11,7 +11,7 @@ import java.lang.annotation.Target;
/**
* Annotion for APT based Domain query type generation.
* Annotate Domain types with this annotation.
* Annotate Literal types with this annotation.
*/
public @interface Literal {

View File

@ -19,7 +19,8 @@ import com.mysema.query.grammar.Ops;
public final class PathMetadata<T> {
private static SimpleExprFactory factory = new SimpleExprFactory();
// TODO : refactor to operation
public static final PathType ARRAY_SIZE = new PathType("array size");
public static final PathType ARRAYVALUE = new PathType("array value");
@ -36,6 +37,7 @@ public final class PathMetadata<T> {
public static final PathType PROPERTY = new PathType("propery");
// TODO : refactor to operation
public static final PathType SIZE = new PathType("size");
public static final PathType VARIABLE = new PathType("variable");

View File

@ -12,8 +12,6 @@ import com.mysema.commons.lang.Assert;
* @version $Id$
*/
public class OperationPattern {
// private static final Pattern patter
private final String pattern;