use of org.datanucleus.state.DNStateManager in project datanucleus-rdbms by datanucleus.
the class ElementContainerStore method validateElementForWriting.
/**
* Method to check if an element is already persistent, or is managed by a different ExecutionContext. If not persistent, this will persist it.
* @param ec execution context
* @param element The element
* @param fieldValues any initial field values to use if persisting the element
* @return Whether the element was persisted during this call
*/
protected boolean validateElementForWriting(ExecutionContext ec, Object element, FieldValues fieldValues) {
// Check the element type for this collection
if (!elementIsPersistentInterface && !validateElementType(ec.getClassLoaderResolver(), element)) {
throw new ClassCastException(Localiser.msg("056033", element.getClass().getName(), ownerMemberMetaData.getFullFieldName(), elementType));
}
boolean persisted = false;
if (elementsAreEmbedded || elementsAreSerialised) {
// Element is embedded/serialised so has no id
} else {
DNStateManager elementSM = ec.findStateManager(element);
if (elementSM != null && elementSM.isEmbedded()) {
// Element is already with StateManager and is embedded in another field!
throw new NucleusUserException(Localiser.msg("056028", ownerMemberMetaData.getFullFieldName(), element));
}
persisted = SCOUtils.validateObjectForWriting(ec, element, fieldValues);
}
return persisted;
}
use of org.datanucleus.state.DNStateManager in project datanucleus-rdbms by datanucleus.
the class FKArrayStore method iterator.
@Override
public Iterator<E> iterator(DNStateManager ownerSM) {
ExecutionContext ec = ownerSM.getExecutionContext();
if (elementInfo == null || elementInfo.length == 0) {
return null;
}
// Generate the statement, and statement mapping/parameter information
ElementIteratorStatement iterStmt = getIteratorStatement(ownerSM.getExecutionContext(), ownerSM.getExecutionContext().getFetchPlan(), true);
SelectStatement sqlStmt = iterStmt.getSelectStatement();
StatementClassMapping iteratorMappingDef = iterStmt.getElementClassMapping();
// Input parameter(s) - the owner
int inputParamNum = 1;
StatementMappingIndex ownerIdx = new StatementMappingIndex(ownerMapping);
if (sqlStmt.getNumberOfUnions() > 0) {
// Add parameter occurrence for each union of statement
for (int j = 0; j < sqlStmt.getNumberOfUnions() + 1; j++) {
int[] paramPositions = new int[ownerMapping.getNumberOfColumnMappings()];
for (int k = 0; k < ownerMapping.getNumberOfColumnMappings(); k++) {
paramPositions[k] = inputParamNum++;
}
ownerIdx.addParameterOccurrence(paramPositions);
}
} else {
int[] paramPositions = new int[ownerMapping.getNumberOfColumnMappings()];
for (int k = 0; k < ownerMapping.getNumberOfColumnMappings(); k++) {
paramPositions[k] = inputParamNum++;
}
ownerIdx.addParameterOccurrence(paramPositions);
}
StatementParameterMapping iteratorMappingParams = new StatementParameterMapping();
iteratorMappingParams.addMappingForParameter("owner", ownerIdx);
if (ec.getTransaction().getSerializeRead() != null && ec.getTransaction().getSerializeRead()) {
sqlStmt.addExtension(SQLStatement.EXTENSION_LOCK_FOR_UPDATE, true);
}
String stmt = sqlStmt.getSQLText().toSQL();
try {
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try {
// Create the statement
PreparedStatement ps = sqlControl.getStatementForQuery(mconn, stmt);
// Set the owner
DNStateManager stmtOwnerSM = BackingStoreHelper.getOwnerStateManagerForBackingStore(ownerSM);
int numParams = ownerIdx.getNumberOfParameterOccurrences();
for (int paramInstance = 0; paramInstance < numParams; paramInstance++) {
ownerIdx.getMapping().setObject(ec, ps, ownerIdx.getParameterPositionsForOccurrence(paramInstance), stmtOwnerSM.getObject());
}
try {
ResultSet rs = sqlControl.executeStatementQuery(ec, mconn, stmt, ps);
try {
ResultObjectFactory rof = null;
if (elementsAreEmbedded || elementsAreSerialised) {
throw new NucleusException("Cannot have FK array with non-persistent objects");
}
rof = new PersistentClassROF(ec, rs, false, ec.getFetchPlan(), iteratorMappingDef, elementCmd, clr.classForName(elementType));
return new ArrayStoreIterator(ownerSM, rs, rof, this);
} finally {
rs.close();
}
} finally {
sqlControl.closeStatement(mconn, ps);
}
} finally {
mconn.release();
}
} catch (SQLException e) {
throw new NucleusDataStoreException(Localiser.msg("056006", stmt), e);
}
}
use of org.datanucleus.state.DNStateManager in project datanucleus-rdbms by datanucleus.
the class FKMapStore method remove.
@Override
public void remove(DNStateManager sm, Object key, Object oldValue) {
ExecutionContext ec = sm.getExecutionContext();
if (keyFieldNumber >= 0) {
// Key stored in value
if (oldValue != null) {
boolean deletingValue = false;
DNStateManager valueSM = ec.findStateManager(oldValue);
if (ownerMemberMetaData.getMap().isDependentValue()) {
// Delete the value if it is dependent
deletingValue = true;
ec.deleteObjectInternal(oldValue);
valueSM.flush();
} else if (ownerMapping.isNullable()) {
// Null the owner FK
if (ownerFieldNumber >= 0) {
// Update the field in the value
Object oldOwner = valueSM.provideField(ownerFieldNumber);
valueSM.replaceFieldMakeDirty(ownerFieldNumber, null);
valueSM.flush();
if (ec.getManageRelations()) {
ec.getRelationshipManager(valueSM).relationChange(ownerFieldNumber, oldOwner, null);
}
} else {
// Update the external FK in the value in the datastore
updateValueFkInternal(sm, oldValue, null);
}
} else {
// Not nullable, so must delete since no other way of removing from map
deletingValue = true;
ec.deleteObjectInternal(oldValue);
valueSM.flush();
}
if (ownerMemberMetaData.getMap().isDependentKey()) {
// Delete the key since it is dependent
if (!deletingValue) {
// Null FK in value to key
if (keyMapping.isNullable()) {
valueSM.replaceFieldMakeDirty(keyFieldNumber, null);
valueSM.flush();
if (ec.getManageRelations()) {
ec.getRelationshipManager(valueSM).relationChange(keyFieldNumber, key, null);
}
}
}
ec.deleteObjectInternal(key);
DNStateManager keySM = ec.findStateManager(key);
keySM.flush();
}
}
} else {
// Value stored in key
if (key != null) {
boolean deletingKey = false;
DNStateManager keySM = ec.findStateManager(key);
if (ownerMemberMetaData.getMap().isDependentKey()) {
// Delete the key if it is dependent
deletingKey = true;
ec.deleteObjectInternal(key);
keySM.flush();
} else if (ownerMapping.isNullable()) {
// Null the owner FK
if (ownerFieldNumber >= 0) {
// Update the field in the key
Object oldOwner = keySM.provideField(ownerFieldNumber);
keySM.replaceFieldMakeDirty(ownerFieldNumber, null);
keySM.flush();
if (ec.getManageRelations()) {
ec.getRelationshipManager(keySM).relationChange(ownerFieldNumber, oldOwner, null);
}
} else {
// Update the external FK in the key in the datastore
updateKeyFkInternal(sm, key, null);
}
} else {
// Not nullable, so must delete since no other way of removing from map
deletingKey = true;
ec.deleteObjectInternal(key);
keySM.flush();
}
if (ownerMemberMetaData.getMap().isDependentValue()) {
// Delete the value since it is dependent
if (!deletingKey) {
// Null FK in key to value
if (valueMapping.isNullable()) {
keySM.replaceFieldMakeDirty(valueFieldNumber, null);
keySM.flush();
if (ec.getManageRelations()) {
ec.getRelationshipManager(keySM).relationChange(valueFieldNumber, oldValue, null);
}
}
}
ec.deleteObjectInternal(oldValue);
DNStateManager valSM = ec.findStateManager(oldValue);
valSM.flush();
}
}
}
}
use of org.datanucleus.state.DNStateManager in project datanucleus-rdbms by datanucleus.
the class JoinArrayStore method iterator.
@Override
public Iterator<E> iterator(DNStateManager ownerSM) {
ExecutionContext ec = ownerSM.getExecutionContext();
// Generate the statement, and statement mapping/parameter information
ElementIteratorStatement iterStmt = getIteratorStatement(ec, ec.getFetchPlan(), true);
SelectStatement sqlStmt = iterStmt.sqlStmt;
StatementClassMapping iteratorMappingClass = iterStmt.elementClassMapping;
// Input parameter(s) - the owner
int inputParamNum = 1;
StatementMappingIndex ownerIdx = new StatementMappingIndex(ownerMapping);
if (sqlStmt.getNumberOfUnions() > 0) {
// Add parameter occurrence for each union of statement
for (int j = 0; j < sqlStmt.getNumberOfUnions() + 1; j++) {
int[] paramPositions = new int[ownerMapping.getNumberOfColumnMappings()];
for (int k = 0; k < paramPositions.length; k++) {
paramPositions[k] = inputParamNum++;
}
ownerIdx.addParameterOccurrence(paramPositions);
}
} else {
int[] paramPositions = new int[ownerMapping.getNumberOfColumnMappings()];
for (int k = 0; k < paramPositions.length; k++) {
paramPositions[k] = inputParamNum++;
}
ownerIdx.addParameterOccurrence(paramPositions);
}
StatementParameterMapping iteratorMappingParams = new StatementParameterMapping();
iteratorMappingParams.addMappingForParameter("owner", ownerIdx);
if (ec.getTransaction().getSerializeRead() != null && ec.getTransaction().getSerializeRead()) {
sqlStmt.addExtension(SQLStatement.EXTENSION_LOCK_FOR_UPDATE, true);
}
String stmt = sqlStmt.getSQLText().toSQL();
try {
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try {
// Create the statement
PreparedStatement ps = sqlControl.getStatementForQuery(mconn, stmt);
// Set the owner
DNStateManager stmtOwnerSM = BackingStoreHelper.getOwnerStateManagerForBackingStore(ownerSM);
int numParams = ownerIdx.getNumberOfParameterOccurrences();
for (int paramInstance = 0; paramInstance < numParams; paramInstance++) {
ownerIdx.getMapping().setObject(ec, ps, ownerIdx.getParameterPositionsForOccurrence(paramInstance), stmtOwnerSM.getObject());
}
try {
ResultSet rs = sqlControl.executeStatementQuery(ec, mconn, stmt, ps);
try {
if (elementsAreEmbedded || elementsAreSerialised) {
// No ResultObjectFactory needed - handled by SetStoreIterator
return new ArrayStoreIterator(ownerSM, rs, null, this);
} else if (elementMapping instanceof ReferenceMapping) {
// No ResultObjectFactory needed - handled by SetStoreIterator
return new ArrayStoreIterator(ownerSM, rs, null, this);
} else {
ResultObjectFactory rof = new PersistentClassROF(ec, rs, false, ec.getFetchPlan(), iteratorMappingClass, elementCmd, clr.classForName(elementType));
return new ArrayStoreIterator(ownerSM, rs, rof, this);
}
} finally {
rs.close();
}
} finally {
sqlControl.closeStatement(mconn, ps);
}
} finally {
mconn.release();
}
} catch (SQLException e) {
throw new NucleusDataStoreException(Localiser.msg("056006", stmt), e);
}
}
use of org.datanucleus.state.DNStateManager in project datanucleus-rdbms by datanucleus.
the class PersistableMapping method getValueForColumnMapping.
/**
* Method to return the value to be stored in the specified datastore index given the overall value for this java type.
* @param nucleusCtx Context
* @param index The datastore index
* @param value The overall value for this java type
* @return The value for this datastore index
*/
public Object getValueForColumnMapping(NucleusContext nucleusCtx, int index, Object value) {
ExecutionContext ec = nucleusCtx.getApiAdapter().getExecutionContext(value);
if (cmd.getIdentityType() == IdentityType.APPLICATION) {
AbstractMemberMetaData mmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(cmd.getPKMemberPositions()[index]);
DNStateManager sm = null;
if (ec != null) {
sm = ec.findStateManager(value);
}
if (sm == null) {
// Transient or detached maybe, so use reflection to get PK field values
return (mmd instanceof FieldMetaData) ? ClassUtils.getValueOfFieldByReflection(value, mmd.getName()) : ClassUtils.getValueOfMethodByReflection(value, ClassUtils.getJavaBeanGetterName(mmd.getName(), false));
}
if (!mmd.isPrimaryKey()) {
// Make sure the field is loaded
sm.isLoaded(mmd.getAbsoluteFieldNumber());
}
FieldManager fm = new SingleValueFieldManager();
sm.provideFields(new int[] { mmd.getAbsoluteFieldNumber() }, fm);
return fm.fetchObjectField(mmd.getAbsoluteFieldNumber());
} else if (cmd.getIdentityType() == IdentityType.DATASTORE) {
Object id = nucleusCtx.getApiAdapter().getIdForObject(value);
return id != null ? IdentityUtils.getTargetKeyForDatastoreIdentity(id) : null;
}
return null;
}
Aggregations