Merge in v4 code changes

This commit is contained in:
Rob Bygrave committed 2014-04-03 03:47:55 +13:00
commit 96e4b4640f
171 files changed
+2747 -3848

No files matched your search

@@ -56,19 +56,9 @@ public class DeleteHandler extends DmlHandler {
// Deletes the bean from the PersistenceContext
persistRequest.postDelete();
}
@Override
public boolean isIncluded(BeanProperty prop) {
return prop.isDbUpdatable() && super.isIncluded(prop);
}
@Override
public boolean isIncludedWhere(BeanProperty prop) {
return prop.isDbUpdatable() && (loadedProps == null || loadedProps.contains(prop.getName()));
}
public void registerDerivedRelationship(DerivedRelationshipData assocBean) {
throw new RuntimeException("Never called on delete");
}
public void registerDerivedRelationship(DerivedRelationshipData assocBean) {
throw new RuntimeException("Never called on delete");
}
}
@@ -1,9 +1,9 @@
package com.avaje.ebeaninternal.server.persist.dml;
import java.sql.SQLException;
import java.util.Set;
import com.avaje.ebean.annotation.ConcurrencyMode;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.persist.dmlbind.Bindable;
@@ -23,21 +23,17 @@ public final class DeleteMeta {
private final Bindable version;
private final Bindable all;
private final String tableName;
private final boolean emptyStringAsNull;
public DeleteMeta(boolean emptyStringAsNull, BeanDescriptor<?> desc, BindableId id, Bindable version, Bindable all) {
public DeleteMeta(boolean emptyStringAsNull, BeanDescriptor<?> desc, BindableId id, Bindable version) {
this.emptyStringAsNull = emptyStringAsNull;
this.tableName = desc.getBaseTable();
this.id = id;
this.version = version;
this.all = all;
sqlNone = genSql(ConcurrencyMode.NONE);
sqlVersion = genSql(ConcurrencyMode.VERSION);
this.sqlNone = genSql(ConcurrencyMode.NONE);
this.sqlVersion = genSql(ConcurrencyMode.VERSION);
}
public boolean isEmptyStringAsNull() {
@@ -56,18 +52,13 @@ public final class DeleteMeta {
*/
public void bind(PersistRequestBean<?> persist, DmlHandler bind) throws SQLException {
Object bean = persist.getBean();
EntityBean bean = persist.getEntityBean();
id.dmlBind(bind, false, bean);
id.dmlBind(bind, bean);
switch (persist.getConcurrencyMode()) {
case VERSION:
version.dmlBind(bind, false, bean);
break;
case ALL:
Object oldBean = persist.getOldValues();
all.dmlBindWhere(bind, true, oldBean);
version.dmlBind(bind, bean);
break;
default:
@@ -91,9 +82,6 @@ public final class DeleteMeta {
case VERSION:
return sqlVersion;
case ALL:
return genDynamicWhere(request.getLoadedProperties(), request.getOldValues());
default:
throw new RuntimeException("Invalid mode " + request.determineConcurrencyMode());
}
@@ -109,37 +97,15 @@ public final class DeleteMeta {
request.append(" where ");
request.setWhereIdMode();
id.dmlAppend(request, false);
id.dmlAppend(request);
if (ConcurrencyMode.VERSION.equals(conMode)) {
if (version == null) {
return null;
}
version.dmlAppend(request, false);
} else if (ConcurrencyMode.ALL.equals(conMode)) {
throw new RuntimeException("Never called for ConcurrencyMode.ALL");
version.dmlAppend(request);
}
return request.toString();
}
/**
* Generate the sql dynamically for where using IS NULL for binding null
* values.
*/
private String genDynamicWhere(Set<String> includedProps, Object oldBean) throws SQLException {
// always has a preceding id property(s) so the first
// option is always ' and ' and not blank.
GenerateDmlRequest request = new GenerateDmlRequest(emptyStringAsNull, includedProps, oldBean);
request.append(sqlNone);
request.setWhereMode();
all.dmlWhere(request, true, oldBean);
return request.toString();
}
@@ -4,11 +4,13 @@ import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.OptimisticLockException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.api.SpiTransaction;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.core.PstmtBatch;
@@ -18,8 +20,6 @@ import com.avaje.ebeaninternal.server.persist.BatchedPstmtHolder;
import com.avaje.ebeaninternal.server.persist.dmlbind.BindableRequest;
import com.avaje.ebeaninternal.server.transaction.TransactionManager;
import com.avaje.ebeaninternal.server.type.DataBind;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Base class for Handler implementations.
@@ -35,8 +35,6 @@ public abstract class DmlHandler implements PersistHandler, BindableRequest {
protected final StringBuilder bindLog;
protected final Set<String> loadedProps;
protected final SpiTransaction transaction;
protected final boolean emptyStringToNull;
@@ -52,12 +50,9 @@ public abstract class DmlHandler implements PersistHandler, BindableRequest {
protected ArrayList<UpdateGenValue> updateGenValues;
private Set<String> additionalProps;
protected DmlHandler(PersistRequestBean<?> persistRequest, boolean emptyStringToNull) {
this.persistRequest = persistRequest;
this.emptyStringToNull = emptyStringToNull;
this.loadedProps = persistRequest.getLoadedProperties();
this.transaction = persistRequest.getTransaction();
this.logLevelSql = transaction.isLogSql();
if (logLevelSql) {
@@ -148,20 +143,6 @@ public abstract class DmlHandler implements PersistHandler, BindableRequest {
}
}
public boolean isIncluded(BeanProperty prop) {
return (loadedProps == null || loadedProps.contains(prop.getName()));
}
public boolean isIncludedWhere(BeanProperty prop) {
if (prop.isDbEncrypted()) {
// update without a version property ...
// for encrypted properties only include if it was
// also an updated/modified property
return isIncluded(prop);
}
return prop.isDbUpdatable() && (loadedProps == null || loadedProps.contains(prop.getName()));
}
/**
* Bind a raw value. Used to bind the discriminator column.
*/
@@ -194,80 +175,36 @@ public abstract class DmlHandler implements PersistHandler, BindableRequest {
/**
* Bind the value to the preparedStatement.
*/
public Object bind(Object value, BeanProperty prop, String propName, boolean bindNull)
throws SQLException {
return bindInternal(logLevelSql, value, prop, propName, bindNull);
public Object bind(Object value, BeanProperty prop, String propName) throws SQLException {
return bindInternal(logLevelSql, value, prop, propName);
}
/**
* Bind the value to the preparedStatement without logging.
*/
public Object bindNoLog(Object value, BeanProperty prop, String propName, boolean bindNull)
throws SQLException {
return bindInternal(false, value, prop, propName, bindNull);
public Object bindNoLog(Object value, BeanProperty prop, String propName) throws SQLException {
return bindInternal(false, value, prop, propName);
}
private Object bindInternal(boolean log, Object value, BeanProperty prop, String propName,
boolean bindNull) throws SQLException {
private Object bindInternal(boolean log, Object value, BeanProperty prop, String propName) throws SQLException {
if (!bindNull) {
if (emptyStringToNull && (value instanceof String) && ((String) value).length() == 0) {
// support Oracle conversion of empty string to null
// value = prop.getDbNullValue(value);
value = null;
}
}
if (!bindNull && value == null) {
// where will have IS NULL clause so don't actually bind
if (log) {
bindLog.append("null, ");
}
} else {
if (log) {
if (prop.isLob()) {
bindLog.append("[LOB]");
} else {
String sv = String.valueOf(value);
if (sv.length() > 50) {
sv = sv.substring(0, 47) + "...";
}
bindLog.append(sv);
if (log) {
if (prop.isLob()) {
bindLog.append("[LOB]");
} else {
String sv = String.valueOf(value);
if (sv.length() > 50) {
sv = sv.substring(0, 47) + "...";
}
bindLog.append(",");
bindLog.append(sv);
}
// do the actual binding to PreparedStatement
prop.bind(dataBind, value);
bindLog.append(",");
}
// do the actual binding to PreparedStatement
prop.bind(dataBind, value);
return value;
}
/**
* For generated properties set on insert register as additional loaded
* properties if required.
*/
public final void registerAdditionalProperty(String propertyName) {
if (loadedProps != null && !loadedProps.contains(propertyName)) {
if (additionalProps == null) {
additionalProps = new HashSet<String>();
}
additionalProps.add(propertyName);
}
}
/**
* Set any additional (generated) properties to the set of loaded properties
* if required.
*/
protected void setAdditionalProperties() {
if (additionalProps != null) {
// additional generated properties set on insert
// added to the set of loaded properties
additionalProps.addAll(loadedProps);
persistRequest.setLoadedProps(additionalProps);
}
}
/**
* Register a generated value on a update. This can not be set to the bean
* until after the where clause has been bound for concurrency checking.
@@ -277,12 +214,11 @@ public abstract class DmlHandler implements PersistHandler, BindableRequest {
* generation.
* </p>
*/
public void registerUpdateGenValue(BeanProperty prop, Object bean, Object value) {
public void registerUpdateGenValue(BeanProperty prop, EntityBean bean, Object value) {
if (updateGenValues == null) {
updateGenValues = new ArrayList<UpdateGenValue>();
}
updateGenValues.add(new UpdateGenValue(prop, bean, value));
registerAdditionalProperty(prop.getName());
}
/**
@@ -303,6 +239,7 @@ public abstract class DmlHandler implements PersistHandler, BindableRequest {
*/
protected PreparedStatement getPstmt(SpiTransaction t, String sql, boolean genKeys)
throws SQLException {
Connection conn = t.getInternalConnection();
if (genKeys) {
// the Id generated is always the first column
@@ -353,11 +290,11 @@ public abstract class DmlHandler implements PersistHandler, BindableRequest {
private final BeanProperty property;
private final Object bean;
private final EntityBean bean;
private final Object value;
private UpdateGenValue(BeanProperty property, Object bean, Object value) {
private UpdateGenValue(BeanProperty property, EntityBean bean, Object value) {
this.property = property;
this.bean = bean;
this.value = value;
@@ -15,8 +15,4 @@ public enum DmlMode {
*/
UPDATE,
/**
* The Update or Delete WHERE.
*/
WHERE
}
@@ -1,7 +1,6 @@
package com.avaje.ebeaninternal.server.persist.dml;
import java.util.Set;
import com.avaje.ebean.bean.EntityBeanIntercept;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
/**
@@ -15,10 +14,8 @@ public class GenerateDmlRequest {
private final StringBuilder sb = new StringBuilder(100);
private final Set<String> includeProps;
private final Set<String> includeWhereProps;
private final Object oldValues;
private final EntityBeanIntercept ebi;
private final boolean changesOnly;
private StringBuilder insertBindBuffer;
@@ -28,29 +25,21 @@ public class GenerateDmlRequest {
private int insertMode;
private int bindColumnCount;
/**
* Create with includeWhereProps same as includeProps.
*/
public GenerateDmlRequest(boolean emptyStringAsNull, Set<String> includeProps, Object oldValues) {
this(emptyStringAsNull, includeProps, includeProps, oldValues);
}
/**
* Create from a PersistRequestBean.
*/
public GenerateDmlRequest(boolean emptyStringAsNull, Set<String> includeProps, Set<String> includeWhereProps, Object oldValues) {
public GenerateDmlRequest(boolean emptyStringAsNull, EntityBeanIntercept ebi, boolean changesOnly) {//, Object oldValues) {
this.emptyStringAsNull = emptyStringAsNull;
this.includeProps = includeProps;
this.includeWhereProps = includeWhereProps;
this.oldValues = oldValues;
this.ebi = ebi;
this.changesOnly = changesOnly;
}
/**
* Create for generating standard all properties DML/SQL.
*/
public GenerateDmlRequest(boolean emptyStringAsNull) {
this(emptyStringAsNull, null, null, null);
this(emptyStringAsNull, null, false);
}
public GenerateDmlRequest append(String s) {
@@ -66,14 +55,21 @@ public class GenerateDmlRequest {
* Return true if this property should be included in the set clause.
*/
public boolean isIncluded(BeanProperty prop) {
return (includeProps == null || includeProps.contains(prop.getName()));
if (ebi == null) {
return true;
}
if (changesOnly) {
return ebi.isChangedProperty(prop.getPropertyIndex());
} else {
return ebi.isLoadedProperty(prop.getPropertyIndex());
}
}
/**
* Return true if this property should be included in the where clause.
*/
public boolean isIncludedWhere(BeanProperty prop) {
return (includeWhereProps == null || includeWhereProps.contains(prop.getName()));
return ebi == null || ebi.isLoadedProperty(prop.getPropertyIndex());
}
public void appendColumnIsNull(String column) {
@@ -81,28 +77,29 @@ public class GenerateDmlRequest {
}
public void appendColumn(String column) {
String bind = (insertMode > 0) ? "?" : "=?";
appendColumn(column, bind);
//String bind = (insertMode > 0) ? "?" : "=?";
appendColumn(column, "?");
}
public void appendColumn(String column, String suffik) {
appendColumn(column, "", suffik);
public void appendColumn(String column, String bind) {
appendColumn(column, "", bind);
}
public void appendColumn(String column, String expr, String suffik) {
public void appendColumn(String column, String expr, String bind) {
++bindColumnCount;
sb.append(prefix);
sb.append(column);
sb.append(expr);
//sb.append(expr);
if (insertMode > 0) {
if (insertMode++ > 1) {
insertBindBuffer.append(",");
}
insertBindBuffer.append(suffik);
insertBindBuffer.append(bind);
} else {
sb.append(suffik);
sb.append("=");
sb.append(bind);
}
if (prefix2 != null) {
@@ -145,8 +142,4 @@ public class GenerateDmlRequest {
this.prefix2 = ", ";
}
public Object getOldValues() {
return oldValues;
}
}
@@ -5,14 +5,17 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import java.util.List;
import javax.persistence.OptimisticLockException;
import javax.persistence.PersistenceException;
import com.avaje.ebean.EbeanServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.api.DerivedRelationshipData;
import com.avaje.ebeaninternal.api.SpiEbeanServer;
import com.avaje.ebeaninternal.api.SpiTransaction;
import com.avaje.ebeaninternal.server.core.Message;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
@@ -20,8 +23,6 @@ import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.persist.DmlUtil;
import com.avaje.ebeaninternal.server.type.DataBind;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Insert bean handler.
@@ -59,18 +60,13 @@ public class InsertHandler extends DmlHandler {
this.concatinatedKey = meta.isConcatinatedKey();
}
@Override
public boolean isIncluded(BeanProperty prop) {
return prop.isDbInsertable() && (super.isIncluded(prop));
}
/**
* Generate and bind the insert statement.
*/
public void bind() throws SQLException {
BeanDescriptor<?> desc = persistRequest.getBeanDescriptor();
Object bean = persistRequest.getBean();
EntityBean bean = persistRequest.getEntityBean();
Object idValue = desc.getId(bean);
@@ -142,20 +138,28 @@ public class InsertHandler extends DmlHandler {
}
checkRowCount(rc);
setAdditionalProperties();
//setAdditionalProperties();
executeDerivedRelationships();
persistRequest.postInsert();
}
protected void executeDerivedRelationships() {
List<DerivedRelationshipData> derivedRelationships = persistRequest.getDerivedRelationships();
if (derivedRelationships != null) {
SpiEbeanServer ebeanServer = (SpiEbeanServer)persistRequest.getEbeanServer();
for (int i = 0; i < derivedRelationships.size(); i++) {
DerivedRelationshipData derivedRelationshipData = derivedRelationships.get(i);
EbeanServer ebeanServer = persistRequest.getEbeanServer();
HashSet<String> updateProps = new HashSet<String>();
updateProps.add(derivedRelationshipData.getLogicalName());
ebeanServer.update(derivedRelationshipData.getBean(), updateProps, transaction, false, true);
BeanDescriptor<?> beanDescriptor = ebeanServer.getBeanDescriptor(derivedRelationshipData.getBean().getClass());
BeanProperty prop = beanDescriptor.getBeanProperty(derivedRelationshipData.getLogicalName());
EntityBean entityBean = (EntityBean)derivedRelationshipData.getBean();
entityBean._ebean_getIntercept().markPropertyAsChanged(prop.getPropertyIndex());
ebeanServer.update(entityBean, transaction);
}
}
}
@@ -1,8 +1,8 @@
package com.avaje.ebeaninternal.server.persist.dml;
import java.sql.SQLException;
import java.util.Set;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebean.config.dbplatform.DatabasePlatform;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
@@ -53,7 +53,7 @@ public final class InsertMeta {
this.all = all;
this.shadowFKey = shadowFKey;
this.sqlWithId = genSql(false, null);
this.sqlWithId = genSql(false);
// only available for single Id property
if (id.isConcatenated()) {
@@ -68,7 +68,7 @@ public final class InsertMeta {
// insert sql for db identity or sequence insert
this.concatinatedKey = false;
this.identityDbColumns = new String[]{id.getIdentityColumn()};
this.sqlNullId = genSql(true, null);
this.sqlNullId = genSql(true);
this.supportsGetGeneratedKeys = dbPlatform.getDbIdentity().isSupportsGetGeneratedKeys();
this.selectLastInsertedId = desc.getSelectLastInsertedId();
}
@@ -131,18 +131,18 @@ public final class InsertMeta {
/**
* Bind the request based on whether the id value(s) are null.
*/
public void bind(DmlHandler request, Object bean, boolean withId) throws SQLException {
public void bind(DmlHandler request, EntityBean bean, boolean withId) throws SQLException {
if (withId) {
id.dmlBind(request, false, bean);
id.dmlBind(request, bean);
}
if (shadowFKey != null){
shadowFKey.dmlBind(request, false, bean);
shadowFKey.dmlBind(request, bean);
}
if (discriminator != null){
discriminator.dmlBind(request, false, bean);
discriminator.dmlBind(request, bean);
}
all.dmlBind(request, false, bean);
all.dmlBind(request, bean);
}
/**
@@ -157,27 +157,27 @@ public final class InsertMeta {
}
}
private String genSql(boolean nullId, Set<String> loadedProps) {
private String genSql(boolean nullId) {
GenerateDmlRequest request = new GenerateDmlRequest(emptyStringToNull, loadedProps, null);
GenerateDmlRequest request = new GenerateDmlRequest(emptyStringToNull, null, true);
request.setInsertSetMode();
request.append("insert into ").append(tableName);
request.append(" (");
if (!nullId) {
id.dmlInsert(request, false);
id.dmlAppend(request);
}
if (shadowFKey != null){
shadowFKey.dmlInsert(request, false);
shadowFKey.dmlAppend(request);
}
if (discriminator != null){
discriminator.dmlInsert(request, false);
discriminator.dmlAppend(request);
}
all.dmlInsert(request, false);
all.dmlAppend(request);
request.append(") values (");
request.append(request.getInsertBindBuffer());
@@ -66,16 +66,9 @@ public class MetaFactory {
Bindable ver = versionFact.create(desc);
List<Bindable> allList = new ArrayList<Bindable>();
baseFact.create(allList, desc, DmlMode.WHERE, false);
embeddedFact.create(allList, desc, DmlMode.WHERE, false);
assocOneFact.create(allList, desc, DmlMode.WHERE);
BindableList setBindable = new BindableList(setList);
BindableList allBindable = new BindableList(allList);
return new UpdateMeta(emptyStringAsNull, desc, setBindable, id, ver, allBindable);
return new UpdateMeta(emptyStringAsNull, desc, setBindable, id, ver);
}
/**
@@ -87,15 +80,7 @@ public class MetaFactory {
Bindable ver = versionFact.create(desc);
List<Bindable> allList = new ArrayList<Bindable>();
baseFact.create(allList, desc, DmlMode.WHERE, false);
embeddedFact.create(allList, desc, DmlMode.WHERE, false);
assocOneFact.create(allList, desc, DmlMode.WHERE);
Bindable allBindable = new BindableList(allList);
return new DeleteMeta(emptyStringAsNull, desc, id, ver, allBindable);
return new DeleteMeta(emptyStringAsNull, desc, id, ver);
}
/**
@@ -10,7 +10,6 @@ import com.avaje.ebeaninternal.api.DerivedRelationshipData;
import com.avaje.ebeaninternal.api.SpiTransaction;
import com.avaje.ebeaninternal.api.SpiUpdatePlan;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.type.DataBind;
/**
@@ -18,10 +17,7 @@ import com.avaje.ebeaninternal.server.type.DataBind;
*/
public class UpdateHandler extends DmlHandler {
private final UpdateMeta meta;
private Set<String> updatedProperties;
private boolean emptySetClause;
@@ -41,8 +37,6 @@ public class UpdateHandler extends DmlHandler {
emptySetClause = true;
return;
}
updatedProperties = updatePlan.getProperties();
sql = updatePlan.getSql();
@@ -79,18 +73,11 @@ public class UpdateHandler extends DmlHandler {
if (!emptySetClause){
int rowCount = dataBind.executeUpdate();
checkRowCount(rowCount);
setAdditionalProperties();
}
}
@Override
public boolean isIncluded(BeanProperty prop) {
return prop.isDbUpdatable() && (updatedProperties == null || updatedProperties.contains(prop.getName()));
}
public void registerDerivedRelationship(DerivedRelationshipData derivedRelationship) {
persistRequest.getTransaction().registerDerivedRelationship(derivedRelationship);
}
public void registerDerivedRelationship(DerivedRelationshipData derivedRelationship) {
persistRequest.getTransaction().registerDerivedRelationship(derivedRelationship);
}
}
@@ -3,14 +3,15 @@ package com.avaje.ebeaninternal.server.persist.dml;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.persistence.PersistenceException;
import com.avaje.ebean.annotation.ConcurrencyMode;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebean.bean.EntityBeanIntercept;
import com.avaje.ebeaninternal.api.SpiUpdatePlan;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne;
import com.avaje.ebeaninternal.server.persist.dmlbind.Bindable;
import com.avaje.ebeaninternal.server.persist.dmlbind.BindableId;
import com.avaje.ebeaninternal.server.persist.dmlbind.BindableList;
@@ -28,7 +29,6 @@ public final class UpdateMeta {
private final BindableList set;
private final BindableId id;
private final Bindable version;
private final Bindable all;
private final String tableName;
@@ -37,20 +37,18 @@ public final class UpdateMeta {
private final boolean emptyStringAsNull;
public UpdateMeta(boolean emptyStringAsNull, BeanDescriptor<?> desc, BindableList set, BindableId id, Bindable version, Bindable all) {
public UpdateMeta(boolean emptyStringAsNull, BeanDescriptor<?> desc, BindableList set, BindableId id, Bindable version) {
this.emptyStringAsNull = emptyStringAsNull;
this.tableName = desc.getBaseTable();
this.set = set;
this.id = id;
this.version = version;
this.all = all;
this.sqlNone = genSql(ConcurrencyMode.NONE, null, null);
this.sqlVersion = genSql(ConcurrencyMode.VERSION, null, null);
this.sqlNone = genSql(ConcurrencyMode.NONE, null, set);
this.sqlVersion = genSql(ConcurrencyMode.VERSION, null, set);
this.modeNoneUpdatePlan = new UpdatePlan(ConcurrencyMode.NONE, sqlNone, set);
this.modeVersionUpdatePlan = new UpdatePlan(ConcurrencyMode.VERSION, sqlVersion, set);
}
/**
@@ -72,19 +70,15 @@ public final class UpdateMeta {
*/
public void bind(PersistRequestBean<?> persist, DmlHandler bind, SpiUpdatePlan updatePlan) throws SQLException {
Object bean = persist.getBean();
EntityBean bean = persist.getEntityBean();
updatePlan.bindSet(bind, bean);
id.dmlBind(bind, false, bean);
id.dmlBind(bind, bean);
switch (persist.getConcurrencyMode()) {
case VERSION:
version.dmlBind(bind, false, bean);
break;
case ALL:
Object oldBean = persist.getOldValues();
all.dmlBindWhere(bind, true, oldBean);
version.dmlBind(bind, bean);
break;
default:
@@ -110,14 +104,6 @@ public final class UpdateMeta {
case VERSION:
return modeVersionUpdatePlan;
case ALL:
Object oldValues = request.getOldValues();
if (oldValues == null) {
throw new PersistenceException("OldValues are null?");
}
String sql = genDynamicWhere(request.getUpdatedProperties(), request.getLoadedProperties(), oldValues);
return new UpdatePlan(ConcurrencyMode.ALL, sql, set);
default:
throw new RuntimeException("Invalid mode " + mode);
}
@@ -125,26 +111,33 @@ public final class UpdateMeta {
private SpiUpdatePlan getDynamicUpdatePlan(ConcurrencyMode mode, PersistRequestBean<?> persistRequest) {
Set<String> updatedProps = persistRequest.getUpdatedProperties();
if (ConcurrencyMode.ALL.equals(mode)) {
// due to is null in where clause we won't bother trying to
// cache plans for ConcurrencyMode.ALL
String sql = genSql(mode, persistRequest, null);
if (sql == null) {
// changed properties must have been updatable=false
return UpdatePlan.EMPTY_SET_CLAUSE;
// we can use a cached UpdatePlan for the changed properties
EntityBeanIntercept ebi = persistRequest.getEntityBeanIntercept();
int hash = ebi.getChangedPropertiesHash();
BeanDescriptor<?> beanDescriptor = persistRequest.getBeanDescriptor();
BeanPropertyAssocOne<?>[] propertiesEmbedded = beanDescriptor.propertiesEmbedded();
for (int i=0; i< propertiesEmbedded.length; i++) {
EntityBean embeddedBean = (EntityBean)propertiesEmbedded[i].getValue(persistRequest.getEntityBean());
if (embeddedBean == null) {
hash = hash * 31;
} else {
return new UpdatePlan(null, mode, sql, set, updatedProps);
hash = hash * 31 + embeddedBean._ebean_getIntercept().getChangedPropertiesHash();
}
}
BeanProperty versionProperty = beanDescriptor.getVersionProperty();
if (versionProperty != null) {
if (ebi.isLoadedProperty(versionProperty.getPropertyIndex())) {
hash = hash * 31 + 7;
}
}
// we can use a cached UpdatePlan for the changed properties
int hash = mode.hashCode();
hash = hash * 31 + (updatedProps == null ? 0 : updatedProps.hashCode());
Integer key = Integer.valueOf(hash);
BeanDescriptor<?> beanDescriptor = persistRequest.getBeanDescriptor();
SpiUpdatePlan updatePlan = beanDescriptor.getUpdatePlan(key);
if (updatePlan != null) {
return updatePlan;
@@ -154,18 +147,13 @@ public final class UpdateMeta {
// build a bindableList that only contains the changed properties
List<Bindable> list = new ArrayList<Bindable>();
if (updatedProps == null) {
// update all the properties
set.addAll(list);
} else {
set.addChanged(persistRequest, list);
}
set.addToUpdate(persistRequest, list);
BindableList bindableList = new BindableList(list);
// build the SQL for this update statement
String sql = genSql(mode, persistRequest, bindableList);
updatePlan = new UpdatePlan(key, mode, sql, bindableList, null);
updatePlan = new UpdatePlan(key, mode, sql, bindableList);
// add the UpdatePlan to the cache
beanDescriptor.putUpdatePlan(key, updatePlan);
@@ -188,12 +176,8 @@ public final class UpdateMeta {
request.append("update ").append(tableName).append(" set ");
request.setUpdateSetMode();
if (bindableList != null) {
bindableList.dmlAppend(request, false);
} else {
set.dmlAppend(request, true);
}
bindableList.dmlAppend(request);
if (request.getBindColumnCount() == 0) {
// update properties must have been updatable=false
// with the result that nothing is in the set clause
@@ -203,38 +187,15 @@ public final class UpdateMeta {
request.append(" where ");
request.setWhereIdMode();
id.dmlAppend(request, false);
id.dmlAppend(request);
if (ConcurrencyMode.VERSION.equals(conMode)) {
if (version == null) {
return null;
}
version.dmlAppend(request, false);
} else if (ConcurrencyMode.ALL.equals(conMode)) {
all.dmlWhere(request, true, request.getOldValues());
version.dmlAppend(request);
}
return request.toString();
}
/**
* Generate the sql dynamically for where using IS NULL for binding null
* values.
*/
private String genDynamicWhere(Set<String> loadedProps, Set<String> whereProps, Object oldBean) {
// always has a preceding id property(s) so the first
// option is always ' and ' and not blank.
GenerateDmlRequest request = new GenerateDmlRequest(emptyStringAsNull, loadedProps, whereProps, oldBean);
request.append(sqlNone);
request.setWhereMode();
all.dmlWhere(request, true, oldBean);
return request.toString();
}
@@ -1,9 +1,9 @@
package com.avaje.ebeaninternal.server.persist.dml;
import java.sql.SQLException;
import java.util.Set;
import com.avaje.ebean.annotation.ConcurrencyMode;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.api.SpiUpdatePlan;
import com.avaje.ebeaninternal.server.persist.dmlbind.Bindable;
@@ -15,12 +15,12 @@ import com.avaje.ebeaninternal.server.persist.dmlbind.Bindable;
*/
public class UpdatePlan implements SpiUpdatePlan {
/**
* Special plan used when there is nothing in the set clause and the update
* should in fact be skipped. Occurs when the updated properties have
* updatable=false in their deployment.
*/
public static final UpdatePlan EMPTY_SET_CLAUSE = new UpdatePlan();
/**
* Special plan used when there is nothing in the set clause and the update
* should in fact be skipped. Occurs when the updated properties have
* updatable=false in their deployment.
*/
public static final UpdatePlan EMPTY_SET_CLAUSE = new UpdatePlan();
private final Integer key;
@@ -30,10 +30,6 @@ public class UpdatePlan implements SpiUpdatePlan {
private final Bindable set;
private final Set<String> properties;
private final boolean checkIncludes;
private final long timeCreated;
private final boolean emptySetClause;
@@ -45,22 +41,19 @@ public class UpdatePlan implements SpiUpdatePlan {
*/
public UpdatePlan(ConcurrencyMode mode, String sql, Bindable set) {
this(null, mode, sql, set, null);
this(null, mode, sql, set);
}
/**
* Create a cachable UpdatePlan with a given key.
*/
public UpdatePlan(Integer key, ConcurrencyMode mode, String sql,
Bindable set, Set<String> properties) {
public UpdatePlan(Integer key, ConcurrencyMode mode, String sql, Bindable set) {
this.emptySetClause = false;
this.emptySetClause = (sql == null);
this.key = key;
this.mode = mode;
this.sql = sql;
this.set = set;
this.properties = properties;
this.checkIncludes = properties != null;
this.timeCreated = System.currentTimeMillis();
}
@@ -73,8 +66,6 @@ public class UpdatePlan implements SpiUpdatePlan {
this.mode = ConcurrencyMode.NONE;
this.sql = null;
this.set = null;
this.properties = null;
this.checkIncludes = false;
this.timeCreated = 0;
}
@@ -86,9 +77,9 @@ public class UpdatePlan implements SpiUpdatePlan {
/**
* Run the prepared statement binding for the 'update set' properties.
*/
public void bindSet(DmlHandler bind, Object bean) throws SQLException {
public void bindSet(DmlHandler bind, EntityBean bean) throws SQLException {
set.dmlBind(bind, checkIncludes, bean);
set.dmlBind(bind, bean);
// not strictly 'thread safe' but object assignment is atomic
Long touched = Long.valueOf(System.currentTimeMillis());
@@ -139,15 +130,4 @@ public class UpdatePlan implements SpiUpdatePlan {
return set;
}
/**
* Return the set of changed properties.
* <p>
* This can return null when all properties in the set are being bound in
* the update statement.
* </p>
*/
public Set<String> getProperties() {
return properties;
}
}