use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.
the class AbstractListStore method getIndicesOf.
/**
* Utility to find the indices of a collection of elements.
* The returned list are in reverse order (highest index first).
* @param op ObjectProvider
* @param elements The elements
* @return The indices of the elements in the List.
*/
protected int[] getIndicesOf(ObjectProvider op, Collection elements) {
if (elements == null || elements.isEmpty()) {
return null;
}
Iterator iter = elements.iterator();
while (iter.hasNext()) {
validateElementForReading(op, iter.next());
}
String stmt = getIndicesOfStmt(elements);
try {
ExecutionContext ec = op.getExecutionContext();
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try {
PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, stmt, false);
try {
Iterator elemIter = elements.iterator();
int jdbcPosition = 1;
while (elemIter.hasNext()) {
Object element = elemIter.next();
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);
}
}
List<Integer> indexes = new ArrayList();
ResultSet rs = sqlControl.executeStatementQuery(ec, mconn, stmt, ps);
try {
while (rs.next()) {
indexes.add(rs.getInt(1));
}
JDBCUtils.logWarnings(rs);
} finally {
rs.close();
}
if (indexes.isEmpty()) {
return null;
}
int i = 0;
int[] indicesReturn = new int[indexes.size()];
for (Integer idx : indexes) {
indicesReturn[i++] = idx;
}
return indicesReturn;
} finally {
sqlControl.closeStatement(mconn, ps);
}
} finally {
mconn.release();
}
} catch (SQLException e) {
throw new NucleusDataStoreException(Localiser.msg("056017", stmt), e);
}
}
use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.
the class AbstractMapStore method containsValue.
/**
* Method to check if a value exists in the Map.
* @param op ObjectProvider for the map
* @param value The value to check for.
* @return Whether the value exists in the Map.
*/
public boolean containsValue(ObjectProvider op, Object value) {
if (value == null) {
// nulls not allowed
return false;
}
if (!validateValueForReading(op, value)) {
return false;
}
boolean exists = false;
try {
ExecutionContext ec = op.getExecutionContext();
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try {
PreparedStatement ps = sqlControl.getStatementForQuery(mconn, containsValueStmt);
try {
int jdbcPosition = 1;
jdbcPosition = BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
BackingStoreHelper.populateValueInStatement(ec, ps, value, jdbcPosition, getValueMapping());
ResultSet rs = sqlControl.executeStatementQuery(ec, mconn, containsValueStmt, ps);
try {
if (rs.next()) {
exists = true;
}
JDBCUtils.logWarnings(rs);
} finally {
rs.close();
}
} finally {
sqlControl.closeStatement(mconn, ps);
}
} finally {
mconn.release();
}
} catch (SQLException e) {
NucleusLogger.DATASTORE_RETRIEVE.warn("Exception during backing store select", e);
throw new NucleusDataStoreException(Localiser.msg("056019", containsValueStmt), e);
}
return exists;
}
use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.
the class AbstractSetStore method remove.
/**
* Removes the association to one element
* @param op ObjectProvider for the container
* @param element Element to remove
* @param size Current size
* @param allowDependentField Whether to allow any cascade deletes caused by this removal
* @return Whether it was successful
*/
public boolean remove(ObjectProvider op, Object element, int size, boolean allowDependentField) {
if (!validateElementForReading(op, element)) {
NucleusLogger.DATASTORE.debug("Attempt to remove element=" + StringUtils.toJVMIDString(element) + " but doesn't exist in this Set.");
return false;
}
Object elementToRemove = element;
ExecutionContext ec = op.getExecutionContext();
if (ec.getApiAdapter().isDetached(element)) {
// Element passed in is detached so find attached version (DON'T attach this object)
elementToRemove = ec.findObject(ec.getApiAdapter().getIdForObject(element), true, false, element.getClass().getName());
}
// Remove the element
boolean modified = false;
String removeStmt = getRemoveStmt(elementToRemove);
try {
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try {
PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, removeStmt, false);
try {
int jdbcPosition = 1;
jdbcPosition = BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
jdbcPosition = BackingStoreHelper.populateElementForWhereClauseInStatement(ec, ps, elementToRemove, jdbcPosition, elementMapping);
if (relationDiscriminatorMapping != null) {
jdbcPosition = BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, this);
}
int[] rowsDeleted = sqlControl.executeStatementUpdate(ec, mconn, removeStmt, ps, true);
modified = (rowsDeleted[0] == 1);
} finally {
sqlControl.closeStatement(mconn, ps);
}
} finally {
mconn.release();
}
} catch (SQLException e) {
String msg = Localiser.msg("056012", removeStmt);
NucleusLogger.DATASTORE.error(msg, e);
throw new NucleusDataStoreException(msg, e);
}
CollectionMetaData collmd = ownerMemberMetaData.getCollection();
boolean dependent = collmd.isDependentElement();
if (ownerMemberMetaData.isCascadeRemoveOrphans()) {
dependent = true;
}
if (allowDependentField && dependent && !collmd.isEmbeddedElement()) {
// Delete the element if it is dependent
op.getExecutionContext().deleteObjectInternal(elementToRemove);
}
return modified;
}
use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.
the class ElementContainerStore method getSize.
public int getSize(ObjectProvider ownerOP) {
int numRows;
String sizeStmt = getSizeStmt();
try {
ExecutionContext ec = ownerOP.getExecutionContext();
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try {
PreparedStatement ps = sqlControl.getStatementForQuery(mconn, sizeStmt);
try {
int jdbcPosition = 1;
if (elementInfo == null) {
jdbcPosition = BackingStoreHelper.populateOwnerInStatement(ownerOP, ec, ps, jdbcPosition, this);
} else {
if (usingJoinTable()) {
jdbcPosition = BackingStoreHelper.populateOwnerInStatement(ownerOP, ec, ps, jdbcPosition, this);
if (elementInfo[0].getDiscriminatorMapping() != null) {
jdbcPosition = BackingStoreHelper.populateElementDiscriminatorInStatement(ec, ps, jdbcPosition, true, elementInfo[0], clr);
}
if (relationDiscriminatorMapping != null) {
jdbcPosition = BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, this);
}
} else {
for (int i = 0; i < elementInfo.length; i++) {
jdbcPosition = BackingStoreHelper.populateOwnerInStatement(ownerOP, ec, ps, jdbcPosition, this);
if (elementInfo[i].getDiscriminatorMapping() != null) {
jdbcPosition = BackingStoreHelper.populateElementDiscriminatorInStatement(ec, ps, jdbcPosition, true, elementInfo[i], clr);
}
if (relationDiscriminatorMapping != null) {
jdbcPosition = BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, this);
}
}
}
}
ResultSet rs = sqlControl.executeStatementQuery(ec, mconn, sizeStmt, ps);
try {
if (!rs.next()) {
throw new NucleusDataStoreException(Localiser.msg("056007", sizeStmt));
}
numRows = rs.getInt(1);
if (elementInfo != null && elementInfo.length > 1) {
while (rs.next()) {
numRows = numRows + rs.getInt(1);
}
}
JDBCUtils.logWarnings(rs);
} finally {
rs.close();
}
} catch (SQLException sqle) {
NucleusLogger.GENERAL.error("Exception in size", sqle);
throw sqle;
} finally {
sqlControl.closeStatement(mconn, ps);
}
} finally {
mconn.release();
}
} catch (SQLException e) {
throw new NucleusDataStoreException(Localiser.msg("056007", sizeStmt), e);
}
return numRows;
}
use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.
the class AbstractArrayStore method set.
/**
* Method to set the array for the specified owner to the passed value.
* @param op ObjectProvider for the owner
* @param array the array
* @return Whether the array was updated successfully
*/
public boolean set(ObjectProvider op, Object array) {
if (array == null || Array.getLength(array) == 0) {
return true;
}
// Validate all elements for writing
ExecutionContext ec = op.getExecutionContext();
int length = Array.getLength(array);
for (int i = 0; i < length; i++) {
Object obj = Array.get(array, i);
validateElementForWriting(ec, obj, null);
}
boolean modified = false;
List exceptions = new ArrayList();
boolean batched = allowsBatching() && length > 1;
try {
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
try {
processBatchedWrites(mconn);
// Loop through all elements to be added
E element = null;
for (int i = 0; i < length; i++) {
element = (E) Array.get(array, i);
try {
// Add the row to the join table
int[] rc = internalAdd(op, element, mconn, batched, i, (i == length - 1));
if (rc != null) {
for (int j = 0; j < rc.length; j++) {
if (rc[j] > 0) {
// At least one record was inserted
modified = true;
}
}
}
} catch (MappedDatastoreException mde) {
exceptions.add(mde);
NucleusLogger.DATASTORE.error("Exception thrown in set of element", mde);
}
}
} finally {
mconn.release();
}
} catch (MappedDatastoreException e) {
exceptions.add(e);
NucleusLogger.DATASTORE.error("Exception thrown in set of element", e);
}
if (!exceptions.isEmpty()) {
// Throw all exceptions received as the cause of a NucleusDataStoreException so the user can see which
// record(s) didn't persist
String msg = Localiser.msg("056009", ((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