mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-22 15:10:02 +00:00
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)
This commit is contained in:
1 parent
96e79248dc
commit
8565f5f807
9 files changed
+101
-29
No files matched your search
@@ -1768,4 +1768,11 @@ public interface Query<T> extends CancelableQuery {
|
|||||||
*/
|
*/
|
||||||
Query<T> orderById(boolean orderById);
|
Query<T> orderById(boolean orderById);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type safe query bean properties and expressions (marker interface).
|
||||||
|
* <p>
|
||||||
|
* Implemented by query bean properties and expressions based on those properties.
|
||||||
|
*/
|
||||||
|
interface Property {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -14,7 +14,7 @@ package io.ebean;
|
|||||||
*
|
*
|
||||||
* int rows = DB.update(Customer.class)
|
* int rows = DB.update(Customer.class)
|
||||||
* .set("status", Customer.Status.ACTIVE)
|
* .set("status", Customer.Status.ACTIVE)
|
||||||
* .set("updtime", new Timestamp(System.currentTimeMillis()))
|
* .set("whenUpdated", Instant.now())
|
||||||
* .where()
|
* .where()
|
||||||
* .gt("id", 1000)
|
* .gt("id", 1000)
|
||||||
* .update();
|
* .update();
|
||||||
@@ -25,6 +25,20 @@ package io.ebean;
|
|||||||
* update o_customer set status=?, updtime=? where id > ?
|
* update o_customer set status=?, updtime=? where id > ?
|
||||||
*
|
*
|
||||||
* }</pre>
|
* }</pre>
|
||||||
|
*
|
||||||
|
* <h4>Example: Using query bean</h4>
|
||||||
|
* <pre>{@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();
|
||||||
|
*
|
||||||
|
* }</pre>
|
||||||
* <p>
|
* <p>
|
||||||
* Note that if the where() clause contains a join then the SQL update changes to use a
|
* Note that if the where() clause contains a join then the SQL update changes to use a
|
||||||
* <code> WHERE ID IN () </code> form.
|
* <code> WHERE ID IN () </code> form.
|
||||||
@@ -40,7 +54,7 @@ package io.ebean;
|
|||||||
*
|
*
|
||||||
* int rows = DB.update(Customer.class)
|
* int rows = DB.update(Customer.class)
|
||||||
* .set("status", Customer.Status.ACTIVE)
|
* .set("status", Customer.Status.ACTIVE)
|
||||||
* .set("updtime", new Timestamp(System.currentTimeMillis()))
|
* .set("whenUpdated", Instant.now())
|
||||||
* .where()
|
* .where()
|
||||||
* .eq("status", Customer.Status.NEW)
|
* .eq("status", Customer.Status.NEW)
|
||||||
* .eq("billingAddress.country", nz)
|
* .eq("billingAddress.country", nz)
|
||||||
@@ -73,7 +87,7 @@ public interface UpdateQuery<T> {
|
|||||||
*
|
*
|
||||||
* int rows = DB.update(Customer.class)
|
* int rows = DB.update(Customer.class)
|
||||||
* .set("status", Customer.Status.ACTIVE)
|
* .set("status", Customer.Status.ACTIVE)
|
||||||
* .set("updtime", new Timestamp(System.currentTimeMillis()))
|
* .set("whenUpdated", Instant.now())
|
||||||
* .where()
|
* .where()
|
||||||
* .gt("id", 1000)
|
* .gt("id", 1000)
|
||||||
* .update();
|
* .update();
|
||||||
@@ -85,6 +99,27 @@ public interface UpdateQuery<T> {
|
|||||||
*/
|
*/
|
||||||
UpdateQuery<T> set(String property, Object value);
|
UpdateQuery<T> set(String property, Object value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the value of a property.
|
||||||
|
* <p>
|
||||||
|
* <pre>{@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();
|
||||||
|
*
|
||||||
|
* }</pre>
|
||||||
|
*
|
||||||
|
* @param property The bean property to be set
|
||||||
|
* @param value The value to set the property to
|
||||||
|
*/
|
||||||
|
UpdateQuery<T> set(Query.Property property, Object value);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the property to be null.
|
* Set the property to be null.
|
||||||
* <p>
|
* <p>
|
||||||
@@ -102,6 +137,13 @@ public interface UpdateQuery<T> {
|
|||||||
*/
|
*/
|
||||||
UpdateQuery<T> setNull(String property);
|
UpdateQuery<T> setNull(String property);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the property to be null.
|
||||||
|
*
|
||||||
|
* @param property The bean property to be set
|
||||||
|
*/
|
||||||
|
UpdateQuery<T> setNull(Query.Property property);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set using a property expression that does not need any bind values.
|
* Set using a property expression that does not need any bind values.
|
||||||
* <p>
|
* <p>
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package io.ebeaninternal.server.querydefn;
|
|||||||
|
|
||||||
import io.ebean.ExpressionList;
|
import io.ebean.ExpressionList;
|
||||||
import io.ebean.ProfileLocation;
|
import io.ebean.ProfileLocation;
|
||||||
|
import io.ebean.Query;
|
||||||
import io.ebean.UpdateQuery;
|
import io.ebean.UpdateQuery;
|
||||||
import io.ebean.core.type.ScalarType;
|
import io.ebean.core.type.ScalarType;
|
||||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||||
@@ -34,6 +35,16 @@ public final class DefaultUpdateQuery<T> implements UpdateQuery<T> {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public UpdateQuery<T> set(Query.Property property, Object value) {
|
||||||
|
return set(property.toString(), value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public UpdateQuery<T> setNull(Query.Property property) {
|
||||||
|
return setNull(property.toString());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UpdateQuery<T> setNull(String property) {
|
public UpdateQuery<T> setNull(String property) {
|
||||||
values.setNull(property);
|
values.setNull(property);
|
||||||
|
|||||||
@@ -2,34 +2,35 @@ package io.ebean.typequery;
|
|||||||
|
|
||||||
import io.ebean.Expr;
|
import io.ebean.Expr;
|
||||||
import io.ebean.Expression;
|
import io.ebean.Expression;
|
||||||
|
import io.ebean.Query;
|
||||||
|
|
||||||
public class StdExpressions {
|
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);
|
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);
|
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);
|
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);
|
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);
|
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);
|
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);
|
return Expr.icontains(property.toString(), value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,16 +1,18 @@
|
|||||||
package io.ebean.typequery;
|
package io.ebean.typequery;
|
||||||
|
|
||||||
|
import io.ebean.Query.Property;
|
||||||
|
|
||||||
public final class StdFunctions {
|
public final class StdFunctions {
|
||||||
|
|
||||||
public static TQColumn max(TQColumn property) {
|
public static Property max(Property property) {
|
||||||
return new Standard("max(" + property + ")");
|
return new Standard("max(" + property + ")");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TQColumn sum(TQColumn property) {
|
public static Property sum(Property property) {
|
||||||
return new Standard("sum(" + 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);
|
StringBuilder expression = new StringBuilder(50);
|
||||||
expression.append("concat(").append(property.toString());
|
expression.append("concat(").append(property.toString());
|
||||||
for (Object value : values) {
|
for (Object value : values) {
|
||||||
@@ -20,7 +22,7 @@ public final class StdFunctions {
|
|||||||
return new Standard(expression.toString());
|
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);
|
StringBuilder expression = new StringBuilder(50);
|
||||||
expression.append("coalesce(").append(property.toString()).append(",");
|
expression.append("coalesce(").append(property.toString()).append(",");
|
||||||
expression.append(sqlStringExpression(value));
|
expression.append(sqlStringExpression(value));
|
||||||
@@ -29,14 +31,14 @@ public final class StdFunctions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static String sqlStringExpression(Object value) {
|
private static String sqlStringExpression(Object value) {
|
||||||
if (value instanceof TQColumn || value instanceof Number) {
|
if (value instanceof Property || value instanceof Number) {
|
||||||
return value.toString();
|
return value.toString();
|
||||||
} else {
|
} else {
|
||||||
return "'" + value + "'";
|
return "'" + value + "'";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class Standard implements TQColumn {
|
private static class Standard implements Property {
|
||||||
|
|
||||||
private final String expression;
|
private final String expression;
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
package io.ebean.typequery;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Marker interface for type safe properties and expressions.
|
|
||||||
*/
|
|
||||||
public interface TQColumn {
|
|
||||||
}
|
|
||||||
@@ -1,13 +1,14 @@
|
|||||||
package io.ebean.typequery;
|
package io.ebean.typequery;
|
||||||
|
|
||||||
import io.ebean.ExpressionList;
|
import io.ebean.ExpressionList;
|
||||||
|
import io.ebean.Query;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A property used in type query.
|
* A property used in type query.
|
||||||
*
|
*
|
||||||
* @param <R> The type of the owning root bean
|
* @param <R> The type of the owning root bean
|
||||||
*/
|
*/
|
||||||
public class TQProperty<R> implements TQColumn {
|
public class TQProperty<R> implements Query.Property {
|
||||||
|
|
||||||
protected final String _name;
|
protected final String _name;
|
||||||
|
|
||||||
|
|||||||
@@ -268,9 +268,9 @@ public abstract class TQRootBean<T, R> {
|
|||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Set<String> properties(TQColumn[] properties) {
|
private Set<String> properties(Query.Property[] properties) {
|
||||||
Set<String> props = new LinkedHashSet<>();
|
Set<String> props = new LinkedHashSet<>();
|
||||||
for (TQColumn property : properties) {
|
for (Query.Property property : properties) {
|
||||||
props.add(property.toString());
|
props.add(property.toString());
|
||||||
}
|
}
|
||||||
return props;
|
return props;
|
||||||
@@ -278,11 +278,11 @@ public abstract class TQRootBean<T, R> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Specify the properties to be loaded on the 'main' root level entity bean
|
* 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
|
* @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));
|
((SpiQueryFetch) query).selectProperties(properties(properties));
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import io.ebean.DB;
|
|||||||
import io.ebean.FetchGroup;
|
import io.ebean.FetchGroup;
|
||||||
import io.ebean.Query;
|
import io.ebean.Query;
|
||||||
import io.ebean.test.LoggedSql;
|
import io.ebean.test.LoggedSql;
|
||||||
import io.ebean.typequery.StdExpressions;
|
|
||||||
import org.example.domain.Customer;
|
import org.example.domain.Customer;
|
||||||
import org.example.domain.Order;
|
import org.example.domain.Order;
|
||||||
import org.example.domain.OrderDetail;
|
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<String> 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
|
@Test
|
||||||
public void stdExpression_iLikeConcatCoalesce() {
|
public void stdExpression_iLikeConcatCoalesce() {
|
||||||
QOrder o = QOrder.alias();
|
QOrder o = QOrder.alias();
|
||||||
|
|||||||
Reference in new issue
Block a user