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

@@ -168,7 +168,7 @@ public final class BatchControl {
// special case where the same bean instance has been added
// to the batch more than once
if (logger.isDebugEnabled()) {
logger.debug("Bean instance already in this batch: " + request.getBean());
logger.debug("Bean instance already in this batch: " + request.getEntityBean());
}
return -1;
}
@@ -103,7 +103,7 @@ public class BatchedBeanHolder {
*/
public ArrayList<PersistRequest> getList(PersistRequestBean<?> request) {
Integer objHashCode = Integer.valueOf(System.identityHashCode(request.getBean()));
Integer objHashCode = Integer.valueOf(System.identityHashCode(request.getEntityBean()));
if (!beanHashCodes.add(objHashCode)) {
// special case where the same bean instance has already been
@@ -14,7 +14,6 @@ import com.avaje.ebean.Query;
import com.avaje.ebean.SqlUpdate;
import com.avaje.ebean.Transaction;
import com.avaje.ebean.Update;
import com.avaje.ebean.annotation.ConcurrencyMode;
import com.avaje.ebean.bean.BeanCollection;
import com.avaje.ebean.bean.BeanCollection.ModifyListenMode;
import com.avaje.ebean.bean.EntityBean;
@@ -39,7 +38,6 @@ import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne;
import com.avaje.ebeaninternal.server.deploy.IntersectionRow;
import com.avaje.ebeaninternal.server.deploy.ManyType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -72,11 +70,13 @@ public final class DefaultPersister implements Persister {
private final BeanDescriptorManager beanDescriptorManager;
public DefaultPersister(SpiEbeanServer server, Binder binder, BeanDescriptorManager descMgr, PstmtBatch pstmtBatch) {
this.server = server;
this.beanDescriptorManager = descMgr;
this.persistExecute = new DefaultPersistExecute(binder, pstmtBatch);
}
/**
@@ -153,20 +153,19 @@ public final class DefaultPersister implements Persister {
/**
* Force an Update using the given bean.
*/
public void forceUpdate(Object bean, Set<String> updateProps, Transaction t, boolean deleteMissingChildren, boolean updateNullProperties) {
public void forceUpdate(EntityBean bean, Set<String> updateProps, Transaction t, boolean deleteMissingChildren, boolean updateNullProperties) {
if (bean == null) {
throw new NullPointerException(Message.msg("bean.isnull"));
EntityBean entityBean = (EntityBean)bean;
EntityBeanIntercept ebi = entityBean._ebean_getIntercept();
if (ebi.isNew()) {
ebi.setNewBeanForUpdate();
}
if (updateProps == null) {
// checking to see if this is just a 'normal' update
if (bean instanceof EntityBean) {
EntityBeanIntercept ebi = ((EntityBean) bean)._ebean_getIntercept();
if (ebi.isDirty() || ebi.isLoaded()) {
// a 'normal' update using 'dirty' properties from internal bean state.
// if not dirty we still update in case any cascading save occurs
PersistRequestBean<?> req = createRequest(bean, t, null);
req.setStatelessUpdate(true, deleteMissingChildren, updateNullProperties);
try {
req.initTransIfRequired();
update(req);
@@ -178,76 +177,26 @@ public final class DefaultPersister implements Persister {
req.rollbackTransIfRequired();
throw ex;
}
} else if (ebi.isReference()) {
// just return as no point in cascading (no modified beans/lists)
return;
((SpiTransaction)t).logSql("-- No update as bean is just a reference bean");
return;
} else {
((SpiTransaction)t).logSql("-- No update as bean is not dirty");
}
// loadedProps set by Ebean JSON / XML Marshalling
updateProps = ebi.getLoadedProps();
}
}
BeanManager<?> mgr = getBeanManager(bean);
if (mgr == null) {
throw new PersistenceException(errNotRegistered(bean.getClass()));
}
forceUpdateStateless(bean, t, null, mgr, updateProps, deleteMissingChildren, updateNullProperties);
}
/**
* Force a 'stateless' update determining which properties to update.
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
private void forceUpdateStateless(Object bean, Transaction t, Object parentBean, BeanManager<?> mgr, Set<String> updateProps,
boolean deleteMissingChildren, boolean updateNullProperties) {
BeanDescriptor<?> descriptor = mgr.getBeanDescriptor();
// determine concurrency mode based on version property not null
ConcurrencyMode mode = descriptor.determineConcurrencyMode(bean);
if (updateProps == null) {
// determine based on null treatment (all properties updated or just the non-null ones)
updateProps = updateNullProperties ? null : descriptor.determineLoadedProperties(bean);
} else if (updateProps.isEmpty()) {
// in this case means we want to include all properties in the update
updateProps = null;
} else if (ConcurrencyMode.VERSION.equals(mode)) {
// check that the version property is included
String verName = descriptor.firstVersionProperty().getName();
if (!updateProps.contains(verName)) {
// defensively copy the updateProps and add the version property name
updateProps = new HashSet<String>(updateProps);
updateProps.add(verName);
}
}
PersistRequestBean<?> req = new PersistRequestBean(server, bean, parentBean, mgr, (SpiTransaction) t, persistExecute, updateProps, mode);
req.setStatelessUpdate(true, deleteMissingChildren, updateNullProperties);
try {
req.initTransIfRequired();
update(req);
req.commitTransIfRequired();
} catch (RuntimeException ex) {
req.rollbackTransIfRequired();
throw ex;
}
}
public void save(Object bean, Transaction t) {
public void save(EntityBean bean, Transaction t) {
saveRecurse(bean, t, null);
}
/**
* Explicitly specify to insert this bean.
*/
public void forceInsert(Object bean, Transaction t) {
public void forceInsert(EntityBean bean, Transaction t) {
PersistRequestBean<?> req = createRequest(bean, t, null);
try {
@@ -320,7 +269,8 @@ public final class DefaultPersister implements Persister {
try {
request.setType(PersistRequest.Type.INSERT);
request.setNotNullAsLoaded();
if (request.isPersistCascade()) {
// save associated One beans recursively first
saveAssocOne(request);
@@ -379,7 +329,7 @@ public final class DefaultPersister implements Persister {
/**
* Delete the bean with the explicit transaction.
*/
public void delete(Object bean, Transaction t) {
public void delete(EntityBean bean, Transaction t) {
PersistRequestBean<?> req = createRequest(bean, t, null);
if (req.isRegisteredForDeleteBean()) {
@@ -404,7 +354,7 @@ public final class DefaultPersister implements Persister {
private void deleteList(List<?> beanList, Transaction t) {
for (int i = 0; i < beanList.size(); i++) {
Object bean = beanList.get(i);
EntityBean bean = (EntityBean)beanList.get(i);
delete(bean, t);
}
}
@@ -468,7 +418,7 @@ public final class DefaultPersister implements Persister {
if (t.isLogSummary()) {
t.logSummary("-- DeleteById of " + descriptor.getName() + " id[" + id + "] requires fetch of foreign key values");
}
Object bean = server.findUnique(q, t);
EntityBean bean = (EntityBean)server.findUnique(q, t);
if (bean == null) {
return 0;
} else {
@@ -610,7 +560,7 @@ public final class DefaultPersister implements Persister {
*/
private void saveAssocMany(boolean insertedParent, PersistRequestBean<?> request) {
Object parentBean = request.getBean();
EntityBean parentBean = request.getEntityBean();
BeanDescriptor<?> desc = request.getBeanDescriptor();
SpiTransaction t = request.getTransaction();
@@ -648,14 +598,14 @@ public final class DefaultPersister implements Persister {
private static class SaveManyPropRequest {
private final boolean insertedParent;
private final BeanPropertyAssocMany<?> many;
private final Object parentBean;
private final EntityBean parentBean;
private final SpiTransaction t;
private final boolean cascade;
private final boolean statelessUpdate;
private final boolean deleteMissingChildren;
private final boolean updateNullProperties;
private SaveManyPropRequest(boolean insertedParent, BeanPropertyAssocMany<?> many, Object parentBean, PersistRequestBean<?> request) {
private SaveManyPropRequest(boolean insertedParent, BeanPropertyAssocMany<?> many, EntityBean parentBean, PersistRequestBean<?> request) {
this.insertedParent = insertedParent;
this.many = many;
this.cascade = many.getCascadeInfo().isSave();
@@ -666,7 +616,7 @@ public final class DefaultPersister implements Persister {
this.updateNullProperties = request.isUpdateNullProperties();
}
private SaveManyPropRequest(BeanPropertyAssocMany<?> many, Object parentBean, SpiTransaction t) {
private SaveManyPropRequest(BeanPropertyAssocMany<?> many, EntityBean parentBean, SpiTransaction t) {
this.insertedParent = false;
this.many = many;
this.parentBean = parentBean;
@@ -709,7 +659,7 @@ public final class DefaultPersister implements Persister {
return many;
}
private Object getParentBean() {
private EntityBean getParentBean() {
return parentBean;
}
@@ -817,7 +767,7 @@ public final class DefaultPersister implements Persister {
// set it to the appropriate property on the
// detail bean before we save it
boolean isMap = ManyType.JAVA_MAP.equals(prop.getManyType());
Object parentBean = saveMany.getParentBean();
EntityBean parentBean = (EntityBean)saveMany.getParentBean();
Object mapKeyValue = null;
boolean saveSkippable = prop.isSaveRecurseSkippable();
@@ -831,59 +781,58 @@ public final class DefaultPersister implements Persister {
detailBean = entry.getValue();
}
if (prop.isManyToMany()) {
if (detailBean instanceof EntityBean) {
skipSavingThisBean = ((EntityBean) detailBean)._ebean_getIntercept().isReference();
}
if (detailBean instanceof EntityBean == false) {
skipSavingThisBean = true;
logger.debug("Skip non entity bean");
} else {
// set the 'parent/master' bean to the detailBean as long
// as we don't make it 'dirty' in doing so
if (detailBean instanceof EntityBean) {
EntityBeanIntercept ebi = ((EntityBean) detailBean)._ebean_getIntercept();
if (ebi.isNewOrDirty()) {
// set the parent bean to detailBean
prop.setJoinValuesToChild(parentBean, detailBean, mapKeyValue);
} else if (ebi.isReference()) {
// we can skip this one
skipSavingThisBean = true;
EntityBean detail = (EntityBean)detailBean;
if (prop.isManyToMany()) {
skipSavingThisBean = detail._ebean_getIntercept().isReference();
} else {
EntityBeanIntercept ebi = detail._ebean_getIntercept();
if (ebi.isNewOrDirty()) {
skipSavingThisBean = false;
// set the parent bean to detailBean
prop.setJoinValuesToChild(parentBean, detail, mapKeyValue);
} else if (ebi.isReference()) {
// we can skip this one
skipSavingThisBean = true;
} else {
// unmodified so skip depending on prop.isSaveRecurseSkippable();
skipSavingThisBean = saveSkippable;
}
} else {
// set the parent bean to detailBean
prop.setJoinValuesToChild(parentBean, detailBean, mapKeyValue);
}
}
} else {
// unmodified so skip depending on prop.isSaveRecurseSkippable();
skipSavingThisBean = saveSkippable;
}
}
if (skipSavingThisBean) {
// unmodified bean that does not recurse its save
// so we can skip the save for this bean.
// Reset skipSavingThisBean for the next detailBean
skipSavingThisBean = false;
if (skipSavingThisBean) {
// unmodified bean that does not recurse its save
// so we can skip the save for this bean.
// Reset skipSavingThisBean for the next detailBean
skipSavingThisBean = false;
} else if (!saveMany.isStatelessUpdate()) {
// normal save recurse
saveRecurse(detailBean, t, parentBean);
} else if (!saveMany.isStatelessUpdate()) {
// normal save recurse
saveRecurse(detailBean, t, parentBean);
} else {
if (targetDescriptor.isStatelessUpdate(detailBean)) {
// update based on the value of Version/Id properties
// cascade update in stateless mode
forceUpdate(detailBean, null, t, deleteMissingChildren, updateNullProperties);
} else {
// cascade insert
forceInsert(detailBean, t);
}
}
if (detailIds != null) {
// remember the Id (other details not in the collection) will be removed
Object id = targetDescriptor.getId(detailBean);
if (!DmlUtil.isNullOrZero(id)) {
detailIds.add(id);
}
} else {
if (targetDescriptor.isStatelessUpdate(detail)) {
// update based on the value of Version/Id properties
// cascade update in stateless mode
forceUpdate(detail, null, t, deleteMissingChildren, updateNullProperties);
} else {
// cascade insert
forceInsert(detail, t);
}
}
if (detailIds != null) {
// remember the Id (other details not in the collection) will be removed
Object id = targetDescriptor.getId(detail);
if (!DmlUtil.isNullOrZero(id)) {
detailIds.add(id);
}
}
}
}
@@ -895,14 +844,14 @@ public final class DefaultPersister implements Persister {
}
public int deleteManyToManyAssociations(Object ownerBean, String propertyName, Transaction t) {
public int deleteManyToManyAssociations(EntityBean ownerBean, String propertyName, Transaction t) {
BeanDescriptor<?> descriptor = beanDescriptorManager.getBeanDescriptor(ownerBean.getClass());
BeanPropertyAssocMany<?> prop = (BeanPropertyAssocMany<?>) descriptor.getBeanProperty(propertyName);
return deleteAssocManyIntersection(ownerBean, prop, t);
}
public void saveManyToManyAssociations(Object ownerBean, String propertyName, Transaction t) {
public void saveManyToManyAssociations(EntityBean ownerBean, String propertyName, Transaction t) {
BeanDescriptor<?> descriptor = beanDescriptorManager.getBeanDescriptor(ownerBean.getClass());
BeanPropertyAssocMany<?> prop = (BeanPropertyAssocMany<?>) descriptor.getBeanProperty(propertyName);
@@ -910,7 +859,7 @@ public final class DefaultPersister implements Persister {
saveAssocManyIntersection(new SaveManyPropRequest(prop, ownerBean, (SpiTransaction) t), false);
}
public void saveAssociation(Object parentBean, String propertyName, Transaction t) {
public void saveAssociation(EntityBean parentBean, String propertyName, Transaction t) {
BeanDescriptor<?> descriptor = beanDescriptorManager.getBeanDescriptor(parentBean.getClass());
SpiTransaction trans = (SpiTransaction) t;
@@ -995,7 +944,8 @@ public final class DefaultPersister implements Persister {
t.depth(+1);
if (additions != null && !additions.isEmpty()) {
for (Object otherBean : additions) {
for (Object other : additions) {
EntityBean otherBean = (EntityBean)other;
// the object from the 'other' side of the ManyToMany
if (deletions != null && deletions.remove(otherBean)) {
String m = "Inserting and Deleting same object? " + otherBean;
@@ -1019,7 +969,8 @@ public final class DefaultPersister implements Persister {
}
}
if (deletions != null && !deletions.isEmpty()) {
for (Object otherDelete : deletions) {
for (Object other : deletions) {
EntityBean otherDelete = (EntityBean)other;
// the object from the 'other' side of the ManyToMany
// build a intersection row for 'delete'
IntersectionRow intRow = prop.buildManyToManyMapBean(saveManyPropRequest.getParentBean(), otherDelete);
@@ -1032,7 +983,7 @@ public final class DefaultPersister implements Persister {
t.depth(-1);
}
private int deleteAssocManyIntersection(Object bean, BeanPropertyAssocMany<?> many, Transaction t) {
private int deleteAssocManyIntersection(EntityBean bean, BeanPropertyAssocMany<?> many, Transaction t) {
// delete all intersection rows for this bean
IntersectionRow intRow = many.buildManyToManyDeleteChildren(bean);
@@ -1053,7 +1004,7 @@ public final class DefaultPersister implements Persister {
t.depth(-1);
BeanDescriptor<?> desc = request.getBeanDescriptor();
Object parentBean = request.getBean();
EntityBean parentBean = request.getEntityBean();
BeanPropertyAssocOne<?>[] expOnes = desc.propertiesOneExportedDelete();
if (expOnes.length > 0) {
@@ -1096,7 +1047,8 @@ public final class DefaultPersister implements Persister {
if (modifyRemovals != null && !modifyRemovals.isEmpty()) {
// delete the orphans that have been removed from the collection
for (Object detailBean : modifyRemovals) {
for (Object detail : modifyRemovals) {
EntityBean detailBean = (EntityBean)detail;
if (manys[i].hasId(detailBean)) {
deleteRecurse(detailBean, t);
}
@@ -1121,7 +1073,7 @@ public final class DefaultPersister implements Persister {
* collection (and should not be deleted).
* </p>
*/
private void deleteManyDetails(SpiTransaction t, BeanDescriptor<?> desc, Object parentBean,
private void deleteManyDetails(SpiTransaction t, BeanDescriptor<?> desc, EntityBean parentBean,
BeanPropertyAssocMany<?> many, ArrayList<Object> excludeDetailIds) {
if (many.getCascadeInfo().isDelete()) {
@@ -1223,9 +1175,12 @@ public final class DefaultPersister implements Persister {
// handled by DeleteUnloadedForeignKeys that was built
// via getDeleteUnloadedForeignKeys();
} else {
Object detailBean = prop.getValue(request.getBean());
if (detailBean != null && prop.hasId(detailBean)) {
deleteRecurse(detailBean, request.getTransaction());
Object detailBean = prop.getValue(request.getEntityBean());
if (detailBean != null) {
EntityBean detail = (EntityBean)detailBean;
if (prop.hasId(detail)) {
deleteRecurse(detail, request.getTransaction());
}
}
}
}
@@ -1247,7 +1202,7 @@ public final class DefaultPersister implements Persister {
return;
}
Object bean = request.getBean();
EntityBean bean = request.getEntityBean();
Object uid = idProp.getValue(bean);
if (DmlUtil.isNullOrZero(uid)) {
@@ -3,6 +3,7 @@ package com.avaje.ebeaninternal.server.persist;
import java.util.ArrayList;
import java.util.List;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.api.SpiEbeanServer;
import com.avaje.ebeaninternal.api.SpiQuery;
import com.avaje.ebeaninternal.api.SpiTransaction;
@@ -29,7 +30,7 @@ public class DeleteUnloadedForeignKeys {
private final PersistRequestBean<?> request;
private Object beanWithForeignKeys;
private EntityBean beanWithForeignKeys;
public DeleteUnloadedForeignKeys(SpiEbeanServer server, PersistRequestBean<?> request) {
this.server = server;
@@ -70,7 +71,7 @@ public class DeleteUnloadedForeignKeys {
if (t.isLogSummary()) {
t.logSummary("-- Ebean fetching foreign key values for delete of " + descriptor.getName() + " id:" + id);
}
beanWithForeignKeys = server.findUnique(q, t);
beanWithForeignKeys = (EntityBean)server.findUnique(q, t);
}
/**
@@ -84,7 +85,7 @@ public class DeleteUnloadedForeignKeys {
Object detailBean = prop.getValue(beanWithForeignKeys);
// if bean exists with a unique id then delete it
if (detailBean != null && prop.hasId(detailBean)) {
if (detailBean != null && prop.hasId((EntityBean)detailBean)) {
server.delete(detailBean, request.getTransaction());
}
}
@@ -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;
}
}
@@ -3,6 +3,7 @@ package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.sql.SQLException;
import java.util.List;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.persist.dml.GenerateDmlRequest;
@@ -25,32 +26,17 @@ public interface Bindable {
* For Updates including only changed properties add the Bindable to the
* list if it should be included in the 'update set'.
*/
public void addChanged(PersistRequestBean<?> request, List<Bindable> list);
public void addToUpdate(PersistRequestBean<?> request, List<Bindable> list);
/**
* append sql to the buffer with prefix and suffix options.
*/
public void dmlInsert(GenerateDmlRequest request, boolean checkIncludes);
/**
* append sql to the buffer with prefix and suffix options.
*/
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes);
/**
* For WHERE clauses append sql to the buffer with prefix and suffix
* options. These need to take into account binding of null values.
*/
public void dmlWhere(GenerateDmlRequest request, boolean checkIncludes, Object bean);
public void dmlAppend(GenerateDmlRequest request);
/**
* Bind given the request and bean. The bean could be the oldValues bean
* when binding a update or delete where clause with ALL concurrency mode.
*/
public void dmlBind(BindableRequest request, boolean checkIncludes, Object bean)
throws SQLException;
public void dmlBindWhere(BindableRequest request, boolean checkIncludes, Object bean)
throws SQLException;
public void dmlBind(BindableRequest request, EntityBean bean) throws SQLException;
}
@@ -3,6 +3,7 @@ package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.sql.SQLException;
import java.util.List;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.api.DerivedRelationshipData;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne;
@@ -14,75 +15,42 @@ import com.avaje.ebeaninternal.server.persist.dml.GenerateDmlRequest;
*/
public class BindableAssocOne implements Bindable {
private final BeanPropertyAssocOne<?> assocOne;
private final BeanPropertyAssocOne<?> assocOne;
private final ImportedId importedId;
private final ImportedId importedId;
public BindableAssocOne(BeanPropertyAssocOne<?> assocOne) {
this.assocOne = assocOne;
this.importedId = assocOne.getImportedId();
public BindableAssocOne(BeanPropertyAssocOne<?> assocOne) {
this.assocOne = assocOne;
this.importedId = assocOne.getImportedId();
}
public String toString() {
return "BindableAssocOne " + assocOne;
}
public void addToUpdate(PersistRequestBean<?> request, List<Bindable> list) {
if (request.isAddToUpdate(assocOne)) {
list.add(this);
}
}
public String toString() {
return "BindableAssocOne " + assocOne;
}
public void addChanged(PersistRequestBean<?> request, List<Bindable> list) {
if (request.hasChanged(assocOne)) {
list.add(this);
}
}
public void dmlInsert(GenerateDmlRequest request, boolean checkIncludes) {
dmlAppend(request, checkIncludes);
}
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes) {
if (checkIncludes && !request.isIncluded(assocOne)) {
return;
}
importedId.dmlAppend(request);
}
/**
* Used for dynamic where clause generation.
*/
public void dmlWhere(GenerateDmlRequest request, boolean checkIncludes, Object bean) {
if (checkIncludes && !request.isIncludedWhere(assocOne)) {
return;
}
Object assocBean = assocOne.getValue(bean);
importedId.dmlWhere(request, assocBean);
}
public void dmlBind(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
if (checkIncludes && !request.isIncluded(assocOne)) {
return;
}
dmlBind(request, bean, true);
}
public void dmlBindWhere(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
if (checkIncludes && !request.isIncludedWhere(assocOne)) {
return;
}
dmlBind(request, bean, false);
}
private void dmlBind(BindableRequest request, Object bean, boolean bindNull)
throws SQLException {
Object assocBean = assocOne.getValue(bean);
Object boundValue = importedId.bind(request, assocBean, bindNull);
if (bindNull && boundValue == null && assocBean != null){
// this is the scenario for a derived foreign key
// which will require an additional update
// register for post insert of assocBean
// update of bean set ... importedId.getLogicalName();
// value of assocBean.getId
DerivedRelationshipData d = new DerivedRelationshipData(assocBean, assocOne.getName(), bean);
request.registerDerivedRelationship(d);
}
public void dmlAppend(GenerateDmlRequest request) {
importedId.dmlAppend(request);
}
public void dmlBind(BindableRequest request, EntityBean bean) throws SQLException {
EntityBean assocBean = (EntityBean)assocOne.getValue(bean);
Object boundValue = importedId.bind(request, assocBean);
if (boundValue == null && assocBean != null) {
// this is the scenario for a derived foreign key
// which will require an additional update
// register for post insert of assocBean
// update of bean set ... importedId.getLogicalName();
// value of assocBean.getId
DerivedRelationshipData d = new DerivedRelationshipData(assocBean, assocOne.getName(), bean);
request.registerDerivedRelationship(d);
}
}
}
@@ -4,6 +4,7 @@ import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyCompound;
import com.avaje.ebeaninternal.server.persist.dml.GenerateDmlRequest;
@@ -26,62 +27,27 @@ public class BindableCompound implements Bindable {
return "BindableCompound " + compound + " items:" + Arrays.toString(items);
}
public void dmlInsert(GenerateDmlRequest request, boolean checkIncludes) {
dmlAppend(request, checkIncludes);
}
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes) {
if (checkIncludes && !request.isIncluded(compound)) {
return;
}
public void dmlAppend(GenerateDmlRequest request) {
for (int i = 0; i < items.length; i++) {
items[i].dmlAppend(request, false);
items[i].dmlAppend(request);
}
}
/**
* Used for dynamic where clause generation.
*/
public void dmlWhere(GenerateDmlRequest request, boolean checkIncludes, Object origBean) {
if (checkIncludes && !request.isIncludedWhere(compound)) {
return;
}
Object valueObject = compound.getValue(origBean);
for (int i = 0; i < items.length; i++) {
items[i].dmlWhere(request, false, valueObject);
}
}
public void addChanged(PersistRequestBean<?> request, List<Bindable> list) {
if (request.hasChanged(compound)) {
public void addToUpdate(PersistRequestBean<?> request, List<Bindable> list) {
if (request.isAddToUpdate(compound)) {
list.add(this);
}
}
public void dmlBind(BindableRequest bindRequest, boolean checkIncludes, Object bean) throws SQLException {
if (checkIncludes && !bindRequest.isIncluded(compound)) {
return;
}
public void dmlBind(BindableRequest bindRequest, EntityBean bean) throws SQLException {
Object valueObject = compound.getValue(bean);
for (int i = 0; i < items.length; i++) {
items[i].dmlBind(bindRequest, false, valueObject);
}
throw new RuntimeException("This is broken, need to break out the scalar values!!");
//Object valueObject = compound.getValue(bean);
//for (int i = 0; i < items.length; i++) {
// items[i].dmlBind(bindRequest, valueObject);
//}
}
public void dmlBindWhere(BindableRequest bindRequest, boolean checkIncludes, Object bean) throws SQLException {
if (checkIncludes && !bindRequest.isIncludedWhere(compound)) {
return;
}
Object valueObject = compound.getValue(bean);
for (int i = 0; i < items.length; i++) {
items[i].dmlBindWhere(bindRequest, false, valueObject);
}
}
}
@@ -5,6 +5,7 @@ import java.util.List;
import javax.persistence.PersistenceException;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.deploy.InheritInfo;
import com.avaje.ebeaninternal.server.persist.dml.GenerateDmlRequest;
@@ -28,31 +29,15 @@ public class BindableDiscriminator implements Bindable {
return columnName + " = " + discValue;
}
public void addChanged(PersistRequestBean<?> request, List<Bindable> list) {
public void addToUpdate(PersistRequestBean<?> request, List<Bindable> list) {
throw new PersistenceException("Never called (only for inserts)");
}
public void dmlInsert(GenerateDmlRequest request, boolean checkIncludes) {
dmlAppend(request, checkIncludes);
}
/**
* Never used in where clause.
*/
public void dmlWhere(GenerateDmlRequest request, boolean checkIncludes, Object bean) {
// never used in where
}
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes) {
public void dmlAppend(GenerateDmlRequest request) {
request.appendColumn(columnName);
}
public void dmlBind(BindableRequest bindRequest, boolean checkIncludes, Object bean) throws SQLException {
bindRequest.bind(columnName, discValue, sqlType);
}
public void dmlBindWhere(BindableRequest bindRequest, boolean checkIncludes, Object bean) throws SQLException {
public void dmlBind(BindableRequest bindRequest, EntityBean bean) throws SQLException {
bindRequest.bind(columnName, discValue, sqlType);
}
@@ -18,103 +18,43 @@ public class BindableEmbedded implements Bindable {
private final BeanPropertyAssocOne<?> embProp;
public BindableEmbedded(BeanPropertyAssocOne<?> embProp, List<Bindable> list) {
public BindableEmbedded(BeanPropertyAssocOne<?> embProp, List<Bindable> bindList) {
this.embProp = embProp;
this.items = list.toArray(new Bindable[list.size()]);
this.items = bindList.toArray(new Bindable[bindList.size()]); //this.props = propList.toArray(new BeanProperty[propList.size()]);
}
public String toString() {
return "BindableEmbedded " + embProp + " items:" + Arrays.toString(items);
}
public void dmlInsert(GenerateDmlRequest request, boolean checkIncludes) {
dmlAppend(request, checkIncludes);
}
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes) {
if (checkIncludes && !request.isIncluded(embProp)) {
return;
}
public void dmlAppend(GenerateDmlRequest request) {
for (int i = 0; i < items.length; i++) {
items[i].dmlAppend(request, false);
items[i].dmlAppend(request);
}
}
/**
* Used for dynamic where clause generation.
*/
public void dmlWhere(GenerateDmlRequest request, boolean checkIncludes, Object origBean) {
if (checkIncludes && !request.isIncludedWhere(embProp)) {
return;
}
Object embBean = embProp.getValue(origBean);
Object oldValues = getOldValue(embBean);
for (int i = 0; i < items.length; i++) {
items[i].dmlWhere(request, false, oldValues);
}
}
public void addChanged(PersistRequestBean<?> request, List<Bindable> list) {
if (request.hasChanged(embProp)) {
public void addToUpdate(PersistRequestBean<?> request, List<Bindable> list) {
if (request.isAddToUpdate(embProp)) {
list.add(this);
}
}
public void dmlBind(BindableRequest bindRequest, boolean checkIncludes, Object bean)
throws SQLException {
if (checkIncludes && !bindRequest.isIncluded(embProp)) {
return;
}
public void dmlBind(BindableRequest bindRequest, EntityBean bean) throws SQLException {
// get the embedded bean
Object embBean = embProp.getValue(bean);
// get the embedded bean
EntityBean embBean = (EntityBean)embProp.getValue(bean);
if (embBean == null) {
for (int i = 0; i < items.length; i++) {
items[i].dmlBind(bindRequest, false, embBean);
items[i].dmlBind(bindRequest, null);
}
}
public void dmlBindWhere(BindableRequest bindRequest, boolean checkIncludes, Object bean)
throws SQLException {
if (checkIncludes && !bindRequest.isIncludedWhere(embProp)) {
return;
}
// get the embedded bean
Object embBean = embProp.getValue(bean);
Object oldEmbBean = getOldValue(embBean);
} else {
//EntityBeanIntercept ebi = embBean._ebean_getIntercept();
for (int i = 0; i < items.length; i++) {
items[i].dmlBindWhere(bindRequest, false, oldEmbBean);
//if (ebi.isLoadedProperty(props[i].getPropertyIndex())) {
items[i].dmlBind(bindRequest, embBean);
//}
}
}
}
/**
* Get the old bean which will have the original values.
* <p>
* These are bound to the WHERE clause for updates.
* </p>
*/
private Object getOldValue(Object embBean) {
Object oldValues = null;
if (embBean instanceof EntityBean) {
// get the old embedded bean (with the original values)
oldValues = ((EntityBean) embBean)._ebean_getIntercept().getOldValues();
}
if (oldValues == null) {
// this embedded bean was not modified
// (or not an EntityBean)
oldValues = embBean;
}
return oldValues;
}
}
@@ -4,6 +4,7 @@ import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.persist.dml.GenerateDmlRequest;
@@ -26,59 +27,25 @@ public class BindableEncryptedProperty implements Bindable {
return prop.toString();
}
public void addChanged(PersistRequestBean<?> request, List<Bindable> list) {
if (request.hasChanged(prop)) {
public void addToUpdate(PersistRequestBean<?> request, List<Bindable> list) {
if (request.isAddToUpdate(prop)) {
list.add(this);
}
}
public void dmlInsert(GenerateDmlRequest request, boolean checkIncludes) {
public void dmlAppend(GenerateDmlRequest request) {
if (checkIncludes && !request.isIncluded(prop)) {
return;
}
// columnName
// AES_ENCRYPT(?,?)
// columnName = AES_ENCRYPT(?,?)
request.appendColumn(prop.getDbColumn(), prop.getDbBind());
}
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes) {
if (checkIncludes && !request.isIncluded(prop)) {
return;
}
// columnName = AES_ENCRYPT(?,?)
request.appendColumn(prop.getDbColumn(), "=", prop.getDbBind());
}
/**
* Used for dynamic where clause generation.
*/
public void dmlWhere(GenerateDmlRequest request, boolean checkIncludes, Object bean) {
// only include encrypted property in where when it is included
// in the update as well (so not using isIncludedWhere)
if (checkIncludes && !request.isIncluded(prop)) {
return;
}
if (bean == null || request.isDbNull(prop.getValue(bean))) {
request.appendColumnIsNull(prop.getDbColumn());
} else {
// ? = AES_DECRYPT(columnName,?)
request.appendColumn("? = ", prop.getDecryptSql());
}
}
/**
* Bind a value in a Insert SET clause.
*/
public void dmlBind(BindableRequest request, boolean checkIncludes, Object bean)
public void dmlBind(BindableRequest request, EntityBean bean)
throws SQLException {
if (checkIncludes && !request.isIncluded(prop)) {
return;
}
Object value = null;
if (bean != null) {
value = prop.getValue(bean);
@@ -91,35 +58,12 @@ public class BindableEncryptedProperty implements Bindable {
// H2 encrypt function ... different parameter order
request.bindNoLog(encryptKeyValue, Types.VARCHAR, prop.getName() + "=****");
}
request.bindNoLog(value, prop, prop.getName(), true);
request.bindNoLog(value, prop, prop.getName());
if (bindEncryptDataFirst){
// MySql, Postgres, Oracle
request.bindNoLog(encryptKeyValue, Types.VARCHAR, prop.getName() + "=****");
}
}
/**
* Bind a value in a Insert SET clause.
*/
public void dmlBindWhere(BindableRequest request, boolean checkIncludes, Object bean)
throws SQLException {
if (checkIncludes && !request.isIncluded(prop)) {
return;
}
Object value = null;
if (bean != null) {
value = prop.getValue(bean);
}
// get Encrypt key
String encryptKeyValue = prop.getEncryptKey().getStringValue();
request.bind(value, prop, prop.getName(), false);
request.bindNoLog(encryptKeyValue, Types.VARCHAR, prop.getName() + "=****");
}
}
@@ -6,6 +6,7 @@ import java.util.List;
import javax.persistence.PersistenceException;
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.deploy.BeanProperty;
@@ -50,53 +51,24 @@ public final class BindableIdEmbedded implements BindableId {
/**
* Does nothing for BindableId.
*/
public void addChanged(PersistRequestBean<?> request, List<Bindable> list) {
public void addToUpdate(PersistRequestBean<?> request, List<Bindable> list) {
// do nothing (id not changing)
}
public void dmlBind(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
dmlBind(request, checkIncludes, bean, true);
}
public void dmlBind(BindableRequest request, EntityBean bean) throws SQLException {
public void dmlBindWhere(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
dmlBind(request, checkIncludes, bean, false);
}
private void dmlBind(BindableRequest bindRequest, boolean checkIncludes, Object bean, boolean bindNull) throws SQLException {
if (checkIncludes && !bindRequest.isIncluded(embId)) {
return;
}
Object idValue = embId.getValue(bean);
EntityBean idValue = (EntityBean)embId.getValue(bean);
for (int i = 0; i < props.length; i++) {
Object value = props[i].getValue(idValue);
bindRequest.bind(value, props[i], props[i].getDbColumn(), bindNull);
request.bind(value, props[i], props[i].getDbColumn());
}
bindRequest.setIdValue(idValue);
request.setIdValue(idValue);
}
/**
* Id values are never null in where clause.
*/
public void dmlWhere(GenerateDmlRequest request, boolean checkIncludes, Object bean) {
if (checkIncludes && !request.isIncluded(embId)) {
return;
}
dmlAppend(request, false);
}
public void dmlInsert(GenerateDmlRequest request, boolean checkIncludes) {
dmlAppend(request, checkIncludes);
}
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes) {
if (checkIncludes && !request.isIncluded(embId)) {
return;
}
public void dmlAppend(GenerateDmlRequest request) {
for (int i = 0; i < props.length; i++) {
request.appendColumn(props[i].getDbColumn());
}
@@ -111,10 +83,10 @@ public final class BindableIdEmbedded implements BindableId {
throw new PersistenceException(m);
}
Object bean = persist.getBean();
EntityBean bean = persist.getEntityBean();
// create the new id
Object newId = embId.createEmbeddedId();
EntityBean newId = (EntityBean)embId.createEmbeddedId();
// populate it from the assoc one id values...
for (int i = 0; i < matches.length; i++) {
@@ -3,6 +3,7 @@ package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.sql.SQLException;
import java.util.List;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.persist.dml.GenerateDmlRequest;
@@ -12,27 +13,15 @@ public class BindableIdEmpty implements BindableId {
return true;
}
public void addChanged(PersistRequestBean<?> request, List<Bindable> list) {
public void addToUpdate(PersistRequestBean<?> request, List<Bindable> list) {
// nothing
}
public void dmlInsert(GenerateDmlRequest request, boolean checkIncludes) {
public void dmlAppend(GenerateDmlRequest request) {
// nothing
}
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes) {
// nothing
}
public void dmlWhere(GenerateDmlRequest request, boolean checkIncludes, Object bean) {
// nothing
}
public void dmlBind(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
// nothing
}
public void dmlBindWhere(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
public void dmlBind(BindableRequest request, EntityBean bean) throws SQLException {
// nothing
}
@@ -7,6 +7,7 @@ import java.util.List;
import javax.persistence.PersistenceException;
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.deploy.BeanProperty;
@@ -47,49 +48,29 @@ public final class BindableIdMap implements BindableId {
/**
* Does nothing for BindableId.
*/
public void addChanged(PersistRequestBean<?> request, List<Bindable> list) {
public void addToUpdate(PersistRequestBean<?> request, List<Bindable> list) {
// do nothing (id not changing)
}
/**
* Id values are never null in where clause.
*/
public void dmlWhere(GenerateDmlRequest request, boolean checkIncludes, Object bean) {
// id values are never null in where clause
dmlAppend(request, false);
}
public void dmlInsert(GenerateDmlRequest request, boolean checkIncludes) {
dmlAppend(request, checkIncludes);
}
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes) {
public void dmlAppend(GenerateDmlRequest request) {
for (int i = 0; i < uids.length; i++) {
request.appendColumn(uids[i].getDbColumn());
}
}
public void dmlBind(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
dmlBind(request, checkIncludes, bean, true);
}
public void dmlBindWhere(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
dmlBind(request, checkIncludes, bean, false);
}
private void dmlBind(BindableRequest bindRequest, boolean checkIncludes, Object bean, boolean bindNull) throws SQLException {
public void dmlBind(BindableRequest request, EntityBean bean) throws SQLException {
LinkedHashMap<String, Object> mapId = new LinkedHashMap<String, Object>();
for (int i = 0; i < uids.length; i++) {
Object value = uids[i].getValue(bean);
bindRequest.bind(value, uids[i], uids[i].getName(), bindNull);
request.bind(value, uids[i], uids[i].getName());
// putting logicalType into map rather than
// the dbType (which may have been converted).
mapId.put(uids[i].getName(), value);
}
bindRequest.setIdValue(mapId);
request.setIdValue(mapId);
}
public boolean deriveConcatenatedId(PersistRequestBean<?> persist) {
@@ -101,7 +82,7 @@ public final class BindableIdMap implements BindableId {
throw new PersistenceException(m);
}
Object bean = persist.getBean();
EntityBean bean = persist.getEntityBean();
// populate it from the assoc one id values...
for (int i = 0; i < matches.length; i++) {
@@ -5,6 +5,7 @@ import java.util.List;
import javax.persistence.PersistenceException;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.persist.dml.GenerateDmlRequest;
@@ -40,7 +41,7 @@ public final class BindableIdScalar implements BindableId {
/**
* Does nothing for BindableId.
*/
public void addChanged(PersistRequestBean<?> request, List<Bindable> list) {
public void addToUpdate(PersistRequestBean<?> request, List<Bindable> list) {
// do nothing (id not changing)
}
@@ -51,39 +52,19 @@ public final class BindableIdScalar implements BindableId {
throw new PersistenceException("Should not be called? only for concatinated keys");
}
/**
* Id values are never null in where clause.
*/
public void dmlWhere(GenerateDmlRequest request, boolean checkIncludes, Object bean) {
// id values are never null in where clause
request.appendColumn(uidProp.getDbColumn());
}
public void dmlInsert(GenerateDmlRequest request, boolean checkIncludes) {
dmlAppend(request, checkIncludes);
}
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes) {
public void dmlAppend(GenerateDmlRequest request) {
request.appendColumn(uidProp.getDbColumn());
}
public void dmlBind(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
dmlBind(request, checkIncludes, bean, true);
}
public void dmlBindWhere(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
dmlBind(request, checkIncludes, bean, false);
}
private void dmlBind(BindableRequest bindRequest, boolean checkIncludes, Object bean, boolean bindNull) throws SQLException {
public void dmlBind(BindableRequest request, EntityBean bean) throws SQLException {
Object value = uidProp.getValue(bean);
bindRequest.bind(value, uidProp, uidProp.getName(), bindNull);
request.bind(value, uidProp, uidProp.getName());
// used for summary logging
bindRequest.setIdValue(value);
request.setIdValue(value);
}
}
@@ -3,6 +3,7 @@ package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.sql.SQLException;
import java.util.List;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.persist.dml.GenerateDmlRequest;
@@ -23,45 +24,25 @@ public class BindableList implements Bindable {
}
}
public void addChanged(PersistRequestBean<?> request, List<Bindable> list) {
public void addToUpdate(PersistRequestBean<?> request, List<Bindable> list) {
for (int i = 0; i < items.length; i++) {
items[i].addChanged(request, list);
items[i].addToUpdate(request, list);
}
}
public void dmlInsert(GenerateDmlRequest request, boolean checkIncludes) {
public void dmlAppend(GenerateDmlRequest request) {
for (int i = 0; i < items.length; i++) {
items[i].dmlInsert(request, checkIncludes);
items[i].dmlAppend(request);
}
}
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes) {
for (int i = 0; i < items.length; i++) {
items[i].dmlAppend(request, checkIncludes);
}
}
public void dmlWhere(GenerateDmlRequest request, boolean checkIncludes, Object bean) {
for (int i = 0; i < items.length; i++) {
items[i].dmlWhere(request, checkIncludes, bean);
}
}
public void dmlBind(BindableRequest bindRequest, boolean checkIncludes, Object bean)
public void dmlBind(BindableRequest bindRequest, EntityBean bean)
throws SQLException {
for (int i = 0; i < items.length; i++) {
items[i].dmlBind(bindRequest, checkIncludes, bean);
items[i].dmlBind(bindRequest, bean);
}
}
public void dmlBindWhere(BindableRequest bindRequest, boolean checkIncludes, Object bean)
throws SQLException {
for (int i = 0; i < items.length; i++) {
items[i].dmlBindWhere(bindRequest, checkIncludes, bean);
}
}
}
@@ -3,6 +3,7 @@ package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.sql.SQLException;
import java.util.List;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.persist.dml.GenerateDmlRequest;
@@ -22,62 +23,23 @@ public class BindableProperty implements Bindable {
return prop.toString();
}
public void addChanged(PersistRequestBean<?> request, List<Bindable> list) {
if (request.hasChanged(prop)) {
public void addToUpdate(PersistRequestBean<?> request, List<Bindable> list) {
if (request.isAddToUpdate(prop)) {
list.add(this);
}
}
public void dmlInsert(GenerateDmlRequest request, boolean checkIncludes) {
dmlAppend(request, checkIncludes);
}
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes) {
if (checkIncludes && !request.isIncluded(prop)) {
return;
}
public void dmlAppend(GenerateDmlRequest request) {
request.appendColumn(prop.getDbColumn());
}
/**
* Used for dynamic where clause generation.
*/
public void dmlWhere(GenerateDmlRequest request, boolean checkIncludes, Object bean) {
if (checkIncludes && !request.isIncludedWhere(prop)) {
return;
}
public void dmlBind(BindableRequest request, EntityBean bean) throws SQLException {
if (bean == null || request.isDbNull(prop.getValue(bean))) {
request.appendColumnIsNull(prop.getDbColumn());
} else {
request.appendColumn(prop.getDbColumn());
}
}
public void dmlBind(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
if (checkIncludes && !request.isIncluded(prop)) {
return;
}
dmlBind(request, bean, true);
}
public void dmlBindWhere(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
if (checkIncludes && !request.isIncludedWhere(prop)) {
return;
}
dmlBind(request, bean, false);
}
private void dmlBind(BindableRequest request, Object bean, boolean bindNull)
throws SQLException {
Object value = null;
if (bean != null) {
value = prop.getValue(bean);
}
// value = prop.getDefaultValue();
request.bind(value, prop, prop.getName(), bindNull);
request.bind(value, prop, prop.getName());
}
}
@@ -2,6 +2,7 @@ package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.sql.SQLException;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.deploy.generatedproperty.GeneratedProperty;
import com.avaje.ebeaninternal.server.persist.dml.GenerateDmlRequest;
@@ -21,36 +22,24 @@ public class BindablePropertyInsertGenerated extends BindableProperty {
this.gen = gen;
}
public void dmlBind(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
dmlBind(request, checkIncludes, bean, true);
}
public void dmlBindWhere(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
dmlBind(request, checkIncludes, bean, false);
}
/**
* Bind a value in a Insert SET clause.
*/
private void dmlBind(BindableRequest request, boolean checkIncludes, Object bean, boolean bindNull) throws SQLException {
public void dmlBind(BindableRequest request, EntityBean bean) throws SQLException {
Object value = gen.getInsertValue(prop, bean);
// generated value should be the correct type
if (bean != null){
// support PropertyChangeSupport
prop.setValueIntercept(bean, value);
request.registerAdditionalProperty(prop.getName());
//prop.setValueIntercept(bean, value);
prop.setValue(bean, value);
}
//value = prop.getDefaultValue();
request.bind(value, prop, prop.getName(), bindNull);
}
request.bind(value, prop, prop.getName());
}
/**
* Always bind on Insert SET.
*/
@Override
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes){
public void dmlAppend(GenerateDmlRequest request){
request.appendColumn(prop.getDbColumn());
}
@@ -3,6 +3,7 @@ package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.sql.SQLException;
import java.util.List;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.deploy.generatedproperty.GeneratedProperty;
@@ -24,37 +25,27 @@ public class BindablePropertyUpdateGenerated extends BindableProperty {
}
/**
* Always add BindablePropertyUpdateGenerated properties.
*/
public void addChanged(PersistRequestBean<?> request, List<Bindable> list) {
list.add(this);
}
public void dmlBind(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
if (checkIncludes && !request.isIncluded(prop)){
return;
}
dmlBind(request, bean, true);
* Add BindablePropertyUpdateGenerated if the property is loaded.
*/
public void addToUpdate(PersistRequestBean<?> request, List<Bindable> list) {
if (gen.includeInAllUpdates()) {
list.add(this);
} else if (request.isLoadedProperty(prop)) {
list.add(this);
}
public void dmlBindWhere(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
if (checkIncludes && !request.isIncludedWhere(prop)){
return;
}
dmlBind(request, bean, false);
}
private void dmlBind(BindableRequest request, Object bean, boolean bindNull) throws SQLException {
}
Object value = gen.getUpdateValue(prop, bean);
public void dmlBind(BindableRequest request, EntityBean bean) throws SQLException {
Object value = gen.getUpdateValue(prop, bean);
// generated value should be the correct type
request.bind(value, prop, prop.getName(), bindNull);
// generated value should be the correct type
request.bind(value, prop, prop.getName());
// only register the update value if it was included
// in the bean in the first place
if (request.isIncluded(prop)) {
if (request.getPersistRequest().isLoadedProperty(prop)) {
//if (request.isIncluded(prop)) {
// need to set the generated value to the bean later
// after the where clause has been generated
request.registerUpdateGenValue(prop, bean, value);
@@ -65,10 +56,7 @@ public class BindablePropertyUpdateGenerated extends BindableProperty {
* Always bind on Insert SET.
*/
@Override
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes){
if (checkIncludes && !request.isIncluded(prop)){
return;
}
public void dmlAppend(GenerateDmlRequest request){
request.appendColumn(prop.getDbColumn());
}
@@ -2,6 +2,7 @@ package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.sql.SQLException;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.api.DerivedRelationshipData;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
@@ -31,7 +32,7 @@ public interface BindableRequest {
* @param bindNull
* if true bind null values, if false use IS NULL.
*/
public Object bind(Object value, BeanProperty prop, String propName, boolean bindNull) throws SQLException;
public Object bind(Object value, BeanProperty prop, String propName) throws SQLException;
/**
* Bind a raw value. Used to bind the discriminator column.
@@ -46,29 +47,14 @@ public interface BindableRequest {
/**
* Bind the value to the preparedStatement without logging.
*/
public Object bindNoLog(Object value, BeanProperty prop, String propName, boolean bindNull) throws SQLException;
public Object bindNoLog(Object value, BeanProperty prop, String propName) throws SQLException;
/**
* Return true if the property is included in this request.
*/
public boolean isIncluded(BeanProperty prop);
/**
* Return true if the property is included in the WHERE clause for this
* request.
*/
public boolean isIncludedWhere(BeanProperty prop);
/**
* Register the value from a update GeneratedValue. This can only be set to
* the bean property after the where clause has bean built.
*/
public void registerUpdateGenValue(BeanProperty prop, Object bean, Object value);
/**
* Register a property into loadedProperties if required.
*/
public void registerAdditionalProperty(String propertyName);
public void registerUpdateGenValue(BeanProperty prop, EntityBean bean, Object value);
/**
* Return the original PersistRequest.
@@ -5,6 +5,7 @@ import java.util.List;
import javax.persistence.PersistenceException;
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.deploy.BeanPropertyAssocOne;
@@ -37,33 +38,17 @@ public class BindableUnidirectional implements Bindable {
return "BindableShadowFKey " + unidirectional;
}
public void addChanged(PersistRequestBean<?> request, List<Bindable> list) {
public void addToUpdate(PersistRequestBean<?> request, List<Bindable> list) {
throw new PersistenceException("Never called (for insert only)");
}
public void dmlInsert(GenerateDmlRequest request, boolean checkIncludes) {
dmlAppend(request, checkIncludes);
}
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes) {
public void dmlAppend(GenerateDmlRequest request) {
// always included (in insert)
importedId.dmlAppend(request);
}
public void dmlWhere(GenerateDmlRequest request, boolean checkIncludes, Object bean) {
throw new RuntimeException("Never called");
}
public void dmlBind(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
dmlBind(request, checkIncludes, bean, true);
}
public void dmlBindWhere(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
dmlBind(request, checkIncludes, bean, false);
}
private void dmlBind(BindableRequest request, boolean checkIncludes, Object bean, boolean bindNull)
throws SQLException {
public void dmlBind(BindableRequest request, EntityBean bean) throws SQLException {
PersistRequestBean<?> persistRequest = request.getPersistRequest();
Object parentBean = persistRequest.getParentBean();
@@ -71,13 +56,13 @@ public class BindableUnidirectional implements Bindable {
if (parentBean == null) {
Class<?> localType = desc.getBeanType();
Class<?> targetType = unidirectional.getTargetType();
;
String msg = "Error inserting bean [" + localType + "] with unidirectional relationship. ";
msg += "For inserts you must use cascade save on the master bean [" + targetType + "].";
throw new PersistenceException(msg);
}
importedId.bind(request, parentBean, bindNull);
importedId.bind(request, (EntityBean)parentBean);
}
}
@@ -27,8 +27,6 @@ public class FactoryAssocOnes {
} else {
switch (mode) {
case WHERE:
break;
case INSERT:
if (!ones[i].isInsertable()) {
continue;
@@ -28,9 +28,10 @@ public class FactoryEmbedded {
for (int j = 0; j < embedded.length; j++) {
List<Bindable> bindList = new ArrayList<Bindable>();
BeanProperty[] props = embedded[j].getProperties();
List<Bindable> bindList = new ArrayList<Bindable>(props.length);
BeanProperty[] props = embedded[j].getProperties();
for (int i = 0; i < props.length; i++) {
Bindable item = factoryProperty.create(props[i], mode, withLobs);
if (item != null){
@@ -32,11 +32,11 @@ public class FactoryProperty {
}
if (prop.isLob()) {
if (DmlMode.WHERE.equals(mode) || !withLobs) {
if (!withLobs) {
// Lob exclusion
return null;
} else {
return prop.isDbEncrypted() ? new BindableEncryptedProperty(prop, bindEncryptDataFirst) : new BindableProperty(prop);
return prop.isDbEncrypted() ? new BindableEncryptedProperty(prop, bindEncryptDataFirst) : new BindableProperty(prop);
}
}
@@ -1,11 +1,7 @@
package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.util.ArrayList;
import java.util.List;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne;
/**
* Creates a Bindable to support version concurrency where clauses.
@@ -21,40 +17,11 @@ public class FactoryVersion {
*/
public Bindable create(BeanDescriptor<?> desc) {
List<Bindable> verList = new ArrayList<Bindable>();
BeanProperty[] vers = desc.propertiesVersion();
for (int i = 0; i < vers.length; i++) {
verList.add(new BindableProperty(vers[i]));
}
// version columns on embedded beans?
BeanPropertyAssocOne<?>[] embedded = desc.propertiesEmbedded();
for (int j = 0; j < embedded.length; j++) {
if (embedded[j].isEmbeddedVersion()) {
List<Bindable> bindList = new ArrayList<Bindable>();
BeanProperty[] embProps = embedded[j].getProperties();
for (int i = 0; i < embProps.length; i++) {
if (embProps[i].isVersion()){
bindList.add(new BindableProperty(embProps[i]));
}
}
verList.add(new BindableEmbedded(embedded[j], bindList));
}
}
if (verList.size() == 0){
return null;
}
if (verList.size() == 1){
return verList.get(0);
}
return new BindableList(verList);
BeanProperty versionProperty = desc.getVersionProperty();
if (versionProperty == null) {
return null;
}
return new BindableProperty(versionProperty);
}
}
@@ -1,5 +1,6 @@
package com.avaje.ebeaninternal.server.persist.dmlbind;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne;
@@ -30,14 +31,14 @@ class MatchedImportedProperty {
this.localProp = localProp;
}
protected void populate(Object sourceBean, Object destBean) {
protected void populate(EntityBean sourceBean, EntityBean destBean) {
Object assocBean = assocOne.getValue(sourceBean);
if (assocBean == null) {
String msg = "The assoc bean for " + assocOne + " is null?";
throw new NullPointerException(msg);
}
Object value = foreignProp.getValue(assocBean);
Object value = foreignProp.getValue((EntityBean)assocBean);
localProp.setValue(destBean, value);
}