use of org.datanucleus.store.connection.ManagedConnection in project datanucleus-rdbms by datanucleus.
the class AbstractCollectionStore method updateEmbeddedElement.
public boolean updateEmbeddedElement(ObjectProvider op, E element, int fieldNumber, Object value, JavaTypeMapping fieldMapping) {
boolean modified = false;
String stmt = getUpdateEmbeddedElementStmt(fieldMapping);
try {
ExecutionContext ec = op.getExecutionContext();
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try {
PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, stmt, false);
try {
int jdbcPosition = 1;
fieldMapping.setObject(ec, ps, MappingHelper.getMappingIndices(jdbcPosition, fieldMapping), value);
jdbcPosition += fieldMapping.getNumberOfDatastoreMappings();
jdbcPosition = BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
jdbcPosition = BackingStoreHelper.populateEmbeddedElementFieldsInStatement(op, element, ps, jdbcPosition, ((JoinTable) containerTable).getOwnerMemberMetaData(), elementMapping, elementCmd, this);
sqlControl.executeStatementUpdate(ec, mconn, stmt, ps, true);
modified = true;
} finally {
sqlControl.closeStatement(mconn, ps);
}
} finally {
mconn.release();
}
} catch (SQLException e) {
NucleusLogger.DATASTORE_PERSIST.error("Exception updating embedded element in collection", e);
// TODO Update this localised message to reflect that it is the update of an embedded element
throw new NucleusDataStoreException(Localiser.msg("056009", stmt), e);
}
return modified;
}
use of org.datanucleus.store.connection.ManagedConnection 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.store.connection.ManagedConnection 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.store.connection.ManagedConnection 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.store.connection.ManagedConnection 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;
}
Aggregations