From 96e79248dce01f5c3e1fb24ecc2c79b8acd291b5 Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Thu, 3 Nov 2022 11:16:30 +1300 Subject: [PATCH 01/11] Add StdFunctions, StdExpressions, TQColumn marker interface --- .../io/ebean/typequery/StdExpressions.java | 35 +++++++++++ .../java/io/ebean/typequery/StdFunctions.java | 52 +++++++++++++++ .../java/io/ebean/typequery/TQColumn.java | 7 +++ .../java/io/ebean/typequery/TQProperty.java | 3 +- .../java/io/ebean/typequery/TQRootBean.java | 38 ++++++++--- .../test/java/org/querytest/QOrderTest.java | 63 ++++++++++++++----- 6 files changed, 176 insertions(+), 22 deletions(-) create mode 100644 ebean-querybean/src/main/java/io/ebean/typequery/StdExpressions.java create mode 100644 ebean-querybean/src/main/java/io/ebean/typequery/StdFunctions.java create mode 100644 ebean-querybean/src/main/java/io/ebean/typequery/TQColumn.java diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/StdExpressions.java b/ebean-querybean/src/main/java/io/ebean/typequery/StdExpressions.java new file mode 100644 index 000000000..8a0b5233c --- /dev/null +++ b/ebean-querybean/src/main/java/io/ebean/typequery/StdExpressions.java @@ -0,0 +1,35 @@ +package io.ebean.typequery; + +import io.ebean.Expr; +import io.ebean.Expression; + +public class StdExpressions { + + public static Expression gt(TQColumn property, Object value) { + return Expr.gt(property.toString(), value); + } + + public static Expression like(TQColumn property, String value) { + return Expr.like(property.toString(), value); + } + + public static Expression ilike(TQColumn property, String value) { + return Expr.ilike(property.toString(), value); + } + + public static Expression startsWith(TQColumn property, String value) { + return Expr.startsWith(property.toString(), value); + } + + public static Expression istartsWith(TQColumn property, String value) { + return Expr.istartsWith(property.toString(), value); + } + + public static Expression contains(TQColumn property, String value) { + return Expr.contains(property.toString(), value); + } + + public static Expression icontains(TQColumn property, String value) { + return Expr.icontains(property.toString(), value); + } +} diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/StdFunctions.java b/ebean-querybean/src/main/java/io/ebean/typequery/StdFunctions.java new file mode 100644 index 000000000..d370c967a --- /dev/null +++ b/ebean-querybean/src/main/java/io/ebean/typequery/StdFunctions.java @@ -0,0 +1,52 @@ +package io.ebean.typequery; + +public final class StdFunctions { + + public static TQColumn max(TQColumn property) { + return new Standard("max(" + property + ")"); + } + + public static TQColumn sum(TQColumn property) { + return new Standard("sum(" + property + ")"); + } + + public static TQColumn concat(TQColumn property, Object... values) { + StringBuilder expression = new StringBuilder(50); + expression.append("concat(").append(property.toString()); + for (Object value : values) { + expression.append(",").append(sqlStringExpression(value)); + } + expression.append(")"); + return new Standard(expression.toString()); + } + + public static TQColumn coalesce(TQColumn property, Object value) { + StringBuilder expression = new StringBuilder(50); + expression.append("coalesce(").append(property.toString()).append(","); + expression.append(sqlStringExpression(value)); + expression.append(")"); + return new Standard(expression.toString()); + } + + private static String sqlStringExpression(Object value) { + if (value instanceof TQColumn || value instanceof Number) { + return value.toString(); + } else { + return "'" + value + "'"; + } + } + + private static class Standard implements TQColumn { + + private final String expression; + + private Standard(String expression) { + this.expression = expression; + } + + @Override + public String toString() { + return expression; + } + } +} diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/TQColumn.java b/ebean-querybean/src/main/java/io/ebean/typequery/TQColumn.java new file mode 100644 index 000000000..dfe5fd9d1 --- /dev/null +++ b/ebean-querybean/src/main/java/io/ebean/typequery/TQColumn.java @@ -0,0 +1,7 @@ +package io.ebean.typequery; + +/** + * Marker interface for type safe properties and expressions. + */ +public interface TQColumn { +} diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/TQProperty.java b/ebean-querybean/src/main/java/io/ebean/typequery/TQProperty.java index 364452832..f5d6bfd2e 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/TQProperty.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/TQProperty.java @@ -7,7 +7,7 @@ import io.ebean.ExpressionList; * * @param The type of the owning root bean */ -public class TQProperty { +public class TQProperty implements TQColumn { protected final String _name; @@ -31,6 +31,7 @@ public class TQProperty { this._name = TQPath.add(prefix, name); } + @Override public String toString() { return _name; } diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/TQRootBean.java b/ebean-querybean/src/main/java/io/ebean/typequery/TQRootBean.java index 5680185fb..4a98d0c0e 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/TQRootBean.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/TQRootBean.java @@ -233,8 +233,13 @@ public abstract class TQRootBean { } /** - * Tune the query by specifying the properties to be loaded on the - * 'main' root level entity bean (aka partial object). + * Specify the properties to be loaded on the 'main' root level entity bean. + *

+ * The resulting entities with be "partially loaded" aka partial objects. + *

+ * Alternatively we can use a {@link #select(FetchGroup)} to specify all properties + * to load on all parts of the graph. + * *

{@code
    *
    *   // alias for the customer properties in select()
@@ -245,12 +250,12 @@ public abstract class TQRootBean {
    *
    *   List customers =
    *     new QCustomer()
-   *       // tune query
+   *       // specify the parts of the graph we want to load
    *       .select(cust.id, cust.name)
    *       .contacts.fetch(contact.firstName, contact.lastName, contact.email)
    *
    *       // predicates
-   *       .id.greaterThan(1)
+   *       .id.gt(1)
    *       .findList();
    *
    * }
@@ -263,14 +268,25 @@ public abstract class TQRootBean { return root; } - private Set properties(TQProperty[] properties) { + private Set properties(TQColumn[] properties) { Set props = new LinkedHashSet<>(); - for (TQProperty property : properties) { - props.add(property.propertyName()); + for (TQColumn property : properties) { + props.add(property.toString()); } return props; } + /** + * Specify the properties to be loaded on the 'main' root level entity bean + * also allowing for functions to be used like {@link StdFunctions#max(TQColumn)}. + * + * @param properties the list of properties to fetch + */ + public final R select(TQColumn... properties) { + ((SpiQueryFetch) query).selectProperties(properties(properties)); + return root; + } + /** * Specify a path to load including all its properties. * @@ -499,6 +515,14 @@ public abstract class TQRootBean { return root; } + /** + * Add an expression to the WHERE or HAVING clause. + */ + public R add(Expression expression) { + peekExprList().add(expression); + return root; + } + /** * Set root table alias. */ diff --git a/ebean-querybean/src/test/java/org/querytest/QOrderTest.java b/ebean-querybean/src/test/java/org/querytest/QOrderTest.java index e8563d879..2d9d88e29 100644 --- a/ebean-querybean/src/test/java/org/querytest/QOrderTest.java +++ b/ebean-querybean/src/test/java/org/querytest/QOrderTest.java @@ -4,7 +4,7 @@ import io.ebean.DB; import io.ebean.FetchGroup; import io.ebean.Query; import io.ebean.test.LoggedSql; -import io.ebean.typequery.TQProperty; +import io.ebean.typequery.StdExpressions; import org.example.domain.Customer; import org.example.domain.Order; import org.example.domain.OrderDetail; @@ -18,6 +18,9 @@ import org.junit.jupiter.api.Test; import java.util.List; +import static io.ebean.typequery.StdExpressions.gt; +import static io.ebean.typequery.StdExpressions.ilike; +import static io.ebean.typequery.StdFunctions.*; import static org.assertj.core.api.Assertions.assertThat; public class QOrderTest { @@ -218,14 +221,51 @@ public class QOrderTest { } + @Test + public void stdExpression_iLikeConcatCoalesce() { + QOrder o = QOrder.alias(); + + // LOWER(CONCAT(COALESCE(a.name, ""), ":", a.description)) LIKE LOWER(:param) + Query query = new QOrder() + .select(o.id, o.status) + .add(ilike(concat(coalesce(o.customer.name, "Not-Provided"), ":", o.status), "rob")) + .query(); + + query.findList(); + String generatedSql = query.getGeneratedSql(); + assertThat(generatedSql).contains("where lower(concat(coalesce(t1.name,'Not-Provided'),':',t0.status)) like ? escape''"); + } + + @Test + public void stdExpression_gtCoalesce() { + QOrder o = QOrder.alias(); + + Query query = new QOrder() + .select(o.id, o.status) + .or() + .add(gt(coalesce(o.customer.version, 0), 42)) + .id.lt(12) + .endOr() + .query(); + + query.findList(); + + String sql = query.getGeneratedSql(); + assertThat(sql).contains(" where (coalesce(t1.version,0) > ? or t0.id < ?)"); + assertThat(sql).isEqualTo("select t0.id, t0.status from o_order t0 join be_customer t1 on t1.id = t0.customer_id where (coalesce(t1.version,0) > ? or t0.id < ?)"); + } + @Test void geSubQuery() { + var c = QCustomer.alias(); + var o = QOrder.alias(); + var subQuery = new QOrder() - .select(StdFunctions.max(QOrder.Alias.orderDate)) + .select(max(o.orderDate)) .query(); var query = new QCustomer() - .select(QCustomer.Alias.id) + .select(c.id) .registered.ge(subQuery) .query(); query.findList(); @@ -236,22 +276,22 @@ public class QOrderTest { @Test void gtSubQuery() { var subQuery = new QOrder() - .select("max(orderDate)") + .select(sum(QOrder.Alias.version)) .query(); var query = new QCustomer() .select(QCustomer.Alias.id) - .registered.gt(subQuery) + .version.gt(subQuery) .query(); query.findList(); - assertThat(query.getGeneratedSql()).contains("select t0.id from be_customer t0 where t0.registered > (select max(t0.order_date) from o_order t0)"); + assertThat(query.getGeneratedSql()).contains("select t0.id from be_customer t0 where t0.version > (select sum(t0.version) from o_order t0)"); } @Test void leSubQuery() { var subQuery = new QOrder() - .select("max(orderDate)") + .select(max(QOrder.Alias.orderDate)) .query(); var query = new QCustomer() @@ -266,7 +306,7 @@ public class QOrderTest { @Test void ltSubQuery() { var subQuery = new QOrder() - .select("max(orderDate)") + .select(max(QOrder.Alias.orderDate)) .query(); var query = new QCustomer() @@ -296,7 +336,7 @@ public class QOrderTest { @Test void neSubQuery() { var subQuery = new QOrder() - .select(StdFunctions.max(QOrder.Alias.orderDate)) + .select(max(QOrder.Alias.orderDate)) .query(); var query = new QCustomer() @@ -375,9 +415,4 @@ public class QOrderTest { order.save(); } - static class StdFunctions { - static String max(TQProperty prop) { - return "max(" + prop + ")"; - } - } } From 8565f5f8077c11d7090213e0a192ec10241af7c9 Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Thu, 3 Nov 2022 17:33:11 +1300 Subject: [PATCH 02/11] Rename TQColumn -> Query.Property and move to api + use with UpdateQuery.set() - Refactor rename TQColumn to Query.Property and move to ebean-api module - Add UpdateQuery.set() methods to use Query.Property (as per #2849) --- ebean-api/src/main/java/io/ebean/Query.java | 7 +++ .../src/main/java/io/ebean/UpdateQuery.java | 48 +++++++++++++++++-- .../server/querydefn/DefaultUpdateQuery.java | 11 +++++ .../io/ebean/typequery/StdExpressions.java | 15 +++--- .../java/io/ebean/typequery/StdFunctions.java | 14 +++--- .../java/io/ebean/typequery/TQColumn.java | 7 --- .../java/io/ebean/typequery/TQProperty.java | 3 +- .../java/io/ebean/typequery/TQRootBean.java | 8 ++-- .../test/java/org/querytest/QOrderTest.java | 17 ++++++- 9 files changed, 101 insertions(+), 29 deletions(-) delete mode 100644 ebean-querybean/src/main/java/io/ebean/typequery/TQColumn.java diff --git a/ebean-api/src/main/java/io/ebean/Query.java b/ebean-api/src/main/java/io/ebean/Query.java index 0d0fe1e5e..495da5977 100644 --- a/ebean-api/src/main/java/io/ebean/Query.java +++ b/ebean-api/src/main/java/io/ebean/Query.java @@ -1768,4 +1768,11 @@ public interface Query extends CancelableQuery { */ Query orderById(boolean orderById); + /** + * Type safe query bean properties and expressions (marker interface). + *

+ * Implemented by query bean properties and expressions based on those properties. + */ + interface Property { + } } diff --git a/ebean-api/src/main/java/io/ebean/UpdateQuery.java b/ebean-api/src/main/java/io/ebean/UpdateQuery.java index 1860fffe9..a7abe00ea 100644 --- a/ebean-api/src/main/java/io/ebean/UpdateQuery.java +++ b/ebean-api/src/main/java/io/ebean/UpdateQuery.java @@ -14,7 +14,7 @@ package io.ebean; * * int rows = DB.update(Customer.class) * .set("status", Customer.Status.ACTIVE) - * .set("updtime", new Timestamp(System.currentTimeMillis())) + * .set("whenUpdated", Instant.now()) * .where() * .gt("id", 1000) * .update(); @@ -25,6 +25,20 @@ package io.ebean; * update o_customer set status=?, updtime=? where id > ? * * } + * + *

Example: Using query bean

+ *
{@code
+ *
+ *   var cust = QCustomer.alias();
+ *
+ *   int rows = new QCustomer()
+ *       .id.gt(1000)
+ *       .asUpdate()
+ *       .set(cust.status, Customer.Status.COMPLETE)
+ *       .set(cust.whenUpdated, Instant.now())
+ *       .update();
+ *
+ * }
*

* Note that if the where() clause contains a join then the SQL update changes to use a * WHERE ID IN () form. @@ -40,7 +54,7 @@ package io.ebean; * * int rows = DB.update(Customer.class) * .set("status", Customer.Status.ACTIVE) - * .set("updtime", new Timestamp(System.currentTimeMillis())) + * .set("whenUpdated", Instant.now()) * .where() * .eq("status", Customer.Status.NEW) * .eq("billingAddress.country", nz) @@ -73,7 +87,7 @@ public interface UpdateQuery { * * int rows = DB.update(Customer.class) * .set("status", Customer.Status.ACTIVE) - * .set("updtime", new Timestamp(System.currentTimeMillis())) + * .set("whenUpdated", Instant.now()) * .where() * .gt("id", 1000) * .update(); @@ -85,6 +99,27 @@ public interface UpdateQuery { */ UpdateQuery set(String property, Object value); + /** + * Set the value of a property. + *

+ *

{@code
+   *
+   *   var cust = QCustomer.alias();
+   *
+   *   int rows = new QCustomer()
+   *       .id.gt(1000)
+   *       .asUpdate()
+   *       .set(cust.status, Customer.Status.COMPLETE)
+   *       .set(cust.whenUpdated, Instant.now())
+   *       .update();
+   *
+   * }
+ * + * @param property The bean property to be set + * @param value The value to set the property to + */ + UpdateQuery set(Query.Property property, Object value); + /** * Set the property to be null. *

@@ -102,6 +137,13 @@ public interface UpdateQuery { */ UpdateQuery setNull(String property); + /** + * Set the property to be null. + * + * @param property The bean property to be set + */ + UpdateQuery setNull(Query.Property property); + /** * Set using a property expression that does not need any bind values. *

diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/querydefn/DefaultUpdateQuery.java b/ebean-core/src/main/java/io/ebeaninternal/server/querydefn/DefaultUpdateQuery.java index 494f4227a..9b67c22dd 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/querydefn/DefaultUpdateQuery.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/querydefn/DefaultUpdateQuery.java @@ -2,6 +2,7 @@ package io.ebeaninternal.server.querydefn; import io.ebean.ExpressionList; import io.ebean.ProfileLocation; +import io.ebean.Query; import io.ebean.UpdateQuery; import io.ebean.core.type.ScalarType; import io.ebeaninternal.server.deploy.BeanDescriptor; @@ -34,6 +35,16 @@ public final class DefaultUpdateQuery implements UpdateQuery { return this; } + @Override + public UpdateQuery set(Query.Property property, Object value) { + return set(property.toString(), value); + } + + @Override + public UpdateQuery setNull(Query.Property property) { + return setNull(property.toString()); + } + @Override public UpdateQuery setNull(String property) { values.setNull(property); diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/StdExpressions.java b/ebean-querybean/src/main/java/io/ebean/typequery/StdExpressions.java index 8a0b5233c..13498f41b 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/StdExpressions.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/StdExpressions.java @@ -2,34 +2,35 @@ package io.ebean.typequery; import io.ebean.Expr; import io.ebean.Expression; +import io.ebean.Query; public class StdExpressions { - public static Expression gt(TQColumn property, Object value) { + public static Expression gt(Query.Property property, Object value) { return Expr.gt(property.toString(), value); } - public static Expression like(TQColumn property, String value) { + public static Expression like(Query.Property property, String value) { return Expr.like(property.toString(), value); } - public static Expression ilike(TQColumn property, String value) { + public static Expression ilike(Query.Property property, String value) { return Expr.ilike(property.toString(), value); } - public static Expression startsWith(TQColumn property, String value) { + public static Expression startsWith(Query.Property property, String value) { return Expr.startsWith(property.toString(), value); } - public static Expression istartsWith(TQColumn property, String value) { + public static Expression istartsWith(Query.Property property, String value) { return Expr.istartsWith(property.toString(), value); } - public static Expression contains(TQColumn property, String value) { + public static Expression contains(Query.Property property, String value) { return Expr.contains(property.toString(), value); } - public static Expression icontains(TQColumn property, String value) { + public static Expression icontains(Query.Property property, String value) { return Expr.icontains(property.toString(), value); } } diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/StdFunctions.java b/ebean-querybean/src/main/java/io/ebean/typequery/StdFunctions.java index d370c967a..b9c295a22 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/StdFunctions.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/StdFunctions.java @@ -1,16 +1,18 @@ package io.ebean.typequery; +import io.ebean.Query.Property; + public final class StdFunctions { - public static TQColumn max(TQColumn property) { + public static Property max(Property property) { return new Standard("max(" + property + ")"); } - public static TQColumn sum(TQColumn property) { + public static Property sum(Property property) { return new Standard("sum(" + property + ")"); } - public static TQColumn concat(TQColumn property, Object... values) { + public static Property concat(Property property, Object... values) { StringBuilder expression = new StringBuilder(50); expression.append("concat(").append(property.toString()); for (Object value : values) { @@ -20,7 +22,7 @@ public final class StdFunctions { return new Standard(expression.toString()); } - public static TQColumn coalesce(TQColumn property, Object value) { + public static Property coalesce(Property property, Object value) { StringBuilder expression = new StringBuilder(50); expression.append("coalesce(").append(property.toString()).append(","); expression.append(sqlStringExpression(value)); @@ -29,14 +31,14 @@ public final class StdFunctions { } private static String sqlStringExpression(Object value) { - if (value instanceof TQColumn || value instanceof Number) { + if (value instanceof Property || value instanceof Number) { return value.toString(); } else { return "'" + value + "'"; } } - private static class Standard implements TQColumn { + private static class Standard implements Property { private final String expression; diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/TQColumn.java b/ebean-querybean/src/main/java/io/ebean/typequery/TQColumn.java deleted file mode 100644 index dfe5fd9d1..000000000 --- a/ebean-querybean/src/main/java/io/ebean/typequery/TQColumn.java +++ /dev/null @@ -1,7 +0,0 @@ -package io.ebean.typequery; - -/** - * Marker interface for type safe properties and expressions. - */ -public interface TQColumn { -} diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/TQProperty.java b/ebean-querybean/src/main/java/io/ebean/typequery/TQProperty.java index f5d6bfd2e..8cc0e6465 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/TQProperty.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/TQProperty.java @@ -1,13 +1,14 @@ package io.ebean.typequery; import io.ebean.ExpressionList; +import io.ebean.Query; /** * A property used in type query. * * @param The type of the owning root bean */ -public class TQProperty implements TQColumn { +public class TQProperty implements Query.Property { protected final String _name; diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/TQRootBean.java b/ebean-querybean/src/main/java/io/ebean/typequery/TQRootBean.java index 4a98d0c0e..e9dc8d590 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/TQRootBean.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/TQRootBean.java @@ -268,9 +268,9 @@ public abstract class TQRootBean { return root; } - private Set properties(TQColumn[] properties) { + private Set properties(Query.Property[] properties) { Set props = new LinkedHashSet<>(); - for (TQColumn property : properties) { + for (Query.Property property : properties) { props.add(property.toString()); } return props; @@ -278,11 +278,11 @@ public abstract class TQRootBean { /** * Specify the properties to be loaded on the 'main' root level entity bean - * also allowing for functions to be used like {@link StdFunctions#max(TQColumn)}. + * also allowing for functions to be used like {@link StdFunctions#max(Query.Property)}. * * @param properties the list of properties to fetch */ - public final R select(TQColumn... properties) { + public final R select(Query.Property... properties) { ((SpiQueryFetch) query).selectProperties(properties(properties)); return root; } diff --git a/ebean-querybean/src/test/java/org/querytest/QOrderTest.java b/ebean-querybean/src/test/java/org/querytest/QOrderTest.java index 2d9d88e29..24eda0518 100644 --- a/ebean-querybean/src/test/java/org/querytest/QOrderTest.java +++ b/ebean-querybean/src/test/java/org/querytest/QOrderTest.java @@ -4,7 +4,6 @@ import io.ebean.DB; import io.ebean.FetchGroup; import io.ebean.Query; import io.ebean.test.LoggedSql; -import io.ebean.typequery.StdExpressions; import org.example.domain.Customer; import org.example.domain.Order; import org.example.domain.OrderDetail; @@ -221,6 +220,22 @@ public class QOrderTest { } + @Test + public void updateQuery() { + LoggedSql.start(); + new QOrder() + .status.eq(Order.Status.COMPLETE) + .orderDate.gt(new java.sql.Date(System.currentTimeMillis())) + .asUpdate() + .set(QOrder.Alias.version, 42L) + .setNull(QOrder.Alias.orderDate) + .update(); + + List sql = LoggedSql.stop(); + assertThat(sql).hasSize(1); + assertThat(sql.get(0)).contains("update o_order set version=?, order_date=null where status = ? and order_date > ?"); + } + @Test public void stdExpression_iLikeConcatCoalesce() { QOrder o = QOrder.alias(); From 5ad313d290eaf0cc093e362b2c7430aa7305b772 Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Thu, 3 Nov 2022 17:46:30 +1300 Subject: [PATCH 03/11] Rename TQColumn -> Query.Property, fix compile for DefaultOrmQuery use of OrderBy.Property --- .../server/querydefn/DefaultOrmQuery.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/querydefn/DefaultOrmQuery.java b/ebean-core/src/main/java/io/ebeaninternal/server/querydefn/DefaultOrmQuery.java index 5c5ff7f14..a8d93df7d 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/querydefn/DefaultOrmQuery.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/querydefn/DefaultOrmQuery.java @@ -2,7 +2,6 @@ package io.ebeaninternal.server.querydefn; import io.avaje.lang.NonNullApi; import io.ebean.*; -import io.ebean.OrderBy.Property; import io.ebean.bean.CallOrigin; import io.ebean.bean.ObjectGraphNode; import io.ebean.bean.ObjectGraphOrigin; @@ -420,7 +419,7 @@ public class DefaultOrmQuery extends AbstractQuery implements SpiQuery { havingExpressions.containsMany(beanDescriptor, manyWhere); } if (orderBy != null) { - for (Property orderProperty : orderBy.getProperties()) { + for (OrderBy.Property orderProperty : orderBy.getProperties()) { ElPropertyDeploy elProp = beanDescriptor.elPropertyDeploy(orderProperty.getProperty()); if (elProp != null && elProp.containsFormulaWithJoin()) { manyWhere.addFormulaWithJoin(elProp.elPrefix(), elProp.name()); @@ -460,10 +459,10 @@ public class DefaultOrmQuery extends AbstractQuery implements SpiQuery { for (OrmQueryProperties joinPath : queryJoins) { // loop through the orderBy properties and // move any ones related to the query join - List properties = orderBy.getProperties(); - Iterator it = properties.iterator(); + List properties = orderBy.getProperties(); + Iterator it = properties.iterator(); while (it.hasNext()) { - Property property = it.next(); + OrderBy.Property property = it.next(); if (property.getProperty().startsWith(joinPath.getPath())) { // remove this orderBy segment and // add it to the secondary join @@ -1970,7 +1969,7 @@ public class DefaultOrmQuery extends AbstractQuery implements SpiQuery { havingExpressions.validate(validation); } if (orderBy != null) { - for (Property property : orderBy.getProperties()) { + for (OrderBy.Property property : orderBy.getProperties()) { validation.validate(property.getProperty()); } } From 3da924b943cc411611c80b2fa4573190f3c7b6d0 Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Mon, 7 Nov 2022 12:57:39 +1300 Subject: [PATCH 04/11] Add Query.Property.of(), Move StdFunctions to ebean-api --- ebean-api/src/main/java/io/ebean/Query.java | 16 +++ .../main/java/io/ebean/SimpleProperty.java | 15 +++ .../src/main/java/io/ebean/StdFunctions.java | 100 ++++++++++++++++++ .../test/java/io/ebean/StdFunctionsTest.java | 68 ++++++++++++ .../io/ebean/typequery/StdExpressions.java | 36 ------- .../java/io/ebean/typequery/StdFunctions.java | 54 ---------- .../test/java/org/querytest/QOrderTest.java | 6 +- .../org/tests/query/TestStdFunctions.java | 44 ++++++++ 8 files changed, 246 insertions(+), 93 deletions(-) create mode 100644 ebean-api/src/main/java/io/ebean/SimpleProperty.java create mode 100644 ebean-api/src/main/java/io/ebean/StdFunctions.java create mode 100644 ebean-api/src/test/java/io/ebean/StdFunctionsTest.java delete mode 100644 ebean-querybean/src/main/java/io/ebean/typequery/StdExpressions.java delete mode 100644 ebean-querybean/src/main/java/io/ebean/typequery/StdFunctions.java create mode 100644 ebean-test/src/test/java/org/tests/query/TestStdFunctions.java diff --git a/ebean-api/src/main/java/io/ebean/Query.java b/ebean-api/src/main/java/io/ebean/Query.java index 495da5977..0a9723d24 100644 --- a/ebean-api/src/main/java/io/ebean/Query.java +++ b/ebean-api/src/main/java/io/ebean/Query.java @@ -1774,5 +1774,21 @@ public interface Query extends CancelableQuery { * Implemented by query bean properties and expressions based on those properties. */ interface Property { + + /** + * Return a property given the expression. + */ + static Property of(String expression) { + return new SimpleProperty(expression); + } + + /** + * Return the property in string expression form. + *

+ * This is a path to a database column (like "name" or "billingAddress.city") or a function + * wrapping a path (like lower(name), concat(name, '-', billingAddress.city) + */ + @Override + String toString(); } } diff --git a/ebean-api/src/main/java/io/ebean/SimpleProperty.java b/ebean-api/src/main/java/io/ebean/SimpleProperty.java new file mode 100644 index 000000000..274978246 --- /dev/null +++ b/ebean-api/src/main/java/io/ebean/SimpleProperty.java @@ -0,0 +1,15 @@ +package io.ebean; + +final class SimpleProperty implements Query.Property { + + private final String expression; + + SimpleProperty(String expression) { + this.expression = expression; + } + + @Override + public String toString() { + return expression; + } +} diff --git a/ebean-api/src/main/java/io/ebean/StdFunctions.java b/ebean-api/src/main/java/io/ebean/StdFunctions.java new file mode 100644 index 000000000..35d9140c5 --- /dev/null +++ b/ebean-api/src/main/java/io/ebean/StdFunctions.java @@ -0,0 +1,100 @@ +package io.ebean; + +import io.ebean.Query.Property; + +public final class StdFunctions { + + public static Property avg(Property property) { + return Property.of("avg(" + property + ")"); + } + + public static Property count(Property property) { + return Property.of("count(" + property + ")"); + } + + public static Property max(Property property) { + return Property.of("max(" + property + ")"); + } + + public static Property min(Property property) { + return Property.of("min(" + property + ")"); + } + + public static Property sum(Property property) { + return Property.of("sum(" + property + ")"); + } + + public static Property lower(Property property) { + return Property.of("lower(" + property + ")"); + } + + public static Property upper(Property property) { + return Property.of("upper(" + property + ")"); + } + + public static Property concat(Property property, Object... values) { + StringBuilder expression = new StringBuilder(50); + expression.append("concat(").append(property.toString()); + for (Object value : values) { + expression.append(",").append(sqlConcatString(value)); + } + expression.append(")"); + return Property.of(expression.toString()); + } + + public static Property coalesce(Property property, Object value) { + return Property.of("coalesce(" + property.toString() + "," + sqlValue(value) + ")"); + } + + private static String sqlConcatString(Object value) { + String asStr = String.valueOf(value); + return (value instanceof Property || isSqlQuoted(asStr)) ? asStr : "'" + value + "'"; + } + + private static boolean isSqlQuoted(String asStr) { + return asStr.length() > 0 && asStr.charAt(0) == '\''; + } + + private static String sqlValue(Object value) { + if (value instanceof Property || value instanceof Number) { + return value.toString(); + } else { + return "'" + value + "'"; + } + } + + // -------------------------------------------------------------------------------------------- // + // ---- Expressions --------------------------------------------------------------------------- // + + public static Expression eq(Property property, Object value) { + return Expr.eq(property.toString(), value); + } + + public static Expression gt(Property property, Object value) { + return Expr.gt(property.toString(), value); + } + + public static Expression like(Property property, String value) { + return Expr.like(property.toString(), value); + } + + public static Expression ilike(Property property, String value) { + return Expr.ilike(property.toString(), value); + } + + public static Expression startsWith(Property property, String value) { + return Expr.startsWith(property.toString(), value); + } + + public static Expression istartsWith(Property property, String value) { + return Expr.istartsWith(property.toString(), value); + } + + public static Expression contains(Property property, String value) { + return Expr.contains(property.toString(), value); + } + + public static Expression icontains(Property property, String value) { + return Expr.icontains(property.toString(), value); + } +} diff --git a/ebean-api/src/test/java/io/ebean/StdFunctionsTest.java b/ebean-api/src/test/java/io/ebean/StdFunctionsTest.java new file mode 100644 index 000000000..e22344c43 --- /dev/null +++ b/ebean-api/src/test/java/io/ebean/StdFunctionsTest.java @@ -0,0 +1,68 @@ +package io.ebean; + +import org.junit.jupiter.api.Test; + +import java.time.LocalDate; + +import static io.ebean.StdFunctions.*; +import static org.assertj.core.api.Assertions.assertThat; + +class StdFunctionsTest { + + final Query.Property foo = Query.Property.of("foo"); + final Query.Property bar = Query.Property.of("bar"); + + @Test + void testAvg() { + assertThat(avg(foo).toString()).isEqualTo("avg(foo)"); + } + + @Test + void testCount() { + assertThat(count(foo).toString()).isEqualTo("count(foo)"); + } + + @Test + void testMax() { + assertThat(max(foo).toString()).isEqualTo("max(foo)"); + } + + @Test + void testMin() { + assertThat(min(foo).toString()).isEqualTo("min(foo)"); + } + + @Test + void testSum() { + assertThat(sum(foo).toString()).isEqualTo("sum(foo)"); + } + + @Test + void testLower() { + assertThat(lower(foo).toString()).isEqualTo("lower(foo)"); + } + + @Test + void testUpper() { + assertThat(upper(foo).toString()).isEqualTo("upper(foo)"); + } + + @Test + void testConcat() { + assertThat(concat(foo, "+").toString()).isEqualTo("concat(foo,'+')"); + assertThat(concat(foo, 42).toString()).isEqualTo("concat(foo,'42')"); + assertThat(concat(foo, LocalDate.of(2022, 9, 1)).toString()).isEqualTo("concat(foo,'2022-09-01')"); + assertThat(concat(foo, bar).toString()).isEqualTo("concat(foo,bar)"); + assertThat(concat(foo, ":", 42, bar).toString()).isEqualTo("concat(foo,':','42',bar)"); + } + + @Test + void testCoalesce() { + assertThat(coalesce(foo, bar).toString()).isEqualTo("coalesce(foo,bar)"); + assertThat(coalesce(foo, 42).toString()).isEqualTo("coalesce(foo,42)"); + assertThat(coalesce(foo, 0).toString()).isEqualTo("coalesce(foo,0)"); + assertThat(coalesce(foo, "apple").toString()).isEqualTo("coalesce(foo,'apple')"); + assertThat(coalesce(foo, "banana").toString()).isEqualTo("coalesce(foo,'banana')"); + } + +} diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/StdExpressions.java b/ebean-querybean/src/main/java/io/ebean/typequery/StdExpressions.java deleted file mode 100644 index 13498f41b..000000000 --- a/ebean-querybean/src/main/java/io/ebean/typequery/StdExpressions.java +++ /dev/null @@ -1,36 +0,0 @@ -package io.ebean.typequery; - -import io.ebean.Expr; -import io.ebean.Expression; -import io.ebean.Query; - -public class StdExpressions { - - public static Expression gt(Query.Property property, Object value) { - return Expr.gt(property.toString(), value); - } - - public static Expression like(Query.Property property, String value) { - return Expr.like(property.toString(), value); - } - - public static Expression ilike(Query.Property property, String value) { - return Expr.ilike(property.toString(), value); - } - - public static Expression startsWith(Query.Property property, String value) { - return Expr.startsWith(property.toString(), value); - } - - public static Expression istartsWith(Query.Property property, String value) { - return Expr.istartsWith(property.toString(), value); - } - - public static Expression contains(Query.Property property, String value) { - return Expr.contains(property.toString(), value); - } - - public static Expression icontains(Query.Property property, String value) { - return Expr.icontains(property.toString(), value); - } -} diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/StdFunctions.java b/ebean-querybean/src/main/java/io/ebean/typequery/StdFunctions.java deleted file mode 100644 index b9c295a22..000000000 --- a/ebean-querybean/src/main/java/io/ebean/typequery/StdFunctions.java +++ /dev/null @@ -1,54 +0,0 @@ -package io.ebean.typequery; - -import io.ebean.Query.Property; - -public final class StdFunctions { - - public static Property max(Property property) { - return new Standard("max(" + property + ")"); - } - - public static Property sum(Property property) { - return new Standard("sum(" + property + ")"); - } - - public static Property concat(Property property, Object... values) { - StringBuilder expression = new StringBuilder(50); - expression.append("concat(").append(property.toString()); - for (Object value : values) { - expression.append(",").append(sqlStringExpression(value)); - } - expression.append(")"); - return new Standard(expression.toString()); - } - - public static Property coalesce(Property property, Object value) { - StringBuilder expression = new StringBuilder(50); - expression.append("coalesce(").append(property.toString()).append(","); - expression.append(sqlStringExpression(value)); - expression.append(")"); - return new Standard(expression.toString()); - } - - private static String sqlStringExpression(Object value) { - if (value instanceof Property || value instanceof Number) { - return value.toString(); - } else { - return "'" + value + "'"; - } - } - - private static class Standard implements Property { - - private final String expression; - - private Standard(String expression) { - this.expression = expression; - } - - @Override - public String toString() { - return expression; - } - } -} diff --git a/ebean-querybean/src/test/java/org/querytest/QOrderTest.java b/ebean-querybean/src/test/java/org/querytest/QOrderTest.java index 24eda0518..fa17ca5a5 100644 --- a/ebean-querybean/src/test/java/org/querytest/QOrderTest.java +++ b/ebean-querybean/src/test/java/org/querytest/QOrderTest.java @@ -17,9 +17,9 @@ import org.junit.jupiter.api.Test; import java.util.List; -import static io.ebean.typequery.StdExpressions.gt; -import static io.ebean.typequery.StdExpressions.ilike; -import static io.ebean.typequery.StdFunctions.*; +import static io.ebean.StdFunctions.gt; +import static io.ebean.StdFunctions.ilike; +import static io.ebean.StdFunctions.*; import static org.assertj.core.api.Assertions.assertThat; public class QOrderTest { diff --git a/ebean-test/src/test/java/org/tests/query/TestStdFunctions.java b/ebean-test/src/test/java/org/tests/query/TestStdFunctions.java new file mode 100644 index 000000000..27ff61760 --- /dev/null +++ b/ebean-test/src/test/java/org/tests/query/TestStdFunctions.java @@ -0,0 +1,44 @@ +package org.tests.query; + +import io.ebean.DB; +import io.ebean.Query; +import org.junit.jupiter.api.Test; +import org.tests.model.basic.Customer; + +import static io.ebean.StdFunctions.*; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Testing the expressions in StdFunctions (but without query beans so using Query.Property.of). + */ +public class TestStdFunctions { + + @Test + void coalesceLike() { + var name = Query.Property.of("name"); + + var query = DB.find(Customer.class) + .select(coalesce(name, "na").toString()) + .where() + .add(like(coalesce(name, "na"), "foo%")) + .query(); + + query.findSingleAttributeList(); + assertThat(query.getGeneratedSql()).contains("select coalesce(t0.name,'na') from o_customer t0 where coalesce(t0.name,'na') like ?"); + } + + @Test + void concatEq() { + var name = Query.Property.of("name"); + var status = Query.Property.of("status"); + + var query = DB.find(Customer.class) + .select(concat(name, "na", status).toString()) + .where() + .add(eq(concat(name, "na", status), "foo")) + .query(); + + query.findSingleAttributeList(); + assertThat(query.getGeneratedSql()).contains("select concat(t0.name,'na',t0.status) from o_customer t0 where concat(t0.name,'na',t0.status) = ?"); + } +} From 0b9d199829d8dc5107d5308725cadab17bcf363a Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Mon, 7 Nov 2022 17:36:04 +1300 Subject: [PATCH 05/11] #2774 - Postgres ANY / MultiValueSupport with byte[]/binary data Add Postgres specific support for binding an array of byte[] with ANY(?) which is used as with client side encryption where the bind values are implicitly converted to byte[]. Fixes Postgres specific failure running TestEncryptClientSide.java --- .../server/expression/InExpression.java | 8 ++----- .../platform/AbstractMultiValueBind.java | 22 +++---------------- .../persist/platform/MultiValueBind.java | 8 +++++++ 3 files changed, 13 insertions(+), 25 deletions(-) diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/expression/InExpression.java b/ebean-core/src/main/java/io/ebeaninternal/server/expression/InExpression.java index 7ec81687b..d0d89b9ff 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/expression/InExpression.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/expression/InExpression.java @@ -95,7 +95,7 @@ public final class InExpression extends AbstractExpression implements IdInCommon public void prepareExpression(BeanQueryRequest request) { initBindValues(); if (bindValues.size() > 0) { - multiValueSupported = request.isMultiValueSupported((bindValues.get(0)).getClass()); + multiValueSupported = request.isMultiValueSupported(bindValues.get(0).getClass()); } } @@ -149,7 +149,6 @@ public final class InExpression extends AbstractExpression implements IdInCommon // if we have no property, we wrap them in a multi value wrapper. // later the binder will decide, which bind strategy to use. request.addBindValue(new MultiValueWrapper(values)); - } @Override @@ -164,7 +163,6 @@ public final class InExpression extends AbstractExpression implements IdInCommon } ElPropertyValue prop = getElProp(request); - if (prop != null) { if (prop.isAssocId()) { request.append(prop.assocIdInExpr(propName)); @@ -172,15 +170,13 @@ public final class InExpression extends AbstractExpression implements IdInCommon return; } if (prop.isDbEncrypted()) { - String dsql = prop.beanProperty().decryptProperty(propName); - request.append(dsql); + request.append(prop.beanProperty().decryptProperty(propName)); request.appendInExpression(not, bindValues); return; } } request.append(propName); request.appendInExpression(not, bindValues); - } /** diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/persist/platform/AbstractMultiValueBind.java b/ebean-core/src/main/java/io/ebeaninternal/server/persist/platform/AbstractMultiValueBind.java index 18d41b757..5dd334ac5 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/persist/platform/AbstractMultiValueBind.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/persist/platform/AbstractMultiValueBind.java @@ -7,25 +7,7 @@ import io.ebeaninternal.server.bind.DataBind; import java.sql.SQLException; import java.util.Collection; -import static java.sql.Types.BIGINT; -import static java.sql.Types.BIT; -import static java.sql.Types.BOOLEAN; -import static java.sql.Types.CHAR; -import static java.sql.Types.DATE; -import static java.sql.Types.DECIMAL; -import static java.sql.Types.DOUBLE; -import static java.sql.Types.FLOAT; -import static java.sql.Types.INTEGER; -import static java.sql.Types.NCHAR; -import static java.sql.Types.NUMERIC; -import static java.sql.Types.NVARCHAR; -import static java.sql.Types.REAL; -import static java.sql.Types.SMALLINT; -import static java.sql.Types.TIMESTAMP; -import static java.sql.Types.TIMESTAMP_WITH_TIMEZONE; -import static java.sql.Types.TIME_WITH_TIMEZONE; -import static java.sql.Types.TINYINT; -import static java.sql.Types.VARCHAR; +import static java.sql.Types.*; /** * Base MultiValueBind for platform specific support. @@ -86,6 +68,8 @@ abstract class AbstractMultiValueBind extends MultiValueBind { case ExtraDbTypes.UUID: // Postgres cast to uuid[] case ExtraDbTypes.INET: // Postgres cast to inet[] return "varchar"; + case VARBINARY: + return "bytea"; default: return null; diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/persist/platform/MultiValueBind.java b/ebean-core/src/main/java/io/ebeaninternal/server/persist/platform/MultiValueBind.java index db47defcf..aca6ea2b1 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/persist/platform/MultiValueBind.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/persist/platform/MultiValueBind.java @@ -17,6 +17,14 @@ public class MultiValueBind { } Object[] toArray(Collection values, ScalarType type) { + if (type.binary()) { + byte[][] array = new byte[values.size()][]; + int i = 0; + for (Object value : values) { + array[i++] = (byte[])type.toJdbcType(value); + } + return array; + } Object[] array = new Object[values.size()]; int i = 0; for (Object value : values) { From 97bbce321db12109ad54acc5beb7fbce62783c0b Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Tue, 8 Nov 2022 21:57:36 +1300 Subject: [PATCH 06/11] Make Query.Property parameterised with "Base Type" as Number, String, Temporal, Boolean or Object These types then determine which functions are valid for the types. So: - Number functions - String/varchar functions - Temporal Date, DateTime, Time functions - Boolean functions - Object being everything else --- ebean-api/src/main/java/io/ebean/Query.java | 10 +- .../main/java/io/ebean/SimpleProperty.java | 2 +- .../src/main/java/io/ebean/StdFunctions.java | 92 ++++++---- .../src/main/java/io/ebean/UpdateQuery.java | 4 +- .../test/java/io/ebean/StdFunctionsTest.java | 7 +- .../main/java/io/ebean/typequery/PArray.java | 2 +- .../io/ebean/typequery/PBaseCompareable.java | 23 +-- .../java/io/ebean/typequery/PBaseDate.java | 4 +- .../java/io/ebean/typequery/PBaseNumber.java | 4 +- .../java/io/ebean/typequery/PBaseString.java | 159 +++++++++++++++++- .../java/io/ebean/typequery/PBaseTime.java | 52 ++++++ .../io/ebean/typequery/PBaseValueEqual.java | 6 +- .../java/io/ebean/typequery/PBoolean.java | 2 +- .../java/io/ebean/typequery/PByteArray.java | 3 +- .../main/java/io/ebean/typequery/PCidr.java | 2 +- .../java/io/ebean/typequery/PCurrency.java | 2 +- .../main/java/io/ebean/typequery/PEnum.java | 4 +- .../main/java/io/ebean/typequery/PFile.java | 2 +- .../main/java/io/ebean/typequery/PInet.java | 2 +- .../java/io/ebean/typequery/PInetAddress.java | 3 +- .../io/ebean/typequery/PJodaLocalTime.java | 2 +- .../main/java/io/ebean/typequery/PJson.java | 2 +- .../java/io/ebean/typequery/PLocalTime.java | 2 +- .../io/ebean/typequery/POffsetDateTime.java | 2 +- .../java/io/ebean/typequery/POffsetTime.java | 2 +- .../main/java/io/ebean/typequery/PScalar.java | 5 +- .../io/ebean/typequery/PScalarComparable.java | 2 +- .../main/java/io/ebean/typequery/PString.java | 2 +- .../main/java/io/ebean/typequery/PTime.java | 2 +- .../java/io/ebean/typequery/PTimeZone.java | 2 +- .../main/java/io/ebean/typequery/PUuid.java | 4 +- .../main/java/io/ebean/typequery/PZoneId.java | 2 +- .../java/io/ebean/typequery/PZoneOffset.java | 2 +- .../io/ebean/typequery/PZonedDateTime.java | 23 +-- .../java/io/ebean/typequery/TQAssocBean.java | 18 +- .../java/io/ebean/typequery/TQProperty.java | 8 +- .../io/ebean/typequery/TQPropertyBase.java | 5 +- .../java/io/ebean/typequery/TQRootBean.java | 6 +- .../test/java/org/querytest/QOrderTest.java | 2 +- .../org/tests/query/TestStdFunctions.java | 3 +- kotlin-querybean-generator/pom.xml | 2 +- .../generator/KotlinLangAdapter.java | 13 +- .../generator/SimpleQueryBeanWriter.java | 2 +- 43 files changed, 353 insertions(+), 145 deletions(-) create mode 100644 ebean-querybean/src/main/java/io/ebean/typequery/PBaseTime.java diff --git a/ebean-api/src/main/java/io/ebean/Query.java b/ebean-api/src/main/java/io/ebean/Query.java index 0a9723d24..ad15fc047 100644 --- a/ebean-api/src/main/java/io/ebean/Query.java +++ b/ebean-api/src/main/java/io/ebean/Query.java @@ -1772,14 +1772,18 @@ public interface Query extends CancelableQuery { * Type safe query bean properties and expressions (marker interface). *

* Implemented by query bean properties and expressions based on those properties. + *

+ * The base type determines which {@link StdFunctions} can be used on the property. + * + * @param The base type of the property Number, String, Temporal, Boolean or Object. */ - interface Property { + interface Property { /** * Return a property given the expression. */ - static Property of(String expression) { - return new SimpleProperty(expression); + static Property of(String expression) { + return new SimpleProperty<>(expression); } /** diff --git a/ebean-api/src/main/java/io/ebean/SimpleProperty.java b/ebean-api/src/main/java/io/ebean/SimpleProperty.java index 274978246..044f2aa31 100644 --- a/ebean-api/src/main/java/io/ebean/SimpleProperty.java +++ b/ebean-api/src/main/java/io/ebean/SimpleProperty.java @@ -1,6 +1,6 @@ package io.ebean; -final class SimpleProperty implements Query.Property { +final class SimpleProperty implements Query.Property { private final String expression; diff --git a/ebean-api/src/main/java/io/ebean/StdFunctions.java b/ebean-api/src/main/java/io/ebean/StdFunctions.java index 35d9140c5..6a36c58d8 100644 --- a/ebean-api/src/main/java/io/ebean/StdFunctions.java +++ b/ebean-api/src/main/java/io/ebean/StdFunctions.java @@ -2,37 +2,43 @@ package io.ebean; import io.ebean.Query.Property; +import java.time.temporal.Temporal; + public final class StdFunctions { - public static Property avg(Property property) { - return Property.of("avg(" + property + ")"); - } - - public static Property count(Property property) { + public static Property count(Property property) { return Property.of("count(" + property + ")"); } - public static Property max(Property property) { - return Property.of("max(" + property + ")"); - } - - public static Property min(Property property) { - return Property.of("min(" + property + ")"); - } - - public static Property sum(Property property) { + public static Property sum(Property property) { return Property.of("sum(" + property + ")"); } - public static Property lower(Property property) { + public static Property avg(Property property) { + return Property.of("avg(" + property + ")"); + } + + public static Property max(Property property) { + return Property.of("max(" + property + ")"); + } + + public static Property min(Property property) { + return Property.of("min(" + property + ")"); + } + + public static Property coalesce(Property property, Object value) { + return Property.of("coalesce(" + property.toString() + "," + sqlValue(value) + ")"); + } + + public static Property lower(Property property) { return Property.of("lower(" + property + ")"); } - public static Property upper(Property property) { + public static Property upper(Property property) { return Property.of("upper(" + property + ")"); } - public static Property concat(Property property, Object... values) { + public static Property concat(Property property, Object... values) { StringBuilder expression = new StringBuilder(50); expression.append("concat(").append(property.toString()); for (Object value : values) { @@ -42,10 +48,6 @@ public final class StdFunctions { return Property.of(expression.toString()); } - public static Property coalesce(Property property, Object value) { - return Property.of("coalesce(" + property.toString() + "," + sqlValue(value) + ")"); - } - private static String sqlConcatString(Object value) { String asStr = String.valueOf(value); return (value instanceof Property || isSqlQuoted(asStr)) ? asStr : "'" + value + "'"; @@ -66,35 +68,67 @@ public final class StdFunctions { // -------------------------------------------------------------------------------------------- // // ---- Expressions --------------------------------------------------------------------------- // - public static Expression eq(Property property, Object value) { + public static Expression eq(Property property, Number value) { return Expr.eq(property.toString(), value); } - public static Expression gt(Property property, Object value) { + public static Expression eq(Property property, String value) { + return Expr.eq(property.toString(), value); + } + + public static Expression eq(Property property, Temporal value) { + return Expr.eq(property.toString(), value); + } + + public static Expression eq(Property property, boolean value) { + return Expr.eq(property.toString(), value); + } + + public static Expression eq(Property property, Object value) { + return Expr.eq(property.toString(), value); + } + + //---- + + public static Expression gt(Property property, Number value) { return Expr.gt(property.toString(), value); } - public static Expression like(Property property, String value) { + public static Expression gt(Property property, String value) { + return Expr.gt(property.toString(), value); + } + + public static Expression gt(Property property, Temporal value) { + return Expr.gt(property.toString(), value); + } + + public static Expression gt(Property property, Object value) { + return Expr.gt(property.toString(), value); + } + + //---- + + public static Expression like(Property property, String value) { return Expr.like(property.toString(), value); } - public static Expression ilike(Property property, String value) { + public static Expression ilike(Property property, String value) { return Expr.ilike(property.toString(), value); } - public static Expression startsWith(Property property, String value) { + public static Expression startsWith(Property property, String value) { return Expr.startsWith(property.toString(), value); } - public static Expression istartsWith(Property property, String value) { + public static Expression istartsWith(Property property, String value) { return Expr.istartsWith(property.toString(), value); } - public static Expression contains(Property property, String value) { + public static Expression contains(Property property, String value) { return Expr.contains(property.toString(), value); } - public static Expression icontains(Property property, String value) { + public static Expression icontains(Property property, String value) { return Expr.icontains(property.toString(), value); } } diff --git a/ebean-api/src/main/java/io/ebean/UpdateQuery.java b/ebean-api/src/main/java/io/ebean/UpdateQuery.java index a7abe00ea..bb293c748 100644 --- a/ebean-api/src/main/java/io/ebean/UpdateQuery.java +++ b/ebean-api/src/main/java/io/ebean/UpdateQuery.java @@ -118,7 +118,7 @@ public interface UpdateQuery { * @param property The bean property to be set * @param value The value to set the property to */ - UpdateQuery set(Query.Property property, Object value); + UpdateQuery set(Query.Property property, Object value); /** * Set the property to be null. @@ -142,7 +142,7 @@ public interface UpdateQuery { * * @param property The bean property to be set */ - UpdateQuery setNull(Query.Property property); + UpdateQuery setNull(Query.Property property); /** * Set using a property expression that does not need any bind values. diff --git a/ebean-api/src/test/java/io/ebean/StdFunctionsTest.java b/ebean-api/src/test/java/io/ebean/StdFunctionsTest.java index e22344c43..4cce8ed1a 100644 --- a/ebean-api/src/test/java/io/ebean/StdFunctionsTest.java +++ b/ebean-api/src/test/java/io/ebean/StdFunctionsTest.java @@ -9,8 +9,9 @@ import static org.assertj.core.api.Assertions.assertThat; class StdFunctionsTest { - final Query.Property foo = Query.Property.of("foo"); - final Query.Property bar = Query.Property.of("bar"); + final Query.Property amount = Query.Property.of("amount"); + final Query.Property foo = Query.Property.of("foo"); + final Query.Property bar = Query.Property.of("bar"); @Test void testAvg() { @@ -34,7 +35,7 @@ class StdFunctionsTest { @Test void testSum() { - assertThat(sum(foo).toString()).isEqualTo("sum(foo)"); + assertThat(sum(amount).toString()).isEqualTo("sum(amount)"); } @Test diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PArray.java b/ebean-querybean/src/main/java/io/ebean/typequery/PArray.java index 504a4b5d6..17076b9bd 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PArray.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PArray.java @@ -6,7 +6,7 @@ package io.ebean.typequery; * @param the root query bean type * @param the element type of the DbArray */ -public final class PArray extends TQPropertyBase { +public final class PArray extends TQPropertyBase { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PBaseCompareable.java b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseCompareable.java index cc50bd0d0..1faa6d7aa 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PBaseCompareable.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseCompareable.java @@ -6,11 +6,11 @@ import io.ebean.Query; /** * Base property for all comparable types. * - * @param the root query bean type - * @param the type of the scalar property + * @param the root query bean type + * @param the type of the scalar property + * @param the base type of the property */ -@SuppressWarnings("rawtypes") -public class PBaseCompareable extends PBaseValueEqual { +public abstract class PBaseCompareable extends PBaseValueEqual { /** * Construct with a property name and root instance. @@ -44,11 +44,14 @@ public class PBaseCompareable extends PBaseValueEqual { /** * Greater than other property. + *

+ * Note that the other property must have the same common "Base type" (String, Number, Temporal, Boolean or Object). * * @param other the other property to compare + * * @return the root query bean instance */ - public final R gt(TQProperty other) { + public final R gt(TQProperty other) { expr().raw(_name + " > " + other.propertyName()); return _root; } @@ -81,7 +84,7 @@ public class PBaseCompareable extends PBaseValueEqual { * @param other the other property to compare * @return the root query bean instance */ - public final R ge(TQProperty other) { + public final R ge(TQProperty other) { expr().raw(_name + " >= " + other.propertyName()); return _root; } @@ -114,7 +117,7 @@ public class PBaseCompareable extends PBaseValueEqual { * @param other the other property to compare * @return the root query bean instance */ - public final R lt(TQProperty other) { + public final R lt(TQProperty other) { expr().raw(_name + " < " + other.propertyName()); return _root; } @@ -147,7 +150,7 @@ public class PBaseCompareable extends PBaseValueEqual { * @param other the other property to compare * @return the root query bean instance */ - public final R le(TQProperty other) { + public final R le(TQProperty other) { expr().raw(_name + " <= " + other.propertyName()); return _root; } @@ -197,7 +200,7 @@ public class PBaseCompareable extends PBaseValueEqual { * The most common use of this could be called "effective dating" where 2 date or * timestamp columns represent the date range in which */ - public final R inRangeWith(TQProperty highProperty, T value) { + public final R inRangeWith(TQProperty highProperty, T value) { expr().inRangeWith(_name, highProperty._name, value); return _root; } @@ -220,7 +223,7 @@ public class PBaseCompareable extends PBaseValueEqual { *

* This is a convenience expression combining a number of simple expressions. */ - public final R inRangeWith(TQProperty lowProperty, TQProperty highProperty) { + public final R inRangeWith(TQProperty lowProperty, TQProperty highProperty) { expr().inRangeWithProperties(_name, lowProperty.propertyName(), highProperty.propertyName()); return _root; } diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PBaseDate.java b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseDate.java index 740e4f2d2..8759baa7b 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PBaseDate.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseDate.java @@ -1,5 +1,7 @@ package io.ebean.typequery; +import java.time.temporal.Temporal; + /** * Base property for date and date time types. * @@ -7,7 +9,7 @@ package io.ebean.typequery; * @param the date time type */ @SuppressWarnings("rawtypes") -public abstract class PBaseDate extends PBaseCompareable { +public abstract class PBaseDate extends PBaseCompareable { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PBaseNumber.java b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseNumber.java index 3f1de62d9..6e2e2a496 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PBaseNumber.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseNumber.java @@ -7,7 +7,7 @@ package io.ebean.typequery; * @param the number type */ @SuppressWarnings("rawtypes") -public abstract class PBaseNumber extends PBaseCompareable { +public abstract class PBaseNumber extends PBaseCompareable { /** * Construct with a property name and root instance. @@ -16,7 +16,7 @@ public abstract class PBaseNumber extends PBaseCompareab * @param root the root query bean instance */ public PBaseNumber(String name, R root) { - super(name , root); + super(name, root); } /** diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PBaseString.java b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseString.java index a0f3643d5..fb65af6ad 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PBaseString.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseString.java @@ -5,7 +5,7 @@ package io.ebean.typequery; * * @param the root query bean type */ -public abstract class PBaseString extends PBaseCompareable { +public abstract class PBaseString extends PBaseCompareable { /** * Construct with a property name and root instance. @@ -25,22 +25,28 @@ public abstract class PBaseString extends PBaseCompareable { } /** + * Deprecated migrate to eq(). + *

* Is equal to. The same as eq but uses the strong type as argument rather than String. * * @param value the equal to bind value * @return the root query bean instance */ + @Deprecated public final R equalToType(T value) { expr().eq(_name, value); return _root; } /** + * Deprecated migrate to ne(). + *

* Is not equal to. The same as ne but uses the strong type as argument rather than String. * * @param value the equal to bind value * @return the root query bean instance */ + @Deprecated public final R notEqualToType(T value) { expr().ne(_name, value); return _root; @@ -49,7 +55,146 @@ public abstract class PBaseString extends PBaseCompareable { // common string / expressions ------------ /** - * Case insensitive is equal to. + * Equal to. + * + * @param value the bind value + * @return the root query bean instance + */ + public final R eq(String value) { + expr().eq(_name, value); + return _root; + } + + /** + * Not equal to. + * + * @param value the bind value + * @return the root query bean instance + */ + public final R ne(String value) { + expr().ne(_name, value); + return _root; + } + + /** + * Greater than. + * + * @param value the bind value + * @return the root query bean instance + */ + public final R gt(String value) { + expr().gt(_name, value); + return _root; + } + + /** + * Greater than OR Null. + * + * @param value the bind value + * @return the root query bean instance + */ + public final R gtOrNull(String value) { + expr().gtOrNull(_name, value); + return _root; + } + + /** + * Greater than or Equal to. + * + * @param value the bind value + * @return the root query bean instance + */ + public final R ge(String value) { + expr().ge(_name, value); + return _root; + } + + /** + * Greater than or Equal to OR Null. + * + * @param value the bind value + * @return the root query bean instance + */ + public final R geOrNull(String value) { + expr().geOrNull(_name, value); + return _root; + } + + /** + * Less than. + * + * @param value the bind value + * @return the root query bean instance + */ + public final R lt(String value) { + expr().lt(_name, value); + return _root; + } + + /** + * Less than OR Null. + * + * @param value the bind value + * @return the root query bean instance + */ + public final R ltOrNull(String value) { + expr().ltOrNull(_name, value); + return _root; + } + + /** + * Less than or Equal to. + * + * @param value the bind value + * @return the root query bean instance + */ + public final R le(String value) { + expr().le(_name, value); + return _root; + } + + /** + * Less than or Equal to OR null. + * + * @param value the bind value + * @return the root query bean instance + */ + public final R leOrNull(String value) { + expr().leOrNull(_name, value); + return _root; + } + + /** + * Greater or equal to lower value and strictly less than upper value. + *

+ * This is generally preferable over Between for date and datetime types + * as SQL Between is inclusive on the upper bound ({@code <= }) and generally + * we need the upper bound to be exclusive ({@code < }). + *

+ * + * @param lower the lower bind value ({@code >= }) + * @param upper the upper bind value ({@code < }) + * @return the root query bean instance + */ + public final R inRange(String lower, String upper) { + expr().inRange(_name, lower, upper); + return _root; + } + + /** + * Between lower and upper values. + * + * @param lower the lower bind value + * @param upper the upper bind value + * @return the root query bean instance + */ + public final R between(String lower, String upper) { + expr().between(_name, lower, upper); + return _root; + } + + /** + * Case-insensitive is equal to. * * @param value the equal to bind value * @return the root query bean instance @@ -60,7 +205,7 @@ public abstract class PBaseString extends PBaseCompareable { } /** - * Case insensitive is equal to. + * Case-insensitive is equal to. * * @param value the equal to bind value * @return the root query bean instance @@ -115,7 +260,7 @@ public abstract class PBaseString extends PBaseCompareable { } /** - * Case insensitive like. + * Case-insensitive like. * * @param value the equal to bind value * @return the root query bean instance @@ -126,7 +271,7 @@ public abstract class PBaseString extends PBaseCompareable { } /** - * Case insensitive starts with. + * Case-insensitive starts with. * * @param value the equal to bind value * @return the root query bean instance @@ -137,7 +282,7 @@ public abstract class PBaseString extends PBaseCompareable { } /** - * Case insensitive ends with. + * Case-insensitive ends with. * * @param value the equal to bind value * @return the root query bean instance @@ -148,7 +293,7 @@ public abstract class PBaseString extends PBaseCompareable { } /** - * Case insensitive contains. + * Case-insensitive contains. * * @param value the equal to bind value * @return the root query bean instance diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PBaseTime.java b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseTime.java new file mode 100644 index 000000000..54daaae41 --- /dev/null +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseTime.java @@ -0,0 +1,52 @@ +package io.ebean.typequery; + +import java.time.temporal.Temporal; + +/** + * Base property for time types. + * + * @param the root query bean type + * @param the number type + */ +@SuppressWarnings("rawtypes") +public abstract class PBaseTime extends PBaseCompareable { + + /** + * Construct with a property name and root instance. + * + * @param name property name + * @param root the root query bean instance + */ + public PBaseTime(String name, R root) { + super(name, root); + } + + /** + * Construct with additional path prefix. + */ + public PBaseTime(String name, R root, String prefix) { + super(name, root, prefix); + } + + /** + * Same as greater than. + * + * @param value the equal to bind value + * @return the root query bean instance + */ + public final R after(T value) { + expr().gt(_name, value); + return _root; + } + + /** + * Same as less than. + * + * @param value the equal to bind value + * @return the root query bean instance + */ + public final R before(T value) { + expr().lt(_name, value); + return _root; + } +} diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PBaseValueEqual.java b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseValueEqual.java index 58474a371..049ecac26 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PBaseValueEqual.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseValueEqual.java @@ -11,7 +11,7 @@ import java.util.Collection; * @param the root query bean type * @param the number type */ -public abstract class PBaseValueEqual extends TQPropertyBase { +public abstract class PBaseValueEqual extends TQPropertyBase { /** * Construct with a property name and root instance. @@ -89,7 +89,7 @@ public abstract class PBaseValueEqual extends TQPropertyBase { * @param other the other property to compare * @return the root query bean instance */ - public final R eq(TQProperty other) { + public final R eq(TQProperty other) { expr().raw(_name + " = " + other.propertyName()); return _root; } @@ -152,7 +152,7 @@ public abstract class PBaseValueEqual extends TQPropertyBase { * @param other the other property to compare * @return the root query bean instance */ - public final R ne(TQProperty other) { + public final R ne(TQProperty other) { expr().raw(_name + " <> " + other.propertyName()); return _root; } diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PBoolean.java b/ebean-querybean/src/main/java/io/ebean/typequery/PBoolean.java index 55707300e..370cce8a7 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PBoolean.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PBoolean.java @@ -5,7 +5,7 @@ package io.ebean.typequery; * * @param the root query bean type */ -public final class PBoolean extends PBaseValueEqual { +public final class PBoolean extends PBaseValueEqual { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PByteArray.java b/ebean-querybean/src/main/java/io/ebean/typequery/PByteArray.java index d2bc50353..ce275eeda 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PByteArray.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PByteArray.java @@ -5,7 +5,7 @@ package io.ebean.typequery; * * @param the root query bean type */ -public final class PByteArray extends PBaseValueEqual { +public final class PByteArray extends PBaseValueEqual { /** * Construct with a property name and root instance. @@ -24,5 +24,4 @@ public final class PByteArray extends PBaseValueEqual { super(name, root, prefix); } - } diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PCidr.java b/ebean-querybean/src/main/java/io/ebean/typequery/PCidr.java index 645b5d37b..2ad7e4600 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PCidr.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PCidr.java @@ -7,7 +7,7 @@ import io.ebean.types.Cidr; * * @param the root query bean type */ -public final class PCidr extends PBaseValueEqual { +public final class PCidr extends PBaseString { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PCurrency.java b/ebean-querybean/src/main/java/io/ebean/typequery/PCurrency.java index 5f2ecb65c..0d6256ae3 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PCurrency.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PCurrency.java @@ -7,7 +7,7 @@ import java.util.Currency; * * @param the root query bean type */ -public final class PCurrency extends PBaseValueEqual { +public final class PCurrency extends PBaseString { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PEnum.java b/ebean-querybean/src/main/java/io/ebean/typequery/PEnum.java index ccd9ff7aa..124263f55 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PEnum.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PEnum.java @@ -1,12 +1,12 @@ package io.ebean.typequery; /** - * BigDecimal property. + * Enum property. * * @param the enum specific type * @param the root query bean type */ -public final class PEnum extends PBaseValueEqual { +public final class PEnum extends PBaseValueEqual { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PFile.java b/ebean-querybean/src/main/java/io/ebean/typequery/PFile.java index c1ea4c2d5..d0f5b6931 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PFile.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PFile.java @@ -9,7 +9,7 @@ package io.ebean.typequery; * * @param the root query bean type */ -public final class PFile extends TQPropertyBase { +public final class PFile extends TQPropertyBase { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PInet.java b/ebean-querybean/src/main/java/io/ebean/typequery/PInet.java index d0fd0d8ab..a87a6ae8f 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PInet.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PInet.java @@ -7,7 +7,7 @@ import io.ebean.types.Inet; * * @param the root query bean type */ -public final class PInet extends PBaseValueEqual { +public final class PInet extends PBaseString { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PInetAddress.java b/ebean-querybean/src/main/java/io/ebean/typequery/PInetAddress.java index f4a30ca6a..527ca0938 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PInetAddress.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PInetAddress.java @@ -7,8 +7,7 @@ import java.net.InetAddress; * * @param the root query bean type */ - -public final class PInetAddress extends PBaseValueEqual { +public final class PInetAddress extends PBaseString { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PJodaLocalTime.java b/ebean-querybean/src/main/java/io/ebean/typequery/PJodaLocalTime.java index d92165d33..6cc01f7de 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PJodaLocalTime.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PJodaLocalTime.java @@ -8,7 +8,7 @@ import org.joda.time.LocalTime; * * @param the root query bean type */ -public final class PJodaLocalTime extends PBaseNumber { +public final class PJodaLocalTime extends PBaseTime { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PJson.java b/ebean-querybean/src/main/java/io/ebean/typequery/PJson.java index 5eddd0196..546964ed9 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PJson.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PJson.java @@ -17,7 +17,7 @@ package io.ebean.typequery; * * @param the root query bean type */ -public final class PJson extends TQPropertyBase { +public final class PJson extends TQPropertyBase { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PLocalTime.java b/ebean-querybean/src/main/java/io/ebean/typequery/PLocalTime.java index 463365e8e..8232577fd 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PLocalTime.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PLocalTime.java @@ -8,7 +8,7 @@ import java.time.LocalTime; * * @param the root query bean type */ -public final class PLocalTime extends PBaseNumber { +public final class PLocalTime extends PBaseTime { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/POffsetDateTime.java b/ebean-querybean/src/main/java/io/ebean/typequery/POffsetDateTime.java index 809248b86..88ef6875f 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/POffsetDateTime.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/POffsetDateTime.java @@ -7,7 +7,7 @@ import java.time.OffsetDateTime; * * @param the root query bean type */ -public final class POffsetDateTime extends PBaseNumber { +public final class POffsetDateTime extends PBaseDate { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/POffsetTime.java b/ebean-querybean/src/main/java/io/ebean/typequery/POffsetTime.java index e6d226971..70c70704d 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/POffsetTime.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/POffsetTime.java @@ -7,7 +7,7 @@ import java.time.OffsetTime; * * @param the root query bean type */ -public final class POffsetTime extends PBaseNumber { +public final class POffsetTime extends PBaseTime { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PScalar.java b/ebean-querybean/src/main/java/io/ebean/typequery/PScalar.java index 7d109a6fd..6318a1b8a 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PScalar.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PScalar.java @@ -1,13 +1,12 @@ package io.ebean.typequery; /** - * Property for classes that are serialized/deserialized by - * ScalarType/AttributeConverter. + * Property for classes that are serialized/deserialized by ScalarType/AttributeConverter. * * @param the root query bean type * @param the scalar type */ -public final class PScalar extends PBaseValueEqual { +public final class PScalar extends PBaseValueEqual { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PScalarComparable.java b/ebean-querybean/src/main/java/io/ebean/typequery/PScalarComparable.java index 6da31acb2..a303e6530 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PScalarComparable.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PScalarComparable.java @@ -8,7 +8,7 @@ package io.ebean.typequery; * @param the root query bean type * @param the scalar type */ -public final class PScalarComparable> extends PBaseCompareable { +public final class PScalarComparable> extends PBaseCompareable { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PString.java b/ebean-querybean/src/main/java/io/ebean/typequery/PString.java index c37e6e8b2..a24cf1a9b 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PString.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PString.java @@ -5,7 +5,7 @@ package io.ebean.typequery; * * @param the root query bean type */ -public final class PString extends PBaseCompareable { +public final class PString extends PBaseCompareable { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PTime.java b/ebean-querybean/src/main/java/io/ebean/typequery/PTime.java index 62556f480..0052a8e00 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PTime.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PTime.java @@ -8,7 +8,7 @@ import java.sql.Time; * * @param the root query bean type */ -public final class PTime extends PBaseNumber { +public final class PTime extends PBaseTime { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PTimeZone.java b/ebean-querybean/src/main/java/io/ebean/typequery/PTimeZone.java index 8a8352fef..d7906bcbc 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PTimeZone.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PTimeZone.java @@ -7,7 +7,7 @@ import java.util.TimeZone; * * @param the root query bean type */ -public final class PTimeZone extends PBaseValueEqual { +public final class PTimeZone extends PBaseString { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PUuid.java b/ebean-querybean/src/main/java/io/ebean/typequery/PUuid.java index 8f08053eb..ddf7366f2 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PUuid.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PUuid.java @@ -7,7 +7,7 @@ import java.util.UUID; * * @param the root query bean type */ -public final class PUuid extends PBaseValueEqual { +public final class PUuid extends PBaseValueEqual { /** * Construct with a property name and root instance. @@ -16,7 +16,7 @@ public final class PUuid extends PBaseValueEqual { * @param root the root query bean instance */ public PUuid(String name, R root) { - super(name , root); + super(name, root); } /** diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PZoneId.java b/ebean-querybean/src/main/java/io/ebean/typequery/PZoneId.java index b057840d9..1883f87b2 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PZoneId.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PZoneId.java @@ -7,7 +7,7 @@ import java.time.ZoneId; * * @param the root query bean type */ -public final class PZoneId extends PBaseValueEqual { +public final class PZoneId extends PBaseString { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PZoneOffset.java b/ebean-querybean/src/main/java/io/ebean/typequery/PZoneOffset.java index c94a99cc2..82113780d 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PZoneOffset.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PZoneOffset.java @@ -7,7 +7,7 @@ import java.time.ZoneOffset; * * @param the root query bean type */ -public final class PZoneOffset extends PBaseValueEqual { +public final class PZoneOffset extends PBaseString { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PZonedDateTime.java b/ebean-querybean/src/main/java/io/ebean/typequery/PZonedDateTime.java index 3aca1d178..e8d2aa072 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PZonedDateTime.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PZonedDateTime.java @@ -7,7 +7,7 @@ import java.time.ZonedDateTime; * * @param the root query bean type */ -public final class PZonedDateTime extends PBaseCompareable { +public final class PZonedDateTime extends PBaseDate { /** * Construct with a property name and root instance. @@ -26,25 +26,4 @@ public final class PZonedDateTime extends PBaseCompareable super(name, root, prefix); } - /** - * Same as greater than. - * - * @param value the equal to bind value - * @return the root query bean instance - */ - public R after(ZonedDateTime value) { - expr().gt(_name, value); - return _root; - } - - /** - * Same as less than. - * - * @param value the equal to bind value - * @return the root query bean instance - */ - public R before(ZonedDateTime value) { - expr().lt(_name, value); - return _root; - } } diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/TQAssocBean.java b/ebean-querybean/src/main/java/io/ebean/typequery/TQAssocBean.java index 194dcd4f4..1a7372ebc 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/TQAssocBean.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/TQAssocBean.java @@ -17,7 +17,7 @@ import java.util.Set; * @param the specific root query bean type (e.g. QCustomer) */ @SuppressWarnings("rawtypes") -public abstract class TQAssocBean extends TQProperty { +public abstract class TQAssocBean extends TQProperty { private static final FetchConfig FETCH_DEFAULT = FetchConfig.ofDefault(); private static final FetchConfig FETCH_QUERY = FetchConfig.ofQuery(); @@ -111,7 +111,7 @@ public abstract class TQAssocBean extends TQProperty { * Eagerly fetch this association fetching some of the properties. */ @SafeVarargs - protected final R fetchProperties(TQProperty... props) { + protected final R fetchProperties(TQProperty... props) { return fetchWithProperties(FETCH_DEFAULT, props); } @@ -119,7 +119,7 @@ public abstract class TQAssocBean extends TQProperty { * Eagerly fetch query this association fetching some of the properties. */ @SafeVarargs - protected final R fetchQueryProperties(TQProperty... props) { + protected final R fetchQueryProperties(TQProperty... props) { return fetchWithProperties(FETCH_QUERY, props); } @@ -127,7 +127,7 @@ public abstract class TQAssocBean extends TQProperty { * Eagerly fetch this association using L2 bean cache. */ @SafeVarargs - protected final R fetchCacheProperties(TQProperty... props) { + protected final R fetchCacheProperties(TQProperty... props) { return fetchWithProperties(FETCH_CACHE, props); } @@ -135,12 +135,12 @@ public abstract class TQAssocBean extends TQProperty { * Eagerly fetch query this association fetching some of the properties. */ @SafeVarargs - protected final R fetchLazyProperties(TQProperty... props) { + protected final R fetchLazyProperties(TQProperty... props) { return fetchWithProperties(FETCH_LAZY, props); } @SafeVarargs - private R fetchWithProperties(FetchConfig config, TQProperty... props) { + private R fetchWithProperties(FetchConfig config, TQProperty... props) { spiQuery().fetchProperties(_name, properties(props), config); return _root; } @@ -173,13 +173,13 @@ public abstract class TQAssocBean extends TQProperty { } private SpiQueryFetch spiQuery() { - return (SpiQueryFetch)((TQRootBean) _root).query(); + return (SpiQueryFetch) ((TQRootBean) _root).query(); } @SafeVarargs - private Set properties(TQProperty... props) { + private Set properties(TQProperty... props) { Set set = new LinkedHashSet<>(); - for (TQProperty prop : props) { + for (TQProperty prop : props) { set.add(prop.propertyName()); } return set; diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/TQProperty.java b/ebean-querybean/src/main/java/io/ebean/typequery/TQProperty.java index 8cc0e6465..79c4a2cc5 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/TQProperty.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/TQProperty.java @@ -6,12 +6,12 @@ import io.ebean.Query; /** * A property used in type query. * - * @param The type of the owning root bean + * @param The type of the owning root bean + * @param The base type, one of String, Number, Temporal, Boolean, Object. */ -public class TQProperty implements Query.Property { +public class TQProperty implements Query.Property { protected final String _name; - protected final R _root; /** @@ -41,7 +41,7 @@ public class TQProperty implements Query.Property { * Internal method to return the underlying expression list. */ protected final ExpressionList expr() { - return ((TQRootBean) _root).peekExprList(); + return ((TQRootBean) _root).peekExprList(); } /** diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/TQPropertyBase.java b/ebean-querybean/src/main/java/io/ebean/typequery/TQPropertyBase.java index 83a23e6ab..2a3937be4 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/TQPropertyBase.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/TQPropertyBase.java @@ -3,9 +3,10 @@ package io.ebean.typequery; /** * Base scalar property. * - * @param The type of the owning root bean + * @param The type of the owning root bean + * @param The base type of the property */ -public class TQPropertyBase extends TQProperty { +public abstract class TQPropertyBase extends TQProperty { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/TQRootBean.java b/ebean-querybean/src/main/java/io/ebean/typequery/TQRootBean.java index e9dc8d590..422110abc 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/TQRootBean.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/TQRootBean.java @@ -263,14 +263,14 @@ public abstract class TQRootBean { * @param properties the list of properties to fetch */ @SafeVarargs - public final R select(TQProperty... properties) { + public final R select(TQProperty... properties) { ((SpiQueryFetch) query).selectProperties(properties(properties)); return root; } - private Set properties(Query.Property[] properties) { + private Set properties(Query.Property[] properties) { Set props = new LinkedHashSet<>(); - for (Query.Property property : properties) { + for (Query.Property property : properties) { props.add(property.toString()); } return props; diff --git a/ebean-querybean/src/test/java/org/querytest/QOrderTest.java b/ebean-querybean/src/test/java/org/querytest/QOrderTest.java index fa17ca5a5..2ed846c0a 100644 --- a/ebean-querybean/src/test/java/org/querytest/QOrderTest.java +++ b/ebean-querybean/src/test/java/org/querytest/QOrderTest.java @@ -261,7 +261,7 @@ public class QOrderTest { .add(gt(coalesce(o.customer.version, 0), 42)) .id.lt(12) .endOr() - .query(); + .query(); query.findList(); diff --git a/ebean-test/src/test/java/org/tests/query/TestStdFunctions.java b/ebean-test/src/test/java/org/tests/query/TestStdFunctions.java index 27ff61760..940af117a 100644 --- a/ebean-test/src/test/java/org/tests/query/TestStdFunctions.java +++ b/ebean-test/src/test/java/org/tests/query/TestStdFunctions.java @@ -15,8 +15,7 @@ public class TestStdFunctions { @Test void coalesceLike() { - var name = Query.Property.of("name"); - + Query.Property name = Query.Property.of("name"); var query = DB.find(Customer.class) .select(coalesce(name, "na").toString()) .where() diff --git a/kotlin-querybean-generator/pom.xml b/kotlin-querybean-generator/pom.xml index e9aaa3790..a74fe64bf 100644 --- a/kotlin-querybean-generator/pom.xml +++ b/kotlin-querybean-generator/pom.xml @@ -113,7 +113,7 @@ io.ebean kotlin-querybean-generator - 13.7.0 + 13.10.2-SNAPSHOT diff --git a/kotlin-querybean-generator/src/main/java/io/ebean/querybean/generator/KotlinLangAdapter.java b/kotlin-querybean-generator/src/main/java/io/ebean/querybean/generator/KotlinLangAdapter.java index 03837a21a..f3da81840 100644 --- a/kotlin-querybean-generator/src/main/java/io/ebean/querybean/generator/KotlinLangAdapter.java +++ b/kotlin-querybean-generator/src/main/java/io/ebean/querybean/generator/KotlinLangAdapter.java @@ -14,7 +14,6 @@ class KotlinLangAdapter implements LangAdapter { @Override public void alias(Append writer, String shortName) { - writer.append(" companion object {").eol(); writer.append(" /**").eol(); writer.append(" * shared 'Alias' instance used to provide").eol(); @@ -33,7 +32,6 @@ class KotlinLangAdapter implements LangAdapter { @Override public void assocBeanConstructor(Append writer, String shortName) { - writer.append(" constructor(name: String, root: R) : super(name, root)").eol(); writer.eol(); writer.append(" constructor(name: String, root: R, prefix: String) : super(name, root, prefix)").eol(); @@ -41,7 +39,6 @@ class KotlinLangAdapter implements LangAdapter { @Override public void fetch(Append writer, String origShortName) { - writeAssocBeanFetch(writer, origShortName, "", "Eagerly fetch this association loading the specified properties."); writeAssocBeanFetch(writer, origShortName, "Query", "Eagerly fetch this association using a 'query join' loading the specified properties."); writeAssocBeanFetch(writer, origShortName, "Cache", "Eagerly fetch this association using L2 cache."); @@ -49,26 +46,21 @@ class KotlinLangAdapter implements LangAdapter { } private void writeAssocBeanFetch(Append writer, String origShortName, String fetchType, String comment) { - -// fun fetch(vararg properties: TQProperty): R { +// fun fetch(vararg properties: TQProperty): R { // return fetchProperties(*properties) // } - writer.append(" /**").eol(); writer.append(" * ").append(comment).eol(); writer.append(" */").eol(); - writer.append(" fun fetch%s(vararg properties: TQProperty) : R {", fetchType, origShortName).eol(); + writer.append(" fun fetch%s(vararg properties: TQProperty) : R {", fetchType, origShortName).eol(); writer.append(" return fetch%sProperties(*properties)", fetchType).eol(); writer.append(" }").eol(); writer.eol(); } - @Override public void rootBeanConstructor(Append writer, String shortName, String dbName) { - String name = (dbName == null) ? "default" : dbName; - writer.append(" /**").eol(); writer.append(" * Construct using the %s Database.", name).eol(); writer.append(" */").eol(); @@ -106,7 +98,6 @@ class KotlinLangAdapter implements LangAdapter { @Override public void fieldDefn(Append writer, String propertyName, String typeDefn) { - writer.append(" lateinit var %s: ", propertyName); if (typeDefn.endsWith(",Integer>")) { typeDefn = typeDefn.replace(",Integer>", ",Int>"); diff --git a/querybean-generator/src/main/java/io/ebean/querybean/generator/SimpleQueryBeanWriter.java b/querybean-generator/src/main/java/io/ebean/querybean/generator/SimpleQueryBeanWriter.java index f8e965f04..75400b935 100644 --- a/querybean-generator/src/main/java/io/ebean/querybean/generator/SimpleQueryBeanWriter.java +++ b/querybean-generator/src/main/java/io/ebean/querybean/generator/SimpleQueryBeanWriter.java @@ -275,7 +275,7 @@ class SimpleQueryBeanWriter { writer.append(" * ").append(comment).eol(); writer.append(" */").eol(); writer.append(" @SafeVarargs @SuppressWarnings(\"varargs\")").eol(); - writer.append(" public final R fetch%s(TQProperty... properties) {", fetchType, origShortName).eol(); + writer.append(" public final R fetch%s(TQProperty... properties) {", fetchType, origShortName).eol(); writer.append(" return fetch%sProperties(properties);", fetchType).eol(); writer.append(" }").eol(); writer.eol(); From d29ef8934d3dea8a769c9d9ddf5b04da3e797b89 Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Wed, 9 Nov 2022 17:47:16 +1300 Subject: [PATCH 07/11] Add more operators to StdOperators - stricter use of UpdateQuery.set() - Numbers just use their own type - PBaseNumber - PEnum just uses its own type - Expr.factory() to expose factory --- ebean-api/src/main/java/io/ebean/Expr.java | 7 + ebean-api/src/main/java/io/ebean/Query.java | 8 +- .../src/main/java/io/ebean/StdFunctions.java | 134 ---------- .../src/main/java/io/ebean/StdLegacyOps.java | 37 +++ .../src/main/java/io/ebean/StdOperators.java | 251 ++++++++++++++++++ .../src/main/java/io/ebean/UpdateQuery.java | 2 +- .../test/java/io/ebean/StdFunctionsTest.java | 2 +- .../java/io/ebean/typequery/PBaseNumber.java | 2 +- .../main/java/io/ebean/typequery/PEnum.java | 2 +- .../java/io/ebean/typequery/TQRootBean.java | 2 +- .../java/org/querytest/QCustomerTest.java | 28 ++ .../test/java/org/querytest/QOrderTest.java | 8 +- .../org/tests/query/TestStdFunctions.java | 2 +- 13 files changed, 336 insertions(+), 149 deletions(-) delete mode 100644 ebean-api/src/main/java/io/ebean/StdFunctions.java create mode 100644 ebean-api/src/main/java/io/ebean/StdLegacyOps.java create mode 100644 ebean-api/src/main/java/io/ebean/StdOperators.java diff --git a/ebean-api/src/main/java/io/ebean/Expr.java b/ebean-api/src/main/java/io/ebean/Expr.java index 525bc3272..072eb7e95 100644 --- a/ebean-api/src/main/java/io/ebean/Expr.java +++ b/ebean-api/src/main/java/io/ebean/Expr.java @@ -30,6 +30,13 @@ public final class Expr { private Expr() { } + /** + * Return the underlying expression factory. + */ + public static ExpressionFactory factory() { + return DB.expressionFactory(); + } + /** * Equal To - property equal to the given value. */ diff --git a/ebean-api/src/main/java/io/ebean/Query.java b/ebean-api/src/main/java/io/ebean/Query.java index ad15fc047..ad7a955ee 100644 --- a/ebean-api/src/main/java/io/ebean/Query.java +++ b/ebean-api/src/main/java/io/ebean/Query.java @@ -1773,16 +1773,16 @@ public interface Query extends CancelableQuery { *

* Implemented by query bean properties and expressions based on those properties. *

- * The base type determines which {@link StdFunctions} can be used on the property. + * The base type determines which {@link StdOperators} can be used on the property. * - * @param The base type of the property Number, String, Temporal, Boolean or Object. + * @param The property type. */ - interface Property { + interface Property { /** * Return a property given the expression. */ - static Property of(String expression) { + static Property of(String expression) { return new SimpleProperty<>(expression); } diff --git a/ebean-api/src/main/java/io/ebean/StdFunctions.java b/ebean-api/src/main/java/io/ebean/StdFunctions.java deleted file mode 100644 index 6a36c58d8..000000000 --- a/ebean-api/src/main/java/io/ebean/StdFunctions.java +++ /dev/null @@ -1,134 +0,0 @@ -package io.ebean; - -import io.ebean.Query.Property; - -import java.time.temporal.Temporal; - -public final class StdFunctions { - - public static Property count(Property property) { - return Property.of("count(" + property + ")"); - } - - public static Property sum(Property property) { - return Property.of("sum(" + property + ")"); - } - - public static Property avg(Property property) { - return Property.of("avg(" + property + ")"); - } - - public static Property max(Property property) { - return Property.of("max(" + property + ")"); - } - - public static Property min(Property property) { - return Property.of("min(" + property + ")"); - } - - public static Property coalesce(Property property, Object value) { - return Property.of("coalesce(" + property.toString() + "," + sqlValue(value) + ")"); - } - - public static Property lower(Property property) { - return Property.of("lower(" + property + ")"); - } - - public static Property upper(Property property) { - return Property.of("upper(" + property + ")"); - } - - public static Property concat(Property property, Object... values) { - StringBuilder expression = new StringBuilder(50); - expression.append("concat(").append(property.toString()); - for (Object value : values) { - expression.append(",").append(sqlConcatString(value)); - } - expression.append(")"); - return Property.of(expression.toString()); - } - - private static String sqlConcatString(Object value) { - String asStr = String.valueOf(value); - return (value instanceof Property || isSqlQuoted(asStr)) ? asStr : "'" + value + "'"; - } - - private static boolean isSqlQuoted(String asStr) { - return asStr.length() > 0 && asStr.charAt(0) == '\''; - } - - private static String sqlValue(Object value) { - if (value instanceof Property || value instanceof Number) { - return value.toString(); - } else { - return "'" + value + "'"; - } - } - - // -------------------------------------------------------------------------------------------- // - // ---- Expressions --------------------------------------------------------------------------- // - - public static Expression eq(Property property, Number value) { - return Expr.eq(property.toString(), value); - } - - public static Expression eq(Property property, String value) { - return Expr.eq(property.toString(), value); - } - - public static Expression eq(Property property, Temporal value) { - return Expr.eq(property.toString(), value); - } - - public static Expression eq(Property property, boolean value) { - return Expr.eq(property.toString(), value); - } - - public static Expression eq(Property property, Object value) { - return Expr.eq(property.toString(), value); - } - - //---- - - public static Expression gt(Property property, Number value) { - return Expr.gt(property.toString(), value); - } - - public static Expression gt(Property property, String value) { - return Expr.gt(property.toString(), value); - } - - public static Expression gt(Property property, Temporal value) { - return Expr.gt(property.toString(), value); - } - - public static Expression gt(Property property, Object value) { - return Expr.gt(property.toString(), value); - } - - //---- - - public static Expression like(Property property, String value) { - return Expr.like(property.toString(), value); - } - - public static Expression ilike(Property property, String value) { - return Expr.ilike(property.toString(), value); - } - - public static Expression startsWith(Property property, String value) { - return Expr.startsWith(property.toString(), value); - } - - public static Expression istartsWith(Property property, String value) { - return Expr.istartsWith(property.toString(), value); - } - - public static Expression contains(Property property, String value) { - return Expr.contains(property.toString(), value); - } - - public static Expression icontains(Property property, String value) { - return Expr.icontains(property.toString(), value); - } -} diff --git a/ebean-api/src/main/java/io/ebean/StdLegacyOps.java b/ebean-api/src/main/java/io/ebean/StdLegacyOps.java new file mode 100644 index 000000000..c8da08b0a --- /dev/null +++ b/ebean-api/src/main/java/io/ebean/StdLegacyOps.java @@ -0,0 +1,37 @@ +package io.ebean; + +import io.ebean.Query.Property; + +import java.time.temporal.Temporal; + +@Deprecated(since = "experimental") +public final class StdLegacyOps { + + public static Expression eq(Property property, java.util.Calendar value) { + return Expr.eq(property.toString(), value); + } + + public static Expression eqOrNull(Property property, java.util.Calendar value) { + return Expr.factory().eqOrNull(property.toString(), value); + } + + public static Expression ne(Property property, java.util.Calendar value) { + return Expr.factory().ne(property.toString(), value); + } + + public static Expression lt(Property property, java.util.Calendar value) { + return Expr.factory().lt(property.toString(), value); + } + + public static Expression ltOrNull(Property property, java.util.Calendar value) { + return Expr.factory().ltOrNull(property.toString(), value); + } + + public static Expression le(Property property, java.util.Calendar value) { + return Expr.factory().le(property.toString(), value); + } + + public static Expression leOrNull(Property property, java.util.Calendar value) { + return Expr.factory().leOrNull(property.toString(), value); + } +} diff --git a/ebean-api/src/main/java/io/ebean/StdOperators.java b/ebean-api/src/main/java/io/ebean/StdOperators.java new file mode 100644 index 000000000..e049401d5 --- /dev/null +++ b/ebean-api/src/main/java/io/ebean/StdOperators.java @@ -0,0 +1,251 @@ +package io.ebean; + +import io.ebean.Query.Property; + +import java.time.temporal.Temporal; +import java.util.Collection; + +@Deprecated(since = "experimental") +public final class StdOperators { + + // ---- Functions ---- // + + public static Property sum(Property property) { + return Property.of("sum(" + property + ")"); + } + + public static Property count(Property property) { + return Property.of("count(" + property + ")"); + } + + public static Property avg(Property property) { + return Property.of("avg(" + property + ")"); + } + + public static Property max(Property property) { + return Property.of("max(" + property + ")"); + } + + public static Property min(Property property) { + return Property.of("min(" + property + ")"); + } + + public static Property coalesce(Property property, Object value) { + return Property.of("coalesce(" + property.toString() + "," + sqlValue(value) + ")"); + } + + public static Property lower(Property property) { + return Property.of("lower(" + property + ")"); + } + + public static Property upper(Property property) { + return Property.of("upper(" + property + ")"); + } + + public static Property concat(Property property, Object... values) { + StringBuilder expression = new StringBuilder(50); + expression.append("concat(").append(property.toString()); + for (Object value : values) { + expression.append(",").append(sqlConcatString(value)); + } + expression.append(")"); + return Property.of(expression.toString()); + } + + private static String sqlConcatString(Object value) { + String asStr = String.valueOf(value); + return (value instanceof Property || isSqlQuoted(asStr)) ? asStr : "'" + value + "'"; + } + + private static boolean isSqlQuoted(String asStr) { + return asStr.length() > 0 && asStr.charAt(0) == '\''; + } + + private static String sqlValue(Object value) { + if (value instanceof Property || value instanceof Number) { + return value.toString(); + } else { + return "'" + value + "'"; + } + } + + // ---- Operators ---- // + + public static Expression eq(Property property, T value) { + return Expr.eq(property.toString(), value); + } + + public static Expression eq(Property property, Query subQuery) { + return Expr.in(property.toString(), subQuery); + } + + public static Expression eq(Property property, java.util.Date value) { + return Expr.eq(property.toString(), value); + } + + public static Expression eqOrNull(Property property, T value) { + return Expr.factory().eqOrNull(property.toString(), value); + } + + public static Expression eqOrNull(Property property, java.util.Date value) { + return Expr.factory().eqOrNull(property.toString(), value); + } + + public static Expression ne(Property property, T value) { + return Expr.ne(property.toString(), value); + } + + public static Expression ne(Property property, Query subQuery) { + return Expr.ne(property.toString(), subQuery); + } + + public static Expression ne(Property property, java.util.Date value) { + return Expr.ne(property.toString(), value); + } + + public static Expression gt(Property property, T value) { + return Expr.gt(property.toString(), value); + } + + public static Expression gt(Property property, Query subQuery) { + return Expr.gt(property.toString(), subQuery); + } + + public static Expression gt(Property property, java.util.Date value) { + return Expr.gt(property.toString(), value); + } + + public static Expression gtOrNull(Property property, T value) { + return Expr.factory().gtOrNull(property.toString(), value); + } + + public static Expression gtOrNull(Property property, java.util.Date value) { + return Expr.factory().gtOrNull(property.toString(), value); + } + + public static Expression ge(Property property, T value) { + return Expr.ge(property.toString(), value); + } + + public static Expression ge(Property property, Query subQuery) { + return Expr.ge(property.toString(), subQuery); + } + + public static Expression ge(Property property, java.util.Date value) { + return Expr.ge(property.toString(), value); + } + + public static Expression geOrNull(Property property, T value) { + return Expr.factory().geOrNull(property.toString(), value); + } + + public static Expression geOrNull(Property property, java.util.Date value) { + return Expr.factory().geOrNull(property.toString(), value); + } + + public static Expression lt(Property property, T value) { + return Expr.lt(property.toString(), value); + } + + public static Expression lt(Property property, Query subQuery) { + return Expr.lt(property.toString(), subQuery); + } + + public static Expression lt(Property property, java.util.Date value) { + return Expr.lt(property.toString(), value); + } + + public static Expression ltOrNull(Property property, T value) { + return Expr.factory().ltOrNull(property.toString(), value); + } + + public static Expression ltOrNull(Property property, java.util.Date value) { + return Expr.factory().ltOrNull(property.toString(), value); + } + + public static Expression le(Property property, T value) { + return Expr.le(property.toString(), value); + } + + public static Expression le(Property property, Query subQuery) { + return Expr.le(property.toString(), subQuery); + } + + public static Expression le(Property property, java.util.Date value) { + return Expr.le(property.toString(), value); + } + + public static Expression leOrNull(Property property, T value) { + return Expr.factory().leOrNull(property.toString(), value); + } + + public static Expression leOrNull(Property property, java.util.Date value) { + return Expr.factory().leOrNull(property.toString(), value); + } + + public static Expression inRange(Property property, T lowValue, T highValue) { + return Expr.factory().inRange(property.toString(), lowValue, highValue); + } + + public static Expression inRange(Property property, java.util.Date lowValue, java.util.Date highValue) { + return Expr.factory().inRange(property.toString(), lowValue, highValue); + } + + public static Expression inRange(Property lowProperty, Property highProperty, T value) { + return Expr.factory().inRangeWith(lowProperty.toString(), highProperty.toString(), value); + } + + public static Expression inRange(Property lowProperty, Property highProperty, java.util.Date value) { + return Expr.factory().inRangeWith(lowProperty.toString(), highProperty.toString(), value); + } + + public static Expression inRange(Property lowProperty, Property property, Property highProperty) { + return Expr.factory().inRangeWithProperties(lowProperty.toString(), property.toString(), highProperty.toString()); + } + + public static Expression in(Property property, Collection value) { + return Expr.in(property.toString(), value); + } + + public static Expression in(Property property, Query subQuery) { + return Expr.in(property.toString(), subQuery); + } + + public static Expression inOrEmpty(Property property, Collection value) { + return Expr.inOrEmpty(property.toString(), value); + } + + public static Expression notIn(Property property, Collection value) { + return Expr.factory().notIn(property.toString(), value); + } + + public static Expression notIn(Property property, Query subQuery) { + return Expr.factory().notIn(property.toString(), subQuery); + } + + // ---- String operators ---- // + + public static Expression like(Property property, String value) { + return Expr.like(property.toString(), value); + } + + public static Expression ilike(Property property, String value) { + return Expr.ilike(property.toString(), value); + } + + public static Expression startsWith(Property property, String value) { + return Expr.startsWith(property.toString(), value); + } + + public static Expression istartsWith(Property property, String value) { + return Expr.istartsWith(property.toString(), value); + } + + public static Expression contains(Property property, String value) { + return Expr.contains(property.toString(), value); + } + + public static Expression icontains(Property property, String value) { + return Expr.icontains(property.toString(), value); + } +} diff --git a/ebean-api/src/main/java/io/ebean/UpdateQuery.java b/ebean-api/src/main/java/io/ebean/UpdateQuery.java index bb293c748..a4a874328 100644 --- a/ebean-api/src/main/java/io/ebean/UpdateQuery.java +++ b/ebean-api/src/main/java/io/ebean/UpdateQuery.java @@ -118,7 +118,7 @@ public interface UpdateQuery { * @param property The bean property to be set * @param value The value to set the property to */ - UpdateQuery set(Query.Property property, Object value); +

UpdateQuery set(Query.Property

property, P value); /** * Set the property to be null. diff --git a/ebean-api/src/test/java/io/ebean/StdFunctionsTest.java b/ebean-api/src/test/java/io/ebean/StdFunctionsTest.java index 4cce8ed1a..7c48d7fe7 100644 --- a/ebean-api/src/test/java/io/ebean/StdFunctionsTest.java +++ b/ebean-api/src/test/java/io/ebean/StdFunctionsTest.java @@ -4,7 +4,7 @@ import org.junit.jupiter.api.Test; import java.time.LocalDate; -import static io.ebean.StdFunctions.*; +import static io.ebean.StdOperators.*; import static org.assertj.core.api.Assertions.assertThat; class StdFunctionsTest { diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PBaseNumber.java b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseNumber.java index 6e2e2a496..140998bc9 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PBaseNumber.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseNumber.java @@ -7,7 +7,7 @@ package io.ebean.typequery; * @param the number type */ @SuppressWarnings("rawtypes") -public abstract class PBaseNumber extends PBaseCompareable { +public abstract class PBaseNumber extends PBaseCompareable { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PEnum.java b/ebean-querybean/src/main/java/io/ebean/typequery/PEnum.java index 124263f55..d70ffb3e5 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PEnum.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PEnum.java @@ -6,7 +6,7 @@ package io.ebean.typequery; * @param the enum specific type * @param the root query bean type */ -public final class PEnum extends PBaseValueEqual { +public final class PEnum extends PBaseValueEqual { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/TQRootBean.java b/ebean-querybean/src/main/java/io/ebean/typequery/TQRootBean.java index 422110abc..1126b2026 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/TQRootBean.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/TQRootBean.java @@ -278,7 +278,7 @@ public abstract class TQRootBean { /** * Specify the properties to be loaded on the 'main' root level entity bean - * also allowing for functions to be used like {@link StdFunctions#max(Query.Property)}. + * also allowing for functions to be used like {@link StdOperators#max(Query.Property)}. * * @param properties the list of properties to fetch */ diff --git a/ebean-querybean/src/test/java/org/querytest/QCustomerTest.java b/ebean-querybean/src/test/java/org/querytest/QCustomerTest.java index 2db089274..8808df5d8 100644 --- a/ebean-querybean/src/test/java/org/querytest/QCustomerTest.java +++ b/ebean-querybean/src/test/java/org/querytest/QCustomerTest.java @@ -15,11 +15,13 @@ import org.junit.jupiter.api.Test; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; +import java.sql.Timestamp; import java.time.LocalDate; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Stream; +import static io.ebean.StdOperators.*; import static org.assertj.core.api.Assertions.assertThat; import static org.example.domain.query.QAddress.Alias.country; import static org.example.domain.query.QAddress.Alias.line1; @@ -692,6 +694,32 @@ public class QCustomerTest { assertThat(customerExists).isFalse(); } + @Test + void checkingPropertyTypesToEQOperator() { + + QCustomer c = QCustomer.alias(); + + new QCustomer() + .select(c.version, count(c.id)) + .version.gt(0) + .having() + .add(gt(count(c.id), 1)) + .findList(); + + new QCustomer() + .add(in(QCustomer.Alias.name, List.of("foo", "bar"))) + .add(eq(QCustomer.Alias.currentInet, Inet.of("asd").toString())) + .findList(); + + new QCustomer() + //.add(gt(sum(QCustomer.Alias.version), 45)) + .add(eq(QCustomer.Alias.version, 45L)) + .add(eq(QCustomer.Alias.name, "junk")) + .add(eq(QCustomer.Alias.registered, new Date())) + .add(eq(QCustomer.Alias.whenUpdated, new Timestamp(System.currentTimeMillis()))) + .findList(); + } + @Test public void testQuery() { diff --git a/ebean-querybean/src/test/java/org/querytest/QOrderTest.java b/ebean-querybean/src/test/java/org/querytest/QOrderTest.java index 2ed846c0a..16e2c47e4 100644 --- a/ebean-querybean/src/test/java/org/querytest/QOrderTest.java +++ b/ebean-querybean/src/test/java/org/querytest/QOrderTest.java @@ -17,9 +17,7 @@ import org.junit.jupiter.api.Test; import java.util.List; -import static io.ebean.StdFunctions.gt; -import static io.ebean.StdFunctions.ilike; -import static io.ebean.StdFunctions.*; +import static io.ebean.StdOperators.*; import static org.assertj.core.api.Assertions.assertThat; public class QOrderTest { @@ -227,7 +225,7 @@ public class QOrderTest { .status.eq(Order.Status.COMPLETE) .orderDate.gt(new java.sql.Date(System.currentTimeMillis())) .asUpdate() - .set(QOrder.Alias.version, 42L) + .set(QOrder.Alias.version, 56L) .setNull(QOrder.Alias.orderDate) .update(); @@ -258,7 +256,7 @@ public class QOrderTest { Query query = new QOrder() .select(o.id, o.status) .or() - .add(gt(coalesce(o.customer.version, 0), 42)) + .add(gt(coalesce(o.customer.version, 0), 42L)) .id.lt(12) .endOr() .query(); diff --git a/ebean-test/src/test/java/org/tests/query/TestStdFunctions.java b/ebean-test/src/test/java/org/tests/query/TestStdFunctions.java index 940af117a..d75bf14a6 100644 --- a/ebean-test/src/test/java/org/tests/query/TestStdFunctions.java +++ b/ebean-test/src/test/java/org/tests/query/TestStdFunctions.java @@ -5,7 +5,7 @@ import io.ebean.Query; import org.junit.jupiter.api.Test; import org.tests.model.basic.Customer; -import static io.ebean.StdFunctions.*; +import static io.ebean.StdOperators.*; import static org.assertj.core.api.Assertions.assertThat; /** From 3c8c52ca7f3c284f8e3e22fae0e1c4f4bda02878 Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Wed, 9 Nov 2022 23:30:51 +1300 Subject: [PATCH 08/11] Simplify to use single type (not use another different type for Query.Property) --- .../main/java/io/ebean/SimpleProperty.java | 2 +- .../src/main/java/io/ebean/StdLegacyOps.java | 37 ---------- .../src/main/java/io/ebean/StdOperators.java | 53 --------------- ...eCompareable.java => PBaseComparable.java} | 36 +++++----- .../java/io/ebean/typequery/PBaseDate.java | 10 ++- .../java/io/ebean/typequery/PBaseNumber.java | 4 +- .../java/io/ebean/typequery/PBaseString.java | 2 +- .../java/io/ebean/typequery/PBaseTime.java | 4 +- .../io/ebean/typequery/PBaseValueEqual.java | 10 +-- .../java/io/ebean/typequery/PBoolean.java | 2 +- .../java/io/ebean/typequery/PByteArray.java | 2 +- .../java/io/ebean/typequery/PCalendar.java | 2 +- .../main/java/io/ebean/typequery/PEnum.java | 2 +- .../java/io/ebean/typequery/PInstant.java | 2 +- .../io/ebean/typequery/PJodaDateMidnight.java | 2 +- .../io/ebean/typequery/PJodaDateTime.java | 2 +- .../io/ebean/typequery/PJodaLocalDate.java | 2 +- .../ebean/typequery/PJodaLocalDateTime.java | 2 +- .../main/java/io/ebean/typequery/PJson.java | 16 ++--- .../java/io/ebean/typequery/PLocalDate.java | 2 +- .../io/ebean/typequery/PLocalDateTime.java | 2 +- .../main/java/io/ebean/typequery/PMonth.java | 2 +- .../java/io/ebean/typequery/PMonthDay.java | 2 +- .../io/ebean/typequery/POffsetDateTime.java | 2 +- .../main/java/io/ebean/typequery/PScalar.java | 2 +- .../io/ebean/typequery/PScalarComparable.java | 2 +- .../java/io/ebean/typequery/PSqlDate.java | 2 +- .../main/java/io/ebean/typequery/PString.java | 2 +- .../java/io/ebean/typequery/PTimestamp.java | 2 +- .../java/io/ebean/typequery/PUtilDate.java | 2 +- .../main/java/io/ebean/typequery/PUuid.java | 2 +- .../java/io/ebean/typequery/PYearMonth.java | 2 +- .../java/io/ebean/typequery/TQProperty.java | 6 +- .../io/ebean/typequery/TQPropertyBase.java | 6 +- .../test/java/org/example/domain/Country.java | 67 ++++++++++--------- .../java/org/querytest/QCustomerTest.java | 10 +-- .../test/java/org/querytest/QOrderTest.java | 4 +- 37 files changed, 106 insertions(+), 205 deletions(-) delete mode 100644 ebean-api/src/main/java/io/ebean/StdLegacyOps.java rename ebean-querybean/src/main/java/io/ebean/typequery/{PBaseCompareable.java => PBaseComparable.java} (87%) diff --git a/ebean-api/src/main/java/io/ebean/SimpleProperty.java b/ebean-api/src/main/java/io/ebean/SimpleProperty.java index 044f2aa31..d4c3b8a68 100644 --- a/ebean-api/src/main/java/io/ebean/SimpleProperty.java +++ b/ebean-api/src/main/java/io/ebean/SimpleProperty.java @@ -1,6 +1,6 @@ package io.ebean; -final class SimpleProperty implements Query.Property { +final class SimpleProperty implements Query.Property { private final String expression; diff --git a/ebean-api/src/main/java/io/ebean/StdLegacyOps.java b/ebean-api/src/main/java/io/ebean/StdLegacyOps.java deleted file mode 100644 index c8da08b0a..000000000 --- a/ebean-api/src/main/java/io/ebean/StdLegacyOps.java +++ /dev/null @@ -1,37 +0,0 @@ -package io.ebean; - -import io.ebean.Query.Property; - -import java.time.temporal.Temporal; - -@Deprecated(since = "experimental") -public final class StdLegacyOps { - - public static Expression eq(Property property, java.util.Calendar value) { - return Expr.eq(property.toString(), value); - } - - public static Expression eqOrNull(Property property, java.util.Calendar value) { - return Expr.factory().eqOrNull(property.toString(), value); - } - - public static Expression ne(Property property, java.util.Calendar value) { - return Expr.factory().ne(property.toString(), value); - } - - public static Expression lt(Property property, java.util.Calendar value) { - return Expr.factory().lt(property.toString(), value); - } - - public static Expression ltOrNull(Property property, java.util.Calendar value) { - return Expr.factory().ltOrNull(property.toString(), value); - } - - public static Expression le(Property property, java.util.Calendar value) { - return Expr.factory().le(property.toString(), value); - } - - public static Expression leOrNull(Property property, java.util.Calendar value) { - return Expr.factory().leOrNull(property.toString(), value); - } -} diff --git a/ebean-api/src/main/java/io/ebean/StdOperators.java b/ebean-api/src/main/java/io/ebean/StdOperators.java index e049401d5..40bf84916 100644 --- a/ebean-api/src/main/java/io/ebean/StdOperators.java +++ b/ebean-api/src/main/java/io/ebean/StdOperators.java @@ -2,7 +2,6 @@ package io.ebean; import io.ebean.Query.Property; -import java.time.temporal.Temporal; import java.util.Collection; @Deprecated(since = "experimental") @@ -79,18 +78,10 @@ public final class StdOperators { return Expr.in(property.toString(), subQuery); } - public static Expression eq(Property property, java.util.Date value) { - return Expr.eq(property.toString(), value); - } - public static Expression eqOrNull(Property property, T value) { return Expr.factory().eqOrNull(property.toString(), value); } - public static Expression eqOrNull(Property property, java.util.Date value) { - return Expr.factory().eqOrNull(property.toString(), value); - } - public static Expression ne(Property property, T value) { return Expr.ne(property.toString(), value); } @@ -99,10 +90,6 @@ public final class StdOperators { return Expr.ne(property.toString(), subQuery); } - public static Expression ne(Property property, java.util.Date value) { - return Expr.ne(property.toString(), value); - } - public static Expression gt(Property property, T value) { return Expr.gt(property.toString(), value); } @@ -111,18 +98,10 @@ public final class StdOperators { return Expr.gt(property.toString(), subQuery); } - public static Expression gt(Property property, java.util.Date value) { - return Expr.gt(property.toString(), value); - } - public static Expression gtOrNull(Property property, T value) { return Expr.factory().gtOrNull(property.toString(), value); } - public static Expression gtOrNull(Property property, java.util.Date value) { - return Expr.factory().gtOrNull(property.toString(), value); - } - public static Expression ge(Property property, T value) { return Expr.ge(property.toString(), value); } @@ -131,18 +110,10 @@ public final class StdOperators { return Expr.ge(property.toString(), subQuery); } - public static Expression ge(Property property, java.util.Date value) { - return Expr.ge(property.toString(), value); - } - public static Expression geOrNull(Property property, T value) { return Expr.factory().geOrNull(property.toString(), value); } - public static Expression geOrNull(Property property, java.util.Date value) { - return Expr.factory().geOrNull(property.toString(), value); - } - public static Expression lt(Property property, T value) { return Expr.lt(property.toString(), value); } @@ -151,18 +122,10 @@ public final class StdOperators { return Expr.lt(property.toString(), subQuery); } - public static Expression lt(Property property, java.util.Date value) { - return Expr.lt(property.toString(), value); - } - public static Expression ltOrNull(Property property, T value) { return Expr.factory().ltOrNull(property.toString(), value); } - public static Expression ltOrNull(Property property, java.util.Date value) { - return Expr.factory().ltOrNull(property.toString(), value); - } - public static Expression le(Property property, T value) { return Expr.le(property.toString(), value); } @@ -171,34 +134,18 @@ public final class StdOperators { return Expr.le(property.toString(), subQuery); } - public static Expression le(Property property, java.util.Date value) { - return Expr.le(property.toString(), value); - } - public static Expression leOrNull(Property property, T value) { return Expr.factory().leOrNull(property.toString(), value); } - public static Expression leOrNull(Property property, java.util.Date value) { - return Expr.factory().leOrNull(property.toString(), value); - } - public static Expression inRange(Property property, T lowValue, T highValue) { return Expr.factory().inRange(property.toString(), lowValue, highValue); } - public static Expression inRange(Property property, java.util.Date lowValue, java.util.Date highValue) { - return Expr.factory().inRange(property.toString(), lowValue, highValue); - } - public static Expression inRange(Property lowProperty, Property highProperty, T value) { return Expr.factory().inRangeWith(lowProperty.toString(), highProperty.toString(), value); } - public static Expression inRange(Property lowProperty, Property highProperty, java.util.Date value) { - return Expr.factory().inRangeWith(lowProperty.toString(), highProperty.toString(), value); - } - public static Expression inRange(Property lowProperty, Property property, Property highProperty) { return Expr.factory().inRangeWithProperties(lowProperty.toString(), property.toString(), highProperty.toString()); } diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PBaseCompareable.java b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseComparable.java similarity index 87% rename from ebean-querybean/src/main/java/io/ebean/typequery/PBaseCompareable.java rename to ebean-querybean/src/main/java/io/ebean/typequery/PBaseComparable.java index 1faa6d7aa..5a31eded7 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PBaseCompareable.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseComparable.java @@ -6,11 +6,10 @@ import io.ebean.Query; /** * Base property for all comparable types. * - * @param the root query bean type - * @param the type of the scalar property - * @param the base type of the property + * @param the root query bean type + * @param the type of the scalar property */ -public abstract class PBaseCompareable extends PBaseValueEqual { +public abstract class PBaseComparable extends PBaseValueEqual { /** * Construct with a property name and root instance. @@ -18,14 +17,14 @@ public abstract class PBaseCompareable extends PBaseValueEqual extends PBaseValueEqual other) { - expr().raw(_name + " > " + other.propertyName()); + public final R gt(Query.Property other) { + expr().raw(_name + " > " + other.toString()); return _root; } @@ -84,8 +82,8 @@ public abstract class PBaseCompareable extends PBaseValueEqual other) { - expr().raw(_name + " >= " + other.propertyName()); + public final R ge(Query.Property other) { + expr().raw(_name + " >= " + other.toString()); return _root; } @@ -117,8 +115,8 @@ public abstract class PBaseCompareable extends PBaseValueEqual other) { - expr().raw(_name + " < " + other.propertyName()); + public final R lt(Query.Property other) { + expr().raw(_name + " < " + other.toString()); return _root; } @@ -150,8 +148,8 @@ public abstract class PBaseCompareable extends PBaseValueEqual other) { - expr().raw(_name + " <= " + other.propertyName()); + public final R le(Query.Property other) { + expr().raw(_name + " <= " + other.toString()); return _root; } @@ -200,8 +198,8 @@ public abstract class PBaseCompareable extends PBaseValueEqual highProperty, T value) { - expr().inRangeWith(_name, highProperty._name, value); + public final R inRangeWith(Query.Property highProperty, T value) { + expr().inRangeWith(_name, highProperty.toString(), value); return _root; } @@ -223,8 +221,8 @@ public abstract class PBaseCompareable extends PBaseValueEqual * This is a convenience expression combining a number of simple expressions. */ - public final R inRangeWith(TQProperty lowProperty, TQProperty highProperty) { - expr().inRangeWithProperties(_name, lowProperty.propertyName(), highProperty.propertyName()); + public final R inRangeWith(Query.Property lowProperty, Query.Property highProperty) { + expr().inRangeWithProperties(_name, lowProperty.toString(), highProperty.toString()); return _root; } diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PBaseDate.java b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseDate.java index 8759baa7b..9fd3baa9d 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PBaseDate.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseDate.java @@ -1,15 +1,13 @@ package io.ebean.typequery; -import java.time.temporal.Temporal; - /** * Base property for date and date time types. * * @param the root query bean type - * @param the date time type + * @param the scalar type */ @SuppressWarnings("rawtypes") -public abstract class PBaseDate extends PBaseCompareable { +public abstract class PBaseDate extends PBaseComparable { /** * Construct with a property name and root instance. @@ -34,7 +32,7 @@ public abstract class PBaseDate extends PBaseCompareabl * @param value the equal to bind value * @return the root query bean instance */ - public final R after(D value) { + public final R after(T value) { expr().gt(_name, value); return _root; } @@ -45,7 +43,7 @@ public abstract class PBaseDate extends PBaseCompareabl * @param value the equal to bind value * @return the root query bean instance */ - public final R before(D value) { + public final R before(T value) { expr().lt(_name, value); return _root; } diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PBaseNumber.java b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseNumber.java index 140998bc9..03b3d37ea 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PBaseNumber.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseNumber.java @@ -4,10 +4,10 @@ package io.ebean.typequery; * Base property for number types. * * @param the root query bean type - * @param the number type + * @param the property type */ @SuppressWarnings("rawtypes") -public abstract class PBaseNumber extends PBaseCompareable { +public abstract class PBaseNumber extends PBaseComparable { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PBaseString.java b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseString.java index fb65af6ad..aaf62c6bb 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PBaseString.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseString.java @@ -5,7 +5,7 @@ package io.ebean.typequery; * * @param the root query bean type */ -public abstract class PBaseString extends PBaseCompareable { +public abstract class PBaseString extends PBaseComparable { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PBaseTime.java b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseTime.java index 54daaae41..f338ed2f1 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PBaseTime.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseTime.java @@ -1,7 +1,5 @@ package io.ebean.typequery; -import java.time.temporal.Temporal; - /** * Base property for time types. * @@ -9,7 +7,7 @@ import java.time.temporal.Temporal; * @param the number type */ @SuppressWarnings("rawtypes") -public abstract class PBaseTime extends PBaseCompareable { +public abstract class PBaseTime extends PBaseComparable { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PBaseValueEqual.java b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseValueEqual.java index 049ecac26..1f25c916e 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PBaseValueEqual.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseValueEqual.java @@ -11,7 +11,7 @@ import java.util.Collection; * @param the root query bean type * @param the number type */ -public abstract class PBaseValueEqual extends TQPropertyBase { +public abstract class PBaseValueEqual extends TQPropertyBase { /** * Construct with a property name and root instance. @@ -89,8 +89,8 @@ public abstract class PBaseValueEqual extends TQPropertyBase { * @param other the other property to compare * @return the root query bean instance */ - public final R eq(TQProperty other) { - expr().raw(_name + " = " + other.propertyName()); + public final R eq(Query.Property other) { + expr().raw(_name + " = " + other.toString()); return _root; } @@ -152,8 +152,8 @@ public abstract class PBaseValueEqual extends TQPropertyBase { * @param other the other property to compare * @return the root query bean instance */ - public final R ne(TQProperty other) { - expr().raw(_name + " <> " + other.propertyName()); + public final R ne(Query.Property other) { + expr().raw(_name + " <> " + other.toString()); return _root; } diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PBoolean.java b/ebean-querybean/src/main/java/io/ebean/typequery/PBoolean.java index 370cce8a7..55707300e 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PBoolean.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PBoolean.java @@ -5,7 +5,7 @@ package io.ebean.typequery; * * @param the root query bean type */ -public final class PBoolean extends PBaseValueEqual { +public final class PBoolean extends PBaseValueEqual { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PByteArray.java b/ebean-querybean/src/main/java/io/ebean/typequery/PByteArray.java index ce275eeda..a94f8bbc1 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PByteArray.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PByteArray.java @@ -5,7 +5,7 @@ package io.ebean.typequery; * * @param the root query bean type */ -public final class PByteArray extends PBaseValueEqual { +public final class PByteArray extends PBaseValueEqual { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PCalendar.java b/ebean-querybean/src/main/java/io/ebean/typequery/PCalendar.java index b64ae4f2c..7aa9d9f3f 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PCalendar.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PCalendar.java @@ -7,7 +7,7 @@ import java.util.Calendar; * * @param the root query bean type */ -public final class PCalendar extends PBaseDate { +public final class PCalendar extends PBaseDate { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PEnum.java b/ebean-querybean/src/main/java/io/ebean/typequery/PEnum.java index d70ffb3e5..1659591b0 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PEnum.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PEnum.java @@ -6,7 +6,7 @@ package io.ebean.typequery; * @param the enum specific type * @param the root query bean type */ -public final class PEnum extends PBaseValueEqual { +public final class PEnum extends PBaseValueEqual { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PInstant.java b/ebean-querybean/src/main/java/io/ebean/typequery/PInstant.java index 3387668e0..3a2980d44 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PInstant.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PInstant.java @@ -8,7 +8,7 @@ import java.time.Instant; * * @param the root query bean type */ -public final class PInstant extends PBaseDate { +public final class PInstant extends PBaseDate { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PJodaDateMidnight.java b/ebean-querybean/src/main/java/io/ebean/typequery/PJodaDateMidnight.java index c6f7051e0..a9f63dc7f 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PJodaDateMidnight.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PJodaDateMidnight.java @@ -7,7 +7,7 @@ import org.joda.time.DateMidnight; * * @param the root query bean type */ -public final class PJodaDateMidnight extends PBaseDate { +public final class PJodaDateMidnight extends PBaseDate { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PJodaDateTime.java b/ebean-querybean/src/main/java/io/ebean/typequery/PJodaDateTime.java index 81132e2a2..aee165038 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PJodaDateTime.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PJodaDateTime.java @@ -7,7 +7,7 @@ import org.joda.time.DateTime; * * @param the root query bean type */ -public final class PJodaDateTime extends PBaseDate { +public final class PJodaDateTime extends PBaseDate { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PJodaLocalDate.java b/ebean-querybean/src/main/java/io/ebean/typequery/PJodaLocalDate.java index e6a5f4d52..7649a6908 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PJodaLocalDate.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PJodaLocalDate.java @@ -7,7 +7,7 @@ import org.joda.time.LocalDate; * * @param the root query bean type */ -public final class PJodaLocalDate extends PBaseDate { +public final class PJodaLocalDate extends PBaseDate { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PJodaLocalDateTime.java b/ebean-querybean/src/main/java/io/ebean/typequery/PJodaLocalDateTime.java index bad5359e3..5a506f1b5 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PJodaLocalDateTime.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PJodaLocalDateTime.java @@ -7,7 +7,7 @@ import org.joda.time.LocalDateTime; * * @param the root query bean type */ -public final class PJodaLocalDateTime extends PBaseDate { +public final class PJodaLocalDateTime extends PBaseDate { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PJson.java b/ebean-querybean/src/main/java/io/ebean/typequery/PJson.java index 546964ed9..2c4fbf908 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PJson.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PJson.java @@ -5,15 +5,11 @@ package io.ebean.typequery; * JSON document type. *

* Type that is JSON content mapped to database types such as Postgres JSON/JSONB and otherwise Varchar,Clob and Blob. - *

*

* The expressions on this type are valid of Postgres and Oracle. - *

- * *

* The path can reference a nested property in the JSON document using dot notation - * for example "documentMeta.score" where "score" is an embedded attribute of "documentMeta" - *

* * @param the root query bean type */ @@ -92,7 +88,7 @@ public final class PJson extends TQPropertyBase { * * } * - * @param path the dot notation path in the JSON document + * @param path the dot notation path in the JSON document * @param value the equal to bind value */ public R jsonEqualTo(String path, Object value) { @@ -103,7 +99,7 @@ public final class PJson extends TQPropertyBase { /** * Not Equal to - for the given path in a JSON document. * - * @param path the nested path in the JSON document in dot notation + * @param path the nested path in the JSON document in dot notation * @param value the value used to test equality against the document path's value */ public R jsonNotEqualTo(String path, Object value) { @@ -114,7 +110,7 @@ public final class PJson extends TQPropertyBase { /** * Greater than - for the given path in a JSON document. * - * @param path the nested path in the JSON document in dot notation + * @param path the nested path in the JSON document in dot notation * @param value the value used to test against the document path's value */ public R jsonGreaterThan(String path, Object value) { @@ -125,7 +121,7 @@ public final class PJson extends TQPropertyBase { /** * Greater than or equal to - for the given path in a JSON document. * - * @param path the nested path in the JSON document in dot notation + * @param path the nested path in the JSON document in dot notation * @param value the value used to test against the document path's value */ public R jsonGreaterOrEqual(String path, Object value) { @@ -136,7 +132,7 @@ public final class PJson extends TQPropertyBase { /** * Less than - for the given path in a JSON document. * - * @param path the nested path in the JSON document in dot notation + * @param path the nested path in the JSON document in dot notation * @param value the value used to test against the document path's value */ public R jsonLessThan(String path, Object value) { @@ -147,7 +143,7 @@ public final class PJson extends TQPropertyBase { /** * Less than or equal to - for the given path in a JSON document. * - * @param path the nested path in the JSON document in dot notation + * @param path the nested path in the JSON document in dot notation * @param value the value used to test against the document path's value */ public R jsonLessOrEqualTo(String path, Object value) { diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PLocalDate.java b/ebean-querybean/src/main/java/io/ebean/typequery/PLocalDate.java index 189b76eba..12e49070c 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PLocalDate.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PLocalDate.java @@ -7,7 +7,7 @@ import java.time.LocalDate; * * @param the root query bean type */ -public final class PLocalDate extends PBaseDate { +public final class PLocalDate extends PBaseDate { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PLocalDateTime.java b/ebean-querybean/src/main/java/io/ebean/typequery/PLocalDateTime.java index feb74e659..59c7ecb61 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PLocalDateTime.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PLocalDateTime.java @@ -7,7 +7,7 @@ import java.time.LocalDateTime; * * @param the root query bean type */ -public final class PLocalDateTime extends PBaseDate { +public final class PLocalDateTime extends PBaseDate { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PMonth.java b/ebean-querybean/src/main/java/io/ebean/typequery/PMonth.java index 0d127d7e7..f3b0ea4f8 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PMonth.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PMonth.java @@ -7,7 +7,7 @@ import java.time.Month; * * @param the root query bean type */ -public final class PMonth extends PBaseDate { +public final class PMonth extends PBaseDate { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PMonthDay.java b/ebean-querybean/src/main/java/io/ebean/typequery/PMonthDay.java index 27b133f47..16c41f349 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PMonthDay.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PMonthDay.java @@ -7,7 +7,7 @@ import java.time.MonthDay; * * @param the root query bean type */ -public final class PMonthDay extends PBaseDate { +public final class PMonthDay extends PBaseDate { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/POffsetDateTime.java b/ebean-querybean/src/main/java/io/ebean/typequery/POffsetDateTime.java index 88ef6875f..f1d06da4a 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/POffsetDateTime.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/POffsetDateTime.java @@ -7,7 +7,7 @@ import java.time.OffsetDateTime; * * @param the root query bean type */ -public final class POffsetDateTime extends PBaseDate { +public final class POffsetDateTime extends PBaseDate { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PScalar.java b/ebean-querybean/src/main/java/io/ebean/typequery/PScalar.java index 6318a1b8a..390923fd0 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PScalar.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PScalar.java @@ -6,7 +6,7 @@ package io.ebean.typequery; * @param the root query bean type * @param the scalar type */ -public final class PScalar extends PBaseValueEqual { +public final class PScalar extends PBaseValueEqual { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PScalarComparable.java b/ebean-querybean/src/main/java/io/ebean/typequery/PScalarComparable.java index a303e6530..38fc6947a 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PScalarComparable.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PScalarComparable.java @@ -8,7 +8,7 @@ package io.ebean.typequery; * @param the root query bean type * @param the scalar type */ -public final class PScalarComparable> extends PBaseCompareable { +public final class PScalarComparable> extends PBaseComparable { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PSqlDate.java b/ebean-querybean/src/main/java/io/ebean/typequery/PSqlDate.java index a935c9212..f69cfb236 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PSqlDate.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PSqlDate.java @@ -7,7 +7,7 @@ import java.sql.Date; * * @param the root query bean type */ -public final class PSqlDate extends PBaseDate { +public final class PSqlDate extends PBaseDate { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PString.java b/ebean-querybean/src/main/java/io/ebean/typequery/PString.java index a24cf1a9b..5903ae827 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PString.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PString.java @@ -5,7 +5,7 @@ package io.ebean.typequery; * * @param the root query bean type */ -public final class PString extends PBaseCompareable { +public final class PString extends PBaseComparable { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PTimestamp.java b/ebean-querybean/src/main/java/io/ebean/typequery/PTimestamp.java index ace329987..319425f93 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PTimestamp.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PTimestamp.java @@ -7,7 +7,7 @@ import java.sql.Timestamp; * * @param the root query bean type */ -public final class PTimestamp extends PBaseDate { +public final class PTimestamp extends PBaseDate { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PUtilDate.java b/ebean-querybean/src/main/java/io/ebean/typequery/PUtilDate.java index 49c410465..1e6f483cb 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PUtilDate.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PUtilDate.java @@ -7,7 +7,7 @@ import java.util.Date; * * @param the root query bean type */ -public final class PUtilDate extends PBaseDate { +public final class PUtilDate extends PBaseDate { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PUuid.java b/ebean-querybean/src/main/java/io/ebean/typequery/PUuid.java index ddf7366f2..e0f4e0d85 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PUuid.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PUuid.java @@ -7,7 +7,7 @@ import java.util.UUID; * * @param the root query bean type */ -public final class PUuid extends PBaseValueEqual { +public final class PUuid extends PBaseValueEqual { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PYearMonth.java b/ebean-querybean/src/main/java/io/ebean/typequery/PYearMonth.java index 77a34d70b..7f4196a59 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PYearMonth.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PYearMonth.java @@ -7,7 +7,7 @@ import java.time.YearMonth; * * @param the root query bean type */ -public final class PYearMonth extends PBaseDate { +public final class PYearMonth extends PBaseDate { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/TQProperty.java b/ebean-querybean/src/main/java/io/ebean/typequery/TQProperty.java index 79c4a2cc5..8fbb9c5dc 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/TQProperty.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/TQProperty.java @@ -6,10 +6,10 @@ import io.ebean.Query; /** * A property used in type query. * - * @param The type of the owning root bean - * @param The base type, one of String, Number, Temporal, Boolean, Object. + * @param The type of the owning root bean + * @param The property type */ -public class TQProperty implements Query.Property { +public class TQProperty implements Query.Property { protected final String _name; protected final R _root; diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/TQPropertyBase.java b/ebean-querybean/src/main/java/io/ebean/typequery/TQPropertyBase.java index 2a3937be4..e6123fa15 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/TQPropertyBase.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/TQPropertyBase.java @@ -3,10 +3,10 @@ package io.ebean.typequery; /** * Base scalar property. * - * @param The type of the owning root bean - * @param The base type of the property + * @param The type of the owning root bean + * @param The property type */ -public abstract class TQPropertyBase extends TQProperty { +public abstract class TQPropertyBase extends TQProperty { /** * Construct with a property name and root instance. diff --git a/ebean-querybean/src/test/java/org/example/domain/Country.java b/ebean-querybean/src/test/java/org/example/domain/Country.java index 30a826e00..07b5dace5 100644 --- a/ebean-querybean/src/test/java/org/example/domain/Country.java +++ b/ebean-querybean/src/test/java/org/example/domain/Country.java @@ -3,20 +3,21 @@ package org.example.domain; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; +import java.time.Instant; +import java.time.OffsetDateTime; /** * Country entity bean. */ @Entity -@Table(name="o_country") +@Table(name = "o_country") public class Country { - @Id - //@Size(max=2) - String code; - - //@Size(max=60) - String name; + @Id + String code; + String name; + Instant whenCreated; + OffsetDateTime offsetTimestamp; public Country(String code, String name) { this.code = code; @@ -24,36 +25,36 @@ public class Country { } public String toString() { - return code; - } + return code; + } - /** - * Return code. - */ - public String getCode() { - return code; - } + /** + * Return code. + */ + public String getCode() { + return code; + } - /** - * Set code. - */ - public void setCode(String code) { - this.code = code; - } + /** + * Set code. + */ + public void setCode(String code) { + this.code = code; + } - /** - * Return name. - */ - public String getName() { - return name; - } + /** + * Return name. + */ + public String getName() { + return name; + } - /** - * Set name. - */ - public void setName(String name) { - this.name = name; - } + /** + * Set name. + */ + public void setName(String name) { + this.name = name; + } } diff --git a/ebean-querybean/src/test/java/org/querytest/QCustomerTest.java b/ebean-querybean/src/test/java/org/querytest/QCustomerTest.java index 8808df5d8..e93488d37 100644 --- a/ebean-querybean/src/test/java/org/querytest/QCustomerTest.java +++ b/ebean-querybean/src/test/java/org/querytest/QCustomerTest.java @@ -456,7 +456,7 @@ public class QCustomerTest { assertContains(new QCustomer().registered.lt(new Date()).query(), " where t0.registered < ?"); assertContains(new QCustomer().registered.before(new Date()).query(), " where t0.registered < ?"); assertContains(new QCustomer().registered.lessThan(new Date()).query(), " where t0.registered < ?"); - assertContains(new QCustomer().registered.lt(QCustomer.Alias.whenUpdated).query(), " where t0.registered < t0.when_updated"); + assertContains(new QCustomer().whenCreated.lt(QCustomer.Alias.whenUpdated).query(), " where t0.when_created < t0.when_updated"); } @Test @@ -464,7 +464,7 @@ public class QCustomerTest { assertContains(new QCustomer().registered.le(new Date()).query(), " where t0.registered <= ?"); assertContains(new QCustomer().registered.lessOrEqualTo(new Date()).query(), " where t0.registered <= ?"); - assertContains(new QCustomer().registered.le(QCustomer.Alias.whenUpdated).query(), " where t0.registered <= t0.when_updated"); + assertContains(new QCustomer().whenCreated.le(QCustomer.Alias.whenUpdated).query(), " where t0.when_created <= t0.when_updated"); } @Test @@ -472,7 +472,7 @@ public class QCustomerTest { assertContains(new QCustomer().registered.after(new Date()).query(), " where t0.registered > ?"); assertContains(new QCustomer().registered.gt(new Date()).query(), " where t0.registered > ?"); - assertContains(new QCustomer().registered.gt(QCustomer.Alias.whenUpdated).query(), " where t0.registered > t0.when_updated"); + assertContains(new QCustomer().whenCreated.gt(QCustomer.Alias.whenUpdated).query(), " where t0.when_created > t0.when_updated"); assertContains(new QCustomer().registered.greaterThan(new Date()).query(), " where t0.registered > ?"); } @@ -482,7 +482,7 @@ public class QCustomerTest { assertContains(new QCustomer().registered.ge(new Date()).query(), " where t0.registered >= ?"); assertContains(new QCustomer().registered.greaterOrEqualTo(new Date()).query(), " where t0.registered >= ?"); - assertContains(new QCustomer().registered.ge(QCustomer.Alias.whenUpdated).query(), " where t0.registered >= t0.when_updated"); + assertContains(new QCustomer().whenCreated.ge(QCustomer.Alias.whenUpdated).query(), " where t0.when_created >= t0.when_updated"); } private void assertContains(Query query, String match) { @@ -708,7 +708,7 @@ public class QCustomerTest { new QCustomer() .add(in(QCustomer.Alias.name, List.of("foo", "bar"))) - .add(eq(QCustomer.Alias.currentInet, Inet.of("asd").toString())) + .add(eq(QCustomer.Alias.currentInet, Inet.of("127.0.0.1"))) .findList(); new QCustomer() diff --git a/ebean-querybean/src/test/java/org/querytest/QOrderTest.java b/ebean-querybean/src/test/java/org/querytest/QOrderTest.java index 16e2c47e4..935327359 100644 --- a/ebean-querybean/src/test/java/org/querytest/QOrderTest.java +++ b/ebean-querybean/src/test/java/org/querytest/QOrderTest.java @@ -364,11 +364,11 @@ public class QOrderTest { @Test void propertyCompare() { Query query = new QOrder() - .orderDate.lt(QOrder.Alias.customer.registered) + .orderDate.lt(QOrder.Alias.shipDate) // have to be the exact same type, in this case java.sql.Date .query(); query.findList(); - assertThat(query.getGeneratedSql()).contains(" from o_order t0 join be_customer t1 on t1.id = t0.customer_id where t0.order_date < t1.registered"); + assertThat(query.getGeneratedSql()).contains(" from o_order t0 where t0.order_date < t0.ship_date"); } @Test From 4a0f8931a02b562ccd3f70eefc7e7fc88a774f63 Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Wed, 9 Nov 2022 23:41:42 +1300 Subject: [PATCH 09/11] Add parameter type for Query.Property with DefaultUpdateQuery and TQRootBean --- .../io/ebeaninternal/server/querydefn/DefaultUpdateQuery.java | 4 ++-- .../src/main/java/io/ebean/typequery/TQRootBean.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/querydefn/DefaultUpdateQuery.java b/ebean-core/src/main/java/io/ebeaninternal/server/querydefn/DefaultUpdateQuery.java index 9b67c22dd..2f062632d 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/querydefn/DefaultUpdateQuery.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/querydefn/DefaultUpdateQuery.java @@ -36,12 +36,12 @@ public final class DefaultUpdateQuery implements UpdateQuery { } @Override - public UpdateQuery set(Query.Property property, Object value) { + public

UpdateQuery set(Query.Property

property, P value) { return set(property.toString(), value); } @Override - public UpdateQuery setNull(Query.Property property) { + public UpdateQuery setNull(Query.Property property) { return setNull(property.toString()); } diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/TQRootBean.java b/ebean-querybean/src/main/java/io/ebean/typequery/TQRootBean.java index 1126b2026..01d3d1ccb 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/TQRootBean.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/TQRootBean.java @@ -282,7 +282,7 @@ public abstract class TQRootBean { * * @param properties the list of properties to fetch */ - public final R select(Query.Property... properties) { + public final R select(Query.Property... properties) { ((SpiQueryFetch) query).selectProperties(properties(properties)); return root; } From 4c0144de6170667252998811277653b95b5e23e9 Mon Sep 17 00:00:00 2001 From: rob Date: Tue, 14 Feb 2023 11:33:28 +1300 Subject: [PATCH 10/11] Fix SqlOperators for sql injection --- .../src/main/java/io/ebean/StdOperators.java | 148 +++++++++++++++++- .../test/java/io/ebean/StdFunctionsTest.java | 9 ++ 2 files changed, 150 insertions(+), 7 deletions(-) diff --git a/ebean-api/src/main/java/io/ebean/StdOperators.java b/ebean-api/src/main/java/io/ebean/StdOperators.java index 40bf84916..1ab0c55cf 100644 --- a/ebean-api/src/main/java/io/ebean/StdOperators.java +++ b/ebean-api/src/main/java/io/ebean/StdOperators.java @@ -4,43 +4,75 @@ import io.ebean.Query.Property; import java.util.Collection; +/** + * Standard Operators for use with strongly typed query construction. + *

+ * This is currently deemed to be experimental and subject to change. + */ @Deprecated(since = "experimental") public final class StdOperators { // ---- Functions ---- // + /** + * Sum of the given property. + */ public static Property sum(Property property) { return Property.of("sum(" + property + ")"); } + /** + * Count of the given property. + */ public static Property count(Property property) { return Property.of("count(" + property + ")"); } + /** + * Average of the given property. + */ public static Property avg(Property property) { return Property.of("avg(" + property + ")"); } + /** + * Max of the given property. + */ public static Property max(Property property) { return Property.of("max(" + property + ")"); } + /** + * Min of the given property. + */ public static Property min(Property property) { return Property.of("min(" + property + ")"); } + /** + * Coalesce of the property and value. + */ public static Property coalesce(Property property, Object value) { return Property.of("coalesce(" + property.toString() + "," + sqlValue(value) + ")"); } + /** + * Lower of the given property. + */ public static Property lower(Property property) { return Property.of("lower(" + property + ")"); } + /** + * Upper of the given property. + */ public static Property upper(Property property) { return Property.of("upper(" + property + ")"); } + /** + * Concat of the given property and values or other properties. + */ public static Property concat(Property property, Object... values) { StringBuilder expression = new StringBuilder(50); expression.append("concat(").append(property.toString()); @@ -52,146 +84,248 @@ public final class StdOperators { } private static String sqlConcatString(Object value) { - String asStr = String.valueOf(value); - return (value instanceof Property || isSqlQuoted(asStr)) ? asStr : "'" + value + "'"; - } - - private static boolean isSqlQuoted(String asStr) { - return asStr.length() > 0 && asStr.charAt(0) == '\''; + if (value instanceof Property) { + return value.toString(); + } else { + return sqlQuote(value); + } } + /** + * Allows numbers to be unquoted. + */ private static String sqlValue(Object value) { if (value instanceof Property || value instanceof Number) { return value.toString(); } else { - return "'" + value + "'"; + return sqlQuote(value); } } + /** + * SQL quoted escaping single quotes. + */ + private static String sqlQuote(Object value) { + return "'" + String.valueOf(value).replace("'", "''") + "'"; + } + // ---- Operators ---- // + /** + * Equal to - for a property and value. + */ public static Expression eq(Property property, T value) { return Expr.eq(property.toString(), value); } + /** + * Equal to - for a property and sub-query. + */ public static Expression eq(Property property, Query subQuery) { return Expr.in(property.toString(), subQuery); } + /** + * Equal to or null - for a property and value. + */ public static Expression eqOrNull(Property property, T value) { return Expr.factory().eqOrNull(property.toString(), value); } + /** + * Not equal to - for a property and value. + */ public static Expression ne(Property property, T value) { return Expr.ne(property.toString(), value); } + /** + * Not equal to - for a property and sub-query. + */ public static Expression ne(Property property, Query subQuery) { return Expr.ne(property.toString(), subQuery); } + /** + * Greater than - for a property and value. + */ public static Expression gt(Property property, T value) { return Expr.gt(property.toString(), value); } + /** + * Greater than - for a property and sub-query. + */ public static Expression gt(Property property, Query subQuery) { return Expr.gt(property.toString(), subQuery); } + /** + * Greater than or null - for a property and value. + */ public static Expression gtOrNull(Property property, T value) { return Expr.factory().gtOrNull(property.toString(), value); } + /** + * Greater than or equal to - for a property and value. + */ public static Expression ge(Property property, T value) { return Expr.ge(property.toString(), value); } + /** + * Greater than or equal to - for a property and sub-query. + */ public static Expression ge(Property property, Query subQuery) { return Expr.ge(property.toString(), subQuery); } + /** + * Greater than or null - for a property and value. + */ public static Expression geOrNull(Property property, T value) { return Expr.factory().geOrNull(property.toString(), value); } + /** + * Less than - for a property and value. + */ public static Expression lt(Property property, T value) { return Expr.lt(property.toString(), value); } + /** + * Less than - for a property and sub-query. + */ public static Expression lt(Property property, Query subQuery) { return Expr.lt(property.toString(), subQuery); } + /** + * Less than or null - for a property and value. + */ public static Expression ltOrNull(Property property, T value) { return Expr.factory().ltOrNull(property.toString(), value); } + /** + * Greater than or equal to - for a property and value. + */ public static Expression le(Property property, T value) { return Expr.le(property.toString(), value); } + /** + * Greater than or equal to - for a property and sub-query. + */ public static Expression le(Property property, Query subQuery) { return Expr.le(property.toString(), subQuery); } + /** + * Greater than or equal to or null - for a property and value. + */ public static Expression leOrNull(Property property, T value) { return Expr.factory().leOrNull(property.toString(), value); } + /** + * In range - for a property and values. + */ public static Expression inRange(Property property, T lowValue, T highValue) { return Expr.factory().inRange(property.toString(), lowValue, highValue); } + /** + * In range - for properties and a value. + */ public static Expression inRange(Property lowProperty, Property highProperty, T value) { return Expr.factory().inRangeWith(lowProperty.toString(), highProperty.toString(), value); } + /** + * In range - for properties. + */ public static Expression inRange(Property lowProperty, Property property, Property highProperty) { return Expr.factory().inRangeWithProperties(lowProperty.toString(), property.toString(), highProperty.toString()); } + /** + * In - for a given property and collection of values. + */ public static Expression in(Property property, Collection value) { return Expr.in(property.toString(), value); } + /** + * In - for a given property and sub-query. + */ public static Expression in(Property property, Query subQuery) { return Expr.in(property.toString(), subQuery); } + /** + * In or empty - for a given property and collection of values. + */ public static Expression inOrEmpty(Property property, Collection value) { return Expr.inOrEmpty(property.toString(), value); } + /** + * Not In - for a given property and collection of values. + */ public static Expression notIn(Property property, Collection value) { return Expr.factory().notIn(property.toString(), value); } + /** + * Not In - for a given property and sub-query. + */ public static Expression notIn(Property property, Query subQuery) { return Expr.factory().notIn(property.toString(), subQuery); } // ---- String operators ---- // + /** + * Like - for a given property and value. + */ public static Expression like(Property property, String value) { return Expr.like(property.toString(), value); } + /** + * Case-insensitive Like - for a given property and value. + */ public static Expression ilike(Property property, String value) { return Expr.ilike(property.toString(), value); } + /** + * Starts with - for a given property and value. + */ public static Expression startsWith(Property property, String value) { return Expr.startsWith(property.toString(), value); } + /** + * Case-insensitive starts with - for a given property and value. + */ public static Expression istartsWith(Property property, String value) { return Expr.istartsWith(property.toString(), value); } + /** + * Contains - for a given property and value. + */ public static Expression contains(Property property, String value) { return Expr.contains(property.toString(), value); } + /** + * Case-insensitive contains - for a given property and value. + */ public static Expression icontains(Property property, String value) { return Expr.icontains(property.toString(), value); } diff --git a/ebean-api/src/test/java/io/ebean/StdFunctionsTest.java b/ebean-api/src/test/java/io/ebean/StdFunctionsTest.java index 7c48d7fe7..a1a4c9891 100644 --- a/ebean-api/src/test/java/io/ebean/StdFunctionsTest.java +++ b/ebean-api/src/test/java/io/ebean/StdFunctionsTest.java @@ -66,4 +66,13 @@ class StdFunctionsTest { assertThat(coalesce(foo, "banana").toString()).isEqualTo("coalesce(foo,'banana')"); } + @Test + void testCoalesceWithQuoted() { + assertThat(coalesce(foo, "ba'nana").toString()).isEqualTo("coalesce(foo,'ba''nana')"); + } + + @Test + void testConcatWithQuoted() { + assertThat(concat(foo, "ba'nana").toString()).isEqualTo("concat(foo,'ba''nana')"); + } } From f694f2946a1b7f142c74f67e901075ba837fdc7f Mon Sep 17 00:00:00 2001 From: rob Date: Tue, 14 Feb 2023 11:34:00 +1300 Subject: [PATCH 11/11] Rename test to StdOperatorsTest --- .../io/ebean/{StdFunctionsTest.java => StdOperatorsTest.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename ebean-api/src/test/java/io/ebean/{StdFunctionsTest.java => StdOperatorsTest.java} (98%) diff --git a/ebean-api/src/test/java/io/ebean/StdFunctionsTest.java b/ebean-api/src/test/java/io/ebean/StdOperatorsTest.java similarity index 98% rename from ebean-api/src/test/java/io/ebean/StdFunctionsTest.java rename to ebean-api/src/test/java/io/ebean/StdOperatorsTest.java index a1a4c9891..52574211d 100644 --- a/ebean-api/src/test/java/io/ebean/StdFunctionsTest.java +++ b/ebean-api/src/test/java/io/ebean/StdOperatorsTest.java @@ -7,7 +7,7 @@ import java.time.LocalDate; import static io.ebean.StdOperators.*; import static org.assertj.core.api.Assertions.assertThat; -class StdFunctionsTest { +class StdOperatorsTest { final Query.Property amount = Query.Property.of("amount"); final Query.Property foo = Query.Property.of("foo");