Search in sources :

Example 66 with ExecutionContext

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

the class SCOUtils method attachForMap.

/**
 * Convenience method to attach (recursively) all keys/values for a map field. All keys/values that are
 * persistable and don't already have an attached object will be attached.
 * @param ownerOP ObjectProvider for the owning object with the map
 * @param entries The entries in the map to process
 * @param keysWithoutIdentity Whether the keys have their own identity
 * @param valuesWithoutIdentity Whether the values have their own identity
 */
public static void attachForMap(ObjectProvider ownerOP, Set entries, boolean keysWithoutIdentity, boolean valuesWithoutIdentity) {
    ExecutionContext ec = ownerOP.getExecutionContext();
    ApiAdapter api = ec.getApiAdapter();
    for (Iterator it = entries.iterator(); it.hasNext(); ) {
        Map.Entry entry = (Map.Entry) it.next();
        Object val = entry.getValue();
        Object key = entry.getKey();
        if (api.isPersistable(key)) {
            Object attached = ec.getAttachedObjectForId(api.getIdForObject(key));
            if (attached == null) {
                // Not yet attached so attach
                ownerOP.getExecutionContext().attachObject(ownerOP, key, keysWithoutIdentity);
            }
        }
        if (api.isPersistable(val)) {
            Object attached = ec.getAttachedObjectForId(api.getIdForObject(val));
            if (attached == null) {
                // Not yet attached so attach
                ownerOP.getExecutionContext().attachObject(ownerOP, val, valuesWithoutIdentity);
            }
        }
    }
}
Also used : ExecutionContext(org.datanucleus.ExecutionContext) ApiAdapter(org.datanucleus.api.ApiAdapter) Iterator(java.util.Iterator) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Example 67 with ExecutionContext

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

the class AbstractQueryResult method getSizeUsingMethod.

/**
 * Method to get the size using the "resultSizeMethod".
 * This implementation supports "COUNT" method.
 * Override this in subclasses to implement other methods.
 * @return The size
 */
protected int getSizeUsingMethod() {
    if (resultSizeMethod.equalsIgnoreCase("COUNT")) {
        if (query != null && query.getCompilation() != null) {
            ExecutionContext ec = query.getExecutionContext();
            if (query.getCompilation().getQueryLanguage().equalsIgnoreCase(Query.LANGUAGE_JDOQL)) {
                // JDOQL : "count([DISTINCT ]this)" query
                Query countQuery = query.getStoreManager().newQuery(Query.LANGUAGE_JDOQL, ec, query);
                if (query.getResultDistinct()) {
                    countQuery.setResult("COUNT(DISTINCT this)");
                } else {
                    countQuery.setResult("count(this)");
                }
                // Ordering not relevant to a count
                countQuery.setOrdering(null);
                // Don't want range to interfere with the query
                countQuery.setRange(null);
                Map queryParams = query.getInputParameters();
                long count;
                if (queryParams != null) {
                    count = ((Long) countQuery.executeWithMap(queryParams)).longValue();
                } else {
                    count = ((Long) countQuery.execute()).longValue();
                }
                if (query.getRange() != null) {
                    // Query had a range, so update the returned count() to allow for the required range
                    long rangeStart = query.getRangeFromIncl();
                    long rangeEnd = query.getRangeToExcl();
                    count -= rangeStart;
                    if (count > (rangeEnd - rangeStart)) {
                        count = rangeEnd - rangeStart;
                    }
                }
                countQuery.closeAll();
                return (int) count;
            } else if (query.getCompilation().getQueryLanguage().equalsIgnoreCase(Query.LANGUAGE_JPQL)) {
                // JPQL : "count()" query
                Query countQuery = query.getStoreManager().newQuery(Query.LANGUAGE_JPQL, ec, query);
                countQuery.setResult("count(" + query.getCompilation().getCandidateAlias() + ")");
                countQuery.setOrdering(null);
                // Don't want range to interfere with the query
                countQuery.setRange(null);
                Map queryParams = query.getInputParameters();
                long count;
                if (queryParams != null) {
                    count = ((Long) countQuery.executeWithMap(queryParams)).longValue();
                } else {
                    count = ((Long) countQuery.execute()).longValue();
                }
                if (query.getRange() != null) {
                    // Query had a range, so update the returned count() to allow for the required range
                    long rangeStart = query.getRangeFromIncl();
                    long rangeEnd = query.getRangeToExcl();
                    count -= rangeStart;
                    if (count > (rangeEnd - rangeStart)) {
                        count = rangeEnd - rangeStart;
                    }
                }
                countQuery.closeAll();
                return (int) count;
            }
        }
        throw new NucleusUserException("datanucleus.query.resultSizeMethod of \"COUNT\" is only valid for use with JDOQL or JPQL currently");
    }
    throw new NucleusUserException("DataNucleus doesnt currently support any method \"" + resultSizeMethod + "\" for determining the size of the query results");
}
Also used : ExecutionContext(org.datanucleus.ExecutionContext) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) Map(java.util.Map)

Example 68 with ExecutionContext

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

the class DeleteFieldManager method storeObjectField.

/**
 * Method to store an object field.
 * @param fieldNumber Number of the field (absolute)
 * @param value Value of the field
 */
public void storeObjectField(int fieldNumber, Object value) {
    if (value != null) {
        AbstractMemberMetaData mmd = op.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
        ExecutionContext ec = op.getExecutionContext();
        RelationType relationType = mmd.getRelationType(ec.getClassLoaderResolver());
        if (relationType != RelationType.NONE) {
            if (mmd.hasContainer()) {
                processContainer(fieldNumber, value, mmd, ec, relationType);
            } else {
                processSingleValue(value, mmd, ec, relationType);
            }
        }
    }
}
Also used : ExecutionContext(org.datanucleus.ExecutionContext) RelationType(org.datanucleus.metadata.RelationType) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 69 with ExecutionContext

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

the class DetachFieldManager method processPersistableCopy.

/**
 * Utility method to process the passed persistable object creating a copy.
 * @param pc The PC object
 * @return The processed object
 */
protected Object processPersistableCopy(Object pc) {
    ExecutionContext ec = op.getExecutionContext();
    ApiAdapter api = ec.getApiAdapter();
    if (!api.isDetached(pc) && api.isPersistent(pc)) {
        // Detach a copy and return the copy
        return ec.detachObjectCopy(state, pc);
    }
    return pc;
}
Also used : ExecutionContext(org.datanucleus.ExecutionContext) ApiAdapter(org.datanucleus.api.ApiAdapter)

Example 70 with ExecutionContext

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

the class DetachFieldManager method processPersistable.

/**
 * Utility method to process the passed persistable object.
 * @param pc The PC object
 */
protected void processPersistable(Object pc) {
    ExecutionContext ec = op.getExecutionContext();
    ApiAdapter api = ec.getApiAdapter();
    if (!api.isDetached(pc) && api.isPersistent(pc)) {
        // Persistent object that is not yet detached so detach it
        ec.detachObject(state, pc);
    }
}
Also used : ExecutionContext(org.datanucleus.ExecutionContext) ApiAdapter(org.datanucleus.api.ApiAdapter)

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