From 7c83010df1669225a9d022ce49999bc17df13ea8 Mon Sep 17 00:00:00 2001 From: Robin Bygrave Date: Sun, 31 Jul 2016 16:39:23 +1200 Subject: [PATCH] #788 - Deprecate findRowCount() ... add findCount(). Effectively renaming findRowCount() to findCount() --- .../java/com/avaje/ebean/EbeanServer.java | 21 +- src/main/java/com/avaje/ebean/Query.java | 213 +++++++----------- .../server/core/DefaultServer.java | 12 +- .../server/querydefn/DefaultOrmQuery.java | 16 +- .../ebeaninternal/api/TDSpiEbeanServer.java | 10 + 5 files changed, 138 insertions(+), 134 deletions(-) diff --git a/src/main/java/com/avaje/ebean/EbeanServer.java b/src/main/java/com/avaje/ebean/EbeanServer.java index 3392334a6..fc82a2fa7 100644 --- a/src/main/java/com/avaje/ebean/EbeanServer.java +++ b/src/main/java/com/avaje/ebean/EbeanServer.java @@ -739,12 +739,19 @@ public interface EbeanServer { T getReference(Class beanType, Object id); /** - * Return the number of 'top level' or 'root' entities this query should - * return. + * Return the number of 'top level' or 'root' entities this query should return. * * @see Query#findRowCount() * @see com.avaje.ebean.Query#findFutureRowCount() */ + int findCount(Query query, Transaction transaction); + + /** + * Deprecated in favor of findCount() + * + * Return the number of 'top level' or 'root' entities this query should return. + * @deprecated + */ int findRowCount(Query query, Transaction transaction); /** @@ -868,7 +875,15 @@ public interface EbeanServer { * @return a Future object for the row count query * @see com.avaje.ebean.Query#findFutureRowCount() */ - FutureRowCount findFutureRowCount(Query query, Transaction transaction); + FutureRowCount findFutureCount(Query query, Transaction transaction); + + /** + * Deprecated in favor of findFutureCount(). + * + * Execute find row count query in a background thread. + * @deprecated + */ + FutureRowCount findFutureRowCount(Query query, Transaction transaction); /** * Execute find Id's query in a background thread. diff --git a/src/main/java/com/avaje/ebean/Query.java b/src/main/java/com/avaje/ebean/Query.java index 42bbd424c..f860cd11c 100644 --- a/src/main/java/com/avaje/ebean/Query.java +++ b/src/main/java/com/avaje/ebean/Query.java @@ -13,10 +13,10 @@ import java.util.Set; *

* Example: Create the query using the API. *

- * + *

*

{@code
  *
- * List orderList = 
+ * List orderList =
  *   ebeanServer.find(Order.class)
  *     .fetch("customer")
  *     .fetch("details")
@@ -26,46 +26,41 @@ import java.util.Set;
  *     .orderBy("customer.id, id desc")
  *     .setMaxRows(50)
  *     .findList();
- *   
+ *
  * ...
  * }
- * *

* Example: The same query using the query language *

- * *
{@code
  *
- * String oql = 
+ * String oql =
  *   	"  find  order "
  *   	+" fetch customer "
  *   	+" fetch details "
  *   	+" where customer.name like :custName and orderDate > :minOrderDate "
  *   	+" order by customer.id, id desc "
  *   	+" limit 50 ";
- *   
+ *
  * Query query = ebeanServer.createQuery(Order.class, oql);
  * query.setParameter("custName", "Rob%");
  * query.setParameter("minOrderDate", lastWeek);
- *   
+ *
  * List orderList = query.findList();
  * ...
  * }
- * *

* Example: Using a named query called "with.cust.and.details" *

- * *
{@code
  *
  * Query query = ebeanServer.createNamedQuery(Order.class,"with.cust.and.details");
  * query.setParameter("custName", "Rob%");
  * query.setParameter("minOrderDate", lastWeek);
- *   
+ *
  * List orderList = query.findList();
  * ...
  * }
- * *

AutoTune

*

* Ebean has built in support for "AutoTune". This is a mechanism where a query @@ -82,7 +77,6 @@ import java.util.Set; * to a remote client or where there is some requirement for "Read Consistency" * guarantees. *

- * *

Query Language

*

* Partial Objects @@ -101,7 +95,6 @@ import java.util.Set; * concurrency checking will occur but only include the fetched properties. * Refer to "ALL Properties/Columns" mode of Optimistic Concurrency checking. *

- * *
{@code
  * [ find  {bean type} [ ( * | {fetch properties} ) ] ]
  * [ fetch {associated bean} [ ( * | {fetch properties} ) ] ]
@@ -109,7 +102,6 @@ import java.util.Set;
  * [ order by {order by properties} ]
  * [ limit {max rows} [ offset {first row} ] ]
  * }
- * *

* FIND {bean type} [ ( * | {fetch properties} ) ] *

@@ -163,50 +155,40 @@ import java.util.Set; *

* Find orders fetching all its properties *

- * *
{@code
  * find order
  * }
- * *

* Find orders fetching all its properties *

- * *
{@code
  * find order (*)
  * }
- * *

* Find orders fetching its id, shipDate and status properties. Note that the id * property is always fetched even if it is not included in the list of fetch * properties. *

- * *
{@code
  * find order (shipDate, status)
  * }
- * *

* Find orders with a named bind variable (that will need to be bound via * {@link Query#setParameter(String, Object)}). *

- * *
{@code
  * find order
  * where customer.name like :custLike
  * }
- * *

* Find orders and also fetch the customer with a named bind parameter. This * will fetch and populate both the order and customer objects. *

- * *
{@code
  * find  order
  * fetch customer
  * where customer.id = :custId
  * }
- * *

* Find orders and also fetch the customer, customer shippingAddress, order * details and related product. Note that customer and product objects will be @@ -215,7 +197,6 @@ import java.util.Set; * objects (associated with each order detail) will have their id, sku and name * populated. *

- * *
{@code
  * find  order
  * fetch customer (name)
@@ -223,7 +204,6 @@ import java.util.Set;
  * fetch details
  * fetch details.product (sku, name)
  * }
- * *

Early parsing of the Query

*

* When you get a Query object from a named query, the query statement has @@ -269,9 +249,8 @@ import java.util.Set; * to make it as easy as possible to use your own SQL to populate entity beans. * Refer to {@link RawSql} . *

- * - * @param - * the type of Entity bean this query will fetch. + * + * @param the type of Entity bean this query will fetch. */ public interface Query { @@ -289,7 +268,7 @@ public interface Query { * Perform an 'As of' query using history tables to return the object graph * as of a time in the past. *

- * To perform this query the DB must have underlying history tables. + * To perform this query the DB must have underlying history tables. *

* * @param asOf the date time in the past at which you want to view the data @@ -399,7 +378,6 @@ public interface Query { * You use {@link #fetch(String, String)} to specify specific properties to fetch * on other non-root level paths of the object graph. *

- * *
{@code
    *
    * List customers =
@@ -412,8 +390,7 @@ public interface Query {
    *
    * }
* - * @param fetchProperties - * the properties to fetch for this bean (* = all properties). + * @param fetchProperties the properties to fetch for this bean (* = all properties). */ Query select(String fetchProperties); @@ -428,7 +405,6 @@ public interface Query { * only those properties are fetched and populated resulting in a * "Partial Object" - a bean that only has some of its properties populated. *

- * *
{@code
    *
    * // query orders...
@@ -437,17 +413,15 @@ public interface Query {
    *       // fetch the customer...
    *       // ... getting the customers name and phone number
    *       .fetch("customer", "name, phoneNumber")
-   * 
+   *
    *       // ... also fetch the customers billing address (* = all properties)
    *       .fetch("customer.billingAddress", "*")
    *       .findList();
    * }
- * *

* If columns is null or "*" then all columns/properties for that path are * fetched. *

- * *
{@code
    *
    * // fetch customers (their id, name and status)
@@ -458,19 +432,16 @@ public interface Query {
    *     .findList();
    *
    * }
- * - * @param path - * the path of an associated (1-1,1-M,M-1,M-M) bean. - * @param fetchProperties - * properties of the associated bean that you want to include in the - * fetch (* means all properties, null also means all properties). + * + * @param path the path of an associated (1-1,1-M,M-1,M-M) bean. + * @param fetchProperties properties of the associated bean that you want to include in the + * fetch (* means all properties, null also means all properties). */ Query fetch(String path, String fetchProperties); /** * Additionally specify a FetchConfig to use a separate query or lazy loading * to load this path. - * *
{@code
    *
    * // fetch customers (their id, name and status)
@@ -500,16 +471,13 @@ public interface Query {
    *
    * }
* - * @param path - * the property of an associated (1-1,1-M,M-1,M-M) bean. + * @param path the property of an associated (1-1,1-M,M-1,M-M) bean. */ Query fetch(String path); /** * Additionally specify a JoinConfig to specify a "query join" and or define * the lazy loading query. - * - * *
{@code
    *
    * // fetch customers (their id, name and status)
@@ -536,7 +504,7 @@ public interface Query {
    * 

* This query will execute against the EbeanServer that was used to create it. *

- * + * * @see EbeanServer#findIds(Query, Transaction) */ List findIds(); @@ -564,7 +532,6 @@ public interface Query { * iterator uses the QueryEachConsumer (SAM) interface which is better suited to use * with Java8 closures. *

- * *
{@code
    *
    *  ebeanServer.find(Customer.class)
@@ -578,8 +545,7 @@ public interface Query {
    *
    * }
* - * @param consumer - * the consumer used to process the queried beans. + * @param consumer the consumer used to process the queried beans. */ void findEach(QueryEachConsumer consumer); @@ -591,8 +557,6 @@ public interface Query { * iterator uses the QueryEachWhileConsumer (SAM) interface which is better suited to use * with Java8 closures. *

- - * *
{@code
    *
    *  ebeanServer.find(Customer.class)
@@ -611,8 +575,7 @@ public interface Query {
    *
    * }
* - * @param consumer - * the consumer used to process the queried beans. + * @param consumer the consumer used to process the queried beans. */ void findEachWhile(QueryEachWhileConsumer consumer); @@ -621,7 +584,6 @@ public interface Query { *

* This query will execute against the EbeanServer that was used to create it. *

- * *
{@code
    *
    * List customers =
@@ -640,7 +602,6 @@ public interface Query {
    * 

* This query will execute against the EbeanServer that was used to create it. *

- * *
{@code
    *
    * Set customers =
@@ -663,7 +624,6 @@ public interface Query {
    * You can use setMapKey() so specify the property values to be used as keys
    * on the map. If one is not specified then the id property is used.
    * 

- * *
{@code
    *
    * Map map =
@@ -672,7 +632,7 @@ public interface Query {
    *     .findMap();
    *
    * }
- * + * * @see EbeanServer#findMap(Query, Transaction) */ Map findMap(); @@ -693,7 +653,6 @@ public interface Query { * This is useful when your predicates dictate that your query should only * return 0 or 1 results. *

- * *
{@code
    *
    * // assuming the sku of products is unique...
@@ -703,16 +662,14 @@ public interface Query {
    *         .findUnique();
    * ...
    * }
- * *

* It is also useful with finding objects by their id when you want to specify * further join information. *

- * *
{@code
    *
    * // Fetch order 1 and additionally fetch join its order details...
-   * Order order = 
+   * Order order =
    *     ebeanServer.find(Order.class)
    *       .setId(1)
    *       .fetch("details")
@@ -731,13 +688,13 @@ public interface Query {
   /**
    * Return versions of a @History entity bean.
    * 

- * Note that this query will work against view based history implementations - * but not sql2011 standards based implementations that require a start and - * end timestamp to be specified. + * Note that this query will work against view based history implementations + * but not sql2011 standards based implementations that require a start and + * end timestamp to be specified. *

*

- * Generally this query is expected to be a find by id or unique predicates query. - * It will execute the query against the history returning the versions of the bean. + * Generally this query is expected to be a find by id or unique predicates query. + * It will execute the query against the history returning the versions of the bean. *

*/ List> findVersions(); @@ -745,8 +702,8 @@ public interface Query { /** * Return versions of a @History entity bean between the 2 timestamps. *

- * Generally this query is expected to be a find by id or unique predicates query. - * It will execute the query against the history returning the versions of the bean. + * Generally this query is expected to be a find by id or unique predicates query. + * It will execute the query against the history returning the versions of the bean. *

*/ List> findVersionsBetween(Timestamp start, Timestamp end); @@ -774,6 +731,14 @@ public interface Query { * This is the number of 'top level' or 'root level' entities. *

*/ + int findCount(); + + /** + * Deprecated in favor of findCount(). + *

+ * Return the count of entities this query should return. + * @deprecated + */ int findRowCount(); /** @@ -783,9 +748,24 @@ public interface Query { * execution status (isDone etc) and get the value (with or without a * timeout). *

- * + * * @return a Future object for the row count query */ + FutureRowCount findFutureCount(); + + /** + * Deprecated in favor of findFutureCount(). + *

+ * Execute find row count query in a background thread. + *

+ * This returns a Future object which can be used to cancel, check the + * execution status (isDone etc) and get the value (with or without a + * timeout). + *

+ * + * @return a Future object for the row count query + * @deprecated + */ FutureRowCount findFutureRowCount(); /** @@ -795,7 +775,7 @@ public interface Query { * execution status (isDone etc) and get the value (with or without a * timeout). *

- * + * * @return a Future object for the list of Id's */ FutureIds findFutureIds(); @@ -821,7 +801,6 @@ public interface Query { * If maxRows is not set on the query prior to calling findPagedList() then a * PersistenceException is thrown. *

- * *
{@code
    *
    *  PagedList pagedList = Ebean.find(Order.class)
@@ -843,24 +822,21 @@ public interface Query {
 
   /**
    * Set a named bind parameter. Named parameters have a colon to prefix the name.
-   * 
    * 
{@code
    *
    * // a query with a named parameter
    * String oql = "find order where status = :orderStatus";
-   * 
+   *
    * Query query = ebeanServer.find(Order.class, oql);
-   * 
+   *
    * // bind the named parameter
    * query.bind("orderStatus", OrderStatus.NEW);
    * List list = query.findList();
    *
    * }
- * - * @param name - * the parameter name - * @param value - * the parameter value + * + * @param name the parameter name + * @param value the parameter value */ Query setParameter(String name, Object value); @@ -868,25 +844,22 @@ public interface Query { * Set an ordered bind parameter according to its position. Note that the * position starts at 1 to be consistent with JDBC PreparedStatement. You need * to set a parameter value for each ? you have in the query. - * *
{@code
    *
    * // a query with a positioned parameter
    * String oql = "where status = ? order by id desc";
-   * 
+   *
    * Query query = ebeanServer.createQuery(Order.class, oql);
-   * 
+   *
    * // bind the parameter
    * query.setParameter(1, OrderStatus.NEW);
-   * 
+   *
    * List list = query.findList();
    *
    * }
- * - * @param position - * the parameter bind position starting from 1 (not 0) - * @param value - * the parameter bind value. + * + * @param position the parameter bind position starting from 1 (not 0) + * @param value the parameter bind value. */ Query setParameter(int position, Object value); @@ -896,7 +869,6 @@ public interface Query { * You can use this to have further control over the query. For example adding * fetch joins. *

- * *
{@code
    *
    * Order order =
@@ -919,10 +891,9 @@ public interface Query {
 
   /**
    * Add a single Expression to the where clause returning the query.
-   * 
    * 
{@code
    *
-   * List newOrders = 
+   * List newOrders =
    *     ebeanServer.find(Order.class)
    * 		.where().eq("status", Order.NEW)
    * 		.findList();
@@ -936,7 +907,6 @@ public interface Query {
    * Add Expressions to the where clause with the ability to chain on the
    * ExpressionList. You can use this for adding multiple expressions to the
    * where clause.
-   * 
    * 
{@code
    *
    * List orders =
@@ -947,9 +917,9 @@ public interface Query {
    *     .findList();
    *
    * }
- * - * @see Expr + * * @return The ExpressionList for adding expressions to. + * @see Expr */ ExpressionList where(); @@ -984,9 +954,8 @@ public interface Query { * each customer you only want to get the new orders they placed since last * week. In this case you can use filterMany() to filter the orders. *

- * *
{@code
-   * 
+   *
    * List list =
    *     ebeanServer.find(Customer.class)
    *     // .fetch("orders", new FetchConfig().lazy())
@@ -995,20 +964,17 @@ public interface Query {
    *     .where().ilike("name", "rob%")
    *     .filterMany("orders").eq("status", Order.Status.NEW).gt("orderDate", lastWeek)
    *     .findList();
-   * 
+   *
    * }
- * *

* Please note you have to be careful that you add expressions to the correct * expression list - as there is one for the 'root level' and one for each * filterMany that you have. *

- * - * @param propertyName - * the name of the many property that you want to have a filter on. - * + * + * @param propertyName the name of the many property that you want to have a filter on. * @return the expression list that you add filter expressions for the many - * to. + * to. */ ExpressionList filterMany(String propertyName); @@ -1021,9 +987,9 @@ public interface Query { * Note that this returns the ExpressionList (so you can add multiple * expressions to the query in a fluent API way). *

- * - * @see Expr + * * @return The ExpressionList for adding more expressions to. + * @see Expr */ ExpressionList having(); @@ -1037,9 +1003,8 @@ public interface Query { * than the ExpressionList. This is useful when you want to further specify * something on the query. *

- * - * @param addExpressionToHaving - * the expression to add to the having clause. + * + * @param addExpressionToHaving the expression to add to the having clause. * @return the Query object */ Query having(Expression addExpressionToHaving); @@ -1151,9 +1116,8 @@ public interface Query { /** * Set the maximum number of rows to return in the query. - * - * @param maxRows - * the maximum number of rows to return in the query. + * + * @param maxRows the maximum number of rows to return in the query. */ Query setMaxRows(int maxRows); @@ -1162,11 +1126,10 @@ public interface Query { *

* If no property is set then the id property is used. *

- * *
{@code
    *
    * // Assuming sku is unique for products...
-   *    
+   *
    * Map productMap =
    *     ebeanServer.find(Product.class)
    *     // use sku for keys...
@@ -1174,9 +1137,8 @@ public interface Query {
    *     .findMap();
    *
    * }
- * - * @param mapKey - * the property to use as keys for a map. + * + * @param mapKey the property to use as keys for a map. */ Query setMapKey(String mapKey); @@ -1203,7 +1165,7 @@ public interface Query { /** * Set to true if this query should execute against the doc store. *

- * When setting this you may also consider disabling lazy loading. + * When setting this you may also consider disabling lazy loading. *

*/ Query setUseDocStore(boolean useDocStore); @@ -1226,9 +1188,8 @@ public interface Query { * preparedStatement. If the timeout occurs an exception will be thrown - this * will be a SQLException wrapped up in a PersistenceException. *

- * - * @param secs - * the query timeout limit in seconds. Zero means there is no limit. + * + * @param secs the query timeout limit in seconds. Zero means there is no limit. */ Query setTimeout(int secs); @@ -1260,7 +1221,7 @@ public interface Query { * Return true if this query has forUpdate set. */ boolean isForUpdate(); - + /** * Set root table alias. */ @@ -1274,7 +1235,7 @@ public interface Query { /** * Set true if you want to disable lazy loading. *

- * That is, once the object graph is returned further lazy loading is disabled. + * That is, once the object graph is returned further lazy loading is disabled. *

*/ Query setDisableLazyLoading(boolean disableLazyLoading); diff --git a/src/main/java/com/avaje/ebeaninternal/server/core/DefaultServer.java b/src/main/java/com/avaje/ebeaninternal/server/core/DefaultServer.java index 5bc59014d..75f91947e 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/core/DefaultServer.java +++ b/src/main/java/com/avaje/ebeaninternal/server/core/DefaultServer.java @@ -1152,12 +1152,16 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer { } } - public int findRowCount(Query query, Transaction t) { + public int findCount(Query query, Transaction t) { SpiQuery copy = ((SpiQuery) query).copy(); return findRowCountWithCopy(copy, t); } + public int findRowCount(Query query, Transaction t) { + return findCount(query, t); + } + public int findRowCountWithCopy(Query query, Transaction t) { SpiOrmQueryRequest request = createQueryRequest(Type.ROWCOUNT, query, t); @@ -1213,7 +1217,7 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer { } } - public FutureRowCount findFutureRowCount(Query q, Transaction t) { + public FutureRowCount findFutureCount(Query q, Transaction t) { SpiQuery copy = ((SpiQuery) q).copy(); copy.setFutureFetch(true); @@ -1228,6 +1232,10 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer { return queryFuture; } + public FutureRowCount findFutureRowCount(Query q, Transaction t) { + return findFutureCount(q, t); + } + public FutureIds findFutureIds(Query query, Transaction t) { SpiQuery copy = ((SpiQuery) query).copy(); diff --git a/src/main/java/com/avaje/ebeaninternal/server/querydefn/DefaultOrmQuery.java b/src/main/java/com/avaje/ebeaninternal/server/querydefn/DefaultOrmQuery.java index befb221ce..073e9215d 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/querydefn/DefaultOrmQuery.java +++ b/src/main/java/com/avaje/ebeaninternal/server/querydefn/DefaultOrmQuery.java @@ -1048,11 +1048,16 @@ public class DefaultOrmQuery implements SpiQuery { } @Override - public int findRowCount() { + public int findCount() { // a copy of this query is made in the server // as the query needs to modified (so we modify // the copy rather than this query instance) - return server.findRowCount(this, null); + return server.findCount(this, null); + } + + @Override + public int findRowCount() { + return findCount(); } @Override @@ -1119,9 +1124,14 @@ public class DefaultOrmQuery implements SpiQuery { return server.findFutureList(this, null); } + @Override + public FutureRowCount findFutureCount() { + return server.findFutureCount(this, null); + } + @Override public FutureRowCount findFutureRowCount() { - return server.findFutureRowCount(this, null); + return findFutureCount(); } @Override diff --git a/src/test/java/com/avaje/ebeaninternal/api/TDSpiEbeanServer.java b/src/test/java/com/avaje/ebeaninternal/api/TDSpiEbeanServer.java index 97c93f13f..345a4472f 100644 --- a/src/test/java/com/avaje/ebeaninternal/api/TDSpiEbeanServer.java +++ b/src/test/java/com/avaje/ebeaninternal/api/TDSpiEbeanServer.java @@ -455,6 +455,11 @@ public class TDSpiEbeanServer implements SpiEbeanServer { return null; } + @Override + public int findCount(Query query, Transaction transaction) { + return 0; + } + @Override public int findRowCount(Query query, Transaction transaction) { return 0; @@ -480,6 +485,11 @@ public class TDSpiEbeanServer implements SpiEbeanServer { return null; } + @Override + public FutureRowCount findFutureCount(Query query, Transaction transaction) { + return null; + } + @Override public FutureRowCount findFutureRowCount(Query query, Transaction transaction) { return null;