initial add of EbeanORM server based on v2.8.1

This commit is contained in:
rbygrave committed 2012-09-14 00:40:39 +12:00
1 parent 2b74d0f9d9
commit 96ce4c0ddf
1188 files changed
+135034

No files matched your search

@@ -0,0 +1,269 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.PersistenceException;
import com.avaje.ebeaninternal.api.SpiTransaction;
import com.avaje.ebeaninternal.server.core.PersistRequest;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
/**
* Controls the batch ordering of persist requests.
* <p>
* Persist requests include bean inserts updates deletes and UpdateSql and
* CallableSql requests.
* </p>
* <p>
* This object queues up the requests into appropriate entries according to the
* 'depth' and the 'type' of the requests. The depth relates to how saves and
* deletes cascade following the associations of a bean. For saving Associated
* One cascades reduce the depth (-1) and associated many's increase the depth.
* The initial depth of a request is 0.
* </p>
*/
public final class BatchControl {
private static final Logger logger = Logger.getLogger(BatchControl.class.getName());
/**
* Used to sort queue entries by depth.
*/
private static final BatchDepthComparator depthComparator = new BatchDepthComparator();
/**
* The associated transaction.
*/
private final SpiTransaction transaction;
/**
* Controls batching of the PreparedStatements. This should be flushed after
* each 'depth'.
*/
private final BatchedPstmtHolder pstmtHolder = new BatchedPstmtHolder();
/**
* The size at which the batch queue will flush. This should be close to the
* number of statements that are batched into a single PreparedStatement. This
* size relates to the size of a list in a BatchQueueEntry and not the total
* number of request which could be more than that.
*/
private int batchSize;
/**
* If true try to get generated keys from inserts.
*/
private boolean getGeneratedKeys;
private boolean batchFlushOnMixed = true;
private final BatchedBeanControl beanControl;
/**
* Create for a given transaction, PersistExecute, default size and
* getGeneratedKeys.
*/
public BatchControl(SpiTransaction t, int batchSize, boolean getGenKeys) {
this.transaction = t;
this.batchSize = batchSize;
this.getGeneratedKeys = getGenKeys;
this.beanControl = new BatchedBeanControl(t, this);
transaction.setBatchControl(this);
}
/**
* Set this flag to false to allow batching of a mix of Beans and UpdateSql
* (or CallableSql). Normally if you mix the two this will result in an
* automatic flush.
* <p>
* Note that UpdateSql and CallableSql will ALWAYS flush first. This is due to
* it already having been bound to a PreparedStatement where as the Beans go
* through a 2 step process when they are flushed (delayed binding).
* </p>
*/
public void setBatchFlushOnMixed(boolean flushBatchOnMixed) {
this.batchFlushOnMixed = flushBatchOnMixed;
}
/**
* Return the batchSize.
*/
public int getBatchSize() {
return batchSize;
}
/**
* Set the size of batch execution.
* <p>
* The user can set this via the Transaction.
* </p>
*/
public void setBatchSize(int batchSize) {
if (batchSize > 1) {
this.batchSize = batchSize;
}
}
/**
* Set whether or not to use getGeneratedKeys for this batch execution.
* <p>
* The user can set this via the transaction
* </p>
*/
public void setGetGeneratedKeys(Boolean getGeneratedKeys) {
if (getGeneratedKeys != null) {
this.getGeneratedKeys = getGeneratedKeys;
}
}
/**
* Execute a Orm Update, SqlUpdate or CallableSql.
* <p>
* These all go straight to jdbc and use addBatch(). Entity beans goto a queue
* and wait there so that the jdbc is executed in the correct order according
* to the depth.
* </p>
*/
public int executeStatementOrBatch(PersistRequest request, boolean batch) {
if (!batch || (batchFlushOnMixed && !beanControl.isEmpty())) {
// flush when mixing beans and updateSql
flush();
}
if (!batch) {
// execute the request immediately without batching
return request.executeNow();
}
if (pstmtHolder.getMaxSize() >= batchSize) {
flush();
}
// for OrmUpdate, SqlUpdate, CallableSql there is no queue...
// so straight to jdbc prepared statement and use addBatch().
// aka executeNow() may use addBatch().
request.executeNow();
return -1;
}
/**
* Entity Bean insert, update or delete. This will either execute the request
* immediately or queue it for batch processing later. The queue is flushed
* according to the depth (object graph depth).
*/
public int executeOrQueue(PersistRequestBean<?> request, boolean batch) {
if (!batch || (batchFlushOnMixed && !pstmtHolder.isEmpty())) {
// flush when mixing beans and updateSql
flush();
}
if (!batch) {
return request.executeNow();
}
// get the list we will add this request to
ArrayList<PersistRequest> persistList = beanControl.getPersistList(request);
if (persistList == null) {
// special case where the same bean instance has been added
// to the batch more than once
if (logger.isLoggable(Level.FINE)) {
logger.fine("Bean instance already in this batch: " + request.getBean());
}
return -1;
}
if (persistList.size() >= batchSize) {
// flush everything that has been batched
flush();
// we need to get the persistList again after the
// flush as the flush clears out the bean holders
persistList = beanControl.getPersistList(request);
}
persistList.add(request);
return -1;
}
/**
* Return the actual batch of PreparedStatements.
*/
public BatchedPstmtHolder getPstmtHolder() {
return pstmtHolder;
}
/**
* Return true if the queue is empty.
*/
public boolean isEmpty() {
return (beanControl.isEmpty() && pstmtHolder.isEmpty());
}
/**
* Flush any batched PreparedStatements.
*/
protected void flushPstmtHolder() {
pstmtHolder.flush(getGeneratedKeys);
}
/**
* Execute all the requests contained in the list.
*/
protected void executeNow(ArrayList<PersistRequest> list) {
for (int i = 0; i < list.size(); i++) {
list.get(i).executeNow();
}
}
/**
* execute all the requests currently queued or batched.
*/
public void flush() throws PersistenceException {
if (!pstmtHolder.isEmpty()) {
// Flush existing pstmts (updateSql or callableSql)
flushPstmtHolder();
}
if (beanControl.isEmpty()) {
// Nothing in queue to flush
return;
}
// convert entry map to array for sorting
BatchedBeanHolder[] bsArray = beanControl.getArray();
// sort the entries by depth
Arrays.sort(bsArray, depthComparator);
if (transaction.isLogSummary()) {
transaction.logInternal("BatchControl flush " + Arrays.toString(bsArray));
}
for (int i = 0; i < bsArray.length; i++) {
BatchedBeanHolder bs = bsArray[i];
bs.executeNow();
// flush all the batched Pstmts
flushPstmtHolder();
}
}
}
@@ -0,0 +1,49 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist;
import java.io.Serializable;
import java.util.Comparator;
/**
* Used to sort BatchedBeanHolder by their depth.
* <p>
* Beans are queued and put into BatchedBeanHolder along with their depth. This
* delays the actually binding to PreparedStatements until the
* BatchedBeanHolder's are flushed. This is so that we can get the generated
* keys from inserts. These values are required to persist the 'detail' beans.
* </p>
*/
public class BatchDepthComparator implements Comparator<BatchedBeanHolder>, Serializable {
private static final long serialVersionUID = 264611821665757991L;
public int compare(BatchedBeanHolder b1, BatchedBeanHolder b2) {
if (b1.getOrder() < b2.getOrder()) {
return -1;
}
if (b1.getOrder() == b2.getOrder()) {
return 0;
}
return 1;
}
}
@@ -0,0 +1,55 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist;
import java.sql.SQLException;
/**
* Handles the processing required after batch execution.
* <p>
* This includes concurrency checking, generated keys on inserts, transaction
* logging, transaction event table modifcation and for beans resetting their
* 'loaded' status.
* </p>
*/
public interface BatchPostExecute {
/**
* Check that the rowCount is correct for this execute. This is for
* performing concurrency checking in batch execution.
*/
public void checkRowCount(int rowCount) throws SQLException;
/**
* For inserts with generated keys. Otherwise not used.
*/
public void setGeneratedKey(Object idValue);
/**
* Execute the post execute processing.
* <p>
* This includes transaction logging, transaction event table modification
* and for beans resetting their 'loaded' status.
* </p>
*/
public void postExecute() throws SQLException;
}
@@ -0,0 +1,98 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist;
import java.util.ArrayList;
import java.util.HashMap;
import com.avaje.ebeaninternal.api.SpiTransaction;
import com.avaje.ebeaninternal.server.core.PersistRequest;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
/**
* Holds all the batched beans.
* <p>
* The beans are held here which delays the binding to a PreparedStatement. This
* 'delayed' binding is required as the beans need to be bound and executed in
* the correct order (according to the depth).
* </p>
*/
public class BatchedBeanControl {
/**
* Map of the BatchedBeanHolder objects. They each have a depth and are later
* sorted by their depth to get the execution order.
*/
private final HashMap<String, BatchedBeanHolder> beanHoldMap = new HashMap<String, BatchedBeanHolder>();
private final SpiTransaction transaction;
private final BatchControl batchControl;
private int topOrder;
public BatchedBeanControl(SpiTransaction t, BatchControl batchControl) {
this.transaction = t;
this.batchControl = batchControl;
}
public ArrayList<PersistRequest> getPersistList(PersistRequestBean<?> request) {
return getBeanHolder(request).getList(request);
}
/**
* Return an entry for the given type description. The type description is
* typically the bean class name (or table name for MapBeans).
*/
private BatchedBeanHolder getBeanHolder(PersistRequestBean<?> request) {
BeanDescriptor<?> beanDescriptor = request.getBeanDescriptor();
BatchedBeanHolder batchBeanHolder = beanHoldMap.get(beanDescriptor.getFullName());
if (batchBeanHolder == null) {
int relativeDepth = transaction.depth(0);
if (relativeDepth == 0){
topOrder++;
}
int stmtOrder = topOrder*100 + relativeDepth;
batchBeanHolder = new BatchedBeanHolder(batchControl, beanDescriptor, stmtOrder);
beanHoldMap.put(beanDescriptor.getFullName(), batchBeanHolder);
}
return batchBeanHolder;
}
/**
* Return true if this holds no persist requests.
*/
public boolean isEmpty() {
return beanHoldMap.isEmpty();
}
/**
* Return the BatchedBeanHolder's ready for sorting and executing.
*/
public BatchedBeanHolder[] getArray() {
BatchedBeanHolder[] bsArray = new BatchedBeanHolder[beanHoldMap.size()];
beanHoldMap.values().toArray(bsArray);
return bsArray;
}
}
@@ -0,0 +1,157 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist;
import java.util.ArrayList;
import java.util.HashSet;
import com.avaje.ebeaninternal.server.core.PersistRequest;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
/**
* Holds lists of persist requests for beans of a given typeDescription.
* <p>
* This is used to delay the actual binding of the bean to PreparedStatements.
* The reason is that don't have all the bind values yet in the case of inserts
* with getGeneratedKeys.
* </p>
* <p>
* Has a depth which is used to determine the order in which it should be
* executed. The lowest depth is executed first.
* </p>
*/
public class BatchedBeanHolder {
/**
* The owning queue.
*/
private final BatchControl control;
private final String shortDesc;
/**
* The 'depth' which is used to determine the execution order.
*/
private final int order;
/**
* The list of bean insert requests.
*/
private ArrayList<PersistRequest> inserts;
/**
* The list of bean update requests.
*/
private ArrayList<PersistRequest> updates;
/**
* The list of bean delete requests.
*/
private ArrayList<PersistRequest> deletes;
private HashSet<Integer> beanHashCodes = new HashSet<Integer>();
/**
* Create a new entry with a given type and depth.
*/
public BatchedBeanHolder(BatchControl control, BeanDescriptor<?> beanDescriptor, int order) {
this.control = control;
this.shortDesc = beanDescriptor.getName() + ":" + order;
this.order = order;
}
/**
* Return the depth.
*/
public int getOrder() {
return order;
}
/**
* Execute all the persist requests in this entry.
* <p>
* This will Batch all the similar requests into one or more BatchStatements
* and then execute them.
* </p>
*/
public void executeNow() {
// process the requests. Creates one or more PreparedStatements
// with binding addBatch() for each request.
// Note updates and deletes can result in many PreparedStatements
// if their where clauses differ via use of IS NOT NULL.
if (inserts != null && !inserts.isEmpty()) {
control.executeNow(inserts);
inserts.clear();
}
if (updates != null && !updates.isEmpty()) {
control.executeNow(updates);
updates.clear();
}
if (deletes != null && !deletes.isEmpty()) {
control.executeNow(deletes);
deletes.clear();
}
beanHashCodes.clear();
}
public String toString() {
return shortDesc;
}
/**
* Return the list for the typeCode.
*/
public ArrayList<PersistRequest> getList(PersistRequestBean<?> request) {
Integer objHashCode = Integer.valueOf(System.identityHashCode(request.getBean()));
if (!beanHashCodes.add(objHashCode)) {
// special case where the same bean instance has already been
// added to the batch (doesn't really occur with non-batching
// as the bean gets changed from dirty to loaded earlier)
return null;
}
switch (request.getType()) {
case INSERT:
if (inserts == null) {
inserts = new ArrayList<PersistRequest>();
}
return inserts;
case UPDATE:
if (updates == null) {
updates = new ArrayList<PersistRequest>();
}
return updates;
case DELETE:
if (deletes == null) {
deletes = new ArrayList<PersistRequest>();
}
return deletes;
default:
throw new RuntimeException("Invalid type code " + request.getType());
}
}
}
@@ -0,0 +1,176 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.avaje.ebeaninternal.server.core.PstmtBatch;
/**
* A batched statement that is held in BatchedPstmtHolder. It has a list of
* BatchPostExecute which it will process after the statement is executed.
* <p>
* This can hold CallableStatements as well.
* </p>
*/
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;
}
/**
* 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();
}
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 {
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]);
}
}
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();
}
}
}
}
@@ -0,0 +1,165 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.PersistenceException;
/**
* Used to hold BatchedPstmt objects for batch based execution.
* <p>
* The BatchControl 'front ends' the batching by queuing the persist requests
* and ordering them according to depth and type. This object should only batch
* statements of a single 'depth' at any given time.
* </p>
*/
public class BatchedPstmtHolder {
private static final Logger logger = Logger.getLogger(BatchedPstmtHolder.class.getName());
/**
* A Map of the statements using a String key. This is used so that the same
* Statement,Prepared,Callable is reused.
*/
private LinkedHashMap<String,BatchedPstmt> stmtMap = new LinkedHashMap<String,BatchedPstmt>();
/**
* The Max size across all the BatchedPstmt.
*/
private int maxSize;
public BatchedPstmtHolder() {
}
/**
* Return the PreparedStatement if it has already been used in this Batch.
* This will return null if no matching PreparedStatement is found.
*/
public PreparedStatement getStmt(String stmtKey, BatchPostExecute postExecute) {
BatchedPstmt bs = stmtMap.get(stmtKey);
if (bs == null) {
// the PreparedStatement has need been created
return null;
}
// add the post execute processing for this bean/row
bs.add(postExecute);
// maintain a max batch size for any given batched stmt.
// Used to determine when to flush.
int bsSize = bs.size();
if (bsSize > maxSize){
maxSize = bsSize;
}
return bs.getStatement();
}
/**
* Add a new PreparedStatement wrapped in the BatchStatement object.
*/
public void addStmt(BatchedPstmt bs, BatchPostExecute postExecute) {
// add the batch post execute to the statement for POST processing
bs.add(postExecute);
// cache so that getStmt() can find it for additional beans/rows
stmtMap.put(bs.getSql(), bs);
}
/**
* Return true if the batch has no statements to execute.
*/
public boolean isEmpty() {
return stmtMap.isEmpty();
}
/**
* Execute all batched PreparedStatements.
*
* @param getGeneratedKeys
* if true try to get generated keys for inserts
*/
public void flush(boolean getGeneratedKeys) throws PersistenceException {
SQLException firstError = null;
String errorSql = null;
// flag set if something fails. Will not execute
// but still need to close PreparedStatements.
boolean isError = false;
Iterator<BatchedPstmt> it = stmtMap.values().iterator();
while (it.hasNext()) {
BatchedPstmt bs = it.next();
try {
if (!isError) {
bs.executeBatch(getGeneratedKeys);
}
} catch (SQLException ex) {
SQLException next = ex.getNextException();
while(next != null) {
logger.log(Level.SEVERE, "Next Exception during batch execution", next);
next = next.getNextException();
}
if (firstError == null) {
firstError = ex;
errorSql = bs.getSql();
} else {
logger.log(Level.SEVERE, null, ex);
}
isError = true;
} finally {
try {
bs.close();
} catch (SQLException ex) {
// error closing PreparedStatement
logger.log(Level.SEVERE, null, ex);
}
}
}
// clear the batch cache
stmtMap.clear();
maxSize = 0;
if (firstError != null) {
String msg = "Error when batch flush on sql: "+errorSql;
throw new PersistenceException(msg, firstError);
}
}
/**
* Return the size of the biggest batched statement.
* <p>
* Used to determine when to flush the batch.
* </p>
*/
public int getMaxSize() {
return maxSize;
}
}
@@ -0,0 +1,46 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist;
import javax.persistence.PersistenceException;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
/**
* Defines bean insert update and delete implementation.
*/
public interface BeanPersister {
/**
* execute the insert bean request.
*/
public void insert(PersistRequestBean<?> request) throws PersistenceException;
/**
* execute the update bean request.
*/
public void update(PersistRequestBean<?> request) throws PersistenceException;
/**
* execute the delete bean request.
*/
public void delete(PersistRequestBean<?> request) throws PersistenceException;
}
@@ -0,0 +1,34 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
/**
* Factory for creating BeanPersister implementations.
*/
public interface BeanPersisterFactory {
/**
* Create the BeanPersister implemenation for a given type.
*/
public BeanPersister create(BeanDescriptor<?> desc);
}
@@ -0,0 +1,135 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist;
import java.util.ArrayList;
/**
* Holds a list of bind values for binding to a PreparedStatement.
*/
public class BindValues {
int commentCount;
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() - commentCount;
}
/**
* 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));
}
public void addComment(String comment){
++commentCount;
list.add(new Value(comment));
}
/**
* 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;
private final boolean isComment;
/**
* Create a comment. This is so that comments can be put into
* the bind log.
*/
public Value(String comment) {
this.name = comment;
this.isComment = true;
value = null;
dbType = 0;
}
/**
* Create the value.
*/
public Value(Object value, int dbType, String name) {
this.isComment = false;
this.value = value;
this.dbType = dbType;
this.name = name;
}
/**
* This is a comment for the bind log and NOT an actual bind value.
*/
public boolean isComment() {
return isComment;
}
/**
* 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;
}
}
}
@@ -0,0 +1,402 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist;
import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.persistence.PersistenceException;
import com.avaje.ebeaninternal.api.BindParams;
import com.avaje.ebeaninternal.server.core.Message;
import com.avaje.ebeaninternal.server.type.DataBind;
import com.avaje.ebeaninternal.server.type.ScalarType;
import com.avaje.ebeaninternal.server.type.TypeManager;
/**
* Binds bean values to a PreparedStatement.
*/
public class Binder {
private static final Logger logger = Logger.getLogger(Binder.class.getName());
//private final Calendar calendar;
private final TypeManager typeManager;
/**
* Set the PreparedStatement with which to bind variables to.
*/
public Binder(TypeManager typeManager) {
this.typeManager = typeManager;
//this.calendar = new GregorianCalendar();
}
/**
* Bind the values to the Prepared Statement.
*/
public void bind(BindValues bindValues, DataBind dataBind, StringBuilder bindBuf)
throws SQLException {
String logPrefix = "";
ArrayList<BindValues.Value> list = bindValues.values();
for (int i = 0; i < list.size(); i++) {
BindValues.Value bindValue = (BindValues.Value) list.get(i);
if (bindValue.isComment()) {
if (bindBuf != null) {
bindBuf.append(bindValue.getName());
if (logPrefix.equals("")) {
logPrefix = ", ";
}
}
} else {
Object val = bindValue.getValue();
int dt = bindValue.getDbType();
bindObject(dataBind, val, dt);
if (bindBuf != null) {
bindBuf.append(logPrefix);
if (logPrefix.equals("")) {
logPrefix = ", ";
}
bindBuf.append(bindValue.getName());
bindBuf.append("=");
if (isLob(dt)) {
bindBuf.append("[LOB]");
} else {
bindBuf.append(String.valueOf(val));
}
}
}
}
}
/**
* Bind the list of positionedParameters in BindParams.
*/
public String bind(BindParams bindParams, DataBind dataBind)
throws SQLException {
StringBuilder bindLog = new StringBuilder();
bind(bindParams, dataBind, bindLog);
return bindLog.toString();
}
/**
* Bind the list of positionedParameters in BindParams.
*/
public void bind(BindParams bindParams, DataBind dataBind, StringBuilder bindLog)
throws SQLException {
bind(bindParams.positionedParameters(), dataBind, bindLog);
}
/**
* Bind the list of parameters..
*/
public void bind(List<BindParams.Param> list, DataBind dataBind, StringBuilder bindLog)
throws SQLException {
CallableStatement cstmt = null;
if (dataBind.getPstmt() instanceof CallableStatement) {
cstmt = (CallableStatement) dataBind.getPstmt();
}
// the iterator is assumed to be in the correct order
Object value = null;
try {
for (int i = 0; i < list.size(); i++) {
BindParams.Param param = list.get(i);
if (param.isOutParam() && cstmt != null){
cstmt.registerOutParameter(dataBind.nextPos(), param.getType());
if (param.isInParam()) {
dataBind.decrementPos();
}
}
if (param.isInParam()) {
value = param.getInValue();
if (bindLog != null) {
if (param.isEncryptionKey()){
bindLog.append("****");
} else {
bindLog.append(value);
}
bindLog.append(", ");
}
if (value == null) {
// this doesn't work for query predicates
bindObject(dataBind, null, param.getType());
} else {
bindObject(dataBind, value);
}
}
}
} catch (SQLException ex) {
logger.warning(Message.msg("fetch.bind.error", "" + (dataBind.currentPos() - 1), value));
throw ex;
}
}
/**
* Bind an Object with unknown data type.
*/
public void bindObject(DataBind dataBind, Object value) throws SQLException {
if (value == null) {
// null of unknown type
bindObject(dataBind, null, Types.OTHER);
} else {
ScalarType<?> type = typeManager.getScalarType(value.getClass());
if (type == null){
// the type is not registered with the TypeManager.
String msg = "No ScalarType registered for "+value.getClass();
throw new PersistenceException(msg);
} else if (!type.isJdbcNative()) {
// convert to a JDBC native type
value = type.toJdbcType(value);
}
int dbType = type.getJdbcType();
bindObject(dataBind, value, dbType);
}
}
/**
* bind a single value.
* <p>
* Note that java.math.BigInteger is supported by converting it to a Long.
* </p>
* <p>
* Note if we get a java.util.Date or java.util.Calendar then these have
* been anonymously passed in (UpdateSql etc). There is a global setting to
* convert then to a java.sql.Date or java.sql.Timestamp for binding. The
* default is that both are converted to java.sql.Timestamp.
* </p>
*/
public void bindObject(DataBind dataBind, Object data, int dbType)
throws SQLException {
if (data == null){
dataBind.setNull(dbType);
return;
}
switch (dbType) {
case java.sql.Types.LONGVARCHAR:
bindLongVarChar(dataBind, data);
break;
case java.sql.Types.LONGVARBINARY:
bindLongVarBinary(dataBind, data);
break;
case java.sql.Types.CLOB:
bindClob(dataBind, data);
break;
case java.sql.Types.BLOB:
bindBlob(dataBind, data);
break;
default:
bindSimpleData(dataBind, dbType, data);
}
}
/**
* Binds the value to the statement according to the data type.
*/
private void bindSimpleData(DataBind b, int dataType, Object data)
throws SQLException {
try {
switch (dataType) {
case java.sql.Types.BOOLEAN:
Boolean bo = (Boolean) data;
b.setBoolean(bo.booleanValue());
break;
case java.sql.Types.BIT:
// Types.BIT should map to Java Boolean
Boolean bitBool = (Boolean) data;
b.setBoolean(bitBool.booleanValue());
break;
case java.sql.Types.VARCHAR:
b.setString((String) data);
break;
case java.sql.Types.CHAR:
b.setString(data.toString());
break;
case java.sql.Types.TINYINT:
b.setByte(((Byte) data).byteValue());
break;
case java.sql.Types.SMALLINT:
b.setShort(((Short) data).shortValue());
break;
case java.sql.Types.INTEGER:
b.setInt(((Integer) data).intValue());
break;
case java.sql.Types.BIGINT:
b.setLong(((Long) data).longValue());
break;
case java.sql.Types.REAL:
b.setFloat(((Float) data).floatValue());
break;
case java.sql.Types.FLOAT:
// DB Float in theory maps to Java Double type
b.setDouble(((Double) data).doubleValue());
break;
case java.sql.Types.DOUBLE:
b.setDouble(((Double) data).doubleValue());
break;
case java.sql.Types.NUMERIC:
b.setBigDecimal((BigDecimal) data);
break;
case java.sql.Types.DECIMAL:
b.setBigDecimal((BigDecimal) data);
break;
case java.sql.Types.TIME:
//pstmt.setTime(index, (java.sql.Time) data, calendar);
b.setTime((java.sql.Time) data);
break;
case java.sql.Types.DATE:
//pstmt.setDate(index, (java.sql.Date) data, calendar);
b.setDate((java.sql.Date) data);
break;
case java.sql.Types.TIMESTAMP:
//pstmt.setTimestamp(index, (java.sql.Timestamp) data, calendar);
b.setTimestamp((java.sql.Timestamp) data);
break;
case java.sql.Types.BINARY:
b.setBytes((byte[]) data);
break;
case java.sql.Types.VARBINARY:
b.setBytes((byte[]) data);
break;
case java.sql.Types.OTHER:
b.setObject(data);
break;
case java.sql.Types.JAVA_OBJECT:
// Not too sure about this.
b.setObject(data);
break;
default:
String msg = Message.msg("persist.bind.datatype", "" + dataType, "" + b.currentPos());
throw new SQLException(msg);
}
} catch (Exception e) {
String dataClass = "Data is null?";
if (data != null) {
dataClass = data.getClass().getName();
}
String m = "Error with property[" + b.currentPos() + "] dt[" + dataType + "]";
m += "data[" + data + "][" + dataClass + "]";
throw new PersistenceException(m, e);
}
}
/**
* Bind String data to a LONGVARCHAR column.
*/
private void bindLongVarChar(DataBind b, Object data)
throws SQLException {
String sd = (String) data;
b.setClob(sd);
}
/**
* Bind byte[] data to a LONGVARBINARY column.
*/
private void bindLongVarBinary(DataBind b, Object data)
throws SQLException {
byte[] bytes = (byte[]) data;
b.setBlob(bytes);
}
/**
* Bind String data to a CLOB column.
*/
private void bindClob(DataBind b, Object data) throws SQLException {
String sd = (String) data;
b.setClob(sd);
}
/**
* Bind byte[] data to a BLOB column.
*/
private void bindBlob(DataBind b, Object data) throws SQLException {
byte[] bytes = (byte[]) data;
b.setBlob(bytes);
}
private boolean isLob(int dbType) {
switch (dbType) {
case Types.CLOB:
return true;
case Types.LONGVARCHAR:
return true;
case Types.BLOB:
return true;
case Types.LONGVARBINARY:
return true;
default:
return false;
}
}
}
@@ -0,0 +1,46 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist;
/**
* Contants used in persist.
*/
public interface Constant {
/**
* An INSERT clause.
*/
public static final int IN_INSERT = 1;
/**
* An UPDATE SET clause.
*/
public static final int IN_UPDATE_SET = 2;
/**
* An UPDATE WHERE clause.
*/
public static final int IN_UPDATE_WHERE = 3;
/**
* A DELETE WHERE clause.
*/
public static final int IN_DELETE_WHERE = 4;
}
@@ -0,0 +1,154 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist;
import com.avaje.ebean.config.GlobalProperties;
import com.avaje.ebean.event.BeanPersistController;
import com.avaje.ebeaninternal.api.SpiTransaction;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.core.PersistRequestCallableSql;
import com.avaje.ebeaninternal.server.core.PersistRequestOrmUpdate;
import com.avaje.ebeaninternal.server.core.PersistRequestUpdateSql;
import com.avaje.ebeaninternal.server.core.PstmtBatch;
import com.avaje.ebeaninternal.server.deploy.BeanManager;
/**
* Default PersistExecute implementation using DML statements.
* <p>
* Supports the use of PreparedStatement batching.
* </p>
*/
public final class DefaultPersistExecute implements PersistExecute {
private final ExeCallableSql exeCallableSql;
private final ExeUpdateSql exeUpdateSql;
private final ExeOrmUpdate exeOrmUpdate;
/**
* The default batch size.
*/
private final int defaultBatchSize;
/**
* Default for whether to call getGeneratedKeys after batch insert.
*/
private final boolean defaultBatchGenKeys;
private final boolean validate;
/**
* Construct this DmlPersistExecute.
*/
public DefaultPersistExecute(boolean validate, Binder binder, PstmtBatch pstmtBatch) {
this.validate = validate;
this.exeOrmUpdate = new ExeOrmUpdate(binder, pstmtBatch);
this.exeUpdateSql = new ExeUpdateSql(binder, pstmtBatch);
this.exeCallableSql = new ExeCallableSql(binder, pstmtBatch);
this.defaultBatchGenKeys = GlobalProperties.getBoolean("batch.getgeneratedkeys", true);
this.defaultBatchSize = GlobalProperties.getInt("batch.size", 20);
}
public BatchControl createBatchControl(SpiTransaction t) {
// create a BatchControl and set its defaults
return new BatchControl(t, defaultBatchSize, defaultBatchGenKeys);
}
/**
* execute the bean insert request.
*/
public <T> void executeInsertBean(PersistRequestBean<T> request) {
BeanManager<T> mgr = request.getBeanManager();
BeanPersister persister = mgr.getBeanPersister();
BeanPersistController controller = request.getBeanController();
if (controller == null || controller.preInsert(request)) {
if (validate){
request.validate();
}
persister.insert(request);
// NOTE: the persister fires the postInsert so that this
// occurs before ebeanIntercept.setLoaded(true)
}
}
/**
* execute the bean update request.
*/
public <T> void executeUpdateBean(PersistRequestBean<T> request) {
BeanManager<T> mgr = request.getBeanManager();
BeanPersister persister = mgr.getBeanPersister();
BeanPersistController controller = request.getBeanController();
if (controller == null || controller.preUpdate(request)) {
if (validate){
request.validate();
}
persister.update(request);
// NOTE: the persister fires the postUpdate so that this
// occurs before ebeanIntercept.setLoaded(true)
}
}
/**
* execute the bean delete request.
*/
public <T> void executeDeleteBean(PersistRequestBean<T> request) {
BeanManager<T> mgr = request.getBeanManager();
BeanPersister persister = mgr.getBeanPersister();
BeanPersistController controller = request.getBeanController();
if (controller == null || controller.preDelete(request)) {
persister.delete(request);
// NOTE: the persister fires the postDelete
}
}
/**
* Execute the updateSqlRequest
*/
public int executeOrmUpdate(PersistRequestOrmUpdate request) {
return exeOrmUpdate.execute(request);
}
/**
* Execute the updateSqlRequest
*/
public int executeSqlUpdate(PersistRequestUpdateSql request) {
return exeUpdateSql.execute(request);
}
/**
* Execute the CallableSqlRequest.
*/
public int executeSqlCallable(PersistRequestCallableSql request) {
return exeCallableSql.execute(request);
}
}
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,111 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist;
import java.util.ArrayList;
import java.util.List;
import com.avaje.ebeaninternal.api.SpiEbeanServer;
import com.avaje.ebeaninternal.api.SpiQuery;
import com.avaje.ebeaninternal.api.SpiTransaction;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne;
import com.avaje.ebeaninternal.server.transaction.DefaultPersistenceContext;
/**
* Used for deletion of a partially populated bean where some cascade delete
* properties where not loaded.
* <p>
* 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 Object 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(",");
}
// run query in a separate persistence context
q.setPersistenceContext(new DefaultPersistenceContext());
q.setAutofetch(false);
q.select(sb.toString());
q.where().idEq(id);
SpiTransaction t = request.getTransaction();
if (t.isLogSummary()) {
t.logInternal("-- Ebean fetching foreign key values for delete of " + descriptor.getName() + " id:" + id);
}
beanWithForeignKeys = 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() {
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(detailBean)) {
server.delete(detailBean, request.getTransaction());
}
}
}
}
@@ -0,0 +1,34 @@
package com.avaje.ebeaninternal.server.persist;
import com.avaje.ebeaninternal.server.deploy.parse.DetectScala;
/**
* Utility object with helper methods for DML.
*/
public class DmlUtil {
private static final boolean hasScalaSupport = DetectScala.hasScalaSupport();
/**
* 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){
if (value == null){
return true;
}
if (value instanceof Number){
return ((Number)value).longValue() == 0l;
}
if (hasScalaSupport){
if (value instanceof scala.Option<?>) {
if (((scala.Option<?>) value).isEmpty()) {
return true;
}
}
}
return false;
}
}
@@ -0,0 +1,138 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist;
import java.sql.CallableStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.PersistenceException;
import com.avaje.ebeaninternal.api.BindParams;
import com.avaje.ebeaninternal.api.SpiCallableSql;
import com.avaje.ebeaninternal.api.SpiTransaction;
import com.avaje.ebeaninternal.server.core.PersistRequestCallableSql;
import com.avaje.ebeaninternal.server.core.PstmtBatch;
import com.avaje.ebeaninternal.server.type.DataBind;
import com.avaje.ebeaninternal.server.util.BindParamsParser;
/**
* Handles the execution of CallableSql requests.
*/
public class ExeCallableSql {
private static final Logger logger = Logger.getLogger(ExeCallableSql.class.getName());
private final Binder binder;
private final PstmtFactory pstmtFactory;
public ExeCallableSql(Binder binder, PstmtBatch pstmtBatch) {
this.binder = binder;
// no batch support for CallableStatement in Oracle anyway
this.pstmtFactory = new PstmtFactory(null);
}
/**
* execute the CallableSql requests.
*/
public int execute(PersistRequestCallableSql request) {
SpiTransaction t = request.getTransaction();
boolean batchThisRequest = t.isBatchThisRequest();
CallableStatement cstmt = null;
try {
cstmt = bindStmt(request, batchThisRequest);
if (batchThisRequest){
cstmt.addBatch();
// return -1 to indicate batch mode
return -1;
} else {
// handles executeOverride() and also
// reading of registered OUT parameters
int rowCount = request.executeUpdate();
request.postExecute();
return rowCount;
}
} catch (SQLException ex) {
throw new PersistenceException(ex);
} finally {
if (!batchThisRequest && cstmt != null) {
try {
cstmt.close();
} catch (SQLException e) {
logger.log(Level.SEVERE, null, e);
}
}
}
}
private CallableStatement bindStmt(PersistRequestCallableSql request, boolean batchThisRequest) throws SQLException {
SpiCallableSql callableSql = request.getCallableSql();
SpiTransaction t = request.getTransaction();
String sql = callableSql.getSql();
BindParams bindParams = callableSql.getBindParams();
// process named parameters if required
sql = BindParamsParser.parse(bindParams, sql);
boolean logSql = request.isLogSql();
CallableStatement cstmt;
if (batchThisRequest){
cstmt = pstmtFactory.getCstmt(t, logSql, sql, request);
} else {
if (logSql){
t.logInternal(sql);
}
cstmt = pstmtFactory.getCstmt(t, sql);
}
if (callableSql.getTimeout() > 0){
cstmt.setQueryTimeout(callableSql.getTimeout());
}
String bindLog = null;
if (!bindParams.isEmpty()){
bindLog = binder.bind(bindParams, new DataBind(cstmt));
}
request.setBindLog(bindLog);
// required to read OUT params later
request.setBound(bindParams, cstmt);
return cstmt;
}
}
@@ -0,0 +1,160 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.PersistenceException;
import com.avaje.ebeaninternal.api.BindParams;
import com.avaje.ebeaninternal.api.SpiTransaction;
import com.avaje.ebeaninternal.api.SpiUpdate;
import com.avaje.ebeaninternal.server.core.PersistRequestOrmUpdate;
import com.avaje.ebeaninternal.server.core.PstmtBatch;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.type.DataBind;
import com.avaje.ebeaninternal.server.util.BindParamsParser;
/**
* Executes the UpdateSql requests.
*/
public class ExeOrmUpdate {
private static final Logger logger = Logger.getLogger(ExeOrmUpdate.class.getName());
private final Binder binder;
private final PstmtFactory pstmtFactory;
/**
* Create with a given binder.
*/
public ExeOrmUpdate(Binder binder, PstmtBatch pstmtBatch) {
this.pstmtFactory = new PstmtFactory(pstmtBatch);
this.binder = binder;
}
/**
* Execute the orm update request.
*/
public int execute(PersistRequestOrmUpdate request) {
SpiTransaction t = request.getTransaction();
boolean batchThisRequest = t.isBatchThisRequest();
PreparedStatement pstmt = null;
try {
pstmt = bindStmt(request, batchThisRequest);
if (batchThisRequest){
PstmtBatch pstmtBatch = request.getPstmtBatch();
if (pstmtBatch != null){
pstmtBatch.addBatch(pstmt);
} else {
pstmt.addBatch();
}
// return -1 to indicate batch mode
return -1;
} else {
SpiUpdate<?> ormUpdate = request.getOrmUpdate();
if (ormUpdate.getTimeout() > 0){
pstmt.setQueryTimeout(ormUpdate.getTimeout());
}
int rowCount = pstmt.executeUpdate();
request.checkRowCount(rowCount);
request.postExecute();
return rowCount;
}
} catch (SQLException ex) {
SpiUpdate<?> ormUpdate = request.getOrmUpdate();
String msg = "Error executing: "+ormUpdate.getGeneratedSql();
throw new PersistenceException(msg, ex);
} finally {
if (!batchThisRequest && pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
logger.log(Level.SEVERE, null, e);
}
}
}
}
/**
* Convert bean and property names to db table and columns.
*/
private String translate(PersistRequestOrmUpdate request, String sql) {
BeanDescriptor<?> descriptor = request.getBeanDescriptor();
return descriptor.convertOrmUpdateToSql(sql);
}
private PreparedStatement bindStmt(PersistRequestOrmUpdate request, boolean batchThisRequest) throws SQLException {
SpiUpdate<?> ormUpdate = request.getOrmUpdate();
SpiTransaction t = request.getTransaction();
String sql = ormUpdate.getUpdateStatement();
// convert bean and property names to table and
// column names if required
sql = translate(request, sql);
BindParams bindParams = ormUpdate.getBindParams();
// process named parameters if required
sql = BindParamsParser.parse(bindParams, sql);
ormUpdate.setGeneratedSql(sql);
boolean logSql = request.isLogSql();
PreparedStatement pstmt;
if (batchThisRequest){
pstmt = pstmtFactory.getPstmt(t, logSql, sql, request);
} else {
if (logSql){
t.logInternal(sql);
}
pstmt = pstmtFactory.getPstmt(t, sql);
}
String bindLog = null;
if (!bindParams.isEmpty()){
bindLog = binder.bind(bindParams, new DataBind(pstmt));
}
request.setBindLog(bindLog);
return pstmt;
}
}
@@ -0,0 +1,218 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.PersistenceException;
import com.avaje.ebeaninternal.api.BindParams;
import com.avaje.ebeaninternal.api.SpiSqlUpdate;
import com.avaje.ebeaninternal.api.SpiTransaction;
import com.avaje.ebeaninternal.server.core.PersistRequestUpdateSql;
import com.avaje.ebeaninternal.server.core.PstmtBatch;
import com.avaje.ebeaninternal.server.core.PersistRequestUpdateSql.SqlType;
import com.avaje.ebeaninternal.server.type.DataBind;
import com.avaje.ebeaninternal.server.util.BindParamsParser;
/**
* Executes the UpdateSql requests.
*/
public class ExeUpdateSql {
private static final Logger logger = Logger.getLogger(ExeUpdateSql.class.getName());
private final Binder binder;
private final PstmtFactory pstmtFactory;
private final PstmtBatch pstmtBatch;
//TODO: get defaultBatchSize
private int defaultBatchSize = 20;
/**
* Create with a given binder.
*/
public ExeUpdateSql(Binder binder, PstmtBatch pstmtBatch) {
this.binder = binder;
this.pstmtBatch = pstmtBatch;
this.pstmtFactory = new PstmtFactory(pstmtBatch);
}
/**
* Execute the UpdateSql request.
*/
public int execute(PersistRequestUpdateSql request) {
SpiTransaction t = request.getTransaction();
boolean batchThisRequest = t.isBatchThisRequest();
PreparedStatement pstmt = null;
try {
pstmt = bindStmt(request, batchThisRequest);
if (batchThisRequest){
if (pstmtBatch != null){
pstmtBatch.addBatch(pstmt);
} else {
pstmt.addBatch();
}
// return -1 to indicate batch mode
return -1;
} else {
int rowCount = pstmt.executeUpdate();
request.checkRowCount(rowCount);
request.postExecute();
return rowCount;
}
} catch (SQLException ex) {
throw new PersistenceException(ex);
} finally {
if (!batchThisRequest && pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
logger.log(Level.SEVERE, null, e);
}
}
}
}
private PreparedStatement bindStmt(PersistRequestUpdateSql request, boolean batchThisRequest) throws SQLException {
SpiSqlUpdate updateSql = request.getUpdateSql();
SpiTransaction t = request.getTransaction();
String sql = updateSql.getSql();
BindParams bindParams = updateSql.getBindParams();
// process named parameters if required
sql = BindParamsParser.parse(bindParams, sql);
boolean logSql = request.isLogSql();
PreparedStatement pstmt;
if (batchThisRequest){
pstmt = pstmtFactory.getPstmt(t, logSql, sql, request);
if (pstmtBatch != null){
// oracle specific JDBC setting batch size ahead of time
int batchSize = t.getBatchSize();
if (batchSize < 1){
batchSize = defaultBatchSize;
}
pstmtBatch.setBatchSize(pstmt, batchSize);
}
} else {
if (logSql){
t.logInternal(sql);
}
pstmt = pstmtFactory.getPstmt(t, sql);
}
if (updateSql.getTimeout() > 0){
pstmt.setQueryTimeout(updateSql.getTimeout());
}
String bindLog = null;
if (!bindParams.isEmpty()){
bindLog = binder.bind(bindParams, new DataBind(pstmt));
}
request.setBindLog(bindLog);
// derive the statement type (for TransactionEvent)
parseUpdate(sql, request);
return pstmt;
}
private void determineType(String word1, String word2, String word3, PersistRequestUpdateSql request) {
if (word1.equalsIgnoreCase("UPDATE")) {
request.setType(SqlType.SQL_UPDATE, word2, "UpdateSql");
} else if (word1.equalsIgnoreCase("DELETE")) {
request.setType(SqlType.SQL_DELETE, word3, "DeleteSql");
} else if (word1.equalsIgnoreCase("INSERT")) {
request.setType(SqlType.SQL_INSERT, word3, "InsertSql");
} else {
request.setType(SqlType.SQL_UNKNOWN, null, "UnknownSql");
}
}
private void parseUpdate(String sql, PersistRequestUpdateSql request) {
int start = ltrim(sql);
int[] pos = new int[3];
int spaceCount = 0;
int len = sql.length();
for (int i = start; i < len; i++) {
char c = sql.charAt(i);
if (Character.isWhitespace(c)) {
pos[spaceCount] = i;
spaceCount++;
if (spaceCount > 2){
break;
}
}
}
String firstWord = sql.substring(0, pos[0]);
String secWord = sql.substring(pos[0]+1, pos[1]);
String thirdWord;
if (pos[2] == 0){
// there is nothing after the table name
thirdWord = sql.substring(pos[1]+1);
} else {
thirdWord = sql.substring(pos[1]+1, pos[2]);
}
determineType(firstWord, secWord, thirdWord, request);
}
private int ltrim(String s) {
int len = s.length();
int i = 0;
for (i = 0; i < len; i++) {
if (!Character.isWhitespace(s.charAt(i))) {
return i;
}
}
return 0;
}
}
@@ -0,0 +1,73 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist;
import com.avaje.ebeaninternal.api.SpiTransaction;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.core.PersistRequestCallableSql;
import com.avaje.ebeaninternal.server.core.PersistRequestOrmUpdate;
import com.avaje.ebeaninternal.server.core.PersistRequestUpdateSql;
/**
* The actual execution of persist requests.
* <p>
* 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.
*/
public BatchControl createBatchControl(SpiTransaction t);
/**
* Execute a Bean (or MapBean) insert.
*/
public <T> void executeInsertBean(PersistRequestBean<T> request);
/**
* Execute a Bean (or MapBean) update.
*/
public <T> void executeUpdateBean(PersistRequestBean<T> request);
/**
* Execute a Bean (or MapBean) delete.
*/
public <T> void executeDeleteBean(PersistRequestBean<T> request);
/**
* Execute a Update.
*/
public int executeOrmUpdate(PersistRequestOrmUpdate request);
/**
* Execute a CallableSql.
*/
public int executeSqlCallable(PersistRequestCallableSql request);
/**
* Execute a UpdateSql.
*/
public int executeSqlUpdate(PersistRequestUpdateSql request);
}
@@ -0,0 +1,115 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.avaje.ebeaninternal.api.SpiTransaction;
import com.avaje.ebeaninternal.server.core.PstmtBatch;
/**
* Factory for creating Statements.
* <p>
* This is only used by CallableSql and UpdateSql requests and does not support
* getGeneratedKeys.
* </p>
*/
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);
}
/**
* Get a prepared statement without any batching.
*/
public PreparedStatement getPstmt(SpiTransaction t, String sql) throws SQLException {
Connection conn = t.getInternalConnection();
return conn.prepareStatement(sql);
}
/**
* Return a prepared statement taking into account batch requirements.
*/
public PreparedStatement getPstmt(SpiTransaction t, boolean logSql, String sql, BatchPostExecute batchExe)
throws SQLException {
BatchedPstmtHolder batch = t.getBatchControl().getPstmtHolder();
PreparedStatement stmt = batch.getStmt(sql, batchExe);
if (stmt != null) {
return stmt;
}
if (logSql){
t.logInternal(sql);
}
Connection conn = t.getInternalConnection();
stmt = conn.prepareStatement(sql);
if (pstmtBatch != null){
pstmtBatch.setBatchSize(stmt, t.getBatchControl().getBatchSize());
}
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.logInternal(sql);
}
Connection conn = t.getInternalConnection();
stmt = conn.prepareCall(sql);
BatchedPstmt bs = new BatchedPstmt(stmt, false, sql, pstmtBatch, false);
batch.addStmt(bs, batchExe);
return stmt;
}
}
@@ -0,0 +1,99 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
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.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.type.DataBind;
/**
* Delete bean handler.
*/
public class DeleteHandler extends DmlHandler {
private final DeleteMeta meta;
public DeleteHandler(PersistRequestBean<?> persist, DeleteMeta meta) {
super(persist, meta.isEmptyStringAsNull());
this.meta = meta;
}
/**
* Generate and bind the delete statement.
*/
public void bind() throws SQLException {
sql = meta.getSql(persistRequest);
SpiTransaction t = persistRequest.getTransaction();
boolean isBatch = t.isBatchThisRequest();
PreparedStatement pstmt;
if (isBatch) {
pstmt = getPstmt(t, sql, persistRequest, false);
} else {
logSql(sql);
pstmt = getPstmt(t, sql, false);
}
dataBind = new DataBind(pstmt);
bindLogAppend("Binding Delete [");
bindLogAppend(meta.getTableName());
bindLogAppend("] where[");
meta.bind(persistRequest, this);
bindLogAppend("]");
// log the binding to transaction log if requested
logBinding();
}
/**
* Execute the delete non-batch.
*/
public void execute() throws SQLException, OptimisticLockException {
int rowCount = dataBind.executeUpdate();
checkRowCount(rowCount);
}
@Override
public boolean isIncluded(BeanProperty prop) {
return prop.isDbUpdatable() && super.isIncluded(prop);
}
@Override
public boolean isIncludedWhere(BeanProperty prop) {
return prop.isDbUpdatable() && (loadedProps == null || loadedProps.contains(prop.getName()));
}
public void registerDerivedRelationship(DerivedRelationshipData assocBean) {
throw new RuntimeException("Never called on delete");
}
}
@@ -0,0 +1,165 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dml;
import java.sql.SQLException;
import java.util.Set;
import com.avaje.ebeaninternal.server.core.ConcurrencyMode;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.persist.dmlbind.Bindable;
import com.avaje.ebeaninternal.server.persist.dmlbind.BindableId;
/**
* Meta data for delete handler. The meta data is for a particular bean type. It
* is considered immutable and is thread safe.
*/
public final class DeleteMeta {
private final String sqlVersion;
private final String sqlNone;
private final BindableId id;
private final Bindable version;
private final Bindable all;
private final String tableName;
private final boolean emptyStringAsNull;
public DeleteMeta(boolean emptyStringAsNull, BeanDescriptor<?> desc, BindableId id, Bindable version, Bindable all) {
this.emptyStringAsNull = emptyStringAsNull;
this.tableName = desc.getBaseTable();
this.id = id;
this.version = version;
this.all = all;
sqlNone = genSql(ConcurrencyMode.NONE);
sqlVersion = genSql(ConcurrencyMode.VERSION);
}
public boolean isEmptyStringAsNull() {
return emptyStringAsNull;
}
/**
* Return the table name.
*/
public String getTableName() {
return tableName;
}
/**
* Bind the request based on the concurrency mode.
*/
public void bind(PersistRequestBean<?> persist, DmlHandler bind) throws SQLException {
Object bean = persist.getBean();
id.dmlBind(bind, false, bean);
switch (persist.getConcurrencyMode()) {
case VERSION:
version.dmlBind(bind, false, bean);
break;
case ALL:
Object oldBean = persist.getOldValues();
all.dmlBindWhere(bind, true, oldBean);
break;
default:
break;
}
}
/**
* get or generate the sql based on the concurrency mode.
*/
public String getSql(PersistRequestBean<?> request) throws SQLException {
if (id.isEmpty()) {
throw new IllegalStateException("Can not deleteById on " + request.getFullName() + " as no @Id property");
}
switch (request.determineConcurrencyMode()) {
case NONE:
return sqlNone;
case VERSION:
return sqlVersion;
case ALL:
return genDynamicWhere(request.getLoadedProperties(), request.getOldValues());
default:
throw new RuntimeException("Invalid mode " + request.determineConcurrencyMode());
}
}
private String genSql(ConcurrencyMode conMode) {
// delete ... where bcol=? and bc1=? and bc2 is null and ...
GenerateDmlRequest request = new GenerateDmlRequest(emptyStringAsNull);
request.append("delete from ").append(tableName);
request.append(" where ");
request.setWhereIdMode();
id.dmlAppend(request, false);
if (ConcurrencyMode.VERSION.equals(conMode)) {
if (version == null) {
return null;
}
version.dmlAppend(request, false);
} else if (ConcurrencyMode.ALL.equals(conMode)) {
throw new RuntimeException("Never called for ConcurrencyMode.ALL");
}
return request.toString();
}
/**
* Generate the sql dynamically for where using IS NULL for binding null
* values.
*/
private String genDynamicWhere(Set<String> includedProps, Object oldBean) throws SQLException {
// always has a preceding id property(s) so the first
// option is always ' and ' and not blank.
GenerateDmlRequest request = new GenerateDmlRequest(emptyStringAsNull, includedProps, oldBean);
request.append(sqlNone);
request.setWhereMode();
all.dmlWhere(request, true, oldBean);
return request.toString();
}
}
@@ -0,0 +1,129 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dml;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.PersistenceException;
import com.avaje.ebeaninternal.api.SpiTransaction;
import com.avaje.ebeaninternal.server.core.PersistRequest;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.lib.util.StringHelper;
import com.avaje.ebeaninternal.server.persist.BeanPersister;
/**
* Bean persister that uses the Handler and Meta objects.
* <p>
* The design of this is based on the immutable Meta objects. They hold a
* information in the form of lists of Bindable objects. This effectively
* flattens the structure of the bean with embedded and associated objects into
* a flat list of Bindable objects.
* </p>
*/
public final class DmlBeanPersister implements BeanPersister {
private static final Logger logger = Logger.getLogger(DmlBeanPersister.class.getName());
private final UpdateMeta updateMeta;
private final InsertMeta insertMeta;
private final DeleteMeta deleteMeta;
public DmlBeanPersister(UpdateMeta updateMeta, InsertMeta insertMeta, DeleteMeta deleteMeta) {
this.updateMeta = updateMeta;
this.insertMeta = insertMeta;
this.deleteMeta = deleteMeta;
}
/**
* execute the bean delete request.
*/
public void delete(PersistRequestBean<?> request) {
DeleteHandler delete = new DeleteHandler(request, deleteMeta);
execute(request, delete);
}
/**
* execute the bean insert request.
*/
public void insert(PersistRequestBean<?> request) {
InsertHandler insert = new InsertHandler(request, insertMeta);
execute(request, insert);
}
/**
* execute the bean update request.
*/
public void update(PersistRequestBean<?> request) {
UpdateHandler update = new UpdateHandler(request, updateMeta);
execute(request, update);
}
/**
* execute request taking batching into account.
*/
private void execute(PersistRequest request, PersistHandler handler) {
SpiTransaction trans = request.getTransaction();
boolean batchThisRequest = trans.isBatchThisRequest();
try {
handler.bind();
if (batchThisRequest) {
handler.addBatch();
} else {
// immediate insert
handler.execute();
}
} catch (SQLException e) {
// log the error to the transaction log
String errMsg = StringHelper.replaceStringMulti(e.getMessage(), new String[]{"\r","\n"}, "\\n ");
String msg = "ERROR executing DML bindLog["+handler.getBindLog()+"] error["+errMsg+"]";
if (request.getTransaction().isLogSummary()) {
request.getTransaction().logInternal(msg);
}
throw new PersistenceException(msg, e);
} finally {
if (!batchThisRequest && handler != null) {
try {
handler.close();
} catch (SQLException e) {
logger.log(Level.SEVERE, null, e);
}
}
}
}
}
@@ -0,0 +1,52 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dml;
import com.avaje.ebean.config.dbplatform.DatabasePlatform;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.persist.BeanPersister;
import com.avaje.ebeaninternal.server.persist.BeanPersisterFactory;
/**
* Factory for creating a DmlBeanPersister for a bean type.
*/
public class DmlBeanPersisterFactory implements BeanPersisterFactory {
private final MetaFactory metaFactory;
public DmlBeanPersisterFactory(DatabasePlatform dbPlatform) {
this.metaFactory = new MetaFactory(dbPlatform);
}
/**
* Create a DmlBeanPersister for the given bean type.
*/
public BeanPersister create(BeanDescriptor<?> desc) {
UpdateMeta updMeta = metaFactory.createUpdate(desc);
DeleteMeta delMeta = metaFactory.createDelete(desc);
InsertMeta insMeta = metaFactory.createInsert(desc);
return new DmlBeanPersister(updMeta, insMeta, delMeta);
}
}
@@ -0,0 +1,418 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dml;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.OptimisticLockException;
import com.avaje.ebeaninternal.api.SpiTransaction;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.core.PstmtBatch;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.persist.BatchedPstmt;
import com.avaje.ebeaninternal.server.persist.BatchedPstmtHolder;
import com.avaje.ebeaninternal.server.persist.dmlbind.BindableRequest;
import com.avaje.ebeaninternal.server.type.DataBind;
/**
* Base class for Handler implementations.
*/
public abstract class DmlHandler implements PersistHandler, BindableRequest {
protected static final Logger logger = Logger.getLogger(DmlHandler.class.getName());
/**
* The originating request.
*/
protected final PersistRequestBean<?> persistRequest;
protected final StringBuilder bindLog;
protected final Set<String> loadedProps;
protected final SpiTransaction transaction;
protected final boolean emptyStringToNull;
protected final boolean logLevelSql;
/**
* The PreparedStatement used for the dml.
*/
protected DataBind dataBind;
protected String sql;
protected ArrayList<UpdateGenValue> updateGenValues;
private Set<String> additionalProps;
// private boolean checkDelta;
//
// private BeanDelta deltaBean;
protected DmlHandler(PersistRequestBean<?> persistRequest, boolean emptyStringToNull) {
this.persistRequest = persistRequest;
this.emptyStringToNull = emptyStringToNull;
this.loadedProps = persistRequest.getLoadedProperties();
this.transaction = persistRequest.getTransaction();
this.logLevelSql = transaction.isLogSql();
if (logLevelSql) {
this.bindLog = new StringBuilder();
} else {
this.bindLog = null;
}
}
// protected void setCheckDelta(boolean checkDelta) {
// this.checkDelta = checkDelta;
// }
public PersistRequestBean<?> getPersistRequest() {
return persistRequest;
}
/**
* Get the sql and bind the statement.
*/
public abstract void bind() throws SQLException;
/**
* Execute now for non-batch execution.
*/
public abstract void execute() throws SQLException;
/**
* Check the rowCount.
*/
protected void checkRowCount(int rowCount) throws SQLException, OptimisticLockException {
try {
persistRequest.checkRowCount(rowCount);
persistRequest.postExecute();
} catch (OptimisticLockException e){
// add the SQL and bind values to error message
String m = e.getMessage()+" sql["+sql+"] bind["+bindLog+"]";
persistRequest.getTransaction().log("OptimisticLockException:"+m);
throw new OptimisticLockException(m, null, e.getEntity());
}
}
/**
* Add this for batch execution.
*/
public void addBatch() throws SQLException {
PstmtBatch pstmtBatch = persistRequest.getPstmtBatch();
if (pstmtBatch != null){
pstmtBatch.addBatch(dataBind.getPstmt());
} else {
dataBind.getPstmt().addBatch();
}
}
/**
* Close the underlying statement.
*/
public void close() {
try {
if (dataBind != null){
dataBind.close();
}
} catch (SQLException ex) {
logger.log(Level.SEVERE, null, ex);
}
}
/**
* Return the bind log.
*/
public String getBindLog() {
return bindLog == null ? "" : bindLog.toString();
}
/**
* Set the Id value that was bound. This value is used for logging summary
* level information.
*/
public void setIdValue(Object idValue) {
persistRequest.setBoundId(idValue);
}
/**
* Log the bind information to the transaction log.
*/
protected void logBinding() {
if (logLevelSql) {
transaction.logInternal(bindLog.toString());
}
}
/**
* Log the sql to the transaction log.
*/
protected void logSql(String sql) {
if (logLevelSql) {
transaction.logInternal(sql);
}
}
public boolean isIncluded(BeanProperty prop) {
return (loadedProps == null || loadedProps.contains(prop.getName()));
}
public boolean isIncludedWhere(BeanProperty prop) {
if (prop.isDbEncrypted()){
// update without a version property ...
// for encrypted properties only include if it was
// also an updated/modified property
return isIncluded(prop);
}
return prop.isDbUpdatable() && (loadedProps == null || loadedProps.contains(prop.getName()));
}
/**
* Bind a raw value. Used to bind the discriminator column.
*/
public Object bind(String propName, Object value, int sqlType) throws SQLException {
if (logLevelSql) {
bindLog.append(propName).append("=");
bindLog.append(value).append(", ");
}
dataBind.setObject(value, sqlType);
return value;
}
public Object bindNoLog(Object value, int sqlType, String logPlaceHolder) throws SQLException {
if (logLevelSql) {
bindLog.append(logPlaceHolder).append(" ");
}
dataBind.setObject(value, sqlType);
return value;
}
/**
* Bind the value to the preparedStatement.
*/
public Object bind(Object value, BeanProperty prop, String propName, boolean bindNull) throws SQLException {
return bindInternal(logLevelSql, value, prop, propName, bindNull);
}
/**
* Bind the value to the preparedStatement without logging.
*/
public Object bindNoLog(Object value, BeanProperty prop, String propName, boolean bindNull) throws SQLException {
return bindInternal(false, value, prop, propName, bindNull);
}
private Object bindInternal(boolean log, Object value, BeanProperty prop, String propName, boolean bindNull) throws SQLException {
if (!bindNull){
if (emptyStringToNull && (value instanceof String) && ((String)value).length() == 0){
// support Oracle conversion of empty string to null
//value = prop.getDbNullValue(value);
value = null;
}
}
if (!bindNull && value == null) {
// where will have IS NULL clause so don't actually bind
if (log) {
bindLog.append(propName).append("=");
bindLog.append("null, ");
}
} else {
if (log) {
bindLog.append(propName).append("=");
if (prop.isLob()){
bindLog.append("[LOB]");
} else {
String sv = String.valueOf(value);
if (sv.length() > 50){
sv = sv.substring(0,47)+"...";
}
bindLog.append(sv);
}
bindLog.append(", ");
}
// do the actual binding to PreparedStatement
prop.bind(dataBind, value);
// if (checkDelta) {
// if (!prop.isId() && prop.isDeltaRequired()){
// if (deltaBean == null){
// deltaBean = persistRequest.createDeltaBean();
// transaction.getEvent().addBeanDelta(deltaBean);
// }
// deltaBean.add(prop, value);
// }
// }
}
return value;
}
/**
* Add the comment to the bind information log.
*/
protected void bindLogAppend(String comment) {
if (logLevelSql) {
bindLog.append(comment);
}
}
/**
* For generated properties set on insert register as additional
* loaded properties if required.
*/
public final void registerAdditionalProperty(String propertyName) {
if (loadedProps != null && !loadedProps.contains(propertyName)){
if (additionalProps == null){
additionalProps = new HashSet<String>();
}
additionalProps.add(propertyName);
}
}
/**
* Set any additional (generated) properties to the set of loaded properties
* if required.
*/
protected void setAdditionalProperties() {
if (additionalProps != null){
// additional generated properties set on insert
// added to the set of loaded properties
additionalProps.addAll(loadedProps);
persistRequest.setLoadedProps(additionalProps);
}
}
/**
* Register a generated value on a update. This can not be set to the bean
* until after the where clause has been bound for concurrency checking.
* <p>
* GeneratedProperty values are likely going to be used for optimistic
* concurrency checking. This includes 'counter' and 'update timestamp'
* generation.
* </p>
*/
public void registerUpdateGenValue(BeanProperty prop, Object bean, Object value) {
if (updateGenValues == null) {
updateGenValues = new ArrayList<UpdateGenValue>();
}
updateGenValues.add(new UpdateGenValue(prop, bean, value));
registerAdditionalProperty(prop.getName());
}
/**
* Set any update generated values to the bean. Must be called after where
* clause has been bound.
*/
public void setUpdateGenValues() {
if (updateGenValues != null) {
for (int i = 0; i < updateGenValues.size(); i++) {
UpdateGenValue updGenVal = updateGenValues.get(i);
updGenVal.setValue();
}
}
}
/**
* Check with useGeneratedKeys to get appropriate PreparedStatement.
*/
protected 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.
int[] columns = {1};
return conn.prepareStatement(sql, columns);
} else {
return conn.prepareStatement(sql);
}
}
/**
* Return a prepared statement taking into account batch requirements.
*/
protected PreparedStatement getPstmt(SpiTransaction t, String sql, PersistRequestBean<?> request, boolean genKeys)
throws SQLException {
BatchedPstmtHolder batch = t.getBatchControl().getPstmtHolder();
PreparedStatement stmt = batch.getStmt(sql, request);
if (stmt != null) {
return stmt;
}
if (logLevelSql){
t.logInternal(sql);
}
stmt = getPstmt(t, sql, genKeys);
PstmtBatch pstmtBatch = request.getPstmtBatch();
if (pstmtBatch != null){
pstmtBatch.setBatchSize(stmt, t.getBatchControl().getBatchSize());
}
BatchedPstmt bs = new BatchedPstmt(stmt, genKeys, sql, request.getPstmtBatch(), true);
batch.addStmt(bs, request);
return stmt;
}
/**
* Hold the values from GeneratedValue that need to be set to the bean
* property after the where clause has been built.
*/
private static final class UpdateGenValue {
private final BeanProperty property;
private final Object bean;
private final Object value;
private UpdateGenValue(BeanProperty property, Object bean, Object value) {
this.property = property;
this.bean = bean;
this.value = value;
}
/**
* Set the value to the bean property.
*/
private void setValue() {
// support PropertyChangeSupport
property.setValueIntercept(bean, value);
}
}
}
@@ -0,0 +1,22 @@
package com.avaje.ebeaninternal.server.persist.dml;
/**
* Used to indicate the part of DML being processed.
*/
public enum DmlMode {
/**
* The Insert SET.
*/
INSERT,
/**
* The Update SET.
*/
UPDATE,
/**
* The Update or Delete WHERE.
*/
WHERE
}
@@ -0,0 +1,152 @@
package com.avaje.ebeaninternal.server.persist.dml;
import java.util.Set;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
/**
* Helper to support the generation of DML statements.
*/
public class GenerateDmlRequest {
private static final String IS_NULL = " is null";
private final boolean emptyStringAsNull;
private final StringBuilder sb = new StringBuilder(100);
private final Set<String> includeProps;
private final Set<String> includeWhereProps;
private final Object oldValues;
private StringBuilder insertBindBuffer;
private String prefix;
private String prefix2;
private int insertMode;
private int bindColumnCount;
/**
* Create with includeWhereProps same as includeProps.
*/
public GenerateDmlRequest(boolean emptyStringAsNull, Set<String> includeProps, Object oldValues) {
this(emptyStringAsNull, includeProps, includeProps, oldValues);
}
/**
* Create from a PersistRequestBean.
*/
public GenerateDmlRequest(boolean emptyStringAsNull, Set<String> includeProps, Set<String> includeWhereProps, Object oldValues) {
this.emptyStringAsNull = emptyStringAsNull;
this.includeProps = includeProps;
this.includeWhereProps = includeWhereProps;
this.oldValues = oldValues;
}
/**
* Create for generating standard all properties DML/SQL.
*/
public GenerateDmlRequest(boolean emptyStringAsNull) {
this(emptyStringAsNull, null, null, null);
}
public GenerateDmlRequest append(String s) {
sb.append(s);
return this;
}
public boolean isDbNull(Object v) {
return v == null || (emptyStringAsNull && (v instanceof String) && ((String) v).length() == 0);
}
/**
* Return true if this property should be included in the set clause.
*/
public boolean isIncluded(BeanProperty prop) {
return (includeProps == null || includeProps.contains(prop.getName()));
}
/**
* Return true if this property should be included in the where clause.
*/
public boolean isIncludedWhere(BeanProperty prop) {
return (includeWhereProps == null || includeWhereProps.contains(prop.getName()));
}
public void appendColumnIsNull(String column) {
appendColumn(column, IS_NULL);
}
public void appendColumn(String column) {
String bind = (insertMode > 0) ? "?" : "=?";
appendColumn(column, bind);
}
public void appendColumn(String column, String suffik) {
appendColumn(column, "", suffik);
}
public void appendColumn(String column, String expr, String suffik) {
++bindColumnCount;
sb.append(prefix);
sb.append(column);
sb.append(expr);
if (insertMode > 0) {
if (insertMode++ > 1) {
insertBindBuffer.append(",");
}
insertBindBuffer.append(suffik);
} else {
sb.append(suffik);
}
if (prefix2 != null) {
prefix = prefix2;
prefix2 = null;
}
}
public int getBindColumnCount() {
return bindColumnCount;
}
public String getInsertBindBuffer() {
return insertBindBuffer.toString();
}
public String toString() {
return sb.toString();
}
public void setWhereMode() {
this.prefix = " and ";
this.prefix2 = " and ";
}
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 Object getOldValues() {
return oldValues;
}
}
@@ -0,0 +1,258 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
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.sql.Statement;
import java.util.HashSet;
import java.util.List;
import java.util.logging.Level;
import javax.persistence.OptimisticLockException;
import javax.persistence.PersistenceException;
import com.avaje.ebean.EbeanServer;
import com.avaje.ebeaninternal.api.DerivedRelationshipData;
import com.avaje.ebeaninternal.api.SpiTransaction;
import com.avaje.ebeaninternal.server.core.Message;
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.DmlUtil;
import com.avaje.ebeaninternal.server.type.DataBind;
/**
* Insert bean handler.
*/
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 boolean useGeneratedKeys;
/**
* A SQL Select used to fetch back the Id where generatedKeys is not
* supported.
*/
private String selectLastInsertedId;
/**
* Create to handle the insert execution.
*/
public InsertHandler(PersistRequestBean<?> persist, InsertMeta meta) {
super(persist, meta.isEmptyStringToNull());
this.meta = meta;
this.concatinatedKey = meta.isConcatinatedKey();
}
@Override
public boolean isIncluded(BeanProperty prop) {
return prop.isDbInsertable() && (super.isIncluded(prop));
}
/**
* Generate and bind the insert statement.
*/
public void bind() throws SQLException {
BeanDescriptor<?> desc = persistRequest.getBeanDescriptor();
Object bean = persistRequest.getBean();
Object idValue = desc.getId(bean);
boolean withId = !DmlUtil.isNullOrZero(idValue);
// check to see if we are going to use generated keys
if (!withId) {
if (concatinatedKey) {
// expecting a concatenated key that can
// be built from supplied AssocOne beans
withId = meta.deriveConcatenatedId(persistRequest);
} else if (meta.supportsGetGeneratedKeys()) {
// Identity with getGeneratedKeys
useGeneratedKeys = true;
} else {
// use a query to get the last inserted id
selectLastInsertedId = meta.getSelectLastInsertedId();
}
}
SpiTransaction t = persistRequest.getTransaction();
boolean isBatch = t.isBatchThisRequest();
// get the appropriate sql
sql = meta.getSql(withId);
PreparedStatement pstmt;
if (isBatch) {
pstmt = getPstmt(t, sql, persistRequest, useGeneratedKeys);
} else {
logSql(sql);
pstmt = getPstmt(t, sql, useGeneratedKeys);
}
dataBind = new DataBind(pstmt);
bindLogAppend("Binding Insert [");
bindLogAppend(desc.getBaseTable());
bindLogAppend("] set[");
// bind the bean property values
meta.bind(this, bean, withId);
bindLogAppend("]");
logBinding();
}
/**
* Check with useGeneratedKeys to get appropriate PreparedStatement.
*/
@Override
protected PreparedStatement getPstmt(SpiTransaction t, String sql, boolean useGeneratedKeys) throws SQLException {
Connection conn = t.getInternalConnection();
if (useGeneratedKeys) {
return conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
} else {
return conn.prepareStatement(sql);
}
}
/**
* Execute the insert in a normal non batch fashion. Additionally using
* getGeneratedKeys if required.
*/
public void execute() throws SQLException, OptimisticLockException {
int rc = dataBind.executeUpdate();
if (useGeneratedKeys) {
// get the auto-increment value back and set into the bean
getGeneratedKeys();
} else if (selectLastInsertedId != null) {
// fetch back the Id using a query
fetchGeneratedKeyUsingSelect();
}
checkRowCount(rc);
setAdditionalProperties();
executeDerivedRelationships();
}
protected void executeDerivedRelationships() {
List<DerivedRelationshipData> derivedRelationships = persistRequest.getDerivedRelationships();
if (derivedRelationships != null) {
for (int i = 0; i < derivedRelationships.size(); i++) {
DerivedRelationshipData derivedRelationshipData = derivedRelationships.get(i);
EbeanServer ebeanServer = persistRequest.getEbeanServer();
HashSet<String> updateProps = new HashSet<String>();
updateProps.add(derivedRelationshipData.getLogicalName());
ebeanServer.update(derivedRelationshipData.getBean(), updateProps, transaction, false, true);
}
}
}
/**
* For non batch insert with generated keys.
*/
private void getGeneratedKeys() throws SQLException {
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"));
}
} finally {
try {
rset.close();
} catch (SQLException ex) {
String msg = "Error closing rset for returning generatedKeys?";
logger.log(Level.WARNING, msg, ex);
}
}
}
/**
* For non batch insert with DBs that do not support getGeneratedKeys. Use a
* SQL select to fetch back the Id value.
*/
private void fetchGeneratedKeyUsingSelect() throws SQLException {
Connection conn = transaction.getConnection();
PreparedStatement stmt = null;
ResultSet rset = null;
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"));
}
} finally {
try {
if (rset != null) {
rset.close();
}
} catch (SQLException ex) {
String msg = "Error closing rset for fetchGeneratedKeyUsingSelect?";
logger.log(Level.WARNING, msg, ex);
}
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException ex) {
String msg = "Error closing stmt for fetchGeneratedKeyUsingSelect?";
logger.log(Level.WARNING, msg, ex);
}
}
}
public void registerDerivedRelationship(DerivedRelationshipData derivedRelationship) {
persistRequest.getTransaction().registerDerivedRelationship(derivedRelationship);
}
}
@@ -0,0 +1,208 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dml;
import java.sql.SQLException;
import java.util.Set;
import com.avaje.ebean.config.dbplatform.DatabasePlatform;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
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;
/**
* 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 sqlWithId;
private final BindableId id;
private final Bindable discriminator;
private final Bindable all;
private final boolean supportsGetGeneratedKeys;
private final boolean concatinatedKey;
private final String tableName;
/**
* 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) {
this.emptyStringToNull = dbPlatform.isTreatEmptyStringsAsNull();
this.tableName = desc.getBaseTable();
this.discriminator = getDiscriminator(desc);
this.id = id;
this.all = all;
this.shadowFKey = shadowFKey;
this.sqlWithId = genSql(false, null);
// 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;
this.identityDbColumns = new String[]{id.getIdentityColumn()};
this.sqlNullId = genSql(true, null);
this.supportsGetGeneratedKeys = dbPlatform.getDbIdentity().isSupportsGetGeneratedKeys();
this.selectLastInsertedId = desc.getSelectLastInsertedId();
}
}
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, Object bean, boolean withId) throws SQLException {
if (withId) {
id.dmlBind(request, false, bean);
}
if (shadowFKey != null){
shadowFKey.dmlBind(request, false, bean);
}
if (discriminator != null){
discriminator.dmlBind(request, false, bean);
}
all.dmlBind(request, false, 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, Set<String> loadedProps) {
GenerateDmlRequest request = new GenerateDmlRequest(emptyStringToNull, loadedProps, null);
request.setInsertSetMode();
request.append("insert into ").append(tableName);
request.append(" (");
if (!nullId) {
id.dmlInsert(request, false);
}
if (shadowFKey != null){
shadowFKey.dmlInsert(request, false);
}
if (discriminator != null){
discriminator.dmlInsert(request, false);
}
all.dmlInsert(request, false);
request.append(") values (");
request.append(request.getInsertBindBuffer());
request.append(")");
return request.toString();
}
}
@@ -0,0 +1,146 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
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;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne;
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 com.avaje.ebeaninternal.server.persist.dmlbind.BindableUnidirectional;
import com.avaje.ebeaninternal.server.persist.dmlbind.FactoryAssocOnes;
import com.avaje.ebeaninternal.server.persist.dmlbind.FactoryBaseProperties;
import com.avaje.ebeaninternal.server.persist.dmlbind.FactoryEmbedded;
import com.avaje.ebeaninternal.server.persist.dmlbind.FactoryId;
import com.avaje.ebeaninternal.server.persist.dmlbind.FactoryVersion;
/**
* Factory for creating InsertMeta UpdateMeta and DeleteMeta.
*/
public 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 DatabasePlatform dbPlatform;
private final boolean emptyStringAsNull;
public MetaFactory(DatabasePlatform dbPlatform) {
this.dbPlatform = dbPlatform;
this.emptyStringAsNull = dbPlatform.isTreatEmptyStringsAsNull();
// to bind encryption data before or after the encryption key
DbEncrypt dbEncrypt = dbPlatform.getDbEncrypt();
boolean bindEncryptDataFirst = dbEncrypt == null ? true : dbEncrypt.isBindEncryptDataFirst();
this.baseFact = new FactoryBaseProperties(bindEncryptDataFirst);
this.embeddedFact = new FactoryEmbedded(bindEncryptDataFirst);
}
/**
* Create the UpdateMeta for the given bean type.
*/
public UpdateMeta createUpdate(BeanDescriptor<?> desc) {
List<Bindable> setList = new ArrayList<Bindable>();
baseFact.create(setList, desc, DmlMode.UPDATE, includeLobs);
embeddedFact.create(setList, desc, DmlMode.UPDATE, includeLobs);
assocOneFact.create(setList, desc, DmlMode.UPDATE);
BindableId id = idFact.createId(desc);
Bindable ver = versionFact.create(desc);
List<Bindable> allList = new ArrayList<Bindable>();
baseFact.create(allList, desc, DmlMode.WHERE, false);
embeddedFact.create(allList, desc, DmlMode.WHERE, false);
assocOneFact.create(allList, desc, DmlMode.WHERE);
Bindable setBindable = new BindableList(setList);
Bindable allBindable = new BindableList(allList);
return new UpdateMeta(emptyStringAsNull, desc, setBindable, id, ver, allBindable);
}
/**
* Create the DeleteMeta for the given bean type.
*/
public DeleteMeta createDelete(BeanDescriptor<?> desc) {
BindableId id = idFact.createId(desc);
Bindable ver = versionFact.create(desc);
List<Bindable> allList = new ArrayList<Bindable>();
baseFact.create(allList, desc, DmlMode.WHERE, false);
embeddedFact.create(allList, desc, DmlMode.WHERE, false);
assocOneFact.create(allList, desc, DmlMode.WHERE);
Bindable allBindable = new BindableList(allList);
return new DeleteMeta(emptyStringAsNull, desc, id, ver, allBindable);
}
/**
* Create the InsertMeta for the given bean type.
*/
public InsertMeta createInsert(BeanDescriptor<?> desc) {
BindableId id = idFact.createId(desc);
List<Bindable> allList = new ArrayList<Bindable>();
baseFact.create(allList, desc, DmlMode.INSERT, includeLobs);
embeddedFact.create(allList, desc, DmlMode.INSERT, includeLobs);
assocOneFact.create(allList, desc, DmlMode.INSERT);
Bindable allBindable = new BindableList(allList);
BeanPropertyAssocOne<?> unidirectional = desc.getUnidirectional();
Bindable shadowFkey;
if (unidirectional == null) {
shadowFkey = null;
} else {
shadowFkey = new BindableUnidirectional(desc, unidirectional);
}
return new InsertMeta(dbPlatform, desc, shadowFkey, id, allBindable);
}
}
@@ -0,0 +1,53 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dml;
import java.sql.SQLException;
/**
* Implementation API for insert update and delete handlers.
*/
public interface PersistHandler {
/**
* Return the bind log.
*/
public String getBindLog();
/**
* Get the sql and bind the statement.
*/
public void bind() throws SQLException;
/**
* Add this for batch execution.
*/
public void addBatch() throws SQLException;
/**
* Execute now for non-batch execution.
*/
public void execute() throws SQLException;
/**
* Close resources including underlying preparedStatement.
*/
public void close() throws SQLException;
}
@@ -0,0 +1,122 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dml;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Set;
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.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.type.DataBind;
/**
* Update bean handler.
*/
public class UpdateHandler extends DmlHandler {
private final UpdateMeta meta;
private Set<String> updatedProperties;
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 {
SpiUpdatePlan updatePlan = meta.getUpdatePlan(persistRequest);
if (updatePlan.isEmptySetClause()) {
emptySetClause = true;
return;
}
updatedProperties = updatePlan.getProperties();
sql = updatePlan.getSql();
SpiTransaction t = persistRequest.getTransaction();
boolean isBatch = t.isBatchThisRequest();
PreparedStatement pstmt;
if (isBatch) {
pstmt = getPstmt(t, sql, persistRequest, false);
} else {
logSql(sql);
pstmt = getPstmt(t, sql, false);
}
dataBind = new DataBind(pstmt);
bindLogAppend("Binding Update [");
bindLogAppend(meta.getTableName());
bindLogAppend("] ");
meta.bind(persistRequest, this, updatePlan);
setUpdateGenValues();
bindLogAppend("]");
logBinding();
}
@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);
setAdditionalProperties();
}
}
@Override
public boolean isIncluded(BeanProperty prop) {
return prop.isDbUpdatable() && (updatedProperties == null || updatedProperties.contains(prop.getName()));
}
public void registerDerivedRelationship(DerivedRelationshipData derivedRelationship) {
persistRequest.getTransaction().registerDerivedRelationship(derivedRelationship);
}
}
@@ -0,0 +1,259 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dml;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.persistence.PersistenceException;
import com.avaje.ebeaninternal.api.SpiUpdatePlan;
import com.avaje.ebeaninternal.server.core.ConcurrencyMode;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.persist.dmlbind.Bindable;
import com.avaje.ebeaninternal.server.persist.dmlbind.BindableId;
import com.avaje.ebeaninternal.server.persist.dmlbind.BindableList;
/**
* Meta data for update handler. The meta data is for a particular bean type. It
* is considered immutable and is thread safe.
*/
public final class UpdateMeta {
private final String sqlVersion;
private final String sqlNone;
private final Bindable set;
private final BindableId id;
private final Bindable version;
private final Bindable all;
private final String tableName;
private final UpdatePlan modeNoneUpdatePlan;
private final UpdatePlan modeVersionUpdatePlan;
private final boolean emptyStringAsNull;
public UpdateMeta(boolean emptyStringAsNull, BeanDescriptor<?> desc, Bindable set, BindableId id, Bindable version, Bindable all) {
this.emptyStringAsNull = emptyStringAsNull;
this.tableName = desc.getBaseTable();
this.set = set;
this.id = id;
this.version = version;
this.all = all;
this.sqlNone = genSql(ConcurrencyMode.NONE, null, null);
this.sqlVersion = genSql(ConcurrencyMode.VERSION, null, null);
this.modeNoneUpdatePlan = new UpdatePlan(ConcurrencyMode.NONE, sqlNone, set);
this.modeVersionUpdatePlan = new UpdatePlan(ConcurrencyMode.VERSION, sqlVersion, set);
}
/**
* Return true if empty strings should be treated as null.
*/
public boolean isEmptyStringAsNull() {
return emptyStringAsNull;
}
/**
* Return the base table name.
*/
public String getTableName() {
return tableName;
}
/**
* Bind the request based on the concurrency mode.
*/
public void bind(PersistRequestBean<?> persist, DmlHandler bind, SpiUpdatePlan updatePlan) throws SQLException {
Object bean = persist.getBean();
bind.bindLogAppend(" set[");
// bind.setCheckDelta(true);
updatePlan.bindSet(bind, bean);
// bind.setCheckDelta(false);
bind.bindLogAppend("] where[");
id.dmlBind(bind, false, bean);
switch (persist.getConcurrencyMode()) {
case VERSION:
version.dmlBind(bind, false, bean);
break;
case ALL:
Object oldBean = persist.getOldValues();
all.dmlBindWhere(bind, true, oldBean);
break;
default:
break;
}
}
/**
* get or generate the sql based on the concurrency mode.
*/
public SpiUpdatePlan getUpdatePlan(PersistRequestBean<?> request) {
ConcurrencyMode mode = request.determineConcurrencyMode();
if (request.isDynamicUpdateSql()) {
return getDynamicUpdatePlan(mode, request);
}
// 'full bean' update...
switch (mode) {
case NONE:
return modeNoneUpdatePlan;
case VERSION:
return modeVersionUpdatePlan;
case ALL:
Object oldValues = request.getOldValues();
if (oldValues == null) {
throw new PersistenceException("OldValues are null?");
}
String sql = genDynamicWhere(request.getUpdatedProperties(), request.getLoadedProperties(), oldValues);
return new UpdatePlan(ConcurrencyMode.ALL, sql, set);
default:
throw new RuntimeException("Invalid mode " + mode);
}
}
private SpiUpdatePlan getDynamicUpdatePlan(ConcurrencyMode mode, PersistRequestBean<?> persistRequest) {
Set<String> updatedProps = persistRequest.getUpdatedProperties();
if (ConcurrencyMode.ALL.equals(mode)) {
// due to is null in where clause we won't bother trying to
// cache plans for ConcurrencyMode.ALL
String sql = genSql(mode, persistRequest, null);
if (sql == null) {
// changed properties must have been updatable=false
return UpdatePlan.EMPTY_SET_CLAUSE;
} else {
return new UpdatePlan(null, mode, sql, set, updatedProps);
}
}
// we can use a cached UpdatePlan for the changed properties
int hash = mode.hashCode();
hash = hash * 31 + (updatedProps == null ? 0 : updatedProps.hashCode());
Integer key = Integer.valueOf(hash);
BeanDescriptor<?> beanDescriptor = persistRequest.getBeanDescriptor();
SpiUpdatePlan updatePlan = beanDescriptor.getUpdatePlan(key);
if (updatePlan != null) {
return updatePlan;
}
// build a new UpdatePlan and cache it
// build a bindableList that only contains the changed properties
List<Bindable> list = new ArrayList<Bindable>();
set.addChanged(persistRequest, list);
BindableList bindableList = new BindableList(list);
// build the SQL for this update statement
String sql = genSql(mode, persistRequest, bindableList);
updatePlan = new UpdatePlan(key, mode, sql, bindableList, null);
// add the UpdatePlan to the cache
beanDescriptor.putUpdatePlan(key, updatePlan);
return updatePlan;
}
private String genSql(ConcurrencyMode conMode, PersistRequestBean<?> persistRequest, BindableList bindableList) {
// update set col0=?, col1=?, col2=? where bcol=? and bc1=? and bc2=?
GenerateDmlRequest request;
if (persistRequest == null) {
// For generation of None and Version DML/SQL
request = new GenerateDmlRequest(emptyStringAsNull);
} else {
request = persistRequest.createGenerateDmlRequest(emptyStringAsNull);
}
request.append("update ").append(tableName).append(" set ");
request.setUpdateSetMode();
if (bindableList != null) {
bindableList.dmlAppend(request, false);
} else {
set.dmlAppend(request, true);
}
if (request.getBindColumnCount() == 0) {
// update properties must have been updatable=false
// with the result that nothing is in the set clause
return null;
}
request.append(" where ");
request.setWhereIdMode();
id.dmlAppend(request, false);
if (ConcurrencyMode.VERSION.equals(conMode)) {
if (version == null) {
return null;
}
version.dmlAppend(request, false);
} else if (ConcurrencyMode.ALL.equals(conMode)) {
all.dmlWhere(request, true, request.getOldValues());
}
return request.toString();
}
/**
* Generate the sql dynamically for where using IS NULL for binding null
* values.
*/
private String genDynamicWhere(Set<String> loadedProps, Set<String> whereProps, Object oldBean) {
// always has a preceding id property(s) so the first
// option is always ' and ' and not blank.
GenerateDmlRequest request = new GenerateDmlRequest(emptyStringAsNull, loadedProps, whereProps, oldBean);
request.append(sqlNone);
request.setWhereMode();
all.dmlWhere(request, true, oldBean);
return request.toString();
}
}
@@ -0,0 +1,172 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dml;
import java.sql.SQLException;
import java.util.Set;
import com.avaje.ebeaninternal.api.SpiUpdatePlan;
import com.avaje.ebeaninternal.server.core.ConcurrencyMode;
import com.avaje.ebeaninternal.server.persist.dmlbind.Bindable;
/**
* Cachable plan for executing bean updates for a given set of changed
* properties.
*
* @author rbygrave
*/
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;
private final String sql;
private final Bindable set;
private final Set<String> properties;
private final boolean checkIncludes;
private final long timeCreated;
private final boolean emptySetClause;
private Long timeLastUsed;
/**
* Create a non cachable UpdatePlan.
*/
public UpdatePlan(ConcurrencyMode mode, String sql, Bindable set) {
this(null, mode, sql, set, null);
}
/**
* Create a cachable UpdatePlan with a given key.
*/
public UpdatePlan(Integer key, ConcurrencyMode mode, String sql,
Bindable set, Set<String> properties) {
this.emptySetClause = false;
this.key = key;
this.mode = mode;
this.sql = sql;
this.set = set;
this.properties = properties;
this.checkIncludes = properties != null;
this.timeCreated = System.currentTimeMillis();
}
/**
* Special constructor for emptySetClause=true instance.
*/
private UpdatePlan(){
this.emptySetClause = true;
this.key = Integer.valueOf(0);
this.mode = ConcurrencyMode.NONE;
this.sql = null;
this.set = null;
this.properties = null;
this.checkIncludes = false;
this.timeCreated = 0;
}
public boolean isEmptySetClause() {
return emptySetClause;
}
/**
* Run the prepared statement binding for the 'update set' properties.
*/
public void bindSet(DmlHandler bind, Object bean) throws SQLException {
set.dmlBind(bind, checkIncludes, bean);
// not strictly 'thread safe' but object assignment is atomic
Long touched = Long.valueOf(System.currentTimeMillis());
this.timeLastUsed = touched;
}
/**
* Return the time this plan was created.
*/
public long getTimeCreated() {
return timeCreated;
}
/**
* Return the time this plan was last used.
*/
public Long getTimeLastUsed() {
// not thread safe but atomic
return timeLastUsed;
}
/**
* Return the hash key.
*/
public Integer getKey() {
return key;
}
/**
* Return the concurrency mode for this plan.
*/
public ConcurrencyMode getMode() {
return mode;
}
/**
* Return the DML statement.
*/
public String getSql() {
return sql;
}
/**
* Return the Bindable properties for the update set.
*/
public Bindable getSet() {
return set;
}
/**
* Return the set of changed properties.
* <p>
* This can return null when all properties in the set are being bound in
* the update statement.
* </p>
*/
public Set<String> getProperties() {
return properties;
}
}
@@ -0,0 +1,10 @@
<HTML>
<HEAD>
<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=iso-8859-1">
<TITLE>Bean DML persistence implementation</TITLE>
</HEAD>
<Body BGCOLOR="#ffffff">
Bean DML persistence implementation
</Body>
</HTML>
@@ -0,0 +1,75 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.sql.SQLException;
import java.util.List;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.persist.dml.GenerateDmlRequest;
/**
* Item held by Meta objects used to generate and bind bean insert update and
* delete statements.
* <p>
* An implementation is expected to be immutable and thread safe.
* </p>
* <p>
* The design is to take a bean structure with embedded and associated objects
* etc and flatten that into lists of Bindable objects. These are put into
* InsertMeta UpdateMeta and DeleteMeta objects to support the generation of DML
* and binding of statements in a fast and painless manor.
* </p>
*/
public interface Bindable {
/**
* For Updates including only changed properties add the Bindable to the
* list if it should be included in the 'update set'.
*/
public void addChanged(PersistRequestBean<?> request, List<Bindable> list);
/**
* append sql to the buffer with prefix and suffix options.
*/
public void dmlInsert(GenerateDmlRequest request, boolean checkIncludes);
/**
* append sql to the buffer with prefix and suffix options.
*/
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes);
/**
* For WHERE clauses append sql to the buffer with prefix and suffix
* options. These need to take into account binding of null values.
*/
public void dmlWhere(GenerateDmlRequest request, boolean checkIncludes, Object bean);
/**
* Bind given the request and bean. The bean could be the oldValues bean
* when binding a update or delete where clause with ALL concurrency mode.
*/
public void dmlBind(BindableRequest request, boolean checkIncludes, Object bean)
throws SQLException;
public void dmlBindWhere(BindableRequest request, boolean checkIncludes, Object bean)
throws SQLException;
}
@@ -0,0 +1,107 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.sql.SQLException;
import java.util.List;
import com.avaje.ebeaninternal.api.DerivedRelationshipData;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne;
import com.avaje.ebeaninternal.server.deploy.id.ImportedId;
import com.avaje.ebeaninternal.server.persist.dml.GenerateDmlRequest;
/**
* Bindable for an ManyToOne or OneToOne associated bean.
*/
public class BindableAssocOne implements Bindable {
private final BeanPropertyAssocOne<?> assocOne;
private final ImportedId importedId;
public BindableAssocOne(BeanPropertyAssocOne<?> assocOne) {
this.assocOne = assocOne;
this.importedId = assocOne.getImportedId();
}
public String toString() {
return "BindableAssocOne " + assocOne;
}
public void addChanged(PersistRequestBean<?> request, List<Bindable> list) {
if (request.hasChanged(assocOne)) {
list.add(this);
}
}
public void dmlInsert(GenerateDmlRequest request, boolean checkIncludes) {
dmlAppend(request, checkIncludes);
}
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes) {
if (checkIncludes && !request.isIncluded(assocOne)) {
return;
}
importedId.dmlAppend(request);
}
/**
* Used for dynamic where clause generation.
*/
public void dmlWhere(GenerateDmlRequest request, boolean checkIncludes, Object bean) {
if (checkIncludes && !request.isIncludedWhere(assocOne)) {
return;
}
Object assocBean = assocOne.getValue(bean);
importedId.dmlWhere(request, assocBean);
}
public void dmlBind(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
if (checkIncludes && !request.isIncluded(assocOne)) {
return;
}
dmlBind(request, bean, true);
}
public void dmlBindWhere(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
if (checkIncludes && !request.isIncludedWhere(assocOne)) {
return;
}
dmlBind(request, bean, false);
}
private void dmlBind(BindableRequest request, Object bean, boolean bindNull)
throws SQLException {
Object assocBean = assocOne.getValue(bean);
Object boundValue = importedId.bind(request, assocBean, bindNull);
if (bindNull && boundValue == null && assocBean != null){
// this is the scenario for a derived foreign key
// which will require an additional update
// register for post insert of assocBean
// update of bean set ... importedId.getLogicalName();
// value of assocBean.getId
DerivedRelationshipData d = new DerivedRelationshipData(assocBean, assocOne.getName(), bean);
request.registerDerivedRelationship(d);
}
}
}
@@ -0,0 +1,106 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyCompound;
import com.avaje.ebeaninternal.server.persist.dml.GenerateDmlRequest;
/**
* Bindable for a Immutable Compound value object.
*/
public class BindableCompound implements Bindable {
private final Bindable[] items;
private final BeanPropertyCompound compound;
public BindableCompound(BeanPropertyCompound embProp, List<Bindable> list) {
this.compound = embProp;
this.items = list.toArray(new Bindable[list.size()]);
}
public String toString() {
return "BindableCompound " + compound + " items:" + Arrays.toString(items);
}
public void dmlInsert(GenerateDmlRequest request, boolean checkIncludes) {
dmlAppend(request, checkIncludes);
}
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes) {
if (checkIncludes && !request.isIncluded(compound)) {
return;
}
for (int i = 0; i < items.length; i++) {
items[i].dmlAppend(request, false);
}
}
/**
* Used for dynamic where clause generation.
*/
public void dmlWhere(GenerateDmlRequest request, boolean checkIncludes, Object origBean) {
if (checkIncludes && !request.isIncludedWhere(compound)) {
return;
}
Object valueObject = compound.getValue(origBean);
for (int i = 0; i < items.length; i++) {
items[i].dmlWhere(request, false, valueObject);
}
}
public void addChanged(PersistRequestBean<?> request, List<Bindable> list) {
if (request.hasChanged(compound)) {
list.add(this);
}
}
public void dmlBind(BindableRequest bindRequest, boolean checkIncludes, Object bean) throws SQLException {
if (checkIncludes && !bindRequest.isIncluded(compound)) {
return;
}
Object valueObject = compound.getValue(bean);
for (int i = 0; i < items.length; i++) {
items[i].dmlBind(bindRequest, false, valueObject);
}
}
public void dmlBindWhere(BindableRequest bindRequest, boolean checkIncludes, Object bean) throws SQLException {
if (checkIncludes && !bindRequest.isIncludedWhere(compound)) {
return;
}
Object valueObject = compound.getValue(bean);
for (int i = 0; i < items.length; i++) {
items[i].dmlBindWhere(bindRequest, false, valueObject);
}
}
}
@@ -0,0 +1,79 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.sql.SQLException;
import java.util.List;
import javax.persistence.PersistenceException;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.deploy.InheritInfo;
import com.avaje.ebeaninternal.server.persist.dml.GenerateDmlRequest;
/**
* Bindable for inserting a discriminator value.
*/
public class BindableDiscriminator implements Bindable {
private final String columnName;
private final Object discValue;
private final int sqlType;
public BindableDiscriminator(InheritInfo inheritInfo) {
this.columnName = inheritInfo.getDiscriminatorColumn();
this.discValue = inheritInfo.getDiscriminatorValue();
this.sqlType = inheritInfo.getDiscriminatorType();
}
public String toString() {
return columnName + " = " + discValue;
}
public void addChanged(PersistRequestBean<?> request, List<Bindable> list) {
throw new PersistenceException("Never called (only for inserts)");
}
public void dmlInsert(GenerateDmlRequest request, boolean checkIncludes) {
dmlAppend(request, checkIncludes);
}
/**
* Never used in where clause.
*/
public void dmlWhere(GenerateDmlRequest request, boolean checkIncludes, Object bean) {
// never used in where
}
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes) {
request.appendColumn(columnName);
}
public void dmlBind(BindableRequest bindRequest, boolean checkIncludes, Object bean) throws SQLException {
bindRequest.bind(columnName, discValue, sqlType);
}
public void dmlBindWhere(BindableRequest bindRequest, boolean checkIncludes, Object bean) throws SQLException {
bindRequest.bind(columnName, discValue, sqlType);
}
}
@@ -0,0 +1,139 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne;
import com.avaje.ebeaninternal.server.persist.dml.GenerateDmlRequest;
/**
* Bindable for a Embedded bean.
*/
public class BindableEmbedded implements Bindable {
private final Bindable[] items;
private final BeanPropertyAssocOne<?> embProp;
public BindableEmbedded(BeanPropertyAssocOne<?> embProp, List<Bindable> list) {
this.embProp = embProp;
this.items = list.toArray(new Bindable[list.size()]);
}
public String toString() {
return "BindableEmbedded " + embProp + " items:" + Arrays.toString(items);
}
public void dmlInsert(GenerateDmlRequest request, boolean checkIncludes) {
dmlAppend(request, checkIncludes);
}
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes) {
if (checkIncludes && !request.isIncluded(embProp)) {
return;
}
for (int i = 0; i < items.length; i++) {
items[i].dmlAppend(request, false);
}
}
/**
* Used for dynamic where clause generation.
*/
public void dmlWhere(GenerateDmlRequest request, boolean checkIncludes, Object origBean) {
if (checkIncludes && !request.isIncludedWhere(embProp)) {
return;
}
Object embBean = embProp.getValue(origBean);
Object oldValues = getOldValue(embBean);
for (int i = 0; i < items.length; i++) {
items[i].dmlWhere(request, false, oldValues);
}
}
public void addChanged(PersistRequestBean<?> request, List<Bindable> list) {
if (request.hasChanged(embProp)) {
list.add(this);
}
}
public void dmlBind(BindableRequest bindRequest, boolean checkIncludes, Object bean)
throws SQLException {
if (checkIncludes && !bindRequest.isIncluded(embProp)) {
return;
}
// get the embedded bean
Object embBean = embProp.getValue(bean);
for (int i = 0; i < items.length; i++) {
items[i].dmlBind(bindRequest, false, embBean);
}
}
public void dmlBindWhere(BindableRequest bindRequest, boolean checkIncludes, Object bean)
throws SQLException {
if (checkIncludes && !bindRequest.isIncludedWhere(embProp)) {
return;
}
// get the embedded bean
Object embBean = embProp.getValue(bean);
Object oldEmbBean = getOldValue(embBean);
for (int i = 0; i < items.length; i++) {
items[i].dmlBindWhere(bindRequest, false, oldEmbBean);
}
}
/**
* Get the old bean which will have the original values.
* <p>
* These are bound to the WHERE clause for updates.
* </p>
*/
private Object getOldValue(Object embBean) {
Object oldValues = null;
if (embBean instanceof EntityBean) {
// get the old embedded bean (with the original values)
oldValues = ((EntityBean) embBean)._ebean_getIntercept().getOldValues();
}
if (oldValues == null) {
// this embedded bean was not modified
// (or not an EntityBean)
oldValues = embBean;
}
return oldValues;
}
}
@@ -0,0 +1,144 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.persist.dml.GenerateDmlRequest;
/**
* Bindable for a DB encrypted BeanProperty.
*/
public class BindableEncryptedProperty implements Bindable {
private final BeanProperty prop;
private final boolean bindEncryptDataFirst;
public BindableEncryptedProperty(BeanProperty prop, boolean bindEncryptDataFirst) {
this.prop = prop;
this.bindEncryptDataFirst = bindEncryptDataFirst;
}
public String toString() {
return prop.toString();
}
public void addChanged(PersistRequestBean<?> request, List<Bindable> list) {
if (request.hasChanged(prop)) {
list.add(this);
}
}
public void dmlInsert(GenerateDmlRequest request, boolean checkIncludes) {
if (checkIncludes && !request.isIncluded(prop)) {
return;
}
// columnName
// AES_ENCRYPT(?,?)
request.appendColumn(prop.getDbColumn(), prop.getDbBind());
}
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes) {
if (checkIncludes && !request.isIncluded(prop)) {
return;
}
// columnName = AES_ENCRYPT(?,?)
request.appendColumn(prop.getDbColumn(), "=", prop.getDbBind());
}
/**
* Used for dynamic where clause generation.
*/
public void dmlWhere(GenerateDmlRequest request, boolean checkIncludes, Object bean) {
// only include encrypted property in where when it is included
// in the update as well (so not using isIncludedWhere)
if (checkIncludes && !request.isIncluded(prop)) {
return;
}
if (bean == null || request.isDbNull(prop.getValue(bean))) {
request.appendColumnIsNull(prop.getDbColumn());
} else {
// ? = AES_DECRYPT(columnName,?)
request.appendColumn("? = ", prop.getDecryptSql());
}
}
/**
* Bind a value in a Insert SET clause.
*/
public void dmlBind(BindableRequest request, boolean checkIncludes, Object bean)
throws SQLException {
if (checkIncludes && !request.isIncluded(prop)) {
return;
}
Object value = null;
if (bean != null) {
value = prop.getValue(bean);
}
// get Encrypt key
String encryptKeyValue = prop.getEncryptKey().getStringValue();
if (!bindEncryptDataFirst){
// H2 encrypt function ... different parameter order
request.bindNoLog(encryptKeyValue, Types.VARCHAR, prop.getName() + "=****");
}
request.bindNoLog(value, prop, prop.getName(), true);
if (bindEncryptDataFirst){
// MySql, Postgres, Oracle
request.bindNoLog(encryptKeyValue, Types.VARCHAR, prop.getName() + "=****");
}
}
/**
* Bind a value in a Insert SET clause.
*/
public void dmlBindWhere(BindableRequest request, boolean checkIncludes, Object bean)
throws SQLException {
if (checkIncludes && !request.isIncluded(prop)) {
return;
}
Object value = null;
if (bean != null) {
value = prop.getValue(bean);
}
// get Encrypt key
String encryptKeyValue = prop.getEncryptKey().getStringValue();
request.bind(value, prop, prop.getName(), false);
request.bindNoLog(encryptKeyValue, Types.VARCHAR, prop.getName() + "=****");
}
}
@@ -0,0 +1,61 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dmlbind;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
/**
* Adds support for id creation for concatenated ids on intersection tables.
* <p>
* Specifically if the concatenated id object is null on insert this can be
* built from the matching ManyToOne associated beans. For example RoleUserId
* embeddedId object could be built from the associated Role and User beans.
* </p>
* <p>
* This is only attempted if the id is null when it gets to the insert.
* </p>
*/
public interface BindableId extends Bindable {
/**
* Return true if there is no Id properties at all.
*/
public boolean isEmpty();
/**
* Return true if this is a concatenated key.
*/
public boolean isConcatenated();
/**
* Return the DB Column to use with genGeneratedKeys.
*/
public String getIdentityColumn();
/**
* Create the concatenated id for inserts with PFK relationships.
* <p>
* Really only where there are ManyToOne assoc beans that make up the
* primary key and the values can be got from those.
* </p>
*/
public boolean deriveConcatenatedId(PersistRequestBean<?> persist);
}
@@ -0,0 +1,148 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import javax.persistence.PersistenceException;
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.deploy.BeanPropertyAssocOne;
import com.avaje.ebeaninternal.server.persist.dml.GenerateDmlRequest;
/**
* Bindable for a EmbeddedId.
*/
public final class BindableIdEmbedded implements BindableId {
private final BeanPropertyAssocOne<?> embId;
private final BeanProperty[] props;
private final MatchedImportedProperty[] matches;
public BindableIdEmbedded(BeanPropertyAssocOne<?> embId, BeanDescriptor<?> desc) {
this.embId = embId;
this.props = embId.getTargetDescriptor().propertiesBaseScalar();
matches = MatchedImportedProperty.build(props, desc);
}
public boolean isEmpty() {
return false;
}
public boolean isConcatenated() {
return true;
}
public String getIdentityColumn() {
// return null for concatenated keys
return null;
}
@Override
public String toString() {
return embId + " props:" + Arrays.toString(props);
}
/**
* Does nothing for BindableId.
*/
public void addChanged(PersistRequestBean<?> request, List<Bindable> list) {
// do nothing (id not changing)
}
public void dmlBind(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
dmlBind(request, checkIncludes, bean, true);
}
public void dmlBindWhere(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
dmlBind(request, checkIncludes, bean, false);
}
private void dmlBind(BindableRequest bindRequest, boolean checkIncludes, Object bean, boolean bindNull) throws SQLException {
if (checkIncludes && !bindRequest.isIncluded(embId)) {
return;
}
Object idValue = embId.getValue(bean);
for (int i = 0; i < props.length; i++) {
Object value = props[i].getValue(idValue);
bindRequest.bind(value, props[i], props[i].getDbColumn(), bindNull);
}
bindRequest.setIdValue(idValue);
}
/**
* Id values are never null in where clause.
*/
public void dmlWhere(GenerateDmlRequest request, boolean checkIncludes, Object bean) {
if (checkIncludes && !request.isIncluded(embId)) {
return;
}
dmlAppend(request, false);
}
public void dmlInsert(GenerateDmlRequest request, boolean checkIncludes) {
dmlAppend(request, checkIncludes);
}
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes) {
if (checkIncludes && !request.isIncluded(embId)) {
return;
}
for (int i = 0; i < props.length; i++) {
request.appendColumn(props[i].getDbColumn());
}
}
public boolean deriveConcatenatedId(PersistRequestBean<?> persist) {
if (matches == null) {
String m = "Matches for the concatinated key columns where not found?"
+ " I expect that the concatinated key was null, and this bean does"
+ " not have ManyToOne assoc beans matching the primary key columns?";
throw new PersistenceException(m);
}
Object bean = persist.getBean();
// create the new id
Object newId = embId.createEmbeddedId();
// populate it from the assoc one id values...
for (int i = 0; i < matches.length; i++) {
matches[i].populate(bean, newId);
}
// support PropertyChangeSupport
embId.setValueIntercept(bean, newId);
return true;
}
}
@@ -0,0 +1,51 @@
package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.sql.SQLException;
import java.util.List;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.persist.dml.GenerateDmlRequest;
public class BindableIdEmpty implements BindableId {
public boolean isEmpty() {
return true;
}
public void addChanged(PersistRequestBean<?> request, List<Bindable> list) {
// nothing
}
public void dmlInsert(GenerateDmlRequest request, boolean checkIncludes) {
// nothing
}
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes) {
// nothing
}
public void dmlWhere(GenerateDmlRequest request, boolean checkIncludes, Object bean) {
// nothing
}
public void dmlBind(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
// nothing
}
public void dmlBindWhere(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
// nothing
}
public boolean isConcatenated() {
return false;
}
public String getIdentityColumn() {
return null;
}
public boolean deriveConcatenatedId(PersistRequestBean<?> persist) {
return false;
}
}
@@ -0,0 +1,133 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import javax.persistence.PersistenceException;
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.dml.GenerateDmlRequest;
/**
* Bindable for a concatenated id that is not embedded.
*/
public final class BindableIdMap implements BindableId {
private final BeanProperty[] uids;
private final MatchedImportedProperty[] matches;
public BindableIdMap(BeanProperty[] uids, BeanDescriptor<?> desc) {
this.uids = uids;
matches = MatchedImportedProperty.build(uids, desc);
}
public boolean isEmpty() {
return false;
}
public boolean isConcatenated() {
return true;
}
public String getIdentityColumn() {
// return null for concatenated keys
return null;
}
@Override
public String toString() {
return Arrays.toString(uids);
}
/**
* Does nothing for BindableId.
*/
public void addChanged(PersistRequestBean<?> request, List<Bindable> list) {
// do nothing (id not changing)
}
/**
* Id values are never null in where clause.
*/
public void dmlWhere(GenerateDmlRequest request, boolean checkIncludes, Object bean) {
// id values are never null in where clause
dmlAppend(request, false);
}
public void dmlInsert(GenerateDmlRequest request, boolean checkIncludes) {
dmlAppend(request, checkIncludes);
}
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes) {
for (int i = 0; i < uids.length; i++) {
request.appendColumn(uids[i].getDbColumn());
}
}
public void dmlBind(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
dmlBind(request, checkIncludes, bean, true);
}
public void dmlBindWhere(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
dmlBind(request, checkIncludes, bean, false);
}
private void dmlBind(BindableRequest bindRequest, boolean checkIncludes, Object bean, boolean bindNull) throws SQLException {
LinkedHashMap<String, Object> mapId = new LinkedHashMap<String, Object>();
for (int i = 0; i < uids.length; i++) {
Object value = uids[i].getValue(bean);
bindRequest.bind(value, uids[i], uids[i].getName(), bindNull);
// putting logicalType into map rather than
// the dbType (which may have been converted).
mapId.put(uids[i].getName(), value);
}
bindRequest.setIdValue(mapId);
}
public boolean deriveConcatenatedId(PersistRequestBean<?> persist) {
if (matches == null) {
String m = "Matches for the concatinated key columns where not found?"
+ " I expect that the concatinated key was null, and this bean does"
+ " not have ManyToOne assoc beans matching the primary key columns?";
throw new PersistenceException(m);
}
Object bean = persist.getBean();
// populate it from the assoc one id values...
for (int i = 0; i < matches.length; i++) {
matches[i].populate(bean, bean);
}
return true;
}
}
@@ -0,0 +1,108 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.sql.SQLException;
import java.util.List;
import javax.persistence.PersistenceException;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.persist.dml.GenerateDmlRequest;
/**
* Bindable for a single scalar id property.
*/
public final class BindableIdScalar implements BindableId {
private final BeanProperty uidProp;
public BindableIdScalar(BeanProperty uidProp) {
this.uidProp = uidProp;
}
public boolean isEmpty() {
return false;
}
public boolean isConcatenated() {
return false;
}
public String getIdentityColumn() {
return uidProp.getDbColumn();
}
@Override
public String toString() {
return uidProp.toString();
}
/**
* Does nothing for BindableId.
*/
public void addChanged(PersistRequestBean<?> request, List<Bindable> list) {
// do nothing (id not changing)
}
/**
* Should not be called as this is really only for concatenated keys.
*/
public boolean deriveConcatenatedId(PersistRequestBean<?> persist) {
throw new PersistenceException("Should not be called? only for concatinated keys");
}
/**
* Id values are never null in where clause.
*/
public void dmlWhere(GenerateDmlRequest request, boolean checkIncludes, Object bean) {
// id values are never null in where clause
request.appendColumn(uidProp.getDbColumn());
}
public void dmlInsert(GenerateDmlRequest request, boolean checkIncludes) {
dmlAppend(request, checkIncludes);
}
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes) {
request.appendColumn(uidProp.getDbColumn());
}
public void dmlBind(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
dmlBind(request, checkIncludes, bean, true);
}
public void dmlBindWhere(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
dmlBind(request, checkIncludes, bean, false);
}
private void dmlBind(BindableRequest bindRequest, boolean checkIncludes, Object bean, boolean bindNull) throws SQLException {
Object value = uidProp.getValue(bean);
bindRequest.bind(value, uidProp, uidProp.getName(), bindNull);
// used for summary logging
bindRequest.setIdValue(value);
}
}
@@ -0,0 +1,80 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.sql.SQLException;
import java.util.List;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.persist.dml.GenerateDmlRequest;
/**
* List of Bindable items.
*/
public class BindableList implements Bindable {
private final Bindable[] items;
public BindableList(List<Bindable> list) {
items = list.toArray(new Bindable[list.size()]);
}
public void addChanged(PersistRequestBean<?> request, List<Bindable> list) {
for (int i = 0; i < items.length; i++) {
items[i].addChanged(request, list);
}
}
public void dmlInsert(GenerateDmlRequest request, boolean checkIncludes) {
for (int i = 0; i < items.length; i++) {
items[i].dmlInsert(request, checkIncludes);
}
}
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes) {
for (int i = 0; i < items.length; i++) {
items[i].dmlAppend(request, checkIncludes);
}
}
public void dmlWhere(GenerateDmlRequest request, boolean checkIncludes, Object bean) {
for (int i = 0; i < items.length; i++) {
items[i].dmlWhere(request, checkIncludes, bean);
}
}
public void dmlBind(BindableRequest bindRequest, boolean checkIncludes, Object bean)
throws SQLException {
for (int i = 0; i < items.length; i++) {
items[i].dmlBind(bindRequest, checkIncludes, bean);
}
}
public void dmlBindWhere(BindableRequest bindRequest, boolean checkIncludes, Object bean)
throws SQLException {
for (int i = 0; i < items.length; i++) {
items[i].dmlBindWhere(bindRequest, checkIncludes, bean);
}
}
}
@@ -0,0 +1,102 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.sql.SQLException;
import java.util.List;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.persist.dml.GenerateDmlRequest;
/**
* Bindable for a single BeanProperty.
*/
public class BindableProperty implements Bindable {
protected final BeanProperty prop;
public BindableProperty(BeanProperty prop) {
this.prop = prop;
}
public String toString() {
return prop.toString();
}
public void addChanged(PersistRequestBean<?> request, List<Bindable> list) {
if (request.hasChanged(prop)) {
list.add(this);
}
}
public void dmlInsert(GenerateDmlRequest request, boolean checkIncludes) {
dmlAppend(request, checkIncludes);
}
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes) {
if (checkIncludes && !request.isIncluded(prop)) {
return;
}
request.appendColumn(prop.getDbColumn());
}
/**
* Used for dynamic where clause generation.
*/
public void dmlWhere(GenerateDmlRequest request, boolean checkIncludes, Object bean) {
if (checkIncludes && !request.isIncludedWhere(prop)) {
return;
}
if (bean == null || request.isDbNull(prop.getValue(bean))) {
request.appendColumnIsNull(prop.getDbColumn());
} else {
request.appendColumn(prop.getDbColumn());
}
}
public void dmlBind(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
if (checkIncludes && !request.isIncluded(prop)) {
return;
}
dmlBind(request, bean, true);
}
public void dmlBindWhere(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
if (checkIncludes && !request.isIncludedWhere(prop)) {
return;
}
dmlBind(request, bean, false);
}
private void dmlBind(BindableRequest request, Object bean, boolean bindNull)
throws SQLException {
Object value = null;
if (bean != null) {
value = prop.getValue(bean);
}
// value = prop.getDefaultValue();
request.bind(value, prop, prop.getName(), bindNull);
}
}
@@ -0,0 +1,76 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.sql.SQLException;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.deploy.generatedproperty.GeneratedProperty;
import com.avaje.ebeaninternal.server.persist.dml.GenerateDmlRequest;
/**
* Bindable for insert on a property with a GeneratedProperty.
* <p>
* This is typically a 'insert timestamp', 'update timestamp' or 'counter'.
* </p>
*/
public class BindablePropertyInsertGenerated extends BindableProperty {
private final GeneratedProperty gen;
public BindablePropertyInsertGenerated(BeanProperty prop, GeneratedProperty gen) {
super(prop);
this.gen = gen;
}
public void dmlBind(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
dmlBind(request, checkIncludes, bean, true);
}
public void dmlBindWhere(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
dmlBind(request, checkIncludes, bean, false);
}
/**
* Bind a value in a Insert SET clause.
*/
private void dmlBind(BindableRequest request, boolean checkIncludes, Object bean, boolean bindNull) throws SQLException {
Object value = gen.getInsertValue(prop, bean);
// generated value should be the correct type
if (bean != null){
// support PropertyChangeSupport
prop.setValueIntercept(bean, value);
request.registerAdditionalProperty(prop.getName());
}
//value = prop.getDefaultValue();
request.bind(value, prop, prop.getName(), bindNull);
}
/**
* Always bind on Insert SET.
*/
@Override
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes){
request.appendColumn(prop.getDbColumn());
}
}
@@ -0,0 +1,95 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.sql.SQLException;
import java.util.List;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.deploy.generatedproperty.GeneratedProperty;
import com.avaje.ebeaninternal.server.persist.dml.GenerateDmlRequest;
/**
* Bindable for update on a property with a GeneratedProperty.
* <p>
* This is typically a 'update timestamp' or 'counter'.
* </p>
*/
public class BindablePropertyUpdateGenerated extends BindableProperty {
private final GeneratedProperty gen;
public BindablePropertyUpdateGenerated(BeanProperty prop, GeneratedProperty gen) {
super(prop);
this.gen = gen;
}
/**
* Always add BindablePropertyUpdateGenerated properties.
*/
public void addChanged(PersistRequestBean<?> request, List<Bindable> list) {
list.add(this);
}
public void dmlBind(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
if (checkIncludes && !request.isIncluded(prop)){
return;
}
dmlBind(request, bean, true);
}
public void dmlBindWhere(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
if (checkIncludes && !request.isIncludedWhere(prop)){
return;
}
dmlBind(request, bean, false);
}
private void dmlBind(BindableRequest request, Object bean, boolean bindNull) throws SQLException {
Object value = gen.getUpdateValue(prop, bean);
// generated value should be the correct type
request.bind(value, prop, prop.getName(), bindNull);
// only register the update value if it was included
// in the bean in the first place
if (request.isIncluded(prop)) {
// need to set the generated value to the bean later
// after the where clause has been generated
request.registerUpdateGenValue(prop, bean, value);
}
}
/**
* Always bind on Insert SET.
*/
@Override
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes){
if (checkIncludes && !request.isIncluded(prop)){
return;
}
request.appendColumn(prop.getDbColumn());
}
}
@@ -0,0 +1,98 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.sql.SQLException;
import com.avaje.ebeaninternal.api.DerivedRelationshipData;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
/**
* Request object passed to bindables.
*/
public interface BindableRequest {
/**
* Set the id for use with summary level logging.
*/
public void setIdValue(Object idValue);
/**
* Bind the value to a PreparedStatement.
* <p>
* Takes into account logicalType to dbType conversion if required.
* </p>
* <p>
* Returns the value that was bound (and was potentially converted from
* logicalType to dbType.
* </p>
*
* @param value
* the value of a property
* @param bindNull
* if true bind null values, if false use IS NULL.
*/
public Object bind(Object value, BeanProperty prop, String propName, boolean bindNull) throws SQLException;
/**
* Bind a raw value. Used to bind the discriminator column.
*/
public Object bind(String propName, Object value, int sqlType) throws SQLException;
/**
* Bind a raw value with a placeHolder to put into the transaction log.
*/
public Object bindNoLog(Object value, int sqlType, String logPlaceHolder) throws SQLException;
/**
* Bind the value to the preparedStatement without logging.
*/
public Object bindNoLog(Object value, BeanProperty prop, String propName, boolean bindNull) throws SQLException;
/**
* Return true if the property is included in this request.
*/
public boolean isIncluded(BeanProperty prop);
/**
* Return true if the property is included in the WHERE clause for this
* request.
*/
public boolean isIncludedWhere(BeanProperty prop);
/**
* Register the value from a update GeneratedValue. This can only be set to
* the bean property after the where clause has bean built.
*/
public void registerUpdateGenValue(BeanProperty prop, Object bean, Object value);
/**
* Register a property into loadedProperties if required.
*/
public void registerAdditionalProperty(String propertyName);
/**
* Return the original PersistRequest.
*/
public PersistRequestBean<?> getPersistRequest();
public void registerDerivedRelationship(DerivedRelationshipData assocBean);
}
@@ -0,0 +1,102 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.sql.SQLException;
import java.util.List;
import javax.persistence.PersistenceException;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne;
import com.avaje.ebeaninternal.server.deploy.id.ImportedId;
import com.avaje.ebeaninternal.server.persist.dml.GenerateDmlRequest;
/**
* Bindable for a unidirectional relationship.
* <p>
* This inserts the foreign key value that is retrieved from the id of the
* parentBean.
* </p>
*/
public class BindableUnidirectional implements Bindable {
private final BeanPropertyAssocOne<?> unidirectional;
private final ImportedId importedId;
private final BeanDescriptor<?> desc;
public BindableUnidirectional(BeanDescriptor<?> desc, BeanPropertyAssocOne<?> unidirectional) {
this.desc = desc;
this.unidirectional = unidirectional;
this.importedId = unidirectional.getImportedId();
}
public String toString() {
return "BindableShadowFKey " + unidirectional;
}
public void addChanged(PersistRequestBean<?> request, List<Bindable> list) {
throw new PersistenceException("Never called (for insert only)");
}
public void dmlInsert(GenerateDmlRequest request, boolean checkIncludes) {
dmlAppend(request, checkIncludes);
}
public void dmlAppend(GenerateDmlRequest request, boolean checkIncludes) {
// always included (in insert)
importedId.dmlAppend(request);
}
public void dmlWhere(GenerateDmlRequest request, boolean checkIncludes, Object bean) {
throw new RuntimeException("Never called");
}
public void dmlBind(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
dmlBind(request, checkIncludes, bean, true);
}
public void dmlBindWhere(BindableRequest request, boolean checkIncludes, Object bean) throws SQLException {
dmlBind(request, checkIncludes, bean, false);
}
private void dmlBind(BindableRequest request, boolean checkIncludes, Object bean, boolean bindNull)
throws SQLException {
PersistRequestBean<?> persistRequest = request.getPersistRequest();
Object parentBean = persistRequest.getParentBean();
if (parentBean == null) {
Class<?> localType = desc.getBeanType();
Class<?> targetType = unidirectional.getTargetType();
;
String msg = "Error inserting bean [" + localType + "] with unidirectional relationship. ";
msg += "For inserts you must use cascade save on the master bean [" + targetType + "].";
throw new PersistenceException(msg);
}
importedId.bind(request, parentBean, bindNull);
}
}
@@ -0,0 +1,68 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.util.List;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne;
import com.avaje.ebeaninternal.server.persist.dml.DmlMode;
/**
* A factory that builds Bindable for BeanPropertyAssocOne properties.
*/
public class FactoryAssocOnes {
public FactoryAssocOnes() {
}
/**
* Add foreign key columns from associated one beans.
*/
public List<Bindable> create(List<Bindable> list, BeanDescriptor<?> desc, DmlMode mode) {
BeanPropertyAssocOne<?>[] ones = desc.propertiesOneImported();
for (int i = 0; i < ones.length; i++) {
if (ones[i].isImportedPrimaryKey()){
// excluded as already part of the primary key
} else {
switch (mode) {
case WHERE:
break;
case INSERT:
if (!ones[i].isInsertable()) {
continue;
}
break;
case UPDATE:
if (!ones[i].isUpdateable()) {
continue;
}
break;
}
list.add(new BindableAssocOne(ones[i]));
}
}
return list;
}
}
@@ -0,0 +1,79 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.util.ArrayList;
import java.util.List;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyCompound;
import com.avaje.ebeaninternal.server.persist.dml.DmlMode;
/**
* Add base properties to the BindableList for a bean type.
* <p>
* This excludes unique embedded and associated properties.
* </p>
*/
public class FactoryBaseProperties {
private final FactoryProperty factoryProperty;
public FactoryBaseProperties(boolean bindEncryptDataFirst) {
factoryProperty = new FactoryProperty(bindEncryptDataFirst);
}
/**
* Add Bindable for the base properties to the list.
*/
public void create(List<Bindable> list, BeanDescriptor<?> desc, DmlMode mode, boolean withLobs) {
add(desc.propertiesBaseScalar(), list, desc, mode, withLobs);
BeanPropertyCompound[] compoundProps = desc.propertiesBaseCompound();
for (int i = 0; i < compoundProps.length; i++) {
BeanProperty[] props = compoundProps[i].getScalarProperties();
ArrayList<Bindable> newList = new ArrayList<Bindable>(props.length);
add(props, newList, desc, mode, withLobs);
BindableCompound compoundBindable = new BindableCompound(compoundProps[i], newList);
list.add(compoundBindable);
}
}
private void add(BeanProperty[] props, List<Bindable> list, BeanDescriptor<?> desc, DmlMode mode, boolean withLobs) {
for (int i = 0; i < props.length; i++) {
Bindable item = factoryProperty.create(props[i], mode, withLobs);
if (item != null) {
list.add(item);
} else {
// null where readOnly (Secondary tables) or Lob exclusion
}
}
}
}
@@ -0,0 +1,65 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.util.ArrayList;
import java.util.List;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne;
import com.avaje.ebeaninternal.server.persist.dml.DmlMode;
/**
* A factory that builds Bindable for embedded bean properties.
*/
public class FactoryEmbedded {
private final FactoryProperty factoryProperty;
public FactoryEmbedded(boolean bindEncryptDataFirst) {
factoryProperty = new FactoryProperty(bindEncryptDataFirst);
}
/**
* Add bindable for the embedded properties to the list.
*/
public void create(List<Bindable> list, BeanDescriptor<?> desc, DmlMode mode, boolean withLobs) {
BeanPropertyAssocOne<?>[] embedded = desc.propertiesEmbedded();
for (int j = 0; j < embedded.length; j++) {
List<Bindable> bindList = new ArrayList<Bindable>();
BeanProperty[] props = embedded[j].getProperties();
for (int i = 0; i < props.length; i++) {
Bindable item = factoryProperty.create(props[i], mode, withLobs);
if (item != null){
bindList.add(item);
}
}
list.add(new BindableEmbedded(embedded[j], bindList));
}
}
}
@@ -0,0 +1,55 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dmlbind;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne;
/**
* Create a Bindable for the ids of a bean type.
*/
public class FactoryId {
public FactoryId() {
}
/**
* Add uniqueId properties.
*/
public BindableId createId(BeanDescriptor<?> desc) {
BeanProperty[] uids = desc.propertiesId();
if (uids.length == 0) {
return new BindableIdEmpty();
} else if (uids.length == 1) {
if (!uids[0].isEmbedded()) {
return new BindableIdScalar(uids[0]);
} else {
BeanPropertyAssocOne<?> embId = (BeanPropertyAssocOne<?>) uids[0];
return new BindableIdEmbedded(embId, desc);
}
} else {
return new BindableIdMap(uids, desc);
}
}
}
@@ -0,0 +1,84 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dmlbind;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.deploy.generatedproperty.GeneratedProperty;
import com.avaje.ebeaninternal.server.persist.dml.DmlMode;
/**
* Creates the appropriate Bindable for a BeanProperty.
* <p>
* Lob properties can be excluded and it creates BindablePropertyInsertGenerated
* and BindablePropertyUpdateGenerated as required.
* </p>
*/
public class FactoryProperty {
private final boolean bindEncryptDataFirst;
public FactoryProperty(boolean bindEncryptDataFirst) {
this.bindEncryptDataFirst = bindEncryptDataFirst;
}
/**
* Create a Bindable for the property given the mode and withLobs flag.
*/
public Bindable create(BeanProperty prop, DmlMode mode, boolean withLobs) {
if (DmlMode.INSERT.equals(mode) && !prop.isDbInsertable()){
return null;
}
if (DmlMode.UPDATE.equals(mode) && !prop.isDbUpdatable()){
return null;
}
if (prop.isLob()) {
if (DmlMode.WHERE.equals(mode) || !withLobs) {
// Lob exclusion
return null;
} else {
return prop.isDbEncrypted() ? new BindableEncryptedProperty(prop, bindEncryptDataFirst) : new BindableProperty(prop);
}
}
GeneratedProperty gen = prop.getGeneratedProperty();
if (gen != null) {
if (DmlMode.INSERT.equals(mode)) {
if (gen.includeInInsert()) {
return new BindablePropertyInsertGenerated(prop, gen);
} else {
return null;
}
}
if (DmlMode.UPDATE.equals(mode)) {
if (gen.includeInUpdate()) {
return new BindablePropertyUpdateGenerated(prop, gen);
} else {
// An 'Insert Timestamp' is never updated
return null;
}
}
}
return prop.isDbEncrypted() ? new BindableEncryptedProperty(prop, bindEncryptDataFirst) : new BindableProperty(prop);
}
}
@@ -0,0 +1,79 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dmlbind;
import java.util.ArrayList;
import java.util.List;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne;
/**
* Creates a Bindable to support version concurrency where clauses.
*/
public class FactoryVersion {
public FactoryVersion() {
}
/**
* Create a Bindable for the version property(s) for a bean type.
*/
public Bindable create(BeanDescriptor<?> desc) {
List<Bindable> verList = new ArrayList<Bindable>();
BeanProperty[] vers = desc.propertiesVersion();
for (int i = 0; i < vers.length; i++) {
verList.add(new BindableProperty(vers[i]));
}
// version columns on embedded beans?
BeanPropertyAssocOne<?>[] embedded = desc.propertiesEmbedded();
for (int j = 0; j < embedded.length; j++) {
if (embedded[j].isEmbeddedVersion()) {
List<Bindable> bindList = new ArrayList<Bindable>();
BeanProperty[] embProps = embedded[j].getProperties();
for (int i = 0; i < embProps.length; i++) {
if (embProps[i].isVersion()){
bindList.add(new BindableProperty(embProps[i]));
}
}
verList.add(new BindableEmbedded(embedded[j], bindList));
}
}
if (verList.size() == 0){
return null;
}
if (verList.size() == 1){
return verList.get(0);
}
return new BindableList(verList);
}
}
@@ -0,0 +1,104 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.persist.dmlbind;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne;
/**
* Matches local embedded id properties to 'matching' properties from a
* ManyToOne associated bean that is a 'imported primary key'.
* <p>
* This object is designed to help BindableIdEmbedded and BindableIdMap to
* create a concatenated id from the id values from ManyToOne associated beans.
* This can be done when those ManyToOne associated beans make up the primary
* key. This typically means the BindableIdEmbedded is for a intersection table
* of a Many to Many relationship.
* </p>
*/
class MatchedImportedProperty {
private final BeanPropertyAssocOne<?> assocOne;
private final BeanProperty foreignProp;
private final BeanProperty localProp;
protected MatchedImportedProperty(BeanPropertyAssocOne<?> assocOne, BeanProperty foreignProp,
BeanProperty localProp) {
this.assocOne = assocOne;
this.foreignProp = foreignProp;
this.localProp = localProp;
}
protected void populate(Object sourceBean, Object destBean) {
Object assocBean = assocOne.getValue(sourceBean);
if (assocBean == null) {
String msg = "The assoc bean for " + assocOne + " is null?";
throw new NullPointerException(msg);
}
Object value = foreignProp.getValue(assocBean);
localProp.setValue(destBean, value);
}
/**
* Create the array of matchedImportedProperty based on the properties and descriptor.
*/
protected static MatchedImportedProperty[] build(BeanProperty[] props, BeanDescriptor<?> desc) {
MatchedImportedProperty[] matches = new MatchedImportedProperty[props.length];
for (int i = 0; i < props.length; i++) {
// find matching assoc one property for dbColumn
matches[i] = MatchedImportedProperty.findMatch(props[i], desc);
if (matches[i] == null) {
// ok, the assoc ones are not on the bean?
return null;
}
}
return matches;
}
private static MatchedImportedProperty findMatch(BeanProperty prop, BeanDescriptor<?> desc) {
// find matching against the local database column
String dbColumn = prop.getDbColumn();
BeanPropertyAssocOne<?>[] assocOnes = desc.propertiesOne();
for (int i = 0; i < assocOnes.length; i++) {
if (assocOnes[i].isImportedPrimaryKey()) {
// search using the ImportedId from the assoc one
BeanProperty foreignMatch = assocOnes[i].getImportedId().findMatchImport(dbColumn);
if (foreignMatch != null) {
return new MatchedImportedProperty(assocOnes[i], foreignMatch, prop);
}
}
}
// there was no matching assoc one property.
// example UserRole bean missing assoc one to User?
return null;
}
}
@@ -0,0 +1,10 @@
<HTML>
<HEAD>
<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=iso-8859-1">
<TITLE>Bindable objects to support Bean DML</TITLE>
</HEAD>
<Body BGCOLOR="#ffffff">
Bindable objects to support Bean DML
</Body>
</HTML>
@@ -0,0 +1,15 @@
<HTML>
<HEAD>
<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=iso-8859-1">
<TITLE>Persistence Implementation</TITLE>
</HEAD>
<Body BGCOLOR="#ffffff">
Persistence Implementation
<p>
Implementing the saving and deleting of beans, and the executing of
updateable and callable sql.
</p>
</Body>
</HTML>