use of org.datanucleus.ExecutionContext in project datanucleus-rdbms by datanucleus.
the class AbstractMapStore method validateValueForWriting.
/**
* Utility to validate a value is ok for writing (present in the datastore).
* @param ownerOP ObjectProvider for the owner of the map
* @param value The value to check.
*/
protected void validateValueForWriting(ObjectProvider ownerOP, Object value) {
// TODO Pass in cascade flag and if value not present then throw exception
ExecutionContext ec = ownerOP.getExecutionContext();
validateValueType(ec.getClassLoaderResolver(), value);
if (!valuesAreEmbedded && !valuesAreSerialised) {
SCOUtils.validateObjectForWriting(ec, value, null);
}
}
use of org.datanucleus.ExecutionContext in project datanucleus-rdbms by datanucleus.
the class AbstractMapStore method validateKeyForWriting.
/**
* Utility to validate a key is ok for writing (present in the datastore).
* @param ownerOP ObjectProvider for the owner of the map
* @param key The key to check.
*/
protected void validateKeyForWriting(ObjectProvider ownerOP, Object key) {
// TODO Pass in cascade flag and if key not present then throw exception
ExecutionContext ec = ownerOP.getExecutionContext();
validateKeyType(ec.getClassLoaderResolver(), key);
if (!keysAreEmbedded && !keysAreSerialised) {
SCOUtils.validateObjectForWriting(ec, key, null);
}
}
use of org.datanucleus.ExecutionContext 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.ExecutionContext in project datanucleus-rdbms by datanucleus.
the class BaseContainerStore method getObjectProviderForEmbeddedPCObject.
/**
* Method to return the ObjectProvider for an embedded PC object (element, key, value).
* It creates one if the element is not currently managed.
* @param op ObjectProvider of the owner
* @param obj The embedded PC object
* @param ownerMmd The meta data for the owner field
* @param pcType Object type for the embedded object (see ObjectProvider EMBEDDED_PC etc)
* @return The ObjectProvider
*/
public ObjectProvider getObjectProviderForEmbeddedPCObject(ObjectProvider op, Object obj, AbstractMemberMetaData ownerMmd, short pcType) {
ExecutionContext ec = op.getExecutionContext();
ObjectProvider objOP = ec.findObjectProvider(obj);
if (objOP == null) {
objOP = ec.getNucleusContext().getObjectProviderFactory().newForEmbedded(ec, obj, false, op, ownerMmd.getAbsoluteFieldNumber());
}
objOP.setPcObjectType(pcType);
return objOP;
}
use of org.datanucleus.ExecutionContext 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