Search in sources :

Example 41 with DNStateManager

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();
    }
}
Also used : DNStateManager(org.datanucleus.state.DNStateManager)

Example 42 with DNStateManager

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);
    }
}
Also used : ExecutionContext(org.datanucleus.ExecutionContext) DNStateManager(org.datanucleus.state.DNStateManager)

Example 43 with DNStateManager

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;
}
Also used : ExecutionContext(org.datanucleus.ExecutionContext) TypeManager(org.datanucleus.store.types.TypeManager) DNStateManager(org.datanucleus.state.DNStateManager) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 44 with DNStateManager

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()]));
    }
}
Also used : HashMap(java.util.HashMap) ConcurrentReferenceHashMap(org.datanucleus.util.ConcurrentReferenceHashMap) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) ArrayList(java.util.ArrayList) List(java.util.List) SecondaryDatastoreClass(org.datanucleus.store.rdbms.table.SecondaryDatastoreClass) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) DNStateManager(org.datanucleus.state.DNStateManager) LocateBulkRequest(org.datanucleus.store.rdbms.request.LocateBulkRequest) HashMap(java.util.HashMap) ConcurrentReferenceHashMap(org.datanucleus.util.ConcurrentReferenceHashMap) Map(java.util.Map)

Example 45 with DNStateManager

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;
}
Also used : ExecutionContext(org.datanucleus.ExecutionContext) DNStateManager(org.datanucleus.state.DNStateManager)

Aggregations

DNStateManager (org.datanucleus.state.DNStateManager)64 ExecutionContext (org.datanucleus.ExecutionContext)38 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)22 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)20 SQLException (java.sql.SQLException)17 StatementMappingIndex (org.datanucleus.store.rdbms.query.StatementMappingIndex)16 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)15 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)15 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)14 StatementClassMapping (org.datanucleus.store.rdbms.query.StatementClassMapping)14 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)13 SQLController (org.datanucleus.store.rdbms.SQLController)13 PreparedStatement (java.sql.PreparedStatement)12 ResultSet (java.sql.ResultSet)10 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)9 NucleusException (org.datanucleus.exceptions.NucleusException)9 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)9 Collection (java.util.Collection)8 ArrayList (java.util.ArrayList)7 RelationType (org.datanucleus.metadata.RelationType)7