#120 - ENH: Add support for using where() on bulk Update statements

This commit is contained in:
Robin Bygrave committed 2016-05-23 22:24:07 +12:00
1 parent 1c162b9b18
commit 6c7074e288
27 files changed
+1093 -348

No files matched your search

@@ -230,6 +230,8 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
private boolean useDocStore;
private OrmUpdateProperties updateProperties;
public DefaultOrmQuery(BeanDescriptor<T> desc, EbeanServer server, ExpressionFactory expressionFactory) {
this.beanDescriptor = desc;
this.beanType = desc.getBeanType();
@@ -831,7 +833,7 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
queryPlanKey = new OrmQueryPlanKey(includeTableJoin, type, detail, maxRows, firstRow,
disableLazyLoading, rawWhereClause, orderBy, query, additionalWhere, additionalHaving,
distinct, sqlDistinct, mapKey, id, bindParams, whereExpressions, havingExpressions,
temporalMode, forUpdate, rootTableAlias, rawSql);
temporalMode, forUpdate, rootTableAlias, rawSql, updateProperties);
return queryPlanKey;
}
@@ -1036,6 +1038,11 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
return server.delete(this, null);
}
@Override
public int update() {
return server.update(this, null);
}
@Override
public List<Object> findIds() {
// a copy of this query is made in the server
@@ -1528,4 +1535,13 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
}
return validation.getUnknownProperties();
}
public void setUpdateProperties(OrmUpdateProperties updateProperties) {
this.updateProperties = updateProperties;
}
@Override
public OrmUpdateProperties getUpdateProperties() {
return updateProperties;
}
}
@@ -0,0 +1,48 @@
package com.avaje.ebeaninternal.server.querydefn;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.UpdateQuery;
/**
* Default implementation of UpdateQuery.
*/
public class DefaultUpdateQuery<T> implements UpdateQuery<T> {
private final OrmUpdateProperties values = new OrmUpdateProperties();
private final DefaultOrmQuery<T> query;
public DefaultUpdateQuery(DefaultOrmQuery<T> query) {
this.query = query;
query.setUpdateProperties(values);
}
@Override
public UpdateQuery<T> set(String property, Object value) {
values.set(property, value);
return this;
}
@Override
public UpdateQuery<T> setNull(String property) {
values.set(property, null);
return this;
}
@Override
public UpdateQuery<T> setRaw(String propertyExpression) {
values.setRaw(propertyExpression);
return this;
}
@Override
public UpdateQuery<T> setRaw(String propertyExpression, Object... vals) {
values.setRaw(propertyExpression, vals);
return this;
}
@Override
public ExpressionList<T> where() {
return query.where();
}
}
@@ -35,11 +35,12 @@ public class OrmQueryPlanKey implements CQueryPlanKey {
private final SpiQuery.TemporalMode temporalMode;
private final boolean forUpdate;
private final String rootTableAlias;
private final OrmUpdateProperties updateProperties;
private final int planHash;
private final int bindCount;
public OrmQueryPlanKey(TableJoin includeTableJoin, SpiQuery.Type type, OrmQueryDetail detail, int maxRows, int firstRow, boolean disableLazyLoading, String rawWhereClause, OrderBy<?> orderBy, String query, String additionalWhere, String additionalHaving, boolean distinct, boolean sqlDistinct, String mapKey, Object id, BindParams bindParams, SpiExpression whereExpressions, SpiExpression havingExpressions, SpiQuery.TemporalMode temporalMode, boolean forUpdate, String rootTableAlias, RawSql rawSql) {
public OrmQueryPlanKey(TableJoin includeTableJoin, SpiQuery.Type type, OrmQueryDetail detail, int maxRows, int firstRow, boolean disableLazyLoading, String rawWhereClause, OrderBy<?> orderBy, String query, String additionalWhere, String additionalHaving, boolean distinct, boolean sqlDistinct, String mapKey, Object id, BindParams bindParams, SpiExpression whereExpressions, SpiExpression havingExpressions, SpiQuery.TemporalMode temporalMode, boolean forUpdate, String rootTableAlias, RawSql rawSql, OrmUpdateProperties updateProperties) {
this.includeTableJoin = includeTableJoin;
this.type = type;
@@ -61,6 +62,7 @@ public class OrmQueryPlanKey implements CQueryPlanKey {
this.temporalMode = temporalMode;
this.forUpdate = forUpdate;
this.rootTableAlias = rootTableAlias;
this.updateProperties = updateProperties;
this.rawSqlKey = (rawSql == null) ? null : rawSql.getKey();
// exclude bind values and things unrelated to the sql being generated
@@ -91,6 +93,9 @@ public class OrmQueryPlanKey implements CQueryPlanKey {
if (having != null) {
having.queryPlanHash(builder);
}
if (updateProperties != null) {
updateProperties.buildQueryPlanHash(builder);
}
this.planHash = builder.getPlanHash();
this.bindCount = builder.getBindCount();
@@ -128,6 +133,7 @@ public class OrmQueryPlanKey implements CQueryPlanKey {
if (orderByAsSting != null ? !orderByAsSting.equals(that.orderByAsSting) : that.orderByAsSting != null) return false;
if (where != null ? !where.isSameByPlan(that.where) : that.where != null) return false;
if (having != null ? !having.isSameByPlan(that.having) : that.having != null) return false;
if (updateProperties != null ? !updateProperties.isSameByPlan(that.updateProperties) : that.updateProperties != null) return false;
if (rawSqlKey != null ? !rawSqlKey.equals(that.rawSqlKey) : that.rawSqlKey != null) return false;
// if (detail != null ? !detail.equals(that.detail) : that.detail != null) return false;
@@ -0,0 +1,209 @@
package com.avaje.ebeaninternal.server.querydefn;
import com.avaje.ebeaninternal.api.HashQueryPlanBuilder;
import com.avaje.ebeaninternal.server.deploy.DeployParser;
import com.avaje.ebeaninternal.server.persist.Binder;
import com.avaje.ebeaninternal.server.type.DataBind;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
/**
* Set properties for a UpdateQuery.
*/
public class OrmUpdateProperties {
private static final NullValue NULL_VALUE = new NullValue();
private static final NoneValue NONE_VALUE = new NoneValue();
/**
* Bind value used in the set clause for update query.
* It may/may not have bind values etc.
*/
public static abstract class Value {
public void bind(Binder binder, DataBind dataBind) throws SQLException {
// default to no bind values
}
public String bindClause() {
return "";
}
public int getBindCount() {
return 0;
}
}
/**
* Set property to null.
*/
static class NullValue extends Value {
@Override
public String bindClause() {
return "=null";
}
}
/**
* Set property to a simple value.
*/
static class SimpleValue extends Value {
final Object value;
SimpleValue(Object value) {
this.value = value;
}
@Override
public int getBindCount() {
return 1;
}
@Override
public String bindClause() {
return "=?";
}
@Override
public void bind(Binder binder, DataBind dataBind) throws SQLException {
binder.bindObject(dataBind, value);
}
}
/**
* Set using an expression with no bind value.
*/
static class NoneValue extends Value {
@Override
public String bindClause() {
return "";
}
}
/**
* Set using an expression with many bind values.
*/
static class RawArrayValue extends Value {
final Object[] bindValues;
public RawArrayValue(Object[] bindValues) {
this.bindValues = bindValues;
}
@Override
public int getBindCount() {
return bindValues.length;
}
@Override
public void bind(Binder binder, DataBind dataBind) throws SQLException {
for (Object val : bindValues) {
binder.bindObject(dataBind, val);
}
}
}
/**
* The set properties/expressions and their bind values.
*/
private LinkedHashMap<String, Value> values = new LinkedHashMap<String, Value>();
/**
* Normal set property.
*/
public void set(String propertyName, Object value) {
if (value == null) {
values.put(propertyName, NULL_VALUE);
} else {
values.put(propertyName, new SimpleValue(value));
}
}
/**
* Set a raw expression with no bind values.
*/
public void setRaw(String propertyName) {
values.put(propertyName, NONE_VALUE);
}
/**
* Set a raw expression with many bind values.
*/
public void setRaw(String propertyExpression, Object... vals) {
if (vals.length == 0) {
setRaw(propertyExpression);
} else {
values.put(propertyExpression, new RawArrayValue(vals));
}
}
/**
* Return true if this update has the same logical set clause.
*/
public boolean isSameByPlan(OrmUpdateProperties that) {
return that.values.size() == values.size()
&& logicalSetClause().equals(that.logicalSetClause());
}
/**
* Build the hash for the query plan caching.
*/
public void buildQueryPlanHash(HashQueryPlanBuilder builder) {
builder.add(OrmUpdateProperties.class);
Set<Map.Entry<String, Value>> entries = values.entrySet();
for (Map.Entry<String, Value> entry : entries) {
builder.add(entry.getKey());
builder.bind(entry.getValue().getBindCount());
}
}
/**
* Bind all the bind values for the update set clause.
*/
public void bind(Binder binder, DataBind dataBind) throws SQLException {
for (Value bindValue : values.values()) {
bindValue.bind(binder, dataBind);
}
}
/**
* Build the actual set clause converting logical property names to db columns etc.
*/
public String buildSetClause(DeployParser deployParser) {
int setCount = 0;
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, Value> entry : values.entrySet()) {
String property = entry.getKey();
if (setCount++ > 0) {
sb.append(", ");
}
// translate to db columns and remove table alias placeholders
sb.append(deployParser.parse(property).replace("${}", ""));
sb.append(entry.getValue().bindClause());
}
return sb.toString();
}
/**
* Return a logical set clause to use for isSameByPlan() use.
*/
private String logicalSetClause() {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, Value> entry : values.entrySet()) {
sb.append(", ");
sb.append(entry.getKey()).append(entry.getValue().bindClause());
}
return sb.toString();
}
}