Search in sources :

Example 86 with ExecutionContext

use of org.datanucleus.ExecutionContext in project datanucleus-api-jdo by datanucleus.

the class NucleusJDOHelper method getDirtyFields.

/**
 * Accessor for the names of the dirty fields of the persistable object.
 * @param obj The persistable object
 * @param pm The Persistence Manager (only required if the object is detached)
 * @return Names of the dirty fields
 */
public static String[] getDirtyFields(Object obj, PersistenceManager pm) {
    if (obj == null || !(obj instanceof Persistable)) {
        return null;
    }
    Persistable pc = (Persistable) obj;
    if (isDetached(pc)) {
        ExecutionContext ec = ((JDOPersistenceManager) pm).getExecutionContext();
        // Temporarily attach a StateManager to access the detached field information
        ObjectProvider op = ec.getNucleusContext().getObjectProviderFactory().newForDetached(ec, pc, getObjectId(pc), null);
        pc.dnReplaceStateManager(op);
        op.retrieveDetachState(op);
        String[] dirtyFieldNames = op.getDirtyFieldNames();
        pc.dnReplaceStateManager(null);
        return dirtyFieldNames;
    }
    ExecutionContext ec = ((JDOPersistenceManager) pm).getExecutionContext();
    ObjectProvider op = ec.findObjectProvider(pc);
    if (op == null) {
        return null;
    }
    return op.getDirtyFieldNames();
}
Also used : Persistable(org.datanucleus.enhancement.Persistable) ExecutionContext(org.datanucleus.ExecutionContext) ObjectProvider(org.datanucleus.state.ObjectProvider)

Example 87 with ExecutionContext

use of org.datanucleus.ExecutionContext in project datanucleus-api-jdo by datanucleus.

the class NucleusJDOHelper method getLoadedFields.

/**
 * Accessor for the names of the loaded fields of the persistable object.
 * @param obj Persistable object
 * @param pm The Persistence Manager (only required if the object is detached)
 * @return Names of the loaded fields
 */
public static String[] getLoadedFields(Object obj, PersistenceManager pm) {
    if (obj == null || !(obj instanceof Persistable)) {
        return null;
    }
    Persistable pc = (Persistable) obj;
    if (isDetached(pc)) {
        // Temporarily attach a StateManager to access the detached field information
        ExecutionContext ec = ((JDOPersistenceManager) pm).getExecutionContext();
        ObjectProvider op = ec.getNucleusContext().getObjectProviderFactory().newForDetached(ec, pc, getObjectId(pc), null);
        pc.dnReplaceStateManager(op);
        op.retrieveDetachState(op);
        String[] loadedFieldNames = op.getLoadedFieldNames();
        pc.dnReplaceStateManager(null);
        return loadedFieldNames;
    }
    ExecutionContext ec = ((JDOPersistenceManager) pm).getExecutionContext();
    ObjectProvider op = ec.findObjectProvider(pc);
    if (op == null) {
        return null;
    }
    return op.getLoadedFieldNames();
}
Also used : Persistable(org.datanucleus.enhancement.Persistable) ExecutionContext(org.datanucleus.ExecutionContext) ObjectProvider(org.datanucleus.state.ObjectProvider)

Example 88 with ExecutionContext

use of org.datanucleus.ExecutionContext in project datanucleus-api-jdo by datanucleus.

the class JDOCallbackHandler method preStore.

/**
 * Callback before the object is stored.
 * @param pc The Object
 */
public void preStore(Object pc) {
    for (LifecycleListenerForClass listener : getListenersWorkingCopy()) {
        if (listener.forClass(pc.getClass()) && listener.getListener() instanceof StoreLifecycleListener) {
            ExecutionContext ec = nucleusCtx.getApiAdapter().getExecutionContext(pc);
            String[] fieldNames = null;
            // PRE_STORE will return the fields being stored (DataNucleus extension)
            ObjectProvider op = ec.findObjectProvider(pc);
            fieldNames = op.getDirtyFieldNames();
            if (fieldNames == null) {
                // Must be persisting so just return all loaded fields
                fieldNames = op.getLoadedFieldNames();
            }
            ((StoreLifecycleListener) listener.getListener()).preStore(new FieldInstanceLifecycleEvent(pc, InstanceLifecycleEvent.STORE, null, fieldNames));
        }
    }
    if (pc instanceof StoreCallback) {
        try {
            ((StoreCallback) pc).jdoPreStore();
        } catch (Exception e) {
            throw new JDOUserCallbackException(Localiser.msg("025001", "jdoPreStore"), e);
        }
    }
    if (beanValidationHandler != null) {
        ObjectProvider op = nucleusCtx.getApiAdapter().getExecutionContext(pc).findObjectProvider(pc);
        if (!op.getLifecycleState().isNew()) {
            // Don't fire this when persisting new since we will have done prePersist
            beanValidationHandler.preStore(pc);
        }
    }
}
Also used : JDOUserCallbackException(javax.jdo.JDOUserCallbackException) ExecutionContext(org.datanucleus.ExecutionContext) StoreLifecycleListener(javax.jdo.listener.StoreLifecycleListener) StoreCallback(javax.jdo.listener.StoreCallback) ObjectProvider(org.datanucleus.state.ObjectProvider) JDOUserCallbackException(javax.jdo.JDOUserCallbackException)

Example 89 with ExecutionContext

use of org.datanucleus.ExecutionContext in project tests by datanucleus.

the class FetchPlanTest method getFetchPlan.

/**
 * Convenience accessor for a new FetchPlan.
 * @return The FetchPlan
 */
private FetchPlan getFetchPlan() {
    JDOPersistenceManager pm = (JDOPersistenceManager) pmf.getPersistenceManager();
    ExecutionContext ec = pm.getExecutionContext();
    return new FetchPlan(ec, ec.getClassLoaderResolver());
}
Also used : JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) ExecutionContext(org.datanucleus.ExecutionContext) FetchPlan(org.datanucleus.FetchPlan) JDOFetchPlan(org.datanucleus.api.jdo.JDOFetchPlan)

Example 90 with ExecutionContext

use of org.datanucleus.ExecutionContext in project datanucleus-rdbms by datanucleus.

the class RDBMSPersistenceHandler method insertObject.

/**
 * Inserts a persistent object into the database.
 * The insert can take place in several steps, one insert per table that it is stored in.
 * e.g When persisting an object that uses "new-table" inheritance for each level of the inheritance tree
 * then will get an INSERT into each table. When persisting an object that uses "complete-table"
 * inheritance then will get a single INSERT into its table.
 * @param op The ObjectProvider of the object to be inserted.
 * @throws NucleusDataStoreException when an error occurs in the datastore communication
 */
public void insertObject(ObjectProvider op) {
    // Check if read-only so update not permitted
    assertReadOnlyForUpdateOfObject(op);
    // Check if we need to do any updates to the schema before inserting this object
    checkForSchemaUpdatesForFieldsOfObject(op, op.getLoadedFieldNumbers());
    ExecutionContext ec = op.getExecutionContext();
    ClassLoaderResolver clr = op.getExecutionContext().getClassLoaderResolver();
    String className = op.getClassMetaData().getFullClassName();
    DatastoreClass dc = getDatastoreClass(className, clr);
    if (dc == null) {
        if (op.getClassMetaData().getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUBCLASS_TABLE) {
            throw new NucleusUserException(Localiser.msg("032013", className));
        }
        throw new NucleusException(Localiser.msg("032014", className, op.getClassMetaData().getInheritanceMetaData().getStrategy())).setFatal();
    }
    if (ec.getStatistics() != null) {
        ec.getStatistics().incrementInsertCount();
    }
    insertObjectInTable(dc, op, clr);
}
Also used : ExecutionContext(org.datanucleus.ExecutionContext) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) SecondaryDatastoreClass(org.datanucleus.store.rdbms.table.SecondaryDatastoreClass) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) NucleusException(org.datanucleus.exceptions.NucleusException)

Aggregations

ExecutionContext (org.datanucleus.ExecutionContext)178 ObjectProvider (org.datanucleus.state.ObjectProvider)85 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)73 SQLException (java.sql.SQLException)66 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)64 SQLController (org.datanucleus.store.rdbms.SQLController)63 PreparedStatement (java.sql.PreparedStatement)62 Iterator (java.util.Iterator)56 MappedDatastoreException (org.datanucleus.store.rdbms.exceptions.MappedDatastoreException)27 ResultSet (java.sql.ResultSet)26 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)26 StatementMappingIndex (org.datanucleus.store.rdbms.query.StatementMappingIndex)25 Map (java.util.Map)23 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)20 Collection (java.util.Collection)18 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)18 StatementClassMapping (org.datanucleus.store.rdbms.query.StatementClassMapping)17 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)16 SCOCollectionIterator (org.datanucleus.store.types.SCOCollectionIterator)16 ArrayList (java.util.ArrayList)15