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);
}
}
}
}
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");
}
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);
}
}
}
}
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;
}
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);
}
}
Aggregations