From 2ec50d2c71704fc00cca02e81edc7513670b6247 Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Wed, 12 Nov 2014 22:24:37 +1300 Subject: [PATCH] Add query.apply(PathProperties) --- .../java/com/avaje/ebean/ExpressionList.java | 13 +++++++++++-- src/main/java/com/avaje/ebean/Query.java | 10 ++++++++++ .../server/expression/JunctionExpression.java | 18 +++++++----------- .../server/querydefn/DefaultOrmQuery.java | 7 +++++++ .../util/DefaultExpressionList.java | 6 ++++++ .../com/avaje/tests/text/json/TestJsonMap.java | 6 +++--- 6 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/avaje/ebean/ExpressionList.java b/src/main/java/com/avaje/ebean/ExpressionList.java index 3a06bc9ab..dee737d88 100644 --- a/src/main/java/com/avaje/ebean/ExpressionList.java +++ b/src/main/java/com/avaje/ebean/ExpressionList.java @@ -1,5 +1,7 @@ package com.avaje.ebean; +import com.avaje.ebean.text.PathProperties; + import java.io.Serializable; import java.util.Collection; import java.util.List; @@ -87,6 +89,11 @@ public interface ExpressionList extends Serializable { */ public Query setOrderBy(String orderBy); + /** + * Apply the path properties to the query replacing the select and fetch clauses. + */ + public Query apply(PathProperties pathProperties); + /** * Execute the query iterating over the results. * @@ -242,7 +249,8 @@ public interface ExpressionList extends Serializable { * Specify a property (associated bean) to join and fetch including * all its properties. * - * @see Query#join(String) + * @see Query#fetch(String) + * @deprecated */ public Query join(String assocProperties); @@ -250,7 +258,8 @@ public interface ExpressionList extends Serializable { * Specify a property (associated bean) to join and fetch with its * specific properties to include (aka partial object). * - * @see Query#join(String,String) + * @see Query#fetch(String,String) + * @deprecated */ public Query join(String assocProperty, String assocProperties); diff --git a/src/main/java/com/avaje/ebean/Query.java b/src/main/java/com/avaje/ebean/Query.java index ab7a1bfbe..8110ac2cc 100644 --- a/src/main/java/com/avaje/ebean/Query.java +++ b/src/main/java/com/avaje/ebean/Query.java @@ -1,5 +1,7 @@ package com.avaje.ebean; +import com.avaje.ebean.text.PathProperties; + import java.io.Serializable; import java.util.List; import java.util.Map; @@ -413,6 +415,14 @@ public interface Query extends Serializable { */ public Query fetch(String path, FetchConfig joinConfig); + /** + * Apply the path properties replacing the select and fetch clauses. + *

+ * This is typically used when the PathProperties is applied to both the query and the JSON output. + *

+ */ + public Query apply(PathProperties pathProperties); + /** * Execute the query returning the list of Id's. *

diff --git a/src/main/java/com/avaje/ebeaninternal/server/expression/JunctionExpression.java b/src/main/java/com/avaje/ebeaninternal/server/expression/JunctionExpression.java index 87d3818d9..6bb654df3 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/expression/JunctionExpression.java +++ b/src/main/java/com/avaje/ebeaninternal/server/expression/JunctionExpression.java @@ -5,18 +5,9 @@ import java.util.List; import java.util.Map; import java.util.Set; -import com.avaje.ebean.Expression; -import com.avaje.ebean.ExpressionList; -import com.avaje.ebean.FutureIds; -import com.avaje.ebean.FutureList; -import com.avaje.ebean.FutureRowCount; -import com.avaje.ebean.Junction; -import com.avaje.ebean.OrderBy; -import com.avaje.ebean.PagedList; -import com.avaje.ebean.PagingList; -import com.avaje.ebean.QueryIterator; -import com.avaje.ebean.QueryResultVisitor; +import com.avaje.ebean.*; import com.avaje.ebean.event.BeanQueryRequest; +import com.avaje.ebean.text.PathProperties; import com.avaje.ebeaninternal.api.HashQueryPlanBuilder; import com.avaje.ebeaninternal.api.ManyWhereJoins; import com.avaje.ebeaninternal.api.SpiExpression; @@ -206,6 +197,11 @@ abstract class JunctionExpression implements Junction, SpiExpression, Expr throw new RuntimeException("filterMany not allowed on Junction expression list"); } + @Override + public Query apply(PathProperties pathProperties) { + return exprList.apply(pathProperties); + } + public FutureIds findFutureIds() { return exprList.findFutureIds(); } 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 a9d08db0d..607cdc0f5 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/querydefn/DefaultOrmQuery.java +++ b/src/main/java/com/avaje/ebeaninternal/server/querydefn/DefaultOrmQuery.java @@ -31,6 +31,7 @@ import com.avaje.ebean.bean.ObjectGraphNode; import com.avaje.ebean.bean.ObjectGraphOrigin; import com.avaje.ebean.bean.PersistenceContext; import com.avaje.ebean.event.BeanQueryRequest; +import com.avaje.ebean.text.PathProperties; import com.avaje.ebeaninternal.api.BindParams; import com.avaje.ebeaninternal.api.HashQuery; import com.avaje.ebeaninternal.api.HashQueryPlan; @@ -270,6 +271,12 @@ public class DefaultOrmQuery implements SpiQuery { this.totalHits = totalHits; } + @Override + public Query apply(PathProperties pathProperties) { + pathProperties.apply(this); + return this; + } + /** * Set the BeanDescriptor for the root type of this query. */ diff --git a/src/main/java/com/avaje/ebeaninternal/util/DefaultExpressionList.java b/src/main/java/com/avaje/ebeaninternal/util/DefaultExpressionList.java index 267dea69e..467fd9aae 100644 --- a/src/main/java/com/avaje/ebeaninternal/util/DefaultExpressionList.java +++ b/src/main/java/com/avaje/ebeaninternal/util/DefaultExpressionList.java @@ -20,6 +20,7 @@ import com.avaje.ebean.Query; import com.avaje.ebean.QueryIterator; import com.avaje.ebean.QueryResultVisitor; import com.avaje.ebean.event.BeanQueryRequest; +import com.avaje.ebean.text.PathProperties; import com.avaje.ebeaninternal.api.HashQueryPlanBuilder; import com.avaje.ebeaninternal.api.ManyWhereJoins; import com.avaje.ebeaninternal.api.SpiExpression; @@ -137,6 +138,11 @@ public class DefaultExpressionList implements SpiExpressionList { return query.order(orderBy); } + @Override + public Query apply(PathProperties pathProperties) { + return query.apply(pathProperties); + } + public FutureIds findFutureIds() { return query.findFutureIds(); } diff --git a/src/test/java/com/avaje/tests/text/json/TestJsonMap.java b/src/test/java/com/avaje/tests/text/json/TestJsonMap.java index f408fd5c4..55a5ec3c9 100644 --- a/src/test/java/com/avaje/tests/text/json/TestJsonMap.java +++ b/src/test/java/com/avaje/tests/text/json/TestJsonMap.java @@ -69,9 +69,9 @@ public class TestJsonMap extends BaseTestCase { PathProperties pathProperties = PathProperties.parse("(id,status,name,shippingAddress(id,line1,city),billingAddress(*),contacts(*))"); - Query customerQuery = Ebean.find(Customer.class); - pathProperties.apply(customerQuery); - List customers = customerQuery.findList(); + List customers = Ebean.find(Customer.class) + .apply(pathProperties) + .findList(); JsonWriteOptions options = JsonWriteOptions.parsePath("(id,status,name)");