No effective change - format only

This commit is contained in:
Robin Bygrave
2015-07-31 22:59:02 +12:00
parent 22d36bd1c4
commit 7ab374b4ee
8 changed files with 322 additions and 344 deletions
@@ -20,8 +20,6 @@ import java.sql.SQLException;
*/
public final class DmlBeanPersister implements BeanPersister {
private static final Logger logger = LoggerFactory.getLogger(DmlBeanPersister.class);
private final UpdateMeta updateMeta;
private final InsertMeta insertMeta;
@@ -5,89 +5,89 @@ package com.avaje.ebeaninternal.server.persist.dml;
*/
public class GenerateDmlRequest {
private static final String IS_NULL = " is null";
private static final String IS_NULL = " is null";
private final StringBuilder sb = new StringBuilder(100);
private final StringBuilder sb = new StringBuilder(100);
private StringBuilder insertBindBuffer;
private StringBuilder insertBindBuffer;
private String prefix;
private String prefix2;
private String prefix;
private String prefix2;
private int insertMode;
private int bindColumnCount;
/**
* Create from a PersistRequestBean.
*/
public GenerateDmlRequest() {
private int insertMode;
private int bindColumnCount;
/**
* Create from a PersistRequestBean.
*/
public GenerateDmlRequest() {
}
public GenerateDmlRequest append(String s) {
sb.append(s);
return this;
}
public void appendColumnIsNull(String column) {
appendColumn(column, IS_NULL);
}
public void appendColumn(String column) {
//String bind = (insertMode > 0) ? "?" : "=?";
appendColumn(column, "?");
}
public void appendColumn(String column, String bind) {
++bindColumnCount;
sb.append(prefix);
sb.append(column);
//sb.append(expr);
if (insertMode > 0) {
if (insertMode++ > 1) {
insertBindBuffer.append(",");
}
insertBindBuffer.append(bind);
} else {
sb.append("=");
sb.append(bind);
}
public GenerateDmlRequest append(String s) {
sb.append(s);
return this;
if (prefix2 != null) {
prefix = prefix2;
prefix2 = null;
}
}
public void appendColumnIsNull(String column) {
appendColumn(column, IS_NULL);
}
public int getBindColumnCount() {
return bindColumnCount;
}
public void appendColumn(String column) {
//String bind = (insertMode > 0) ? "?" : "=?";
appendColumn(column, "?");
}
public String getInsertBindBuffer() {
return insertBindBuffer.toString();
}
public void appendColumn(String column, String bind) {
public String toString() {
return sb.toString();
}
++bindColumnCount;
sb.append(prefix);
sb.append(column);
//sb.append(expr);
if (insertMode > 0) {
if (insertMode++ > 1) {
insertBindBuffer.append(",");
}
insertBindBuffer.append(bind);
} else {
sb.append("=");
sb.append(bind);
}
public void setWhereIdMode() {
this.prefix = "";
this.prefix2 = " and ";
}
if (prefix2 != null) {
prefix = prefix2;
prefix2 = null;
}
}
public void setInsertSetMode() {
this.insertBindBuffer = new StringBuilder(100);
this.insertMode = 1;
this.prefix = "";
this.prefix2 = ", ";
}
public int getBindColumnCount() {
return bindColumnCount;
}
public String getInsertBindBuffer() {
return insertBindBuffer.toString();
}
public String toString() {
return sb.toString();
}
public void setWhereIdMode() {
this.prefix = "";
this.prefix2 = " and ";
}
public void setInsertSetMode() {
this.insertBindBuffer = new StringBuilder(100);
this.insertMode = 1;
this.prefix = "";
this.prefix2 = ", ";
}
public void setUpdateSetMode() {
this.prefix = "";
this.prefix2 = ", ";
}
public void setUpdateSetMode() {
this.prefix = "";
this.prefix2 = ", ";
}
}
@@ -1,17 +1,5 @@
package com.avaje.ebeaninternal.server.persist.dml;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.persistence.OptimisticLockException;
import javax.persistence.PersistenceException;
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;
@@ -22,11 +10,22 @@ 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;
import javax.persistence.OptimisticLockException;
import javax.persistence.PersistenceException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
/**
* Insert bean handler.
*/
public class InsertHandler extends DmlHandler {
private static final Logger logger = LoggerFactory.getLogger(InsertHandler.class);
/**
@@ -143,18 +142,18 @@ public class InsertHandler extends DmlHandler {
List<DerivedRelationshipData> derivedRelationships = persistRequest.getDerivedRelationships();
if (derivedRelationships != null) {
SpiEbeanServer ebeanServer = (SpiEbeanServer)persistRequest.getEbeanServer();
SpiEbeanServer ebeanServer = (SpiEbeanServer) persistRequest.getEbeanServer();
for (int i = 0; i < derivedRelationships.size(); i++) {
DerivedRelationshipData derivedRelationshipData = derivedRelationships.get(i);
BeanDescriptor<?> beanDescriptor = ebeanServer.getBeanDescriptor(derivedRelationshipData.getBean().getClass());
BeanProperty prop = beanDescriptor.getBeanProperty(derivedRelationshipData.getLogicalName());
EntityBean entityBean = (EntityBean)derivedRelationshipData.getBean();
EntityBean entityBean = (EntityBean) derivedRelationshipData.getBean();
entityBean._ebean_getIntercept().markPropertyAsChanged(prop.getPropertyIndex());
ebeanServer.update(entityBean, transaction);
ebeanServer.update(entityBean, transaction);
}
}
}
@@ -1,7 +1,5 @@
package com.avaje.ebeaninternal.server.persist.dml;
import java.sql.SQLException;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebean.config.dbplatform.DatabasePlatform;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
@@ -11,185 +9,187 @@ 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 java.sql.SQLException;
/**
* Meta data for insert handler. The meta data is for a particular bean type. It
* is considered immutable and is thread safe.
*/
public final class InsertMeta {
private final String sqlNullId;
private final String sqlNullId;
private final String sqlWithId;
private final String sqlWithId;
private final BindableId id;
private final BindableId id;
private final Bindable discriminator;
private final Bindable discriminator;
private final Bindable all;
private final Bindable all;
private final boolean supportsGetGeneratedKeys;
private final boolean supportsGetGeneratedKeys;
private final boolean concatinatedKey;
private final boolean concatinatedKey;
private final String tableName;
private final String tableName;
/**
* Used for DB that do not support getGeneratedKeys.
*/
private final String selectLastInsertedId;
/**
* Used for DB that do not support getGeneratedKeys.
*/
private final String selectLastInsertedId;
private final Bindable shadowFKey;
private final String[] identityDbColumns;
private final boolean emptyStringToNull;
public InsertMeta(DatabasePlatform dbPlatform, BeanDescriptor<?> desc, Bindable shadowFKey, BindableId id, Bindable all) {
private final Bindable shadowFKey;
this.emptyStringToNull = dbPlatform.isTreatEmptyStringsAsNull();
this.tableName = desc.getBaseTable();
this.discriminator = getDiscriminator(desc);
this.id = id;
this.all = all;
this.shadowFKey = shadowFKey;
private final String[] identityDbColumns;
this.sqlWithId = genSql(false);
private final boolean emptyStringToNull;
// only available for single Id property
if (id.isConcatenated()) {
// concatenated key
this.concatinatedKey = true;
this.identityDbColumns = null;
this.sqlNullId = null;
this.supportsGetGeneratedKeys = false;
this.selectLastInsertedId = null;
public InsertMeta(DatabasePlatform dbPlatform, BeanDescriptor<?> desc, Bindable shadowFKey, BindableId id, Bindable all) {
} else {
// insert sql for db identity or sequence insert
this.concatinatedKey = false;
if (id.getIdentityColumn() == null) {
this.identityDbColumns = new String[]{};
this.supportsGetGeneratedKeys = false;
this.selectLastInsertedId = null;
} else {
this.identityDbColumns = new String[]{id.getIdentityColumn()};
this.supportsGetGeneratedKeys = dbPlatform.getDbIdentity().isSupportsGetGeneratedKeys();
this.selectLastInsertedId = desc.getSelectLastInsertedId();
}
this.sqlNullId = genSql(true);
}
}
this.emptyStringToNull = dbPlatform.isTreatEmptyStringsAsNull();
this.tableName = desc.getBaseTable();
this.discriminator = getDiscriminator(desc);
this.id = id;
this.all = all;
this.shadowFKey = shadowFKey;
private static Bindable getDiscriminator(BeanDescriptor<?> desc){
InheritInfo inheritInfo = desc.getInheritInfo();
if (inheritInfo != null){
return new BindableDiscriminator(inheritInfo);
} else {
return null;
}
}
/**
* Return true if empty strings should be treated as null.
*/
public boolean isEmptyStringToNull() {
return emptyStringToNull;
this.sqlWithId = genSql(false);
// only available for single Id property
if (id.isConcatenated()) {
// concatenated key
this.concatinatedKey = true;
this.identityDbColumns = null;
this.sqlNullId = null;
this.supportsGetGeneratedKeys = false;
this.selectLastInsertedId = null;
} else {
// insert sql for db identity or sequence insert
this.concatinatedKey = false;
if (id.getIdentityColumn() == null) {
this.identityDbColumns = new String[]{};
this.supportsGetGeneratedKeys = false;
this.selectLastInsertedId = null;
} else {
this.identityDbColumns = new String[]{id.getIdentityColumn()};
this.supportsGetGeneratedKeys = dbPlatform.getDbIdentity().isSupportsGetGeneratedKeys();
this.selectLastInsertedId = desc.getSelectLastInsertedId();
}
this.sqlNullId = genSql(true);
}
}
private static Bindable getDiscriminator(BeanDescriptor<?> desc) {
InheritInfo inheritInfo = desc.getInheritInfo();
if (inheritInfo != null) {
return new BindableDiscriminator(inheritInfo);
} else {
return null;
}
}
/**
* Return true if empty strings should be treated as null.
*/
public boolean isEmptyStringToNull() {
return emptyStringToNull;
}
/**
* Return true if this is a concatenated key.
*/
public boolean isConcatinatedKey() {
return concatinatedKey;
}
public String[] getIdentityDbColumns() {
return identityDbColumns;
}
/**
* Returns sql that is used to fetch back the last inserted id. This will
* return null if it should not be used.
* <p>
* This is only for DB's that do not support getGeneratedKeys. For MS
* SQLServer 2000 this could return "SELECT (at)(at)IDENTITY as id".
* </p>
*/
public String getSelectLastInsertedId() {
return selectLastInsertedId;
}
/**
* Return true if getGeneratedKeys is supported by the underlying jdbc
* driver and database.
*/
public boolean supportsGetGeneratedKeys() {
return supportsGetGeneratedKeys;
}
/**
* Return true if the Id can be derived from other property values.
*/
public boolean deriveConcatenatedId(PersistRequestBean<?> persist) {
return id.deriveConcatenatedId(persist);
}
/**
* Bind the request based on whether the id value(s) are null.
*/
public void bind(DmlHandler request, EntityBean bean, boolean withId) throws SQLException {
if (withId) {
id.dmlBind(request, bean);
}
if (shadowFKey != null) {
shadowFKey.dmlBind(request, bean);
}
if (discriminator != null) {
discriminator.dmlBind(request, bean);
}
all.dmlBind(request, bean);
}
/**
* get the sql based whether the id value(s) are null.
*/
public String getSql(boolean withId) {
if (withId) {
return sqlWithId;
} else {
return sqlNullId;
}
}
private String genSql(boolean nullId) {
GenerateDmlRequest request = new GenerateDmlRequest();
request.setInsertSetMode();
request.append("insert into ").append(tableName);
request.append(" (");
if (!nullId) {
id.dmlAppend(request);
}
/**
* Return true if this is a concatenated key.
*/
public boolean isConcatinatedKey() {
return concatinatedKey;
}
if (shadowFKey != null) {
shadowFKey.dmlAppend(request);
}
public String[] getIdentityDbColumns() {
return identityDbColumns;
}
if (discriminator != null) {
discriminator.dmlAppend(request);
}
/**
* Returns sql that is used to fetch back the last inserted id. This will
* return null if it should not be used.
* <p>
* This is only for DB's that do not support getGeneratedKeys. For MS
* SQLServer 2000 this could return "SELECT (at)(at)IDENTITY as id".
* </p>
*/
public String getSelectLastInsertedId() {
return selectLastInsertedId;
}
all.dmlAppend(request);
/**
* Return true if getGeneratedKeys is supported by the underlying jdbc
* driver and database.
*/
public boolean supportsGetGeneratedKeys() {
return supportsGetGeneratedKeys;
}
request.append(") values (");
request.append(request.getInsertBindBuffer());
request.append(")");
/**
* Return true if the Id can be derived from other property values.
*/
public boolean deriveConcatenatedId(PersistRequestBean<?> persist) {
return id.deriveConcatenatedId(persist);
}
/**
* Bind the request based on whether the id value(s) are null.
*/
public void bind(DmlHandler request, EntityBean bean, boolean withId) throws SQLException {
if (withId) {
id.dmlBind(request, bean);
}
if (shadowFKey != null){
shadowFKey.dmlBind(request, bean);
}
if (discriminator != null){
discriminator.dmlBind(request, bean);
}
all.dmlBind(request, bean);
}
/**
* get the sql based whether the id value(s) are null.
*/
public String getSql(boolean withId) {
if (withId) {
return sqlWithId;
} else {
return sqlNullId;
}
}
private String genSql(boolean nullId) {
GenerateDmlRequest request = new GenerateDmlRequest();
request.setInsertSetMode();
request.append("insert into ").append(tableName);
request.append(" (");
if (!nullId) {
id.dmlAppend(request);
}
if (shadowFKey != null){
shadowFKey.dmlAppend(request);
}
if (discriminator != null){
discriminator.dmlAppend(request);
}
all.dmlAppend(request);
request.append(") values (");
request.append(request.getInsertBindBuffer());
request.append(")");
return request.toString();
}
return request.toString();
}
}
@@ -1,8 +1,5 @@
package com.avaje.ebeaninternal.server.persist.dml;
import java.util.ArrayList;
import java.util.List;
import com.avaje.ebean.config.dbplatform.DatabasePlatform;
import com.avaje.ebean.config.dbplatform.DbEncrypt;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
@@ -17,6 +14,9 @@ import com.avaje.ebeaninternal.server.persist.dmlbind.FactoryEmbedded;
import com.avaje.ebeaninternal.server.persist.dmlbind.FactoryId;
import com.avaje.ebeaninternal.server.persist.dmlbind.FactoryVersion;
import java.util.ArrayList;
import java.util.List;
/**
* Factory for creating InsertMeta UpdateMeta and DeleteMeta.
*/
@@ -1,81 +1,80 @@
package com.avaje.ebeaninternal.server.persist.dml;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.persistence.OptimisticLockException;
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.type.DataBind;
import javax.persistence.OptimisticLockException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* Update bean handler.
*/
public class UpdateHandler extends DmlHandler {
private final UpdateMeta meta;
private boolean emptySetClause;
public UpdateHandler(PersistRequestBean<?> persist, UpdateMeta meta) {
super(persist, meta.isEmptyStringAsNull());
this.meta = meta;
}
/**
* Generate and bind the update statement.
*/
public void bind() throws SQLException {
private final UpdateMeta meta;
SpiUpdatePlan updatePlan = meta.getUpdatePlan(persistRequest);
private boolean emptySetClause;
if (updatePlan.isEmptySetClause()) {
emptySetClause = true;
return;
}
public UpdateHandler(PersistRequestBean<?> persist, UpdateMeta meta) {
super(persist, meta.isEmptyStringAsNull());
this.meta = meta;
}
sql = updatePlan.getSql();
SpiTransaction t = persistRequest.getTransaction();
/**
* Generate and bind the update statement.
*/
public void bind() throws SQLException {
PreparedStatement pstmt;
if (persistRequest.isBatched()) {
pstmt = getPstmt(t, sql, persistRequest, false);
} else {
pstmt = getPstmt(t, sql, false);
}
dataBind = new DataBind(pstmt);
meta.bind(persistRequest, this, updatePlan);
setUpdateGenValues();
logSql(sql);
}
SpiUpdatePlan updatePlan = meta.getUpdatePlan(persistRequest);
@Override
public void addBatch() throws SQLException {
if (!emptySetClause){
super.addBatch();
}
if (updatePlan.isEmptySetClause()) {
emptySetClause = true;
return;
}
/**
* Execute the update in non-batch.
*/
@Override
public void execute() throws SQLException, OptimisticLockException {
if (!emptySetClause){
int rowCount = dataBind.executeUpdate();
checkRowCount(rowCount);
}
}
sql = updatePlan.getSql();
SpiTransaction t = persistRequest.getTransaction();
PreparedStatement pstmt;
if (persistRequest.isBatched()) {
pstmt = getPstmt(t, sql, persistRequest, false);
} else {
pstmt = getPstmt(t, sql, false);
}
dataBind = new DataBind(pstmt);
meta.bind(persistRequest, this, updatePlan);
setUpdateGenValues();
logSql(sql);
}
@Override
public void addBatch() throws SQLException {
if (!emptySetClause) {
super.addBatch();
}
}
/**
* Execute the update in non-batch.
*/
@Override
public void execute() throws SQLException, OptimisticLockException {
if (!emptySetClause) {
int rowCount = dataBind.executeUpdate();
checkRowCount(rowCount);
}
}
public void registerDerivedRelationship(DerivedRelationshipData derivedRelationship) {
persistRequest.getTransaction().registerDerivedRelationship(derivedRelationship);
}
}
@@ -1,9 +1,5 @@
package com.avaje.ebeaninternal.server.persist.dml;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.avaje.ebean.annotation.ConcurrencyMode;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebean.bean.EntityBeanIntercept;
@@ -15,6 +11,10 @@ import com.avaje.ebeaninternal.server.persist.dmlbind.Bindable;
import com.avaje.ebeaninternal.server.persist.dmlbind.BindableId;
import com.avaje.ebeaninternal.server.persist.dmlbind.BindableList;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* Meta data for update handler. The meta data is for a particular bean type. It
* is considered immutable and is thread safe.
@@ -72,12 +72,12 @@ public final class UpdateMeta {
id.dmlBind(bind, bean);
switch (persist.getConcurrencyMode()) {
case VERSION:
version.dmlBind(bind, bean);
break;
case VERSION:
version.dmlBind(bind, bean);
break;
default:
break;
default:
break;
}
}
@@ -93,14 +93,14 @@ public final class UpdateMeta {
// 'full bean' update...
ConcurrencyMode mode = request.determineConcurrencyMode();
switch (mode) {
case NONE:
return modeNoneUpdatePlan;
case NONE:
return modeNoneUpdatePlan;
case VERSION:
return modeVersionUpdatePlan;
case VERSION:
return modeVersionUpdatePlan;
default:
throw new RuntimeException("Invalid mode " + mode);
default:
throw new RuntimeException("Invalid mode " + mode);
}
}
@@ -116,7 +116,7 @@ public final class UpdateMeta {
}
BeanDescriptor<?> beanDescriptor = persistRequest.getBeanDescriptor();
BeanProperty versionProperty = beanDescriptor.getVersionProperty();
if (versionProperty != null) {
if (ebi.isLoadedProperty(versionProperty.getPropertyIndex())) {
@@ -136,7 +136,7 @@ public final class UpdateMeta {
// build a bindableList that only contains the changed properties
List<Bindable> list = new ArrayList<Bindable>();
set.addToUpdate(persistRequest, list);
set.addToUpdate(persistRequest, list);
BindableList bindableList = new BindableList(list);
ConcurrencyMode mode = persistRequest.determineConcurrencyMode();
@@ -161,7 +161,7 @@ public final class UpdateMeta {
request.setUpdateSetMode();
bindableList.dmlAppend(request);
if (request.getBindColumnCount() == 0) {
// update properties must have been updatable=false
// with the result that nothing is in the set clause
@@ -179,7 +179,7 @@ public final class UpdateMeta {
}
version.dmlAppend(request);
}
return request.toString();
}
@@ -1,23 +1,17 @@
package com.avaje.ebeaninternal.server.persist.dml;
import java.sql.SQLException;
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;
import java.sql.SQLException;
/**
* Plan for executing bean updates for a given set of changed properties.
*/
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();
private final Integer key;
private final ConcurrencyMode mode;
@@ -53,18 +47,6 @@ public class UpdatePlan implements SpiUpdatePlan {
this.timeCreated = System.currentTimeMillis();
}
/**
* Special constructor for emptySetClause=true instance.
*/
private UpdatePlan() {
this.emptySetClause = true;
this.key = 0;
this.mode = ConcurrencyMode.NONE;
this.sql = null;
this.set = null;
this.timeCreated = 0;
}
public boolean isEmptySetClause() {
return emptySetClause;
}