From 27100ebf645bbfb7d9e2d519595298771b06c0c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Westk=C3=A4mper?= Date: Tue, 19 May 2009 18:22:12 +0000 Subject: [PATCH] --- .../mysema/query/DefaultQueryMetadata.java | 11 ++-- .../java/com/mysema/query/Projectable.java | 54 +++++++++---------- .../com/mysema/query/ProjectableAdapter.java | 1 - .../main/java/com/mysema/query/QueryBase.java | 2 +- .../mysema/query/QueryBaseWithProjection.java | 39 ++++++++------ .../java/com/mysema/query/QueryModifiers.java | 40 +++++++++----- .../java/com/mysema/query/SearchResults.java | 6 +-- .../com/mysema/query/annotations/Literal.java | 2 +- .../query/grammar/types/PathMetadata.java | 4 +- .../query/serialization/OperationPattern.java | 2 - 10 files changed, 92 insertions(+), 69 deletions(-) diff --git a/querydsl-core/src/main/java/com/mysema/query/DefaultQueryMetadata.java b/querydsl-core/src/main/java/com/mysema/query/DefaultQueryMetadata.java index 2bb99910d..98b717b7b 100644 --- a/querydsl-core/src/main/java/com/mysema/query/DefaultQueryMetadata.java +++ b/querydsl-core/src/main/java/com/mysema/query/DefaultQueryMetadata.java @@ -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 implements QueryMetadata{ private boolean distinct; - private QueryModifiers modifiers = QueryModifiers.getDefault(); + private QueryModifiers modifiers = new QueryModifiers(); public List> getGroupBy() { return Collections.unmodifiableList(groupBy); diff --git a/querydsl-core/src/main/java/com/mysema/query/Projectable.java b/querydsl-core/src/main/java/com/mysema/query/Projectable.java index 4ec87014a..de1941134 100644 --- a/querydsl-core/src/main/java/com/mysema/query/Projectable.java +++ b/querydsl-core/src/main/java/com/mysema/query/Projectable.java @@ -37,6 +37,15 @@ public interface Projectable { */ Iterator iterate(Expr first, Expr second, Expr... rest); + /** + * iterate over the results with the given projection + * + * @param generic type of the Iteratpr + * @param projection + * @return an Iterator over the projection + */ + Iterator iterate(Expr projection); + /** * iterate over the distinct results with the given projection * @@ -46,15 +55,6 @@ public interface Projectable { * @return an Iterator over the projection */ Iterator iterateDistinct(Expr first, Expr second, Expr... rest); - - /** - * iterate over the results with the given projection - * - * @param generic type of the Iteratpr - * @param projection - * @return an Iterator over the projection - */ - Iterator iterate(Expr projection); /** * iterate over the distinct results with the given projection @@ -75,6 +75,15 @@ public interface Projectable { */ List list(Expr first, Expr second, Expr... rest); + /** + * list the results with the given projection + * + * @param generic type of the List + * @param projection + * @return a List over the projection + */ + List list(Expr projection); + /** * list the distinct results with the given projection * @@ -84,15 +93,6 @@ public interface Projectable { * @return a List over the projection */ List listDistinct(Expr first, Expr second, Expr... rest); - - /** - * list the results with the given projection - * - * @param generic type of the List - * @param projection - * @return a List over the projection - */ - List list(Expr projection); /** * list the distinct results with the given projection @@ -104,13 +104,12 @@ public interface Projectable { List listDistinct(Expr projection); /** - * return a unique result for the given projection * - * @param return type - * @param projection - * @return the result or null for an empty result + * @param + * @param expr + * @return */ - RT uniqueResult(Expr projection); + SearchResults listResults(Expr 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 - * @param expr - * @return + * @param return type + * @param projection + * @return the result or null for an empty result */ - SearchResults listResults(Expr expr); + RT uniqueResult(Expr projection); } \ No newline at end of file diff --git a/querydsl-core/src/main/java/com/mysema/query/ProjectableAdapter.java b/querydsl-core/src/main/java/com/mysema/query/ProjectableAdapter.java index e3e8f6247..cc0ce308d 100644 --- a/querydsl-core/src/main/java/com/mysema/query/ProjectableAdapter.java +++ b/querydsl-core/src/main/java/com/mysema/query/ProjectableAdapter.java @@ -68,7 +68,6 @@ public abstract class ProjectableAdapter implements Projectable{ return projectable.listResults(expr); } - public List listDistinct(Expr projection) { return projectable.listDistinct(projection); } diff --git a/querydsl-core/src/main/java/com/mysema/query/QueryBase.java b/querydsl-core/src/main/java/com/mysema/query/QueryBase.java index 1b858f7b4..89aa2c8e2 100644 --- a/querydsl-core/src/main/java/com/mysema/query/QueryBase.java +++ b/querydsl-core/src/main/java/com/mysema/query/QueryBase.java @@ -18,7 +18,7 @@ import com.mysema.query.grammar.types.Expr.EBoolean; */ public class QueryBase> implements Query { @SuppressWarnings("unchecked") - private SubType _this = (SubType)this; + protected final SubType _this = (SubType)this; private QueryMetadata metadata; diff --git a/querydsl-core/src/main/java/com/mysema/query/QueryBaseWithProjection.java b/querydsl-core/src/main/java/com/mysema/query/QueryBaseWithProjection.java index 6617e9350..ba28adb23 100644 --- a/querydsl-core/src/main/java/com/mysema/query/QueryBaseWithProjection.java +++ b/querydsl-core/src/main/java/com/mysema/query/QueryBaseWithProjection.java @@ -40,6 +40,21 @@ public abstract class QueryBaseWithProjection iterateDistinct(Expr first, Expr second, Expr... rest) { getMetadata().setDistinct(true); return iterate(first, second, rest); @@ -58,18 +73,6 @@ public abstract class QueryBaseWithProjection SearchResults listResults(Expr projection){ - QueryModifiers modifiers = getMetadata().getModifiers(); - List 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(list.subList(start, end), modifiers, list.size()); - } - } - public final List listDistinct(Expr first, Expr second, Expr... rest) { getMetadata().setDistinct(true); return list(first, second, rest); @@ -80,13 +83,15 @@ public abstract class QueryBaseWithProjection RT uniqueResult(Expr expr) { - Iterator it = iterate(expr); - return it.hasNext() ? it.next() : null; - } - public Object[] uniqueResult(Expr first, Expr second, Expr... rest) { + getMetadata().getModifiers().setLimit(1l); Iterator it = iterate(first, second, rest); return it.hasNext() ? it.next() : null; } + + public RT uniqueResult(Expr expr) { + getMetadata().getModifiers().setLimit(1l); + Iterator it = iterate(expr); + return it.hasNext() ? it.next() : null; + } } diff --git a/querydsl-core/src/main/java/com/mysema/query/QueryModifiers.java b/querydsl-core/src/main/java/com/mysema/query/QueryModifiers.java index 37d1b9c31..169a2118a 100644 --- a/querydsl-core/src/main/java/com/mysema/query/QueryModifiers.java +++ b/querydsl-core/src/main/java/com/mysema/query/QueryModifiers.java @@ -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; + } + + } diff --git a/querydsl-core/src/main/java/com/mysema/query/SearchResults.java b/querydsl-core/src/main/java/com/mysema/query/SearchResults.java index 2fc51d4c1..02dd6b9a6 100644 --- a/querydsl-core/src/main/java/com/mysema/query/SearchResults.java +++ b/querydsl-core/src/main/java/com/mysema/query/SearchResults.java @@ -25,9 +25,9 @@ public final class SearchResults { private final List results; - public SearchResults(List results, long limit, long offset, long total){ - this.limit = limit; - this.offset = offset; + public SearchResults(List 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; } diff --git a/querydsl-core/src/main/java/com/mysema/query/annotations/Literal.java b/querydsl-core/src/main/java/com/mysema/query/annotations/Literal.java index 71ca1e317..95fb72ae7 100644 --- a/querydsl-core/src/main/java/com/mysema/query/annotations/Literal.java +++ b/querydsl-core/src/main/java/com/mysema/query/annotations/Literal.java @@ -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 { diff --git a/querydsl-core/src/main/java/com/mysema/query/grammar/types/PathMetadata.java b/querydsl-core/src/main/java/com/mysema/query/grammar/types/PathMetadata.java index c3dc3655d..d095f7f4f 100644 --- a/querydsl-core/src/main/java/com/mysema/query/grammar/types/PathMetadata.java +++ b/querydsl-core/src/main/java/com/mysema/query/grammar/types/PathMetadata.java @@ -19,7 +19,8 @@ import com.mysema.query.grammar.Ops; public final class PathMetadata { 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 { 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"); diff --git a/querydsl-core/src/main/java/com/mysema/query/serialization/OperationPattern.java b/querydsl-core/src/main/java/com/mysema/query/serialization/OperationPattern.java index 64a3a275c..bcb9a16b4 100644 --- a/querydsl-core/src/main/java/com/mysema/query/serialization/OperationPattern.java +++ b/querydsl-core/src/main/java/com/mysema/query/serialization/OperationPattern.java @@ -12,8 +12,6 @@ import com.mysema.commons.lang.Assert; * @version $Id$ */ public class OperationPattern { - -// private static final Pattern patter private final String pattern;