org.datanucleus.store.rdbms.query.JDOQLQuery Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datanucleus-rdbms Show documentation
Show all versions of datanucleus-rdbms Show documentation
Plugin for DataNucleus providing persistence to RDBMS datastores.
/**********************************************************************
Copyright (c) 2008 Andy Jefferson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Contributors:
...
**********************************************************************/
package org.datanucleus.store.rdbms.query;
import org.datanucleus.ExecutionContext;
import org.datanucleus.FetchPlanForClass;
import org.datanucleus.exceptions.NucleusDataStoreException;
import org.datanucleus.exceptions.NucleusException;
import org.datanucleus.exceptions.NucleusUserException;
import org.datanucleus.metadata.AbstractClassMetaData;
import org.datanucleus.metadata.AbstractMemberMetaData;
import org.datanucleus.metadata.InheritanceStrategy;
import org.datanucleus.metadata.RelationType;
import org.datanucleus.query.QueryUtils;
import org.datanucleus.query.compiler.Symbol;
import org.datanucleus.query.expression.Expression;
import org.datanucleus.query.inmemory.JDOQLInMemoryEvaluator;
import org.datanucleus.store.StoreManager;
import org.datanucleus.store.connection.ManagedConnection;
import org.datanucleus.store.connection.ManagedConnectionResourceListener;
import org.datanucleus.store.query.AbstractJDOQLQuery;
import org.datanucleus.store.query.CandidateIdsQueryResult;
import org.datanucleus.store.query.Query;
import org.datanucleus.store.query.QueryInterruptedException;
import org.datanucleus.store.query.QueryManager;
import org.datanucleus.store.query.QueryResult;
import org.datanucleus.store.query.QueryTimeoutException;
import org.datanucleus.store.rdbms.RDBMSPropertyNames;
import org.datanucleus.store.rdbms.RDBMSStoreManager;
import org.datanucleus.store.rdbms.SQLController;
import org.datanucleus.store.rdbms.adapter.DatastoreAdapter;
import org.datanucleus.store.rdbms.mapping.java.AbstractContainerMapping;
import org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping;
import org.datanucleus.store.rdbms.query.RDBMSQueryCompilation.StatementCompilation;
import org.datanucleus.store.rdbms.scostore.IteratorStatement;
import org.datanucleus.store.rdbms.sql.DeleteStatement;
import org.datanucleus.store.rdbms.sql.SQLJoin.JoinType;
import org.datanucleus.store.rdbms.sql.SQLStatement;
import org.datanucleus.store.rdbms.sql.SQLStatementHelper;
import org.datanucleus.store.rdbms.sql.SQLTable;
import org.datanucleus.store.rdbms.sql.SelectStatement;
import org.datanucleus.store.rdbms.sql.SelectStatementGenerator;
import org.datanucleus.store.rdbms.sql.UpdateStatement;
import org.datanucleus.store.rdbms.sql.expression.BooleanExpression;
import org.datanucleus.store.rdbms.sql.expression.SQLExpression;
import org.datanucleus.store.rdbms.table.DatastoreClass;
import org.datanucleus.store.schema.table.SurrogateColumnType;
import org.datanucleus.store.types.SCOUtils;
import org.datanucleus.util.ClassUtils;
import org.datanucleus.util.Localiser;
import org.datanucleus.util.NucleusLogger;
import org.datanucleus.util.StringUtils;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* RDBMS representation of a JDOQL query for use by DataNucleus.
* The query can be specified via method calls, or via a single-string form.
* This implementation uses the generic query compilation in "org.datanucleus.query".
* There are the following main ways of running a query here
*
* - Totally in the datastore (no candidate collection specified, and no in-memory eval).
* - Totally in-memory (candidate collection specified, and in-memory eval)
* - Retrieve candidates from datastore (no candidate collection), and evaluate in-memory
*
*/
public class JDOQLQuery extends AbstractJDOQLQuery
{
private static final long serialVersionUID = -937448796638243699L;
/** Extension for whether to convert "== ?" with null parameter to "IS NULL". Defaults to true to match Java semantics. */
public static final String EXTENSION_USE_IS_NULL_WHEN_EQUALS_NULL_PARAM = "datanucleus.useIsNullWhenEqualsNullParameter";
/** Extension for whether to disable the setting of DISTINCT when joining with JDOQL. */
public static final String EXTENSION_NON_DISTINCT_IMPLICIT_JOIN = "datanucleus.query.jdoql.dropDistinctFromImplicitJoin";
/** Extension to add NOWAIT when using FOR UPDATE (when supported). */
public static final String EXTENSION_FOR_UPDATE_NOWAIT = "datanucleus.forUpdateNowait";
/** Extension to define the JOIN TYPE to use when navigating single-valued relations, when part of the filter. */
public static final String EXTENSION_NAVIGATION_JOIN_TYPE_FILTER = "datanucleus.query.jdoql.navigationJoinTypeForFilter";
/** Extension to define the JOIN TYPE to use when navigating single-valued relations. */
public static final String EXTENSION_NAVIGATION_JOIN_TYPE = "datanucleus.query.jdoql.navigationJoinType";
/** Extension to not apply a discriminator restriction on the candidate of the query. */
public static final String EXTENSION_CANDIDATE_DONT_RESTRICT_DISCRIMINATOR = "datanucleus.query.dontRestrictDiscriminator";
/** Extension to include soft-deleted objects in any results. */
public static final String EXTENSION_INCLUDE_SOFT_DELETES = "datanucleus.query.includeSoftDeletes";
/** The compilation of the query for this datastore. Not applicable if totally in-memory. */
protected transient RDBMSQueryCompilation datastoreCompilation = null;
boolean statementReturnsEmpty = false;
/**
* Constructs a new query instance that uses the given object manager.
* @param storeMgr StoreManager for this query
* @param ec execution context
*/
public JDOQLQuery(StoreManager storeMgr, ExecutionContext ec)
{
this(storeMgr, ec, (JDOQLQuery) null);
}
/**
* Constructs a new query instance having the same criteria as the given query.
* @param storeMgr StoreManager for this query
* @param ec execution context
* @param q The query from which to copy criteria.
*/
public JDOQLQuery(StoreManager storeMgr, ExecutionContext ec, JDOQLQuery q)
{
super(storeMgr, ec, q);
}
/**
* Constructor for a JDOQL query where the query is specified using the "Single-String" format.
* @param storeMgr StoreManager for this query
* @param ec execution context
* @param query The single-string query form
*/
public JDOQLQuery(StoreManager storeMgr, ExecutionContext ec, String query)
{
super(storeMgr, ec, query);
}
/**
* Utility to remove any previous compilation of this Query.
*/
protected void discardCompiled()
{
super.discardCompiled();
datastoreCompilation = null;
}
/**
* Method to return if the query is compiled.
* @return Whether it is compiled
*/
protected boolean isCompiled()
{
if (evaluateInMemory())
{
// Don't need datastore compilation here since evaluating in-memory
return compilation != null;
}
// Need both to be present to say "compiled"
if (compilation == null || datastoreCompilation == null)
{
return false;
}
if (!datastoreCompilation.isPrecompilable())
{
NucleusLogger.GENERAL.info("Query compiled but not precompilable so ditching datastore compilation");
datastoreCompilation = null;
return false;
}
return true;
}
/**
* Convenience method to return whether the query should be evaluated in-memory.
* @return Use in-memory evaluation?
*/
protected boolean evaluateInMemory()
{
if (candidateCollection != null)
{
if (compilation != null && compilation.getSubqueryAliases() != null)
{
// TODO In-memory evaluation of subqueries isn't fully implemented yet, so remove this when it is
NucleusLogger.QUERY.warn("In-memory evaluator doesn't currently handle subqueries completely so evaluating in datastore");
return false;
}
Object val = getExtension(EXTENSION_EVALUATE_IN_MEMORY);
if (val == null)
{
return true;
}
Boolean bool = Boolean.valueOf((String)val);
if (bool != null && !bool.booleanValue())
{
// User has explicitly said to not evaluate in-memory
return false;
}
return true;
}
return super.evaluateInMemory();
}
/**
* Method to get key for query cache
* @return The cache key
*/
protected String getQueryCacheKey()
{
if (getSerializeRead() != null && getSerializeRead())
{
return super.getQueryCacheKey() + " FOR UPDATE";
}
return super.getQueryCacheKey();
}
/**
* Method to compile the JDOQL query.
* Uses the superclass to compile the generic query populating the "compilation", and then generates
* the datastore-specific "datastoreCompilation".
* @param parameterValues Map of param values keyed by param name (if available at compile time)
*/
protected synchronized void compileInternal(Map parameterValues)
{
if (isCompiled())
{
return;
}
if (getExtension(EXTENSION_INCLUDE_SOFT_DELETES) != null)
{
// If using an extension that can change the datastore query then evict any existing compilation
QueryManager qm = getQueryManager();
qm.removeQueryCompilation(Query.LANGUAGE_JDOQL, getQueryCacheKey());
}
// Compile the generic query expressions
super.compileInternal(parameterValues);
boolean inMemory = evaluateInMemory();
if (candidateCollection != null && inMemory)
{
// Querying a candidate collection in-memory, so just return now (don't need datastore compilation)
// TODO Maybe apply the result class checks ?
return;
}
// Create the SQL statement, and its result/parameter definitions
RDBMSStoreManager storeMgr = (RDBMSStoreManager)getStoreManager();
if (candidateClass == null)
{
throw new NucleusUserException(Localiser.msg("021009", candidateClassName));
}
// Make sure any persistence info is loaded
ec.hasPersistenceInformationForClass(candidateClass);
if (parameterValues != null)
{
// Check for null values on primitive parameters
Set paramNames = parameterValues.entrySet();
Iterator iter = paramNames.iterator();
while (iter.hasNext())
{
Map.Entry entry = iter.next();
Object paramName = entry.getKey();
if (paramName instanceof String)
{
Symbol sym = compilation.getSymbolTable().getSymbol((String)paramName);
Object value = entry.getValue();
if (value == null)
{
// When we have parameters supplied and have the flag
// "datanucleus.query.checkUnusedParameters" set to false, the symbol may be null
// so omit the check for that case
if (sym != null && sym.getValueType() != null && sym.getValueType().isPrimitive())
{
throw new NucleusUserException(Localiser.msg("021117", paramName, sym.getValueType().getName()));
}
}
}
}
}
QueryManager qm = getQueryManager();
String datastoreKey = storeMgr.getQueryCacheKey();
String queryCacheKey = getQueryCacheKey();
if (useCaching() && queryCacheKey != null)
{
// Check if we have any parameters set to null, since this can invalidate a datastore compilation
// e.g " field == :val" can be "COL IS NULL" or "COL = "
boolean nullParameter = false;
if (parameterValues != null)
{
Iterator iter = parameterValues.values().iterator();
while (iter.hasNext())
{
Object val = iter.next();
if (val == null)
{
nullParameter = true;
break;
}
}
}
if (!nullParameter)
{
// Allowing caching so try to find compiled (datastore) query
datastoreCompilation = (RDBMSQueryCompilation)qm.getDatastoreQueryCompilation(datastoreKey, getLanguage(), queryCacheKey);
if (datastoreCompilation != null)
{
// Cached compilation exists for this datastore so reuse it
setResultDistinct(compilation.getResultDistinct());
return;
}
}
}
// Compile the query for the datastore since not cached
AbstractClassMetaData acmd = getCandidateClassMetaData();
if (type == QueryType.BULK_UPDATE)
{
datastoreCompilation = new RDBMSQueryCompilation();
compileQueryUpdate(parameterValues, acmd);
}
else if (type == QueryType.BULK_DELETE)
{
datastoreCompilation = new RDBMSQueryCompilation();
compileQueryDelete(parameterValues, acmd);
}
else
{
datastoreCompilation = new RDBMSQueryCompilation();
synchronized (datastoreCompilation)
{
if (inMemory)
{
// Generate statement to just retrieve all candidate objects for later processing
compileQueryToRetrieveCandidates(parameterValues, acmd);
}
else
{
// Generate statement to perform the full query in the datastore
compileQueryFull(parameterValues, acmd);
if (result != null)
{
// Check existence of invalid selections in the result
StatementResultMapping resultMapping = datastoreCompilation.getResultDefinition();
for (int i=0;i 1)
{
// Invalid number of result expressions
throw new NucleusUserException(Localiser.msg("021201", resultClass.getName()));
}
Object stmtMap = resultMapping.getMappingForResultExpression(0);
if (stmtMap instanceof StatementMappingIndex)
{
StatementMappingIndex idx = (StatementMappingIndex)stmtMap;
Class exprType = idx.getMapping().getJavaType();
boolean typeConsistent = false;
if (exprType == resultClass)
{
typeConsistent = true;
}
else if (exprType.isPrimitive())
{
Class resultClassPrimitive = ClassUtils.getPrimitiveTypeForType(resultClass);
if (resultClassPrimitive == exprType)
{
typeConsistent = true;
}
}
if (!typeConsistent)
{
// Inconsistent expression type not matching the result class type
throw new NucleusUserException(Localiser.msg("021202", resultClass.getName(), exprType));
}
}
else
{
// TODO Handle StatementClassMapping
// TODO Handle StatementNewObjectMapping
throw new NucleusUserException("Don't support result clause of " + result + " with resultClass of " + resultClass.getName());
}
}
else if (QueryUtils.resultClassIsUserType(resultClass.getName()))
{
// Check for valid constructor (either using param types, or using default ctr)
Class[] ctrTypes = new Class[resultMapping.getNumberOfResultExpressions()];
for (int i=0;i 0)
{
hasParams = true;
}
if (!statementReturnsEmpty && queryCacheKey != null && useCaching())
{
// TODO Allow caching of queries with subqueries
if (!datastoreCompilation.isPrecompilable() || (datastoreCompilation.getSQL().indexOf('?') < 0 && hasParams))
{
// Some parameters had their clauses evaluated during compilation so the query didn't gain any parameters, so don't cache it
NucleusLogger.QUERY.debug(Localiser.msg("021075"));
}
else
{
qm.addDatastoreQueryCompilation(datastoreKey, getLanguage(), queryCacheKey, datastoreCompilation);
}
}
}
}
}
/**
* Convenience accessor for the SQL to invoke in the datastore for this query.
* @return The SQL.
*/
public String getSQL()
{
if (datastoreCompilation != null)
{
return datastoreCompilation.getSQL();
}
return null;
}
protected Object performExecute(Map parameters)
{
if (statementReturnsEmpty)
{
return Collections.EMPTY_LIST;
}
boolean inMemory = evaluateInMemory();
if (candidateCollection != null)
{
// Supplied collection of instances, so evaluate in-memory
if (candidateCollection.isEmpty())
{
return Collections.EMPTY_LIST;
}
else if (inMemory)
{
return new JDOQLInMemoryEvaluator(this, new ArrayList(candidateCollection), compilation, parameters, clr).execute(true, true, true, true, true);
}
}
else if (type == QueryType.SELECT)
{
// Query results are cached, so return those
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy