mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-19 23:02:24 +00:00
#461 - ENH: Add @Draftable support - phase 1
This commit is contained in:
1 parent
42e7103aa6
commit
7b76e13bce
56 files changed
+944
-214
No files matched your search
@@ -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=?
|
||||
|
||||
|
||||
Reference in new issue
Block a user