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 0d0fe1e5e..ad7a955ee 100644 --- a/ebean-api/src/main/java/io/ebean/Query.java +++ b/ebean-api/src/main/java/io/ebean/Query.java @@ -1768,4 +1768,31 @@ 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. + *

+ * The base type determines which {@link StdOperators} can be used on the property. + * + * @param The property type. + */ + 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..d4c3b8a68 --- /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/StdOperators.java b/ebean-api/src/main/java/io/ebean/StdOperators.java new file mode 100644 index 000000000..1ab0c55cf --- /dev/null +++ b/ebean-api/src/main/java/io/ebean/StdOperators.java @@ -0,0 +1,332 @@ +package io.ebean; + +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()); + for (Object value : values) { + expression.append(",").append(sqlConcatString(value)); + } + expression.append(")"); + return Property.of(expression.toString()); + } + + private static String sqlConcatString(Object value) { + 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 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/main/java/io/ebean/UpdateQuery.java b/ebean-api/src/main/java/io/ebean/UpdateQuery.java index 1860fffe9..a4a874328 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, P 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-api/src/test/java/io/ebean/StdOperatorsTest.java b/ebean-api/src/test/java/io/ebean/StdOperatorsTest.java new file mode 100644 index 000000000..52574211d --- /dev/null +++ b/ebean-api/src/test/java/io/ebean/StdOperatorsTest.java @@ -0,0 +1,78 @@ +package io.ebean; + +import org.junit.jupiter.api.Test; + +import java.time.LocalDate; + +import static io.ebean.StdOperators.*; +import static org.assertj.core.api.Assertions.assertThat; + +class StdOperatorsTest { + + 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() { + 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(amount).toString()).isEqualTo("sum(amount)"); + } + + @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')"); + } + + @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')"); + } +} 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 5e952761d..075dbaa76 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 @@ -1963,7 +1962,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()); } } 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..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 @@ -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, P 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/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/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 cc50bd0d0..5a31eded7 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PBaseCompareable.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseComparable.java @@ -9,8 +9,7 @@ import io.ebean.Query; * @param the root query bean type * @param the type of the scalar property */ -@SuppressWarnings("rawtypes") -public class PBaseCompareable extends PBaseValueEqual { +public abstract class PBaseComparable extends PBaseValueEqual { /** * Construct with a property name and root instance. @@ -18,14 +17,14 @@ public class PBaseCompareable extends PBaseValueEqual { * @param name property name * @param root the root query bean instance */ - public PBaseCompareable(String name, R root) { + public PBaseComparable(String name, R root) { super(name, root); } /** * Construct with additional path prefix. */ - public PBaseCompareable(String name, R root, String prefix) { + public PBaseComparable(String name, R root, String prefix) { super(name, root, prefix); } @@ -44,12 +43,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) { - expr().raw(_name + " > " + other.propertyName()); + public final R gt(Query.Property other) { + expr().raw(_name + " > " + other.toString()); return _root; } @@ -81,8 +82,8 @@ public class PBaseCompareable extends PBaseValueEqual { * @param other the other property to compare * @return the root query bean instance */ - public final R ge(TQProperty other) { - expr().raw(_name + " >= " + other.propertyName()); + public final R ge(Query.Property other) { + expr().raw(_name + " >= " + other.toString()); return _root; } @@ -114,8 +115,8 @@ public class PBaseCompareable extends PBaseValueEqual { * @param other the other property to compare * @return the root query bean instance */ - public final R lt(TQProperty other) { - expr().raw(_name + " < " + other.propertyName()); + public final R lt(Query.Property other) { + expr().raw(_name + " < " + other.toString()); return _root; } @@ -147,8 +148,8 @@ public class PBaseCompareable extends PBaseValueEqual { * @param other the other property to compare * @return the root query bean instance */ - public final R le(TQProperty other) { - expr().raw(_name + " <= " + other.propertyName()); + public final R le(Query.Property other) { + expr().raw(_name + " <= " + other.toString()); return _root; } @@ -197,8 +198,8 @@ 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) { - expr().inRangeWith(_name, highProperty._name, value); + public final R inRangeWith(Query.Property highProperty, T value) { + expr().inRangeWith(_name, highProperty.toString(), value); return _root; } @@ -220,8 +221,8 @@ public 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 740e4f2d2..9fd3baa9d 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PBaseDate.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseDate.java @@ -4,10 +4,10 @@ package io.ebean.typequery; * 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. @@ -32,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; } @@ -43,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 3f1de62d9..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. @@ -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..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. @@ -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..f338ed2f1 --- /dev/null +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseTime.java @@ -0,0 +1,50 @@ +package io.ebean.typequery; + +/** + * Base property for time types. + * + * @param the root query bean type + * @param the number type + */ +@SuppressWarnings("rawtypes") +public abstract class PBaseTime extends PBaseComparable { + + /** + * 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..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/PByteArray.java b/ebean-querybean/src/main/java/io/ebean/typequery/PByteArray.java index d2bc50353..a94f8bbc1 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PByteArray.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PByteArray.java @@ -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/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/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..1659591b0 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/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/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..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,19 +5,15 @@ 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 */ -public final class PJson extends TQPropertyBase { +public final class PJson extends TQPropertyBase { /** * Construct with a property name and root instance. @@ -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/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/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 809248b86..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 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..390923fd0 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PScalar.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PScalar.java @@ -1,8 +1,7 @@ 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 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..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 c37e6e8b2..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/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/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 8f08053eb..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. @@ -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/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/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 364452832..8fbb9c5dc 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/TQProperty.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/TQProperty.java @@ -1,16 +1,17 @@ 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 + * @param The property type */ -public class TQProperty { +public class TQProperty implements Query.Property { protected final String _name; - protected final R _root; /** @@ -31,6 +32,7 @@ public class TQProperty { this._name = TQPath.add(prefix, name); } + @Override public String toString() { return _name; } @@ -39,7 +41,7 @@ public class TQProperty { * 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..e6123fa15 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/TQPropertyBase.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/TQPropertyBase.java @@ -4,8 +4,9 @@ package io.ebean.typequery; * Base scalar property. * * @param The type of the owning root bean + * @param The property type */ -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 5680185fb..01d3d1ccb 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();
    *
    * }
@@ -258,19 +263,30 @@ 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(TQProperty[] properties) { + private Set properties(Query.Property[] properties) { Set props = new LinkedHashSet<>(); - for (TQProperty property : properties) { - props.add(property.propertyName()); + for (Query.Property 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 StdOperators#max(Query.Property)}. + * + * @param properties the list of properties to fetch + */ + public final R select(Query.Property... 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/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 846b1f9e9..0375ace45 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; @@ -454,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 @@ -462,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 @@ -470,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 > ?"); } @@ -480,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) { @@ -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("127.0.0.1"))) + .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 e8563d879..935327359 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.TQProperty; import org.example.domain.Customer; import org.example.domain.Order; import org.example.domain.OrderDetail; @@ -18,6 +17,7 @@ import org.junit.jupiter.api.Test; import java.util.List; +import static io.ebean.StdOperators.*; import static org.assertj.core.api.Assertions.assertThat; public class QOrderTest { @@ -218,14 +218,67 @@ 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, 56L) + .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(); + + // 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), 42L)) + .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 +289,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 +319,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 +349,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() @@ -311,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 @@ -375,9 +428,4 @@ public class QOrderTest { order.save(); } - static class StdFunctions { - static String max(TQProperty prop) { - return "max(" + prop + ")"; - } - } } 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..d75bf14a6 --- /dev/null +++ b/ebean-test/src/test/java/org/tests/query/TestStdFunctions.java @@ -0,0 +1,43 @@ +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.StdOperators.*; +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() { + Query.Property 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) = ?"); + } +} diff --git a/kotlin-querybean-generator/pom.xml b/kotlin-querybean-generator/pom.xml index 5d03a39a6..92c30d8cd 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();