Tidy internals - server.persist.dml package for whitespace and typos

This commit is contained in:
rbygrave
2021-08-13 09:18:31 +12:00
parent a17683c549
commit fc7379aabc
13 changed files with 14 additions and 151 deletions
@@ -56,7 +56,6 @@ class PstmtFactory {
if (t.isLogSql()) {
t.logSql(TrimLogSql.trim(sql));
}
Connection conn = t.getInternalConnection();
PreparedStatement stmt = conn.prepareStatement(sql);
BatchedPstmt bs = new BatchedPstmt(stmt, false, sql, t);
@@ -68,10 +67,8 @@ class PstmtFactory {
* Return a callable statement taking into account batch requirements.
*/
CallableStatement getCstmtBatch(SpiTransaction t, boolean logSql, String sql, BatchPostExecute batchExe) throws SQLException {
BatchedPstmtHolder batch = t.getBatchControl().getPstmtHolder();
CallableStatement stmt = (CallableStatement) batch.getStmt(sql, batchExe);
if (stmt != null) {
return stmt;
}
@@ -79,10 +76,8 @@ class PstmtFactory {
if (logSql) {
t.logSql(sql);
}
Connection conn = t.getInternalConnection();
stmt = conn.prepareCall(sql);
BatchedPstmt bs = new BatchedPstmt(stmt, false, sql, t);
batch.addStmt(bs, batchExe);
return stmt;
@@ -29,10 +29,8 @@ public class DeleteHandler extends DmlHandler {
*/
@Override
public void bind() throws SQLException {
sql = meta.getSql(persistRequest);
SpiTransaction t = persistRequest.getTransaction();
PreparedStatement pstmt;
if (persistRequest.isBatched()) {
pstmt = getPstmtBatch(t, sql, persistRequest, false);
@@ -41,9 +41,7 @@ final class DeleteMeta extends BaseMeta {
* Bind the request based on the concurrency mode.
*/
public void bind(PersistRequestBean<?> persist, DmlHandler bind) throws SQLException {
EntityBean bean = persist.getEntityBean();
id.dmlBind(bind, bean);
if (tenantId != null) {
tenantId.dmlBind(bind, bean);
@@ -58,7 +56,6 @@ final class DeleteMeta extends BaseMeta {
* get or generate the sql based on the concurrency mode.
*/
public String getSql(PersistRequestBean<?> request) {
if (id.isEmpty()) {
throw new IllegalStateException("Can not deleteById on " + request.getFullName() + " as no @Id property");
}
@@ -77,10 +74,7 @@ final class DeleteMeta extends BaseMeta {
}
private String genSql(ConcurrencyMode conMode, String table) {
GenerateDmlRequest request = new GenerateDmlRequest();
request.append("delete from ").append(table);
request.append(" where ");
GenerateDmlRequest request = new GenerateDmlRequest().append("delete from ").append(table).append(" where ");
return appendWhere(request, conMode);
}
@@ -19,11 +19,8 @@ import java.sql.SQLException;
public final class DmlBeanPersister implements BeanPersister {
private final DatabasePlatform dbPlatform;
private final UpdateMeta updateMeta;
private final InsertMeta insertMeta;
private final DeleteMeta deleteMeta;
DmlBeanPersister(DatabasePlatform dbPlatform, UpdateMeta updateMeta, InsertMeta insertMeta, DeleteMeta deleteMeta) {
@@ -61,7 +58,6 @@ public final class DmlBeanPersister implements BeanPersister {
* execute request taking batching into account.
*/
private int execute(PersistRequestBean<?> request, PersistHandler handler) {
boolean batched = request.isBatched();
try {
handler.bind();
@@ -72,7 +68,6 @@ public final class DmlBeanPersister implements BeanPersister {
} else {
return handler.executeNoBatch();
}
} catch (SQLException e) {
// log the error to the transaction log
String msg = "Error[" + StringHelper.removeNewLines(e.getMessage()) + "]";
@@ -80,7 +75,6 @@ public final class DmlBeanPersister implements BeanPersister {
request.getTransaction().logSummary(msg);
}
throw dbPlatform.translate(msg, e);
} finally {
if (!batched) {
handler.close();
@@ -11,7 +11,6 @@ import io.ebeaninternal.server.persist.BeanPersisterFactory;
public class DmlBeanPersisterFactory implements BeanPersisterFactory {
private final DatabasePlatform dbPlatform;
private final MetaFactory metaFactory;
public DmlBeanPersisterFactory(DatabasePlatform dbPlatform) {
@@ -24,11 +23,9 @@ public class DmlBeanPersisterFactory implements BeanPersisterFactory {
*/
@Override
public BeanPersister create(BeanDescriptor<?> desc) {
if (desc.isDocStoreOnly()) {
return new DocStoreBeanPersister(GeneratedProperties.of(desc));
}
UpdateMeta updMeta = metaFactory.createUpdate(desc);
DeleteMeta delMeta = metaFactory.createDelete(desc);
InsertMeta insMeta = metaFactory.createInsert(desc);
@@ -27,28 +27,14 @@ public abstract class DmlHandler implements PersistHandler, BindableRequest {
private static final short BATCHED_FIRST = 1;
private static final short BATCHED = 2;
/**
* The originating request.
*/
final PersistRequestBean<?> persistRequest;
private final StringBuilder bindLog;
final SpiTransaction transaction;
private final boolean logLevelSql;
private final long now;
/**
* The PreparedStatement used for the dml.
*/
DataBind dataBind;
BatchedPstmt batchedPstmt;
String sql;
private short batchedStatus;
DmlHandler(PersistRequestBean<?> persistRequest) {
@@ -226,7 +212,6 @@ public abstract class DmlHandler implements PersistHandler, BindableRequest {
}
private void bindInternal(boolean log, Object value, BeanProperty prop) throws SQLException {
if (log) {
if (bindLog.length() > 0) {
bindLog.append(",");
@@ -249,14 +234,12 @@ public abstract class DmlHandler implements PersistHandler, BindableRequest {
* Check with useGeneratedKeys to get appropriate PreparedStatement.
*/
PreparedStatement getPstmt(SpiTransaction t, String sql, boolean genKeys) throws SQLException {
Connection conn = t.getInternalConnection();
if (genKeys) {
// the Id generated is always the first column
// Required to stop Oracle10 giving us Oracle rowId??
// Other jdbc drivers seem fine without this hint.
return conn.prepareStatement(sql, GENERATED_KEY_COLUMNS);
} else {
return conn.prepareStatement(sql);
}
@@ -272,7 +255,6 @@ public abstract class DmlHandler implements PersistHandler, BindableRequest {
batchedStatus = batchedPstmt.isEmpty() ? BATCHED_FIRST : BATCHED;
return batchedPstmt.getStatement(request);
}
batchedStatus = BATCHED_FIRST;
PreparedStatement stmt = getPstmt(t, sql, genKeys);
batchedPstmt = new BatchedPstmt(stmt, genKeys, sql, t);
@@ -6,14 +6,10 @@ package io.ebeaninternal.server.persist.dml;
public class GenerateDmlRequest {
private final StringBuilder sb = new StringBuilder(100);
private StringBuilder insertBindBuffer;
private String prefix;
private String prefix2;
private int insertMode;
private int bindColumnCount;
/**
@@ -33,7 +29,6 @@ public class GenerateDmlRequest {
}
public void appendColumn(String column, String bind) {
++bindColumnCount;
sb.append(prefix);
@@ -20,24 +20,11 @@ import static io.ebeaninternal.server.persist.DmlUtil.isNullOrZero;
*/
public class InsertHandler extends DmlHandler {
/**
* The associated InsertMeta data.
*/
private final InsertMeta meta;
/**
* Set to true when the key is concatenated.
*/
private final boolean concatinatedKey;
/**
* Flag set when using getGeneratedKeys.
*/
private final boolean concatenatedKey;
private boolean useGeneratedKeys;
/**
* A SQL Select used to fetch back the Id where generatedKeys is not
* supported.
* SQL Select used to fetch back the Id where generatedKeys is not supported.
*/
private boolean useSelectLastInsertedId;
@@ -47,7 +34,7 @@ public class InsertHandler extends DmlHandler {
public InsertHandler(PersistRequestBean<?> persist, InsertMeta meta) {
super(persist);
this.meta = meta;
this.concatinatedKey = meta.isConcatenatedKey();
this.concatenatedKey = meta.isConcatenatedKey();
}
@Override
@@ -60,17 +47,14 @@ public class InsertHandler extends DmlHandler {
*/
@Override
public void bind() throws SQLException {
BeanDescriptor<?> desc = persistRequest.getBeanDescriptor();
EntityBean bean = persistRequest.getEntityBean();
Object idValue = desc.getId(bean);
boolean withId = !isNullOrZero(idValue);
// check to see if we are going to use generated keys
if (!withId) {
if (concatinatedKey) {
if (concatenatedKey) {
// expecting a concatenated key that can
// be built from supplied AssocOne beans
withId = meta.deriveConcatenatedId(persistRequest);
@@ -84,10 +68,8 @@ public class InsertHandler extends DmlHandler {
}
SpiTransaction t = persistRequest.getTransaction();
// get the appropriate sql
sql = meta.getSql(withId, persistRequest.isPublish());
PreparedStatement pstmt;
if (persistRequest.isBatched()) {
pstmt = getPstmtBatch(t, sql, persistRequest, useGeneratedKeys);
@@ -139,7 +121,6 @@ public class InsertHandler extends DmlHandler {
* For non batch insert with generated keys.
*/
private void getGeneratedKeys() throws SQLException {
ResultSet rset = dataBind.getPstmt().getGeneratedKeys();
try {
setGeneratedKey(rset);
@@ -164,7 +145,6 @@ public class InsertHandler extends DmlHandler {
* SQL select to fetch back the Id value.
*/
private void fetchGeneratedKeyUsingSelect() throws SQLException {
PreparedStatement stmt = null;
ResultSet rset = null;
try {
@@ -26,28 +26,18 @@ final class InsertMeta {
private final String sqlWithId;
private final String sqlDraftNullId;
private final String sqlDraftWithId;
private final BindableId id;
private final Bindable discriminator;
private final BindableList all;
private final BindableList allExcludeDraftOnly;
private final boolean supportsGetGeneratedKeys;
private final boolean concatinatedKey;
private final boolean concatenatedKey;
/**
* Used for DB that do not support getGeneratedKeys.
*/
private final boolean supportsSelectLastInsertedId;
private final Bindable shadowFKey;
private final String[] identityDbColumns;
private final Platform platform;
InsertMeta(DatabasePlatform dbPlatform, BeanDescriptor<?> desc, Bindable shadowFKey, BindableId id, BindableList all) {
@@ -66,7 +56,7 @@ final class InsertMeta {
// only available for single Id property
if (id.isConcatenated()) {
// concatenated key
this.concatinatedKey = true;
this.concatenatedKey = true;
this.identityDbColumns = null;
this.sqlNullId = null;
this.sqlDraftNullId = null;
@@ -75,7 +65,7 @@ final class InsertMeta {
} else {
// insert sql for db identity or sequence insert
this.concatinatedKey = false;
this.concatenatedKey = false;
if (id.getIdentityColumn() == null) {
this.identityDbColumns = new String[]{};
this.supportsGetGeneratedKeys = false;
@@ -99,7 +89,7 @@ final class InsertMeta {
* Return true if this is a concatenated key.
*/
boolean isConcatenatedKey() {
return concatinatedKey;
return concatenatedKey;
}
String[] getIdentityDbColumns() {
@@ -133,7 +123,6 @@ final class InsertMeta {
* Bind the request based on whether the id value(s) are null.
*/
public void bind(DmlHandler request, EntityBean bean, boolean withId, boolean publish) throws SQLException {
if (withId) {
id.dmlBind(request, bean);
}
@@ -154,7 +143,6 @@ final class InsertMeta {
* get the sql based whether the id value(s) are null.
*/
public String getSql(boolean withId, boolean publish) {
if (withId) {
return publish ? sqlWithId : sqlDraftWithId;
} else {
@@ -163,7 +151,6 @@ final class InsertMeta {
}
private String genSql(boolean nullId, String table, boolean draftTable) {
GenerateDmlRequest request = new GenerateDmlRequest();
request.setInsertSetMode();
@@ -24,19 +24,17 @@ import java.util.List;
*/
class MetaFactory {
private final FactoryBaseProperties baseFact;
private final FactoryEmbedded embeddedFact;
private final FactoryVersion versionFact = new FactoryVersion();
private final FactoryAssocOnes assocOneFact = new FactoryAssocOnes();
private final FactoryId idFact = new FactoryId();
/**
* Include Lobs in the base statement. Generally true. Oracle9 used to require
* a separate statement for Clobs and Blobs.
*/
private static final boolean includeLobs = true;
private final FactoryBaseProperties baseFact;
private final FactoryEmbedded embeddedFact;
private final FactoryVersion versionFact = new FactoryVersion();
private final FactoryAssocOnes assocOneFact = new FactoryAssocOnes();
private final FactoryId idFact = new FactoryId();
private final DatabasePlatform dbPlatform;
MetaFactory(DatabasePlatform dbPlatform) {
@@ -52,9 +50,7 @@ class MetaFactory {
* Create the UpdateMeta for the given bean type.
*/
UpdateMeta createUpdate(BeanDescriptor<?> desc) {
List<Bindable> setList = new ArrayList<>();
baseFact.create(setList, desc, DmlMode.UPDATE, includeLobs);
embeddedFact.create(setList, desc, DmlMode.UPDATE, includeLobs);
assocOneFact.create(setList, desc, DmlMode.UPDATE);
@@ -75,7 +71,6 @@ class MetaFactory {
* Create the DeleteMeta for the given bean type.
*/
DeleteMeta createDelete(BeanDescriptor<?> desc) {
BindableId id = idFact.createId(desc);
Bindable version = versionFact.createForDelete(desc);
Bindable tenantId = versionFact.createTenantId(desc);
@@ -86,19 +81,15 @@ class MetaFactory {
* Create the InsertMeta for the given bean type.
*/
InsertMeta createInsert(BeanDescriptor<?> desc) {
BindableId id = idFact.createId(desc);
List<Bindable> allList = new ArrayList<>();
baseFact.create(allList, desc, DmlMode.INSERT, includeLobs);
embeddedFact.create(allList, desc, DmlMode.INSERT, includeLobs);
assocOneFact.create(allList, desc, DmlMode.INSERT);
BindableList allBindable = new BindableList(allList);
BeanPropertyAssocOne<?> unidirectional = desc.getUnidirectional();
Bindable shadowFkey;
if (unidirectional == null) {
shadowFkey = null;
@@ -14,7 +14,6 @@ import java.sql.SQLException;
public class UpdateHandler extends DmlHandler {
private final UpdateMeta meta;
private boolean emptySetClause;
UpdateHandler(PersistRequestBean<?> persist, UpdateMeta meta) {
@@ -32,18 +31,14 @@ public class UpdateHandler extends DmlHandler {
*/
@Override
public void bind() throws SQLException {
SpiUpdatePlan updatePlan = meta.getUpdatePlan(persistRequest);
if (updatePlan.isEmptySetClause()) {
emptySetClause = true;
return;
}
sql = updatePlan.getSql();
SpiTransaction t = persistRequest.getTransaction();
PreparedStatement pstmt;
if (persistRequest.isBatched()) {
pstmt = getPstmtBatch(t, sql, persistRequest, false);
@@ -30,16 +30,12 @@ final class UpdateMeta extends BaseMeta {
* Bind the request based on the concurrency mode.
*/
public void bind(PersistRequestBean<?> persist, DmlHandler bind, SpiUpdatePlan updatePlan) throws SQLException {
EntityBean bean = persist.getEntityBean();
updatePlan.bindSet(bind, bean);
id.dmlBind(bind, bean);
if (tenantId != null) {
tenantId.dmlBind(bind, bean);
}
if (persist.getConcurrencyMode() == ConcurrencyMode.VERSION) {
version.dmlBind(bind, bean);
}
@@ -53,9 +49,7 @@ final class UpdateMeta extends BaseMeta {
}
private SpiUpdatePlan getDynamicUpdatePlan(PersistRequestBean<?> persistRequest) {
String key = persistRequest.getUpdatePlanHash();
// check if we can use a cached UpdatePlan
BeanDescriptor<?> beanDescriptor = persistRequest.getBeanDescriptor();
SpiUpdatePlan updatePlan = beanDescriptor.getUpdatePlan(key);
@@ -71,20 +65,16 @@ final class UpdateMeta extends BaseMeta {
BindableList bindableList = new BindableList(list);
ConcurrencyMode mode = persistRequest.getConcurrencyMode();
// build the SQL for this update statement
String sql = genSql(mode, bindableList, persistRequest.getUpdateTable());
updatePlan = new UpdatePlan(key, mode, sql, bindableList);
// add the UpdatePlan to the cache
beanDescriptor.putUpdatePlan(key, updatePlan);
return updatePlan;
}
private String genSql(ConcurrencyMode conMode, BindableList bindableList, String tableName) {
GenerateDmlRequest request = new GenerateDmlRequest();
request.append("update ").append(tableName).append(" set ");
request.setUpdateSetMode();
@@ -95,7 +85,6 @@ final class UpdateMeta extends BaseMeta {
// with the result that nothing is in the set clause
return null;
}
request.append(" where ");
return appendWhere(request, conMode);
}
@@ -13,26 +13,13 @@ import java.sql.SQLException;
class UpdatePlan implements SpiUpdatePlan {
private final String key;
private final ConcurrencyMode mode;
private final String sql;
private final Bindable set;
private final long timeCreated;
private final boolean emptySetClause;
private long timeLastUsed;
/**
* Create a non cached UpdatePlan.
*/
UpdatePlan(ConcurrencyMode mode, String sql, Bindable set) {
this(null, mode, sql, set);
}
/**
* Create a UpdatePlan with a given key.
*/
@@ -50,9 +37,6 @@ class UpdatePlan implements SpiUpdatePlan {
return emptySetClause;
}
/**
* Run the prepared statement binding for the 'update set' properties.
*/
@Override
public void bindSet(DmlHandler bind, EntityBean bean) throws SQLException {
set.dmlBind(bind, bean);
@@ -60,49 +44,31 @@ class UpdatePlan implements SpiUpdatePlan {
this.timeLastUsed = System.currentTimeMillis();
}
/**
* Return the time this plan was created.
*/
@Override
public long getTimeCreated() {
return timeCreated;
}
/**
* Return the time this plan was last used.
*/
@Override
public long getTimeLastUsed() {
return timeLastUsed;
}
/**
* Return the key.
*/
@Override
public String getKey() {
return key;
}
/**
* Return the concurrency mode for this plan.
*/
@Override
public ConcurrencyMode getMode() {
return mode;
}
/**
* Return the DML statement.
*/
@Override
public String getSql() {
return sql;
}
/**
* Return the Bindable properties for the update set.
*/
@Override
public Bindable getSet() {
return set;