interface.
| Method from org.apache.openjpa.kernel.QueryImpl Detail: |
public void addAggregateListener(AggregateListener listener) {
lock();
try {
assertOpen();
assertNotReadOnly();
if (_aggListeners == null)
_aggListeners = new HashMap(5);
_aggListeners.put(listener.getTag(), listener);
} finally {
unlock();
}
}
|
public void addFilterListener(FilterListener listener) {
lock();
try {
assertOpen();
assertNotReadOnly();
if (_filtListeners == null)
_filtListeners = new HashMap(5);
_filtListeners.put(listener.getTag(), listener);
} finally {
unlock();
}
}
|
public void assertNotReadOnly() {
if (_readOnly)
throw new InvalidStateException(_loc.get("read-only"));
}
|
public void assertNotSerialized() {
if (_broker == null)
throw new InvalidStateException(_loc.get("serialized"));
}
|
public void assertOpen() {
if (_broker != null)
_broker.assertOpen();
}
|
protected void assertParameters(StoreQuery q,
StoreQuery.Executor ex,
Object[] params) {
if (!q.requiresParameterDeclarations())
return;
LinkedMap paramTypes = ex.getParameterTypes(q);
int typeCount = paramTypes.size();
if (typeCount > params.length)
throw new UserException(_loc.get("unbound-params",
paramTypes.keySet()));
Iterator itr = paramTypes.entrySet().iterator();
Map.Entry entry;
for (int i = 0; itr.hasNext(); i++) {
entry = (Map.Entry) itr.next();
if (((Class) entry.getValue()).isPrimitive() && params[i] == null)
throw new UserException(_loc.get("null-primitive-param",
entry.getKey()));
}
}
Checks that the passed parameters match the declarations. |
public Class classForName(String name,
String[] imports) {
// full class name or primitive type?
Class type = toClass(name);
if (type != null)
return type;
// first check the aliases map in the MetaDataRepository
ClassLoader loader = (_class == null) ? _loader
: (ClassLoader) AccessController.doPrivileged(
J2DoPrivHelper.getClassLoaderAction(_class));
ClassMetaData meta = _broker.getConfiguration().
getMetaDataRepositoryInstance().getMetaData(name, loader, false);
if (meta != null)
return meta.getDescribedType();
// try the name in the package of the candidate class
if (_class != null) {
String fullName = _class.getName().substring
(0, _class.getName().lastIndexOf('.") + 1) + name;
type = toClass(fullName);
if (type != null)
return type;
}
// try java.lang
type = toClass("java.lang." + name);
if (type != null)
return type;
// try each import
if (imports != null && imports.length > 0) {
String dotName = "." + name;
String importName;
for (int i = 0; i < imports.length; i++) {
importName = imports[i];
// full class name import
if (importName.endsWith(dotName))
type = toClass(importName);
// wildcard; strip to package
else if (importName.endsWith(".*")) {
importName = importName.substring
(0, importName.length() - 1);
type = toClass(importName + name);
}
if (type != null)
return type;
}
}
return null;
}
|
public void closeAll() {
closeResults(true);
}
|
public void closeResources() {
closeResults(false);
}
|
protected QueryImpl.Compilation compilationFromCache() {
Map compCache =
_broker.getConfiguration().getQueryCompilationCacheInstance();
if (compCache == null) {
return newCompilation();
} else {
CompilationKey key = new CompilationKey();
key.queryType = _storeQuery.getClass();
key.candidateType = getCandidateType();
key.subclasses = hasSubclasses();
key.query = getQueryString();
key.language = getLanguage();
key.storeKey = _storeQuery.newCompilationKey();
Compilation comp = (Compilation) compCache.get(key);
// parse declarations if needed
boolean cache = false;
if (comp == null) {
comp = newCompilation();
// only cache those queries that can be compiled
cache = comp.storeData != null;
} else
_storeQuery.populateFromCompilation(comp.storeData);
// cache parsed state if needed
if (cache)
compCache.put(key, comp);
return comp;
}
}
Find the cached compilation for the current query, creating one if it
does not exist. |
public void compile() {
lock();
try {
assertOpen();
StoreQuery.Executor ex = compileForExecutor();
getResultPacker(_storeQuery, ex);
ex.validate(_storeQuery);
} finally {
unlock();
}
}
|
public void declareParameters(String params) {
if (!_storeQuery.supportsParameterDeclarations())
throw new UnsupportedException(_loc.get("query-nosupport",
_language));
lock();
try {
assertOpen();
assertNotReadOnly();
_params = StringUtils.trimToNull(params);
invalidateCompilation();
} finally {
unlock();
}
}
|
protected ResultList decorateResultList(ResultList res) {
return new RemoveOnCloseResultList(res);
}
Optionally decorate the native result. |
public long deleteAll() {
return deleteAll((Object[]) null);
}
|
public long deleteAll(Object[] params) {
return ((Number) execute(OP_DELETE, params)).longValue();
}
|
public long deleteAll(Map params) {
return ((Number) execute(OP_DELETE, params)).longValue();
}
|
public Number deleteInMemory(StoreQuery q,
StoreQuery.Executor executor,
Object[] params) {
try {
Object o = execute(q, executor, params);
if (!(o instanceof Collection))
o = Collections.singleton(o);
int size = 0;
for (Iterator i = ((Collection) o).iterator(); i.hasNext(); size++)
_broker.delete(i.next(), null);
return Numbers.valueOf(size);
} catch (OpenJPAException ke) {
throw ke;
} catch (Exception e) {
throw new UserException(e);
}
}
|
public Object execute() {
return execute((Object[]) null);
}
|
public Object execute(Object[] params) {
return execute(OP_SELECT, params);
}
|
public Object execute(Map params) {
return execute(OP_SELECT, params);
}
|
public ClassMetaData[] getAccessPathMetaDatas() {
lock();
try {
ClassMetaData[] metas = compileForExecutor().
getAccessPathMetaDatas(_storeQuery);
return (metas == null) ? StoreQuery.EMPTY_METAS : metas;
} finally {
unlock();
}
}
|
public AggregateListener getAggregateListener(String tag) {
// first check listeners for this query
if (_aggListeners != null) {
AggregateListener listen = (AggregateListener) _aggListeners.
get(tag);
if (listen != null)
return listen;
}
// check user-defined listeners from configuration
AggregateListener[] confListeners = _broker.getConfiguration().
getAggregateListenerInstances();
for (int i = 0; i < confListeners.length; i++)
if (confListeners[i].getTag().equals(tag))
return confListeners[i];
// check store listeners
return _storeQuery.getAggregateListener(tag);
}
|
public Collection getAggregateListeners() {
return (_aggListeners == null) ? Collections.EMPTY_LIST
: _aggListeners.values();
}
|
public String getAlias() {
lock();
try {
String alias = compileForExecutor().getAlias(_storeQuery);
if (alias == null)
alias = Strings.getClassName(_class);
return alias;
} finally {
unlock();
}
}
|
public Broker getBroker() {
return _broker;
}
|
public Collection getCandidateCollection() {
assertOpen();
return _collection;
}
|
public Extent getCandidateExtent() {
// if just the class is set, fetch the corresponding extent; if the
// extent is already set but its ignore cache setting is wrong,
// get a new extent with the correct setting (don't modify orig extent
// in case the user has a reference to it and might use it)
lock();
try {
Class cls = getCandidateType();
if (_extent == null && _collection == null && _broker != null
&& cls != null) {
_extent = _broker.newExtent(cls, _subclasses);
_extent.setIgnoreChanges(_ignoreChanges);
} else if (_extent != null
&& _extent.getIgnoreChanges() != _ignoreChanges && cls != null){
_extent = _broker.newExtent(cls, _extent.hasSubclasses());
_extent.setIgnoreChanges(_ignoreChanges);
}
return _extent;
} finally {
unlock();
}
}
|
public Class getCandidateType() {
lock();
try {
assertOpen();
if (_class != null || _compiled != null || _query == null
|| _broker == null)
return _class;
// check again after compilation; maybe encoded in string
compileForCompilation();
return _class;
} finally {
unlock();
}
}
|
public Object getCompilation() {
lock();
try {
return compileForCompilation().storeData;
} finally {
unlock();
}
}
|
public String[] getDataStoreActions(Map params) {
if (params == null)
params = Collections.EMPTY_MAP;
lock();
try {
assertNotSerialized();
assertOpen();
StoreQuery.Executor ex = compileForExecutor();
Object[] arr = toParameterArray(ex.getParameterTypes(_storeQuery),
params);
assertParameters(_storeQuery, ex, arr);
StoreQuery.Range range = new StoreQuery.Range(_startIdx, _endIdx);
if (!_rangeSet)
ex.getRange(_storeQuery, arr, range);
return ex.getDataStoreActions(_storeQuery, arr, range);
} catch (OpenJPAException ke) {
throw ke;
} catch (Exception e) {
throw new UserException(e);
} finally {
unlock();
}
}
|
public long getEndRange() {
assertOpen();
return _endIdx;
}
|
public FetchConfiguration getFetchConfiguration() {
return _fc;
}
|
public FilterListener getFilterListener(String tag) {
// first check listeners for this query
if (_filtListeners != null) {
FilterListener listen = (FilterListener) _filtListeners.get(tag);
if (listen != null)
return listen;
}
// check user-defined listeners from configuration
FilterListener[] confListeners = _broker.getConfiguration().
getFilterListenerInstances();
for (int i = 0; i < confListeners.length; i++)
if (confListeners[i].getTag().equals(tag))
return confListeners[i];
// check store listeners
return _storeQuery.getFilterListener(tag);
}
|
public Collection getFilterListeners() {
return (_filtListeners == null) ? Collections.EMPTY_LIST
: _filtListeners.values();
}
|
public boolean getIgnoreChanges() {
assertOpen();
return _ignoreChanges;
}
|
public String getLanguage() {
return _language;
}
|
public int getOperation() {
lock();
try {
return compileForExecutor().getOperation(_storeQuery);
} finally {
unlock();
}
}
|
public String getParameterDeclaration() {
lock();
try {
assertOpen();
if (_params != null || _compiled != null || _compiling
|| _broker == null)
return _params;
// check again after compilation; maybe encoded in string
compileForCompilation();
return _params;
} finally {
unlock();
}
}
|
public LinkedMap getParameterTypes() {
lock();
try {
return compileForExecutor().getParameterTypes(_storeQuery);
} finally {
unlock();
}
}
|
public String[] getProjectionAliases() {
lock();
try {
return compileForExecutor().getProjectionAliases(_storeQuery);
} finally {
unlock();
}
}
|
public Class[] getProjectionTypes() {
lock();
try {
return compileForExecutor().getProjectionTypes(_storeQuery);
} finally {
unlock();
}
}
|
public Query getQuery() {
return this;
}
|
public String getQueryString() {
return _query;
}
|
public String getResultMappingName() {
assertOpen();
return _resultMappingName;
}
|
public Class getResultMappingScope() {
assertOpen();
return _resultMappingScope;
}
|
public Class getResultType() {
lock();
try {
assertOpen();
if (_resultClass != null || _compiled != null || _query == null
|| _broker == null)
return _resultClass;
// check again after compilation; maybe encoded in string
compileForCompilation();
return _resultClass;
} finally {
unlock();
}
}
|
public long getStartRange() {
assertOpen();
return _startIdx;
}
|
public StoreContext getStoreContext() {
return _broker;
}
|
public StoreQuery getStoreQuery() {
return _storeQuery;
}
|
public Map getUpdates() {
lock();
try {
return compileForExecutor().getUpdates(_storeQuery);
} finally {
unlock();
}
}
|
public boolean hasGrouping() {
lock();
try {
return compileForExecutor().hasGrouping(_storeQuery);
} finally {
unlock();
}
}
|
public boolean hasSubclasses() {
return _subclasses;
}
|
public static boolean isAccessPathDirty(Broker broker,
ClassMetaData[] accessMetas) {
Collection persisted = broker.getPersistedTypes();
Collection updated = broker.getUpdatedTypes();
Collection deleted = broker.getDeletedTypes();
if (persisted.isEmpty() && updated.isEmpty() && deleted.isEmpty())
return false;
// if no access metas, assume every dirty object affects path just
// to be safe
if (accessMetas.length == 0)
return true;
// compare dirty classes to the access path classes
Class accClass;
for (int i = 0; i < accessMetas.length; i++) {
// shortcut if actual class is dirty
accClass = accessMetas[i].getDescribedType();
if (persisted.contains(accClass) || updated.contains(accClass)
|| deleted.contains(accClass))
return true;
// check for dirty subclass
for (Iterator dirty = persisted.iterator(); dirty.hasNext();)
if (accClass.isAssignableFrom((Class) dirty.next()))
return true;
for (Iterator dirty = updated.iterator(); dirty.hasNext();)
if (accClass.isAssignableFrom((Class) dirty.next()))
return true;
for (Iterator dirty = deleted.iterator(); dirty.hasNext();)
if (accClass.isAssignableFrom((Class) dirty.next()))
return true;
}
// no intersection
return false;
}
|
public boolean isAggregate() {
lock();
try {
return compileForExecutor().isAggregate(_storeQuery);
} finally {
unlock();
}
}
|
public boolean isReadOnly() {
assertOpen();
return _readOnly;
}
|
public boolean isUnique() {
lock();
try {
assertOpen();
if (_unique != null)
return _unique.booleanValue();
if (_query == null || _compiling || _broker == null)
return false;
// check again after compilation; maybe encoded in string
if (_compiled == null) {
compileForCompilation();
if (_unique != null)
return _unique.booleanValue();
}
// no explicit setting; default
StoreQuery.Executor ex = compileForExecutor();
if (!ex.isAggregate(_storeQuery))
return false;
return !ex.hasGrouping(_storeQuery);
} finally {
unlock();
}
}
|
public void lock() {
if (_lock != null)
_lock.lock();
}
|
public void removeAggregateListener(AggregateListener listener) {
lock();
try {
assertOpen();
assertNotReadOnly();
if (_aggListeners != null)
_aggListeners.remove(listener.getTag());
} finally {
unlock();
}
}
|
public void removeFilterListener(FilterListener listener) {
lock();
try {
assertOpen();
assertNotReadOnly();
if (_filtListeners != null)
_filtListeners.remove(listener.getTag());
} finally {
unlock();
}
}
|
public void setCandidateCollection(Collection candidateCollection) {
if (!_storeQuery.supportsInMemoryExecution())
throw new UnsupportedException(_loc.get("query-nosupport",
_language));
lock();
try {
assertOpen();
// if collection then not extent
_collection = candidateCollection;
if (_collection != null)
_extent = null;
} finally {
unlock();
}
}
|
public void setCandidateExtent(Extent candidateExtent) {
lock();
try {
assertOpen();
assertNotReadOnly();
if (candidateExtent == _extent)
return;
if (candidateExtent == null) {
_extent = null;
return;
}
// if extent then not collection
_extent = candidateExtent;
_collection = null;
boolean invalidate = false;
if (_extent.getElementType() != _class) {
_class = _extent.getElementType();
_loader = null;
invalidate = true;
}
if (_extent.hasSubclasses() != _subclasses) {
_subclasses = _extent.hasSubclasses();
invalidate = true;
}
if (invalidate)
invalidateCompilation();
} finally {
unlock();
}
}
|
public void setCandidateType(Class candidateClass,
boolean subs) {
lock();
try {
assertOpen();
assertNotReadOnly();
_class = candidateClass;
_subclasses = subs;
_loader = null;
invalidateCompilation();
} finally {
unlock();
}
}
|
public void setIgnoreChanges(boolean flag) {
lock();
try {
assertOpen();
// allowed modification: no read-only check
_ignoreChanges = flag;
} finally {
unlock();
}
}
|
public boolean setQuery(Object query) {
lock();
try {
assertOpen();
assertNotReadOnly();
if (query == null || query instanceof String) {
invalidateCompilation();
_query = (String) query;
if (_query != null)
_query = _query.trim();
return true;
}
if (!(query instanceof QueryImpl))
return _storeQuery.setQuery(query);
// copy all non-transient state from the given query
invalidateCompilation();
QueryImpl q = (QueryImpl) query;
_class = q._class;
_subclasses = q._subclasses;
_query = q._query;
_ignoreChanges = q._ignoreChanges;
_unique = q._unique;
_resultClass = q._resultClass;
_params = q._params;
_resultMappingScope = q._resultMappingScope;
_resultMappingName = q._resultMappingName;
_readOnly = q._readOnly;
// don't share mutable objects
_fc.copy(q._fc);
if (q._filtListeners != null)
_filtListeners = new HashMap(q._filtListeners);
if (q._aggListeners != null)
_aggListeners = new HashMap(q._aggListeners);
return true;
} finally {
unlock();
}
}
|
public void setRange(long start,
long end) {
if (start < 0 || end < 0)
throw new UserException(_loc.get("invalid-range",
String.valueOf(start), String.valueOf(end)));
if (end - start > Integer.MAX_VALUE && end != Long.MAX_VALUE)
throw new UserException(_loc.get("range-too-big",
String.valueOf(start), String.valueOf(end)));
lock();
try {
assertOpen();
// allowed modification: no read-only check
_startIdx = start;
_endIdx = end;
_rangeSet = true;
} finally {
unlock();
}
}
|
public void setReadOnly(boolean flag) {
lock();
try {
assertOpen();
_readOnly = flag;
} finally {
unlock();
}
}
|
public void setResultMapping(Class scope,
String name) {
lock();
try {
assertOpen();
_resultMappingScope = scope;
_resultMappingName = name;
_packer = null;
} finally {
unlock();
}
}
|
public void setResultType(Class cls) {
lock();
try {
assertOpen();
// allowed modification: no read-only check
_resultClass = cls;
_packer = null;
} finally {
unlock();
}
}
|
public void setUnique(boolean unique) {
lock();
try {
assertOpen();
assertNotReadOnly();
_unique = (unique) ? Boolean.TRUE : Boolean.FALSE;
} finally {
unlock();
}
}
|
protected Object toResult(StoreQuery q,
StoreQuery.Executor ex,
ResultObjectProvider rop,
StoreQuery.Range range) throws Exception {
// pack projections if necessary
String[] aliases = ex.getProjectionAliases(q);
if (!ex.isPacking(q)) {
ResultPacker packer = getResultPacker(q, ex);
if (packer != null || aliases.length == 1)
rop = new PackingResultObjectProvider(rop, packer,
aliases.length);
}
// if single result, extract it
if (_unique == Boolean.TRUE || (aliases.length > 0
&& !ex.hasGrouping(q) && ex.isAggregate(q)))
return singleResult(rop, range);
// now that we've executed the query, we can call isAggregate and
// hasGrouping efficiently
boolean detach = (_broker.getAutoDetach() &
AutoDetach.DETACH_NONTXREAD) > 0 && !_broker.isActive();
boolean lrs = range.lrs && !ex.isAggregate(q) && !ex.hasGrouping(q);
ResultList res = (!detach && lrs) ? _fc.newResultList(rop)
: new EagerResultList(rop);
_resultLists.add(decorateResultList(res));
return res;
}
Return the query result for the given result object provider. |
public String toString() {
StringBuffer buf = new StringBuffer(64);
buf.append("Query: ").append(super.toString());
buf.append("; candidate class: ").append(_class);
buf.append("; query: ").append(_query);
return buf.toString();
}
|
public void unlock() {
if (_lock != null && _lock.isLocked())
_lock.unlock();
}
|
public long updateAll() {
return updateAll((Object[]) null);
}
|
public long updateAll(Object[] params) {
return ((Number) execute(OP_UPDATE, params)).longValue();
}
|
public long updateAll(Map params) {
return ((Number) execute(OP_UPDATE, params)).longValue();
}
|
public Number updateInMemory(StoreQuery q,
StoreQuery.Executor executor,
Object[] params) {
try {
Object o = execute(q, executor, params);
if (!(o instanceof Collection))
o = Collections.singleton(o);
int size = 0;
for (Iterator i = ((Collection) o).iterator(); i.hasNext(); size++)
updateInMemory(i.next(), params);
return Numbers.valueOf(size);
} catch (OpenJPAException ke) {
throw ke;
} catch (Exception e) {
throw new UserException(e);
}
}
|