No effective change - format only

This commit is contained in:
Robin Bygrave
2015-07-31 23:10:26 +12:00
parent 39cb85fda6
commit 97cd0909e8
10 changed files with 387 additions and 387 deletions
@@ -54,7 +54,7 @@ public class BatchedBeanHolder {
* Set of beans in this batch. This is used to ensure that a single bean instance is not included
* in the batch twice (two separate insert requests etc).
*/
private final IdentityHashMap<Object,Object> persistedBeans = new IdentityHashMap<Object,Object>();
private final IdentityHashMap<Object, Object> persistedBeans = new IdentityHashMap<Object, Object>();
/**
* Create a new entry with a given type and depth.
@@ -100,7 +100,7 @@ public class BatchedBeanHolder {
}
public String toString() {
StringBuilder sb = new StringBuilder(shortDesc.length()+18);
StringBuilder sb = new StringBuilder(shortDesc.length() + 18);
sb.append(shortDesc);
if (inserts != null) {
sb.append(" i:").append(inserts.size());
@@ -16,142 +16,143 @@ import com.avaje.ebeaninternal.server.core.PstmtBatch;
*/
public class BatchedPstmt {
/**
* The underlying statement.
*/
private PreparedStatement pstmt;
/**
* True if an insert that uses generated keys.
*/
private final boolean isGenKeys;
/**
* The list of BatchPostExecute used to perform post processing.
*/
private final ArrayList<BatchPostExecute> list = new ArrayList<BatchPostExecute>();
private final String sql;
private final PstmtBatch pstmtBatch;
private final boolean occCheck;
/**
* Create with a given statement.
* @param isGenKeys true if an insert that uses generatedKeys
*/
public BatchedPstmt(PreparedStatement pstmt, boolean isGenKeys, String sql, PstmtBatch pstmtBatch, boolean occCheck) {
this.pstmt = pstmt;
this.isGenKeys = isGenKeys;
this.sql = sql;
this.pstmtBatch = pstmtBatch;
this.occCheck = occCheck;
/**
* The underlying statement.
*/
private PreparedStatement pstmt;
/**
* True if an insert that uses generated keys.
*/
private final boolean isGenKeys;
/**
* The list of BatchPostExecute used to perform post processing.
*/
private final ArrayList<BatchPostExecute> list = new ArrayList<BatchPostExecute>();
private final String sql;
private final PstmtBatch pstmtBatch;
private final boolean occCheck;
/**
* Create with a given statement.
*
* @param isGenKeys true if an insert that uses generatedKeys
*/
public BatchedPstmt(PreparedStatement pstmt, boolean isGenKeys, String sql, PstmtBatch pstmtBatch, boolean occCheck) {
this.pstmt = pstmt;
this.isGenKeys = isGenKeys;
this.sql = sql;
this.pstmtBatch = pstmtBatch;
this.occCheck = occCheck;
}
/**
* Return the number of batched statements.
*/
public int size() {
return list.size();
}
/**
* Return the sql
*/
public String getSql() {
return sql;
}
/**
* Return the statement.
*/
public PreparedStatement getStatement() {
return pstmt;
}
/**
* Add the BatchPostExecute to the list for post execute processing.
*/
public void add(BatchPostExecute batchExecute) {
list.add(batchExecute);
}
/**
* Execute the statement using executeBatch().
* Run any post processing including getGeneratedKeys.
*/
public void executeBatch(boolean getGeneratedKeys) throws SQLException {
executeAndCheckRowCounts();
if (isGenKeys && getGeneratedKeys) {
getGeneratedKeys();
}
/**
* Return the number of batched statements.
*/
public int size() {
return list.size();
postExecute();
close();
}
/**
* Close the underlying statement.
*/
public void close() throws SQLException {
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
/**
* Return the sql
*/
public String getSql() {
return sql;
}
private void postExecute() throws SQLException {
for (int i = 0; i < list.size(); i++) {
list.get(i).postExecute();
}
}
private void executeAndCheckRowCounts() throws SQLException {
if (pstmtBatch != null) {
// oracle specific JDBC batch processing
int rc = pstmtBatch.executeBatch(pstmt, list.size(), sql, occCheck);
if (list.size() == 1) {
list.get(0).checkRowCount(rc);
}
// the optimistic concurrency row count check
// has already been done by pstmtBatch so just return
return;
}
/**
* Return the statement.
*/
public PreparedStatement getStatement() {
return pstmt;
}
/**
* Add the BatchPostExecute to the list for post execute processing.
*/
public void add(BatchPostExecute batchExecute){
list.add(batchExecute);
}
/**
* Execute the statement using executeBatch().
* Run any post processing including getGeneratedKeys.
*/
public void executeBatch(boolean getGeneratedKeys) throws SQLException {
executeAndCheckRowCounts();
if (isGenKeys && getGeneratedKeys){
getGeneratedKeys();
}
postExecute();
close();
}
/**
* Close the underlying statement.
*/
public void close() throws SQLException {
if (pstmt != null){
pstmt.close();
pstmt = null;
}
}
private void postExecute() throws SQLException {
for (int i = 0; i < list.size(); i++) {
list.get(i).postExecute();
}
}
private void executeAndCheckRowCounts() throws SQLException {
// normal JDBC batch processing
int[] results = pstmt.executeBatch();
if (pstmtBatch != null){
// oracle specific JDBC batch processing
int rc = pstmtBatch.executeBatch(pstmt, list.size(), sql, occCheck);
if (list.size() == 1){
list.get(0).checkRowCount(rc);
}
// the optimistic concurrency row count check
// has already been done by pstmtBatch so just return
return;
}
// normal JDBC batch processing
int[] results = pstmt.executeBatch();
if (results.length != list.size()){
String s = "results array error "+results.length+" "+list.size();
throw new SQLException(s);
}
// check for concurrency exceptions...
for (int i = 0; i < results.length; i++) {
list.get(i).checkRowCount(results[i]);
}
if (results.length != list.size()) {
String s = "results array error " + results.length + " " + list.size();
throw new SQLException(s);
}
private void getGeneratedKeys() throws SQLException {
int index = 0;
ResultSet rset = pstmt.getGeneratedKeys();
try {
while(rset.next()) {
Object idValue = rset.getObject(1);
list.get(index).setGeneratedKey(idValue);
index++;
}
} finally {
if (rset != null){
rset.close();
}
}
// check for concurrency exceptions...
for (int i = 0; i < results.length; i++) {
list.get(i).checkRowCount(results[i]);
}
}
private void getGeneratedKeys() throws SQLException {
int index = 0;
ResultSet rset = pstmt.getGeneratedKeys();
try {
while (rset.next()) {
Object idValue = rset.getObject(1);
list.get(index).setGeneratedKey(idValue);
index++;
}
} finally {
if (rset != null) {
rset.close();
}
}
}
}
@@ -9,19 +9,19 @@ import com.avaje.ebeaninternal.server.core.PersistRequestBean;
*/
public interface BeanPersister {
/**
* execute the insert bean request.
*/
void insert(PersistRequestBean<?> request) throws PersistenceException;
/**
* execute the insert bean request.
*/
void insert(PersistRequestBean<?> request) throws PersistenceException;
/**
* execute the update bean request.
*/
void update(PersistRequestBean<?> request) throws PersistenceException;
/**
* execute the update bean request.
*/
void update(PersistRequestBean<?> request) throws PersistenceException;
/**
* execute the delete bean request.
*/
void delete(PersistRequestBean<?> request) throws PersistenceException;
/**
* execute the delete bean request.
*/
void delete(PersistRequestBean<?> request) throws PersistenceException;
}
@@ -6,10 +6,10 @@ import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
* Factory for creating BeanPersister implementations.
*/
public interface BeanPersisterFactory {
/**
* Create the BeanPersister implemenation for a given type.
*/
BeanPersister create(BeanDescriptor<?> desc);
/**
* Create the BeanPersister implemenation for a given type.
*/
BeanPersister create(BeanDescriptor<?> desc);
}
@@ -7,81 +7,81 @@ import java.util.ArrayList;
*/
public class BindValues {
final ArrayList<Value> list = new ArrayList<Value>();
/**
* Create with a Binder.
*/
public BindValues(){
}
/**
* Return the number of bind values.
*/
public int size() {
return list.size();
}
/**
* Add a bind value with its JDBC datatype.
*
* @param value the bind value
* @param dbType the type as per java.sql.Types
*/
public void add(Object value, int dbType, String name){
list.add(new Value(value, dbType, name));
}
/**
* List of bind values.
*/
public ArrayList<Value> values() {
return list;
}
/**
* A Value has additionally the JDBC data type.
*/
public static class Value {
private final Object value;
private final int dbType;
private final String name;
/**
* Create the value.
*/
public Value(Object value, int dbType, String name) {
this.value = value;
this.dbType = dbType;
this.name = name;
}
final ArrayList<Value> list = new ArrayList<Value>();
/**
* Return the type as per java.sql.Types.
*/
public int getDbType() {
return dbType;
}
/**
* Return the value.
*/
public Object getValue() {
return value;
}
/**
* Return the property name.
*/
public String getName() {
return name;
}
public String toString(){
return ""+value;
}
/**
* Create with a Binder.
*/
public BindValues() {
}
/**
* Return the number of bind values.
*/
public int size() {
return list.size();
}
/**
* Add a bind value with its JDBC datatype.
*
* @param value the bind value
* @param dbType the type as per java.sql.Types
*/
public void add(Object value, int dbType, String name) {
list.add(new Value(value, dbType, name));
}
/**
* List of bind values.
*/
public ArrayList<Value> values() {
return list;
}
/**
* A Value has additionally the JDBC data type.
*/
public static class Value {
private final Object value;
private final int dbType;
private final String name;
/**
* Create the value.
*/
public Value(Object value, int dbType, String name) {
this.value = value;
this.dbType = dbType;
this.name = name;
}
/**
* Return the type as per java.sql.Types.
*/
public int getDbType() {
return dbType;
}
/**
* Return the value.
*/
public Object getValue() {
return value;
}
/**
* Return the property name.
*/
public String getName() {
return name;
}
public String toString() {
return "" + value;
}
}
}
@@ -1066,7 +1066,7 @@ public final class DefaultPersister implements Persister {
/**
* Cascade delete child entities by Id.
* <p/>
* <p>
* Will use delete by object if the child entity has manyToMany relationships.
*/
private void deleteChildrenById(SpiTransaction t, BeanDescriptor<?> targetDesc, List<Object> childIds) {
@@ -1202,7 +1202,7 @@ public final class DefaultPersister implements Persister {
/**
* Create an Insert or Update PersistRequestBean when cascading.
* <p/>
* <p>
* This call determines the PersistRequest.Type based on bean state and the insert flag (root persist type).
*/
private <T> PersistRequestBean<T> createRequest(T bean, Transaction t, Object parentBean, boolean insertMode) {
@@ -20,76 +20,76 @@ import com.avaje.ebeaninternal.server.transaction.DefaultPersistenceContext;
* This bean effectively holds the foreign properties that where not loaded, and
* helps fetch the foreign keys and delete the appropriate rows.
* </p>
*
*
* @author rbygrave
*/
public class DeleteUnloadedForeignKeys {
private final List<BeanPropertyAssocOne<?>> propList = new ArrayList<BeanPropertyAssocOne<?>>(4);
private final SpiEbeanServer server;
private final PersistRequestBean<?> request;
private final List<BeanPropertyAssocOne<?>> propList = new ArrayList<BeanPropertyAssocOne<?>>(4);
private EntityBean beanWithForeignKeys;
public DeleteUnloadedForeignKeys(SpiEbeanServer server, PersistRequestBean<?> request) {
this.server = server;
this.request = request;
}
public boolean isEmpty() {
return propList.isEmpty();
}
public void add(BeanPropertyAssocOne<?> prop) {
propList.add(prop);
private final SpiEbeanServer server;
private final PersistRequestBean<?> request;
private EntityBean beanWithForeignKeys;
public DeleteUnloadedForeignKeys(SpiEbeanServer server, PersistRequestBean<?> request) {
this.server = server;
this.request = request;
}
public boolean isEmpty() {
return propList.isEmpty();
}
public void add(BeanPropertyAssocOne<?> prop) {
propList.add(prop);
}
/**
* Execute a query fetching the missing (unloaded) foreign keys. We need to
* fetch these key values before the parent bean is deleted.
*/
public void queryForeignKeys() {
BeanDescriptor<?> descriptor = request.getBeanDescriptor();
SpiQuery<?> q = (SpiQuery<?>) server.createQuery(descriptor.getBeanType());
Object id = request.getBeanId();
StringBuilder sb = new StringBuilder(30);
for (int i = 0; i < propList.size(); i++) {
sb.append(propList.get(i).getName()).append(",");
}
/**
* Execute a query fetching the missing (unloaded) foreign keys. We need to
* fetch these key values before the parent bean is deleted.
*/
public void queryForeignKeys() {
// run query in a separate persistence context
q.setPersistenceContext(new DefaultPersistenceContext());
q.setPersistenceContextScope(PersistenceContextScope.QUERY);
q.setAutofetch(false);
q.select(sb.toString());
q.where().idEq(id);
BeanDescriptor<?> descriptor = request.getBeanDescriptor();
SpiQuery<?> q = (SpiQuery<?>) server.createQuery(descriptor.getBeanType());
Object id = request.getBeanId();
StringBuilder sb = new StringBuilder(30);
for (int i = 0; i < propList.size(); i++) {
sb.append(propList.get(i).getName()).append(",");
}
// run query in a separate persistence context
q.setPersistenceContext(new DefaultPersistenceContext());
q.setPersistenceContextScope(PersistenceContextScope.QUERY);
q.setAutofetch(false);
q.select(sb.toString());
q.where().idEq(id);
SpiTransaction t = request.getTransaction();
if (t.isLogSummary()) {
t.logSummary("-- Ebean fetching foreign key values for delete of " + descriptor.getName() + " id:" + id);
}
beanWithForeignKeys = (EntityBean)server.findUnique(q, t);
SpiTransaction t = request.getTransaction();
if (t.isLogSummary()) {
t.logSummary("-- Ebean fetching foreign key values for delete of " + descriptor.getName() + " id:" + id);
}
beanWithForeignKeys = (EntityBean) server.findUnique(q, t);
}
/**
* Delete the rows relating to the foreign keys. These deletions occur after
* the parent bean has been deleted.
*/
public void deleteCascade() {
/**
* Delete the rows relating to the foreign keys. These deletions occur after
* the parent bean has been deleted.
*/
public void deleteCascade() {
for (int i = 0; i < propList.size(); i++) {
BeanPropertyAssocOne<?> prop = propList.get(i);
Object detailBean = prop.getValue(beanWithForeignKeys);
for (int i = 0; i < propList.size(); i++) {
BeanPropertyAssocOne<?> prop = propList.get(i);
Object detailBean = prop.getValue(beanWithForeignKeys);
// if bean exists with a unique id then delete it
if (detailBean != null && prop.hasId((EntityBean)detailBean)) {
server.delete(detailBean, request.getTransaction());
}
}
// if bean exists with a unique id then delete it
if (detailBean != null && prop.hasId((EntityBean) detailBean)) {
server.delete(detailBean, request.getTransaction());
}
}
}
}
@@ -2,14 +2,14 @@ package com.avaje.ebeaninternal.server.persist;
/**
* Utility object with helper methods for DML.
* Utility object with helper methods for DML.
*/
public class DmlUtil {
/**
* Return true if the value is null or a Numeric 0 (for primitive int's and long's) or Option empty.
*/
public static boolean isNullOrZero(Object value) {
/**
* Return true if the value is null or a Numeric 0 (for primitive int's and long's) or Option empty.
*/
public static boolean isNullOrZero(Object value) {
return value == null || value instanceof Number && ((Number) value).longValue() == 0l;
}
}
@@ -12,43 +12,42 @@ import com.avaje.ebeaninternal.server.core.PersistRequestUpdateSql;
* A Persister 'front-ends' this object and handles the
* batching, cascading, concurrency mode detection etc.
* </p>
*
*/
public interface PersistExecute {
/**
* Create a BatchControl for the current transaction.
*/
BatchControl createBatchControl(SpiTransaction t);
/**
* Execute a Bean (or MapBean) insert.
*/
<T> void executeInsertBean(PersistRequestBean<T> request);
/**
* Execute a Bean (or MapBean) update.
*/
<T> void executeUpdateBean(PersistRequestBean<T> request);
/**
* Create a BatchControl for the current transaction.
*/
BatchControl createBatchControl(SpiTransaction t);
/**
* Execute a Bean (or MapBean) delete.
*/
<T> void executeDeleteBean(PersistRequestBean<T> request);
/**
* Execute a Bean (or MapBean) insert.
*/
<T> void executeInsertBean(PersistRequestBean<T> request);
/**
* Execute a Update.
*/
int executeOrmUpdate(PersistRequestOrmUpdate request);
/**
* Execute a CallableSql.
*/
int executeSqlCallable(PersistRequestCallableSql request);
/**
* Execute a Bean (or MapBean) update.
*/
<T> void executeUpdateBean(PersistRequestBean<T> request);
/**
* Execute a UpdateSql.
*/
int executeSqlUpdate(PersistRequestUpdateSql request);
/**
* Execute a Bean (or MapBean) delete.
*/
<T> void executeDeleteBean(PersistRequestBean<T> request);
/**
* Execute a Update.
*/
int executeOrmUpdate(PersistRequestOrmUpdate request);
/**
* Execute a CallableSql.
*/
int executeSqlCallable(PersistRequestCallableSql request);
/**
* Execute a UpdateSql.
*/
int executeSqlUpdate(PersistRequestUpdateSql request);
}
@@ -18,79 +18,79 @@ import com.avaje.ebeaninternal.server.core.PstmtBatch;
public class PstmtFactory {
private final PstmtBatch pstmtBatch;
public PstmtFactory(PstmtBatch pstmtBatch) {
this.pstmtBatch = pstmtBatch;
}
/**
* Get a callable statement without any batching.
*/
public CallableStatement getCstmt(SpiTransaction t, String sql) throws SQLException {
Connection conn = t.getInternalConnection();
return conn.prepareCall(sql);
}
private final PstmtBatch pstmtBatch;
/**
* Get a prepared statement without any batching.
*/
public PreparedStatement getPstmt(SpiTransaction t, String sql) throws SQLException {
Connection conn = t.getInternalConnection();
return conn.prepareStatement(sql);
}
public PstmtFactory(PstmtBatch pstmtBatch) {
this.pstmtBatch = pstmtBatch;
}
/**
* Return a prepared statement taking into account batch requirements.
*/
public PreparedStatement getPstmt(SpiTransaction t, boolean logSql, String sql, BatchPostExecute batchExe)
throws SQLException {
/**
* Get a callable statement without any batching.
*/
public CallableStatement getCstmt(SpiTransaction t, String sql) throws SQLException {
Connection conn = t.getInternalConnection();
return conn.prepareCall(sql);
}
BatchedPstmtHolder batch = t.getBatchControl().getPstmtHolder();
PreparedStatement stmt = batch.getStmt(sql, batchExe);
/**
* Get a prepared statement without any batching.
*/
public PreparedStatement getPstmt(SpiTransaction t, String sql) throws SQLException {
Connection conn = t.getInternalConnection();
return conn.prepareStatement(sql);
}
if (stmt != null) {
return stmt;
}
/**
* Return a prepared statement taking into account batch requirements.
*/
public PreparedStatement getPstmt(SpiTransaction t, boolean logSql, String sql, BatchPostExecute batchExe)
throws SQLException {
if (logSql){
t.logSql(sql);
}
Connection conn = t.getInternalConnection();
stmt = conn.prepareStatement(sql);
BatchedPstmtHolder batch = t.getBatchControl().getPstmtHolder();
PreparedStatement stmt = batch.getStmt(sql, batchExe);
if (pstmtBatch != null){
pstmtBatch.setBatchSize(stmt, t.getBatchControl().getBatchSize());
}
BatchedPstmt bs = new BatchedPstmt(stmt, false, sql, pstmtBatch, false);
batch.addStmt(bs, batchExe);
return stmt;
}
if (stmt != null) {
return stmt;
}
/**
* Return a callable statement taking into account batch requirements.
*/
public CallableStatement getCstmt(SpiTransaction t, boolean logSql, String sql, BatchPostExecute batchExe)
throws SQLException {
if (logSql) {
t.logSql(sql);
}
BatchedPstmtHolder batch = t.getBatchControl().getPstmtHolder();
CallableStatement stmt = (CallableStatement) batch.getStmt(sql, batchExe);
Connection conn = t.getInternalConnection();
stmt = conn.prepareStatement(sql);
if (stmt != null) {
return stmt;
}
if (logSql){
t.logSql(sql);
}
if (pstmtBatch != null) {
pstmtBatch.setBatchSize(stmt, t.getBatchControl().getBatchSize());
}
Connection conn = t.getInternalConnection();
stmt = conn.prepareCall(sql);
BatchedPstmt bs = new BatchedPstmt(stmt, false, sql, pstmtBatch, false);
batch.addStmt(bs, batchExe);
return stmt;
}
BatchedPstmt bs = new BatchedPstmt(stmt, false, sql, pstmtBatch, false);
batch.addStmt(bs, batchExe);
return stmt;
}
/**
* Return a callable statement taking into account batch requirements.
*/
public CallableStatement getCstmt(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;
}
if (logSql) {
t.logSql(sql);
}
Connection conn = t.getInternalConnection();
stmt = conn.prepareCall(sql);
BatchedPstmt bs = new BatchedPstmt(stmt, false, sql, pstmtBatch, false);
batch.addStmt(bs, batchExe);
return stmt;
}
}