#461 - ENH: Add @Draftable support - phase 1

This commit is contained in:
Robin Bygrave committed 2015-11-19 20:44:25 +13:00
1 parent 42e7103aa6
commit 7b76e13bce
56 files changed
+944 -214

No files matched your search

@@ -83,17 +83,7 @@ public final class DefaultPersister implements Persister {
*/
public int executeCallable(CallableSql callSql, Transaction t) {
PersistRequestCallableSql request = new PersistRequestCallableSql(server, callSql, (SpiTransaction) t, persistExecute);
try {
request.initTransIfRequired();
int rc = request.executeOrQueue();
request.commitTransIfRequired();
return rc;
} catch (RuntimeException e) {
request.rollbackTransIfRequired();
throw e;
}
return executeOrQueue(new PersistRequestCallableSql(server, callSql, (SpiTransaction) t, persistExecute));
}
/**
@@ -110,7 +100,10 @@ public final class DefaultPersister implements Persister {
throw new PersistenceException(msg);
}
PersistRequestOrmUpdate request = new PersistRequestOrmUpdate(server, mgr, ormUpdate, (SpiTransaction) t, persistExecute);
return executeOrQueue(new PersistRequestOrmUpdate(server, mgr, ormUpdate, (SpiTransaction) t, persistExecute));
}
private int executeOrQueue(PersistRequest request) {
try {
request.initTransIfRequired();
int rc = request.executeOrQueue();
@@ -128,17 +121,52 @@ public final class DefaultPersister implements Persister {
*/
public int executeSqlUpdate(SqlUpdate updSql, Transaction t) {
PersistRequestUpdateSql request = new PersistRequestUpdateSql(server, updSql, (SpiTransaction) t, persistExecute);
try {
request.initTransIfRequired();
int rc = request.executeOrQueue();
request.commitTransIfRequired();
return rc;
return executeOrQueue(new PersistRequestUpdateSql(server, updSql, (SpiTransaction) t, persistExecute));
}
} catch (RuntimeException e) {
request.rollbackTransIfRequired();
throw e;
@Override
public <T> void publish(Query<T> query, Transaction transaction) {
query.asDraft();
Class<T> beanType = query.getBeanType();
List<T> draftBeans = server.findList(query, transaction);
BeanDescriptor<T> desc = server.getBeanDescriptor(beanType);
// get the list of Id's
List<Object> idList = new ArrayList<Object>();
for (T draftBean: draftBeans) {
idList.add(desc.getBeanId(draftBean));
}
// fetch existing live beans to update (or insert if missing)
Map<?, T> liveBeans = server.find(beanType)
.where().idIn(idList)
.findMap();
List<T> livePublish = new ArrayList<T>(idList.size());
BeanManager<T> mgr = beanDescriptorManager.getBeanManager(beanType);
for (T draftBean: draftBeans) {
Object draftID = desc.getBeanId(draftBean);
T existingLiveBean = liveBeans.get(draftID);
T liveBean = desc.publish(draftBean, existingLiveBean);
livePublish.add(liveBean);
Type persistType = (existingLiveBean == null) ? Type.INSERT : Type.UPDATE;
PersistRequestBean<T> request = createRequest(liveBean, transaction, null, mgr, persistType, false);
request.setPublish();
if (persistType == Type.INSERT) {
insert(request);
} else {
update(request);
}
}
}
/**
@@ -55,7 +55,7 @@ public class InsertHandler extends DmlHandler {
public InsertHandler(PersistRequestBean<?> persist, InsertMeta meta) {
super(persist, meta.isEmptyStringToNull());
this.meta = meta;
this.concatinatedKey = meta.isConcatinatedKey();
this.concatinatedKey = meta.isConcatenatedKey();
}
/**
@@ -90,7 +90,7 @@ public class InsertHandler extends DmlHandler {
SpiTransaction t = persistRequest.getTransaction();
// get the appropriate sql
sql = meta.getSql(withId);
sql = meta.getSql(withId, persistRequest.isPublish());
PreparedStatement pstmt;
if (persistRequest.isBatched()) {
@@ -101,7 +101,7 @@ public class InsertHandler extends DmlHandler {
dataBind = new DataBind(pstmt);
// bind the bean property values
meta.bind(this, bean, withId);
meta.bind(this, bean, withId, persistRequest.isPublish());
logSql(sql);
}
@@ -167,15 +167,7 @@ public class InsertHandler extends DmlHandler {
ResultSet rset = dataBind.getPstmt().getGeneratedKeys();
try {
if (rset.next()) {
Object idValue = rset.getObject(1);
if (idValue != null) {
persistRequest.setGeneratedKey(idValue);
}
} else {
throw new PersistenceException(Message.msg("persist.autoinc.norows"));
}
setGeneratedKey(rset);
} finally {
try {
rset.close();
@@ -186,6 +178,18 @@ public class InsertHandler extends DmlHandler {
}
}
private void setGeneratedKey(ResultSet rset) throws SQLException {
if (rset.next()) {
Object idValue = rset.getObject(1);
if (idValue != null) {
persistRequest.setGeneratedKey(idValue);
}
} else {
throw new PersistenceException(Message.msg("persist.autoinc.norows"));
}
}
/**
* For non batch insert with DBs that do not support getGeneratedKeys. Use a
* SQL select to fetch back the Id value.
@@ -199,14 +203,7 @@ public class InsertHandler extends DmlHandler {
try {
stmt = conn.prepareStatement(selectLastInsertedId);
rset = stmt.executeQuery();
if (rset.next()) {
Object idValue = rset.getObject(1);
if (idValue != null) {
persistRequest.setGeneratedKey(idValue);
}
} else {
throw new PersistenceException(Message.msg("persist.autoinc.norows"));
}
setGeneratedKey(rset);
} finally {
try {
if (rset != null) {
@@ -8,6 +8,7 @@ import com.avaje.ebeaninternal.server.deploy.InheritInfo;
import com.avaje.ebeaninternal.server.persist.dmlbind.Bindable;
import com.avaje.ebeaninternal.server.persist.dmlbind.BindableDiscriminator;
import com.avaje.ebeaninternal.server.persist.dmlbind.BindableId;
import com.avaje.ebeaninternal.server.persist.dmlbind.BindableList;
import java.sql.SQLException;
@@ -18,21 +19,22 @@ import java.sql.SQLException;
public final class InsertMeta {
private final String sqlNullId;
private final String sqlWithId;
private final String sqlDraftNullId;
private final String sqlDraftWithId;
private final BindableId id;
private final Bindable discriminator;
private final Bindable all;
private final BindableList all;
private final BindableList allExcludeDraftOnly;
private final boolean supportsGetGeneratedKeys;
private final boolean concatinatedKey;
private final String tableName;
/**
* Used for DB that do not support getGeneratedKeys.
*/
@@ -44,16 +46,20 @@ public final class InsertMeta {
private final boolean emptyStringToNull;
public InsertMeta(DatabasePlatform dbPlatform, BeanDescriptor<?> desc, Bindable shadowFKey, BindableId id, Bindable all) {
public InsertMeta(DatabasePlatform dbPlatform, BeanDescriptor<?> desc, Bindable shadowFKey, BindableId id, BindableList all) {
this.emptyStringToNull = dbPlatform.isTreatEmptyStringsAsNull();
this.tableName = desc.getBaseTable();
this.discriminator = getDiscriminator(desc);
this.id = id;
this.all = all;
this.allExcludeDraftOnly = all.excludeDraftOnly();
this.shadowFKey = shadowFKey;
this.sqlWithId = genSql(false);
String tableName = desc.getBaseTable();
String draftTableName = desc.getDraftTable();
this.sqlWithId = genSql(false, tableName, false);
this.sqlDraftWithId = desc.isDraftable() ? genSql(false, draftTableName, true) : sqlWithId;
// only available for single Id property
if (id.isConcatenated()) {
@@ -61,6 +67,7 @@ public final class InsertMeta {
this.concatinatedKey = true;
this.identityDbColumns = null;
this.sqlNullId = null;
this.sqlDraftNullId = null;
this.supportsGetGeneratedKeys = false;
this.selectLastInsertedId = null;
@@ -76,7 +83,8 @@ public final class InsertMeta {
this.supportsGetGeneratedKeys = dbPlatform.getDbIdentity().isSupportsGetGeneratedKeys();
this.selectLastInsertedId = desc.getSelectLastInsertedId();
}
this.sqlNullId = genSql(true);
this.sqlNullId = genSql(true, tableName, false);
this.sqlDraftNullId = desc.isDraftable() ? genSql(false, draftTableName, true) : sqlNullId;
}
}
@@ -99,7 +107,7 @@ public final class InsertMeta {
/**
* Return true if this is a concatenated key.
*/
public boolean isConcatinatedKey() {
public boolean isConcatenatedKey() {
return concatinatedKey;
}
@@ -137,7 +145,7 @@ public final class InsertMeta {
/**
* Bind the request based on whether the id value(s) are null.
*/
public void bind(DmlHandler request, EntityBean bean, boolean withId) throws SQLException {
public void bind(DmlHandler request, EntityBean bean, boolean withId, boolean publish) throws SQLException {
if (withId) {
id.dmlBind(request, bean);
@@ -148,27 +156,31 @@ public final class InsertMeta {
if (discriminator != null) {
discriminator.dmlBind(request, bean);
}
all.dmlBind(request, bean);
if (publish) {
allExcludeDraftOnly.dmlBind(request, bean);
} else {
all.dmlBind(request, bean);
}
}
/**
* get the sql based whether the id value(s) are null.
*/
public String getSql(boolean withId) {
public String getSql(boolean withId, boolean publish) {
if (withId) {
return sqlWithId;
return publish ? sqlWithId : sqlDraftWithId;
} else {
return sqlNullId;
return publish ? sqlNullId : sqlDraftNullId;
}
}
private String genSql(boolean nullId) {
private String genSql(boolean nullId, String table, boolean draftTable) {
GenerateDmlRequest request = new GenerateDmlRequest();
request.setInsertSetMode();
request.append("insert into ").append(tableName);
request.append("insert into ").append(table);
request.append(" (");
if (!nullId) {
@@ -183,7 +195,11 @@ public final class InsertMeta {
discriminator.dmlAppend(request);
}
all.dmlAppend(request);
if (draftTable) {
all.dmlAppend(request);
} else {
allExcludeDraftOnly.dmlAppend(request);
}
request.append(") values (");
request.append(request.getInsertBindBuffer());
@@ -96,7 +96,7 @@ public class MetaFactory {
embeddedFact.create(allList, desc, DmlMode.INSERT, includeLobs);
assocOneFact.create(allList, desc, DmlMode.INSERT);
Bindable allBindable = new BindableList(allList);
BindableList allBindable = new BindableList(allList);
BeanPropertyAssocOne<?> unidirectional = desc.getUnidirectional();
@@ -2,11 +2,9 @@ package com.avaje.ebeaninternal.server.persist.dml;
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.persist.dmlbind.Bindable;
import com.avaje.ebeaninternal.server.persist.dmlbind.BindableId;
import com.avaje.ebeaninternal.server.persist.dmlbind.BindableList;
@@ -39,8 +37,8 @@ public final class UpdateMeta {
this.id = id;
this.version = version;
String sqlNone = genSql(ConcurrencyMode.NONE, set);
String sqlVersion = genSql(ConcurrencyMode.VERSION, set);
String sqlNone = genSql(ConcurrencyMode.NONE, set, desc.getBaseTable());
String sqlVersion = genSql(ConcurrencyMode.VERSION, set, desc.getBaseTable());
this.modeNoneUpdatePlan = new UpdatePlan(ConcurrencyMode.NONE, sqlNone, set);
this.modeVersionUpdatePlan = new UpdatePlan(ConcurrencyMode.VERSION, sqlVersion, set);
@@ -106,27 +104,10 @@ public final class UpdateMeta {
private SpiUpdatePlan getDynamicUpdatePlan(PersistRequestBean<?> persistRequest) {
EntityBeanIntercept ebi = persistRequest.getEntityBeanIntercept();
int hash;
if (persistRequest.determineUpdateAllLoadedProperties()) {
hash = ebi.getLoadedPropertyHash();
} else {
hash = ebi.getDirtyPropertyHash();
}
BeanDescriptor<?> beanDescriptor = persistRequest.getBeanDescriptor();
BeanProperty versionProperty = beanDescriptor.getVersionProperty();
if (versionProperty != null) {
if (ebi.isLoadedProperty(versionProperty.getPropertyIndex())) {
hash = hash * 31 + 7;
}
}
Integer key = hash;
int key = persistRequest.getUpdatePlanHash();
// check if we can use a cached UpdatePlan
BeanDescriptor<?> beanDescriptor = persistRequest.getBeanDescriptor();
SpiUpdatePlan updatePlan = beanDescriptor.getUpdatePlan(key);
if (updatePlan != null) {
return updatePlan;
@@ -142,7 +123,7 @@ public final class UpdateMeta {
ConcurrencyMode mode = persistRequest.determineConcurrencyMode();
// build the SQL for this update statement
String sql = genSql(mode, bindableList);
String sql = genSql(mode, bindableList, persistRequest.getUpdateTable());
updatePlan = new UpdatePlan(key, mode, sql, bindableList);
@@ -152,7 +133,7 @@ public final class UpdateMeta {
return updatePlan;
}
private String genSql(ConcurrencyMode conMode, BindableList bindableList) {
private String genSql(ConcurrencyMode conMode, BindableList bindableList, String tableName) {
// update set col0=?, col1=?, col2=? where bcol=? and bc1=? and bc2=?
@@ -39,4 +39,8 @@ public interface Bindable {
*/
void dmlBind(BindableRequest request, EntityBean bean) throws SQLException;
/**
* Return true if the underlying property is 'draft only'.
*/
boolean isDraftOnly();
}
@@ -28,6 +28,11 @@ public class BindableAssocOne implements Bindable {
return "BindableAssocOne " + assocOne;
}
@Override
public boolean isDraftOnly() {
return assocOne.isDraftOnly();
}
public void addToUpdate(PersistRequestBean<?> request, List<Bindable> list) {
if (request.isAddToUpdate(assocOne)) {
list.add(this);
@@ -27,6 +27,11 @@ public class BindableCompound implements Bindable {
return "BindableCompound " + compound + " items:" + Arrays.toString(items);
}
@Override
public boolean isDraftOnly() {
return false;
}
public void dmlAppend(GenerateDmlRequest request) {
for (int i = 0; i < items.length; i++) {
@@ -29,6 +29,11 @@ public class BindableDiscriminator implements Bindable {
return columnName + " = " + discValue;
}
@Override
public boolean isDraftOnly() {
return false;
}
@Override
public void addToUpdate(PersistRequestBean<?> request, List<Bindable> list) {
throw new PersistenceException("Never called (only for inserts)");
@@ -27,6 +27,11 @@ public class BindableEmbedded implements Bindable {
return "BindableEmbedded " + embProp + " items:" + Arrays.toString(items);
}
@Override
public boolean isDraftOnly() {
return embProp.isDraftOnly();
}
public void dmlAppend(GenerateDmlRequest request) {
for (int i = 0; i < items.length; i++) {
@@ -27,6 +27,11 @@ public class BindableEncryptedProperty implements Bindable {
return prop.toString();
}
@Override
public boolean isDraftOnly() {
return prop.isDraftOnly();
}
public void addToUpdate(PersistRequestBean<?> request, List<Bindable> list) {
if (request.isAddToUpdate(prop)) {
list.add(this);
@@ -30,6 +30,11 @@ public final class BindableIdEmbedded implements BindableId {
matches = MatchedImportedProperty.build(props, desc);
}
@Override
public boolean isDraftOnly() {
return false;
}
public boolean isEmpty() {
return false;
}
@@ -13,6 +13,11 @@ public class BindableIdEmpty implements BindableId {
return true;
}
@Override
public boolean isDraftOnly() {
return false;
}
public void addToUpdate(PersistRequestBean<?> request, List<Bindable> list) {
// nothing
}
@@ -38,6 +38,11 @@ public final class BindableIdScalar implements BindableId {
return uidProp.toString();
}
@Override
public boolean isDraftOnly() {
return false;
}
/**
* Does nothing for BindableId.
*/
@@ -1,6 +1,7 @@
package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.avaje.ebean.bean.EntityBean;
@@ -18,6 +19,24 @@ public class BindableList implements Bindable {
items = list.toArray(new Bindable[list.size()]);
}
/**
* Return a bindable list that excludes @DraftOnly properties.
*/
public BindableList excludeDraftOnly() {
List<Bindable> copy = new ArrayList<Bindable>(items.length);
for (Bindable b : items) {
if (!b.isDraftOnly()) {
copy.add(b);
}
}
return new BindableList(copy);
}
@Override
public boolean isDraftOnly() {
return false;
}
public void addAll(List<Bindable> list) {
for (int i = 0; i < items.length; i++) {
list.add(items[i]);
@@ -23,6 +23,11 @@ public class BindableProperty implements Bindable {
return prop.toString();
}
@Override
public boolean isDraftOnly() {
return prop.isDraftOnly();
}
public void addToUpdate(PersistRequestBean<?> request, List<Bindable> list) {
if (request.isAddToUpdate(prop)) {
list.add(this);
@@ -38,6 +38,11 @@ public class BindableUnidirectional implements Bindable {
return "BindableShadowFKey " + unidirectional;
}
@Override
public boolean isDraftOnly() {
return false;
}
public void addToUpdate(PersistRequestBean<?> request, List<Bindable> list) {
throw new PersistenceException("Never called (for insert only)");
}