use of org.datanucleus.store.rdbms.exceptions.MappedDatastoreException in project datanucleus-rdbms by datanucleus.
the class AbstractListStore method internalRemoveAt.
/**
* Internal method to remove an object at a location in the List.
* @param op ObjectProvider
* @param index The location
* @param stmt The statement to remove the element from the List
* @param size Current list size (if known). -1 if not known
*/
protected void internalRemoveAt(ObjectProvider op, int index, String stmt, int size) {
int currentListSize = 0;
if (size < 0) {
// Get the current size from the datastore
currentListSize = size(op);
} else {
currentListSize = size;
}
ExecutionContext ec = op.getExecutionContext();
try {
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try {
PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, stmt, false);
try {
int jdbcPosition = 1;
jdbcPosition = BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
jdbcPosition = BackingStoreHelper.populateOrderInStatement(ec, ps, index, jdbcPosition, orderMapping);
if (relationDiscriminatorMapping != null) {
jdbcPosition = BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, this);
}
int[] rowsDeleted = sqlControl.executeStatementUpdate(ec, mconn, stmt, ps, true);
if (rowsDeleted[0] == 0) {
// ?? throw exception??
}
} finally {
sqlControl.closeStatement(mconn, ps);
}
// shift down
if (index != currentListSize - 1) {
for (int i = index + 1; i < currentListSize; i++) {
// Shift this index down 1
internalShift(op, mconn, false, i, -1, true);
}
}
} finally {
mconn.release();
}
} catch (SQLException e) {
throw new NucleusDataStoreException(Localiser.msg("056012", stmt), e);
} catch (MappedDatastoreException e) {
throw new NucleusDataStoreException(Localiser.msg("056012", stmt), e);
}
}
use of org.datanucleus.store.rdbms.exceptions.MappedDatastoreException in project datanucleus-rdbms by datanucleus.
the class AbstractSetStore method removeAll.
/**
* Remove all elements from a collection from the association owner vs elements.
* This implementation iterates around the remove() method doing each element 1 at a time.
* Please refer to the JoinSetStore and FKSetStore for the variations used there.
* This is used for Map key and value stores.
* @param op ObjectProvider for the container
* @param elements Collection of elements to remove
* @return Whether the database was updated
*/
public boolean removeAll(ObjectProvider op, Collection elements, int size) {
if (elements == null || elements.size() == 0) {
return false;
}
boolean modified = false;
List exceptions = new ArrayList();
boolean batched = (elements.size() > 1);
// Validate all elements exist
Iterator iter = elements.iterator();
while (iter.hasNext()) {
Object element = iter.next();
if (!validateElementForReading(op, element)) {
NucleusLogger.DATASTORE.debug("AbstractSetStore::removeAll element=" + element + " doesn't exist in this Set.");
return false;
}
}
try {
ExecutionContext ec = op.getExecutionContext();
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
try {
// Process all waiting batched statements before we start our work
SQLController sqlControl = storeMgr.getSQLController();
try {
sqlControl.processStatementsForConnection(mconn);
} catch (SQLException e) {
throw new MappedDatastoreException("SQLException", e);
}
iter = elements.iterator();
while (iter.hasNext()) {
Object element = iter.next();
try {
// Process the remove
int[] rc = null;
String removeStmt = getRemoveStmt(element);
try {
PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, removeStmt, batched);
try {
int jdbcPosition = 1;
jdbcPosition = BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
jdbcPosition = BackingStoreHelper.populateElementForWhereClauseInStatement(ec, ps, element, jdbcPosition, elementMapping);
if (relationDiscriminatorMapping != null) {
jdbcPosition = BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, this);
}
// Execute the statement
rc = sqlControl.executeStatementUpdate(ec, mconn, removeStmt, ps, !batched || (batched && !iter.hasNext()));
if (rc != null) {
for (int i = 0; i < rc.length; i++) {
if (rc[i] > 0) {
// At least one record was inserted
modified = true;
}
}
}
} finally {
sqlControl.closeStatement(mconn, ps);
}
} catch (SQLException e) {
throw new MappedDatastoreException("SQLException", e);
}
} catch (MappedDatastoreException mde) {
exceptions.add(mde);
NucleusLogger.DATASTORE.error("Exception in remove", mde);
}
}
} finally {
mconn.release();
}
} catch (MappedDatastoreException e) {
exceptions.add(e);
NucleusLogger.DATASTORE.error("Exception performing removeAll on set backing store", e);
}
if (!exceptions.isEmpty()) {
// Throw all exceptions received as the cause of a NucleusDataStoreException so the user can see which record(s) didn't remove
String msg = Localiser.msg("056012", ((Exception) exceptions.get(0)).getMessage());
NucleusLogger.DATASTORE.error(msg);
throw new NucleusDataStoreException(msg, (Throwable[]) exceptions.toArray(new Throwable[exceptions.size()]), op.getObject());
}
return modified;
}
Aggregations