use of org.datanucleus.state.DNStateManager in project datanucleus-rdbms by datanucleus.
the class SerialisedPCMapping method setObject.
/**
* Method to populate parameter positions in a PreparedStatement with this object
* @param ec The ExecutionContext
* @param ps The Prepared Statement
* @param exprIndex The parameter positions to populate
* @param value The value of the PC to use in populating the parameter positions
* @param ownerSM StateManager for the owning object
* @param fieldNumber field number of this object in the owning object
*/
@Override
public void setObject(ExecutionContext ec, PreparedStatement ps, int[] exprIndex, Object value, DNStateManager ownerSM, int fieldNumber) {
if (value != null) {
// Assign a StateManager to the serialised object if none present
DNStateManager embSM = ec.findStateManager(value);
if (embSM == null || ec.getApiAdapter().getExecutionContext(value) == null) {
embSM = ec.getNucleusContext().getStateManagerFactory().newForEmbedded(ec, value, false, ownerSM, fieldNumber, PersistableObjectType.EMBEDDED_PC);
}
}
DNStateManager sm = null;
if (value != null) {
// Find SM for serialised object
sm = ec.findStateManager(value);
}
if (sm != null) {
sm.setStoringPC();
}
getColumnMapping(0).setObject(ps, exprIndex[0], value);
if (sm != null) {
sm.unsetStoringPC();
}
}
use of org.datanucleus.state.DNStateManager in project datanucleus-rdbms by datanucleus.
the class OracleArrayMapping method postInsert.
/**
* Method to be called after the insert of the owner class element.
* @param ownerSM StateManager of the owner
*/
public void postInsert(DNStateManager ownerSM) {
if (containerIsStoredInSingleColumn()) {
Object value = ownerSM.provideField(mmd.getAbsoluteFieldNumber());
if (value == null) {
return;
}
ExecutionContext ec = ownerSM.getExecutionContext();
if (mmd.getArray().elementIsPersistent()) {
// Make sure all persistable elements have StateManagers
Object[] arrElements = (Object[]) value;
for (Object elem : arrElements) {
if (elem != null) {
DNStateManager elemSM = ec.findStateManager(elem);
if (elemSM == null || ec.getApiAdapter().getExecutionContext(elem) == null) {
elemSM = ec.getNucleusContext().getStateManagerFactory().newForEmbedded(ec, elem, false, ownerSM, mmd.getAbsoluteFieldNumber(), PersistableObjectType.EMBEDDED_ARRAY_ELEMENT_PC);
}
}
}
}
} else {
super.postInsert(ownerSM);
}
}
use of org.datanucleus.state.DNStateManager in project datanucleus-rdbms by datanucleus.
the class EmbeddedPCMapping method getStateManagerForEmbeddedObject.
/**
* Accessor for StateManager of the embedded PC object when provided with the owner object.
* @param ownerSM StateManager of the owner
* @return StateManager of the embedded object
*/
private DNStateManager getStateManagerForEmbeddedObject(DNStateManager ownerSM) {
AbstractMemberMetaData theMmd = getRealMemberMetaData();
// Owner (non-embedded) PC
Object value = ownerSM.provideField(theMmd.getAbsoluteFieldNumber());
TypeManager typeManager = ownerSM.getExecutionContext().getTypeManager();
value = mmd.isSingleCollection() ? typeManager.getContainerAdapter(value).iterator().next() : value;
if (value == null) {
return null;
}
ExecutionContext ec = ownerSM.getExecutionContext();
DNStateManager thisSM = ec.findStateManager(value);
if (thisSM == null) {
// Assign a StateManager to manage our embedded object
thisSM = ec.getNucleusContext().getStateManagerFactory().newForEmbedded(ec, value, false, ownerSM, theMmd.getAbsoluteFieldNumber(), objectType);
}
return thisSM;
}
use of org.datanucleus.state.DNStateManager in project datanucleus-rdbms by datanucleus.
the class RDBMSPersistenceHandler method locateObjects.
// ------------------------------ Locate ----------------------------------
@Override
public void locateObjects(DNStateManager[] sms) {
if (sms == null || sms.length == 0) {
return;
}
ClassLoaderResolver clr = sms[0].getExecutionContext().getClassLoaderResolver();
Map<DatastoreClass, List<DNStateManager>> smsByTable = new HashMap<>();
for (int i = 0; i < sms.length; i++) {
AbstractClassMetaData cmd = sms[i].getClassMetaData();
DatastoreClass table = getDatastoreClass(cmd.getFullClassName(), clr);
// Use root table in hierarchy
table = table.getBaseDatastoreClass();
List<DNStateManager> smList = smsByTable.get(table);
if (smList == null) {
smList = new ArrayList<>();
}
smList.add(sms[i]);
smsByTable.put(table, smList);
}
Iterator<Map.Entry<DatastoreClass, List<DNStateManager>>> tableIter = smsByTable.entrySet().iterator();
while (tableIter.hasNext()) {
Map.Entry<DatastoreClass, List<DNStateManager>> entry = tableIter.next();
DatastoreClass table = entry.getKey();
List<DNStateManager> tableSMs = entry.getValue();
// TODO This just uses the base table. Could change to use the most-derived table
// which would permit us to join to supertables and load more fields during this process
LocateBulkRequest req = new LocateBulkRequest(table);
req.execute(tableSMs.toArray(new DNStateManager[tableSMs.size()]));
}
}
use of org.datanucleus.state.DNStateManager in project datanucleus-rdbms by datanucleus.
the class FKListStore method internalRemove.
/**
* Convenience method to remove the specified element from the List.
* @param ownerSM StateManager for the owner
* @param element The element
* @return Whether the List was modified
*/
protected boolean internalRemove(DNStateManager ownerSM, Object element, int size) {
if (indexedList) {
// Indexed List
// The element can be at one position only (no duplicates allowed in FK list)
int index = indexOf(ownerSM, element);
if (index == -1) {
return false;
}
internalRemoveAt(ownerSM, index, size);
} else {
// Ordered List - no index so null the FK (if nullable) or delete the element
if (ownerMapping.isNullable()) {
// Nullify the FK
ExecutionContext ec = ownerSM.getExecutionContext();
DNStateManager elementSM = ec.findStateManager(element);
if (relationType == RelationType.ONE_TO_MANY_BI) {
// Set field in element to null (so it nulls the FK)
// TODO This is ManagedRelations - move into RelationshipManager
elementSM.replaceFieldMakeDirty(ownerMemberMetaData.getRelatedMemberMetaData(clr)[0].getAbsoluteFieldNumber(), null);
if (ownerSM.getExecutionContext().isFlushing()) {
elementSM.flush();
}
} else {
// Null the (external) FK in the element
updateElementFk(ownerSM, element, null, -1);
}
} else {
// Delete the element
ownerSM.getExecutionContext().deleteObjectInternal(element);
}
}
return true;
}
Aggregations