From f2842f87da99da875dfe0d6afe8dc5d7fea654f4 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Thu, 9 Dec 2010 23:09:24 +0000 Subject: [PATCH] QueryBuilder -> Query. --- .../com/samskivert/depot/CountRecord.java | 2 +- .../com/samskivert/depot/DepotRepository.java | 4 +- .../depot/{QueryBuilder.java => Query.java} | 76 +++++++++---------- .../com/samskivert/depot/ProjectionTest.java | 19 ++--- 4 files changed, 49 insertions(+), 52 deletions(-) rename src/main/java/com/samskivert/depot/{QueryBuilder.java => Query.java} (87%) diff --git a/src/main/java/com/samskivert/depot/CountRecord.java b/src/main/java/com/samskivert/depot/CountRecord.java index bf636d2..d561fbe 100644 --- a/src/main/java/com/samskivert/depot/CountRecord.java +++ b/src/main/java/com/samskivert/depot/CountRecord.java @@ -26,7 +26,7 @@ import com.samskivert.depot.expression.ColumnExp; /** * Handy record for computing the count of something. In general, you need not use this directly, - * but should instead use {@link QueryBuilder#selectCount}. For example: {@code + * but should instead use {@link Query#selectCount}. For example: {@code * from(ForumThreadRecord.class).where(ForumThreadRecord.GROUP_ID.eq(groupId)).selectCount()} */ @Computed @Entity diff --git a/src/main/java/com/samskivert/depot/DepotRepository.java b/src/main/java/com/samskivert/depot/DepotRepository.java index 789b373..6dd4e16 100644 --- a/src/main/java/com/samskivert/depot/DepotRepository.java +++ b/src/main/java/com/samskivert/depot/DepotRepository.java @@ -341,9 +341,9 @@ public abstract class DepotRepository * Returns a builder that can be used to construct a query in a fluent style. For example: * {@code from(FooRecord.class).where(ID.greaterThan(25)).ascending(SIZE).select()} */ - public QueryBuilder from (Class type) + public Query from (Class type) { - return new QueryBuilder(_ctx, this, type); + return new Query(_ctx, this, type); } /** diff --git a/src/main/java/com/samskivert/depot/QueryBuilder.java b/src/main/java/com/samskivert/depot/Query.java similarity index 87% rename from src/main/java/com/samskivert/depot/QueryBuilder.java rename to src/main/java/com/samskivert/depot/Query.java index dfe4e75..56f2bc8 100644 --- a/src/main/java/com/samskivert/depot/QueryBuilder.java +++ b/src/main/java/com/samskivert/depot/Query.java @@ -40,10 +40,10 @@ import static com.google.common.base.Preconditions.checkState; * The root of a fluent mechanism for constructing queries. Obtain an instance via {@link * DepotRepository#from}. */ -public class QueryBuilder +public class Query implements Cloneable { - public QueryBuilder (PersistenceContext ctx, DepotRepository repo, Class pclass) + public Query (PersistenceContext ctx, DepotRepository repo, Class pclass) { _ctx = ctx; _repo = repo; @@ -51,37 +51,37 @@ public class QueryBuilder } /** Disables the use of the cache for this query. */ - public QueryBuilder noCache () { + public Query noCache () { return cache(DepotRepository.CacheStrategy.BEST); } /** Configures the use of {@link CacheStrategy#BEST} for this query. */ - public QueryBuilder cacheBest () { + public Query cacheBest () { return cache(DepotRepository.CacheStrategy.BEST); } /** Configures the use of {@link CacheStrategy#RECORDS} for this query. */ - public QueryBuilder cacheRecords () { + public Query cacheRecords () { return cache(DepotRepository.CacheStrategy.RECORDS); } /** Configures the use of {@link CacheStrategy#SHORT_KEYS} for this query. */ - public QueryBuilder cacheShortKeys () { + public Query cacheShortKeys () { return cache(DepotRepository.CacheStrategy.SHORT_KEYS); } /** Configures the use of {@link CacheStrategy#LONG_KEYS} for this query. */ - public QueryBuilder cacheLongKeys () { + public Query cacheLongKeys () { return cache(DepotRepository.CacheStrategy.LONG_KEYS); } /** Configures the use of {@link CacheStrategy#CONTENTS} for this query. */ - public QueryBuilder cacheContents () { + public Query cacheContents () { return cache(DepotRepository.CacheStrategy.CONTENTS); } /** Configures the specified caching policy this query. */ - public QueryBuilder cache (DepotRepository.CacheStrategy cache) { + public Query cache (DepotRepository.CacheStrategy cache) { _cache = cache; return this; } @@ -91,7 +91,7 @@ public class QueryBuilder * simply be omitted, but for deletions, this method must be used if you intend to delete all * rows in the table. */ - public QueryBuilder whereTrue () + public Query whereTrue () { return where(Exps.literal("true")); } @@ -99,7 +99,7 @@ public class QueryBuilder /** * Configures a {@link Where} clause that ANDs together all of the supplied expressions. */ - public QueryBuilder where (SQLExpression... exprs) + public Query where (SQLExpression... exprs) { return where(Arrays.asList(exprs)); } @@ -107,7 +107,7 @@ public class QueryBuilder /** * Configures a {@link Where} clause that ANDs together all of the supplied expressions. */ - public QueryBuilder where (Iterable> exprs) + public Query where (Iterable> exprs) { Iterator> iter = exprs.iterator(); checkArgument(iter.hasNext(), "Must supply at least one expression."); @@ -119,7 +119,7 @@ public class QueryBuilder * Configures a {@link Where} clause that selects rows where the supplied column equals the * supplied value. */ - public > QueryBuilder where (ColumnExp column, V value) + public > Query where (ColumnExp column, V value) { return where(new Where(column, value)); } @@ -129,7 +129,7 @@ public class QueryBuilder * supplied values. */ public , V2 extends Comparable> - QueryBuilder where (ColumnExp index1, V1 value1, ColumnExp index2, V2 value2) + Query where (ColumnExp index1, V1 value1, ColumnExp index2, V2 value2) { return where(new Where(index1, value1, index2, value2)); } @@ -138,7 +138,7 @@ public class QueryBuilder * Configures a {@link Where} clause that selects rows where both supplied columns equal both * supplied values. */ - public QueryBuilder where (WhereClause where) + public Query where (WhereClause where) { checkState(_where == null, "Where clause is already configured."); _where = where; @@ -148,7 +148,7 @@ public class QueryBuilder /** * Adds a {@link Join} clause configured with the supplied left and right columns. */ - public QueryBuilder join (ColumnExp left, ColumnExp right) + public Query join (ColumnExp left, ColumnExp right) { return join(new Join(left, right)); } @@ -156,8 +156,8 @@ public class QueryBuilder /** * Adds a {@link Join} clause configured with the join condition. */ - public QueryBuilder join (Class joinClass, - SQLExpression joinCondition) + public Query join (Class joinClass, + SQLExpression joinCondition) { return join(new Join(joinClass, joinCondition)); } @@ -166,7 +166,7 @@ public class QueryBuilder * Adds a {@link Join} clause configured with the supplied left and right columns and join * type. */ - public QueryBuilder join (ColumnExp left, ColumnExp right, Join.Type type) + public Query join (ColumnExp left, ColumnExp right, Join.Type type) { return join(new Join(left, right).setType(type)); } @@ -175,7 +175,7 @@ public class QueryBuilder * Configures the query with the supplied {@link Join} clause. Multiple join clauses are * allowed. */ - public QueryBuilder join (Join join) + public Query join (Join join) { if (_joins == null) { _joins = Lists.newArrayList(); @@ -187,7 +187,7 @@ public class QueryBuilder /** * Configures a {@link GroupBy} clause on the supplied group expressions. */ - public QueryBuilder groupBy (SQLExpression... exprs) + public Query groupBy (SQLExpression... exprs) { checkState(_groupBy == null, "GroupBy clause is already configured."); _groupBy = new GroupBy(exprs); @@ -197,7 +197,7 @@ public class QueryBuilder /** * Configures an {@link OrderBy} clause configured to randomly order the results. */ - public QueryBuilder randomOrder () + public Query randomOrder () { return orderBy(OrderBy.random()); } @@ -205,7 +205,7 @@ public class QueryBuilder /** * Configures an {@link OrderBy} clause that ascends on the supplied expression. */ - public QueryBuilder ascending (SQLExpression value) + public Query ascending (SQLExpression value) { return orderBy(OrderBy.ascending(value)); } @@ -213,7 +213,7 @@ public class QueryBuilder /** * Configures an {@link OrderBy} clause that descends on the supplied expression. */ - public QueryBuilder descending (SQLExpression value) + public Query descending (SQLExpression value) { return orderBy(OrderBy.descending(value)); } @@ -221,7 +221,7 @@ public class QueryBuilder /** * Configures the query with the supplied {@link OrderBy} clause. */ - public QueryBuilder orderBy (OrderBy orderBy) + public Query orderBy (OrderBy orderBy) { checkState(_orderBy == null, "OrderBy clause is already configured."); _orderBy = orderBy; @@ -231,7 +231,7 @@ public class QueryBuilder /** * Configures a {@link Limit} clause configured with the supplied count. */ - public QueryBuilder limit (int count) + public Query limit (int count) { checkState(_limit == null, "Limit clause is already configured."); _limit = new Limit(0, count); @@ -241,7 +241,7 @@ public class QueryBuilder /** * Configures a {@link Limit} clause configured with the supplied offset and count. */ - public QueryBuilder limit (int offset, int count) + public Query limit (int offset, int count) { checkState(_limit == null, "Limit clause is already configured."); _limit = new Limit(offset, count); @@ -251,7 +251,7 @@ public class QueryBuilder /** * Configures a {@link FromOverride} clause configured with the supplied override class. */ - public QueryBuilder override (Class fromClass) + public Query override (Class fromClass) { return override(new FromOverride(fromClass)); } @@ -259,8 +259,8 @@ public class QueryBuilder /** * Configures a {@link FromOverride} clause configured with the supplied override classes. */ - public QueryBuilder override (Class fromClass1, - Class fromClass2) + public Query override (Class fromClass1, + Class fromClass2) { return override(new FromOverride(fromClass1, fromClass2)); } @@ -268,7 +268,7 @@ public class QueryBuilder /** * Configures the query with the supplied {@link FromOverride} clause. */ - public QueryBuilder override (FromOverride fromOverride) + public Query override (FromOverride fromOverride) { checkState(_fromOverride == null, "FromOverride clause is already configured."); _fromOverride = fromOverride; @@ -278,7 +278,7 @@ public class QueryBuilder /** * Adds a {@link FieldDefinition} clause. */ - public QueryBuilder fieldDef (String field, String value) + public Query fieldDef (String field, String value) { return fieldDef(new FieldDefinition(field, value)); } @@ -286,7 +286,7 @@ public class QueryBuilder /** * Adds a {@link FieldDefinition} clause. */ - public QueryBuilder fieldDef (String field, SQLExpression override) + public Query fieldDef (String field, SQLExpression override) { return fieldDef(new FieldDefinition(field, override)); } @@ -294,7 +294,7 @@ public class QueryBuilder /** * Adds a {@link FieldDefinition} clause. */ - public QueryBuilder fieldDef (ColumnExp field, SQLExpression override) + public Query fieldDef (ColumnExp field, SQLExpression override) { return fieldDef(new FieldDefinition(field, override)); } @@ -302,7 +302,7 @@ public class QueryBuilder /** * Adds a {@link FieldDefinition} clause. */ - public QueryBuilder fieldDef (FieldDefinition fieldDef) + public Query fieldDef (FieldDefinition fieldDef) { if (_fieldDefs == null) { _fieldDefs = Lists.newArrayList(); @@ -314,7 +314,7 @@ public class QueryBuilder /** * Configures a {@link ForUpdate} clause which marks this query as selecting for update. */ - public QueryBuilder forUpdate () + public Query forUpdate () { checkState(_forUpdate == null, "ForUpdate clause is already configured."); _forUpdate = new ForUpdate(); @@ -498,10 +498,10 @@ public class QueryBuilder * Returns a clone of this query builder, including all partially configured state. Useful for * constructing partially configured queries and then executing variants. */ - public QueryBuilder clone () + public Query clone () { try { - @SuppressWarnings("unchecked") QueryBuilder qb = (QueryBuilder)super.clone(); + @SuppressWarnings("unchecked") Query qb = (Query)super.clone(); // deep copy the list fields, if we have any if (qb._joins != null) { qb._joins = Lists.newArrayList(qb._joins); diff --git a/src/test/java/com/samskivert/depot/ProjectionTest.java b/src/test/java/com/samskivert/depot/ProjectionTest.java index c85ef55..be1cb53 100644 --- a/src/test/java/com/samskivert/depot/ProjectionTest.java +++ b/src/test/java/com/samskivert/depot/ProjectionTest.java @@ -72,20 +72,17 @@ public class ProjectionTest extends TestBase @Test public void testTupleN () { + Query one = _repo.from(TestRecord.class).where(TestRecord.RECORD_ID.eq(1)); assertEquals(Tuple2.create(1, "Elvis"), - _repo.from(TestRecord.class).where(TestRecord.RECORD_ID.eq(1)). - load(TestRecord.RECORD_ID, TestRecord.NAME)); + one.load(TestRecord.RECORD_ID, TestRecord.NAME)); assertEquals(Tuple3.create(1, "Elvis", 99), - _repo.from(TestRecord.class).where(TestRecord.RECORD_ID.eq(1)). - load(TestRecord.RECORD_ID, TestRecord.NAME, TestRecord.AGE)); + one.load(TestRecord.RECORD_ID, TestRecord.NAME, TestRecord.AGE)); assertEquals(Tuple4.create(1, "Elvis", 99, "Right here"), - _repo.from(TestRecord.class).where(TestRecord.RECORD_ID.eq(1)). - load(TestRecord.RECORD_ID, TestRecord.NAME, TestRecord.AGE, - TestRecord.HOME_TOWN)); + one.load(TestRecord.RECORD_ID, TestRecord.NAME, TestRecord.AGE, + TestRecord.HOME_TOWN)); assertEquals(Tuple5.create(1, "Elvis", 99, "Right here", EnumKeyRecord.Type.A), - _repo.from(TestRecord.class).where(TestRecord.RECORD_ID.eq(1)). - load(TestRecord.RECORD_ID, TestRecord.NAME, TestRecord.AGE, - TestRecord.HOME_TOWN, TestRecord.TYPE)); + one.load(TestRecord.RECORD_ID, TestRecord.NAME, TestRecord.AGE, + TestRecord.HOME_TOWN, TestRecord.TYPE)); } @Test public void testProjectedJoin () @@ -98,7 +95,7 @@ public class ProjectionTest extends TestBase @Test public void testNoMatches () { - QueryBuilder empty = + Query empty = _repo.from(TestRecord.class).where(TestRecord.AGE.greaterThan(100)); // test a projection from a query that returns no matches