use of org.datanucleus.query.compiler.QueryCompilation in project datanucleus-core by datanucleus.
the class Query method applyImplicitParameterValueToSubqueries.
protected boolean applyImplicitParameterValueToSubqueries(String name, Object value, QueryCompilation comp) {
boolean symbolFound = false;
Symbol sym = null;
// Apply to any subqueries
String[] subqueryNames = comp.getSubqueryAliases();
if (subqueryNames != null) {
for (int i = 0; i < subqueryNames.length; i++) {
QueryCompilation subCompilation = comp.getCompilationForSubquery(subqueryNames[i]);
sym = subCompilation.getSymbolTable().getSymbol(name);
if (sym != null) {
symbolFound = true;
if (sym.getValueType() == null && value != null) {
// Update the compilation providing the type of this parameter
sym.setValueType(value.getClass());
} else if (sym.getValueType() != null && value != null) {
if (!QueryUtils.queryParameterTypesAreCompatible(sym.getValueType(), value.getClass())) {
// Parameter value supplied is not consistent with what the query compilation expects
throw new QueryInvalidParametersException("Parameter " + name + " needs to be assignable from " + sym.getValueType().getName() + " yet the value is of type " + value.getClass().getName());
}
}
}
boolean subSymbolFound = applyImplicitParameterValueToSubqueries(name, value, subCompilation);
if (subSymbolFound) {
symbolFound = true;
}
}
}
return symbolFound;
}
use of org.datanucleus.query.compiler.QueryCompilation in project datanucleus-core by datanucleus.
the class AbstractJDOQLQuery method compileGeneric.
/* (non-Javadoc)
* @see org.datanucleus.store.query.AbstractJavaQuery#compileGeneric(java.util.Map)
*/
@Override
public void compileGeneric(Map parameterValues) {
if (compilation != null) {
return;
}
QueryManager queryMgr = getQueryManager();
String queryCacheKey = getQueryCacheKey();
if (useCaching() && queryCacheKey != null) {
QueryCompilation cachedCompilation = queryMgr.getQueryCompilationForQuery(getLanguage(), queryCacheKey);
if (cachedCompilation != null) {
compilation = cachedCompilation;
checkParameterTypesAgainstCompilation(parameterValues);
return;
}
}
// Resolve resultClass name if defined
if (resultClassName != null) {
// Throws NucleusUserException if not resolvable
resultClass = resolveClassDeclaration(resultClassName);
resultClassName = null;
}
long startTime = 0;
if (NucleusLogger.QUERY.isDebugEnabled()) {
startTime = System.currentTimeMillis();
NucleusLogger.QUERY.debug(Localiser.msg("021044", getLanguage(), getSingleStringQuery()));
}
JDOQLCompiler compiler = new JDOQLCompiler(ec.getNucleusContext(), ec.getClassLoaderResolver(), from, candidateClass, candidateCollection, this.filter, getParsedImports(), this.ordering, this.result, this.grouping, this.having, explicitParameters, explicitVariables, this.update);
if (getBooleanExtensionProperty(PropertyNames.PROPERTY_QUERY_COMPILE_OPTIMISE_VAR_THIS, false)) {
compiler.setOption(PropertyNames.PROPERTY_QUERY_COMPILE_OPTIMISE_VAR_THIS, true);
}
if (getBooleanExtensionProperty(EXTENSION_JDOQL_STRICT, false)) {
compiler.setOption(EXTENSION_JDOQL_STRICT, "true");
}
boolean allowAllSyntax = ec.getNucleusContext().getConfiguration().getBooleanProperty(PropertyNames.PROPERTY_QUERY_JDOQL_ALLOWALL);
if (ec.getBooleanProperty(PropertyNames.PROPERTY_QUERY_JDOQL_ALLOWALL) != null) {
allowAllSyntax = ec.getBooleanProperty(PropertyNames.PROPERTY_QUERY_JDOQL_ALLOWALL);
}
compiler.setAllowAll(allowAllSyntax);
compilation = compiler.compile(parameterValues, subqueries);
if (QueryUtils.queryReturnsSingleRow(this)) {
compilation.setReturnsSingleRow();
}
if (resultDistinct) {
compilation.setResultDistinct();
}
if (NucleusLogger.QUERY.isDebugEnabled()) {
NucleusLogger.QUERY.debug(Localiser.msg("021045", getLanguage(), "" + (System.currentTimeMillis() - startTime)));
}
if (subqueries != null) {
// Compile any subqueries
compileSubqueries(subqueries, compilation, compiler, parameterValues);
}
if (NucleusLogger.QUERY.isDebugEnabled()) {
// Log the query compilation
NucleusLogger.QUERY.debug(compilation.toString());
}
checkParameterTypesAgainstCompilation(parameterValues);
if (useCaching() && queryCacheKey != null) {
// Cache for future reference
queryMgr.addQueryCompilation(getLanguage(), queryCacheKey, compilation);
}
}
use of org.datanucleus.query.compiler.QueryCompilation in project datanucleus-core by datanucleus.
the class AbstractJDOQLQuery method compileSubqueries.
/**
* Recursively compile the subqueries
* @param subqueryMap The subquery definition map
* @param parentCompilation Parent compilation
* @param parentCompiler The parent compiler
* @param parameterValues The parameters map
*/
protected void compileSubqueries(Map<String, SubqueryDefinition> subqueryMap, QueryCompilation parentCompilation, JavaQueryCompiler parentCompiler, Map parameterValues) {
long startTime = System.currentTimeMillis();
Iterator<Map.Entry<String, SubqueryDefinition>> iter = subqueryMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, SubqueryDefinition> entry = iter.next();
SubqueryDefinition subqueryDefinition = entry.getValue();
Query subquery = subqueryDefinition.getQuery();
if (NucleusLogger.QUERY.isDebugEnabled()) {
startTime = System.currentTimeMillis();
NucleusLogger.QUERY.debug(Localiser.msg("021044", getLanguage(), ((AbstractJDOQLQuery) subquery).getSingleStringQuery()));
}
JDOQLCompiler subCompiler = new JDOQLCompiler(ec.getNucleusContext(), ec.getClassLoaderResolver(), subquery.from, subquery.candidateClass, null, subquery.filter, getParsedImports(), subquery.ordering, subquery.result, subquery.grouping, subquery.having, subquery.explicitParameters, null, null);
if (getBooleanExtensionProperty(EXTENSION_JDOQL_STRICT, false)) {
subCompiler.setOption(EXTENSION_JDOQL_STRICT, "true");
}
boolean allowAllSyntax = ec.getNucleusContext().getConfiguration().getBooleanProperty(PropertyNames.PROPERTY_QUERY_JDOQL_ALLOWALL);
if (ec.getBooleanProperty(PropertyNames.PROPERTY_QUERY_JDOQL_ALLOWALL) != null) {
allowAllSyntax = ec.getBooleanProperty(PropertyNames.PROPERTY_QUERY_JDOQL_ALLOWALL);
}
subCompiler.setAllowAll(allowAllSyntax);
subCompiler.setLinkToParentQuery(parentCompiler, subqueryDefinition.getParameterMap());
QueryCompilation subqueryCompilation = subCompiler.compile(parameterValues, null);
if (QueryUtils.queryReturnsSingleRow(subquery)) {
subqueryCompilation.setReturnsSingleRow();
}
parentCompilation.addSubqueryCompilation(entry.getKey(), subqueryCompilation);
if (NucleusLogger.QUERY.isDebugEnabled()) {
NucleusLogger.QUERY.debug(Localiser.msg("021045", getLanguage(), "" + (System.currentTimeMillis() - startTime)));
}
if (subquery.subqueries != null) {
// Recurse to nested subqueries
compileSubqueries(subquery.subqueries, subqueryCompilation, subCompiler, parameterValues);
}
}
}
use of org.datanucleus.query.compiler.QueryCompilation in project datanucleus-core by datanucleus.
the class AbstractJPQLQuery method compileGeneric.
/* (non-Javadoc)
* @see org.datanucleus.store.query.AbstractJavaQuery#compileGeneric(java.util.Map)
*/
@Override
public void compileGeneric(Map parameterValues) {
if (compilation != null) {
return;
}
QueryManager queryMgr = getQueryManager();
String queryCacheKey = getQueryCacheKey();
if (useCaching() && queryCacheKey != null) {
QueryCompilation cachedCompilation = queryMgr.getQueryCompilationForQuery(getLanguage(), queryCacheKey);
if (cachedCompilation != null) {
compilation = cachedCompilation;
if (compilation.getExprResult() == null) {
// If the result was "Object(e)" or "e" then this is meaningless so remove
result = null;
}
checkParameterTypesAgainstCompilation(parameterValues);
return;
}
}
long startTime = 0;
if (NucleusLogger.QUERY.isDebugEnabled()) {
startTime = System.currentTimeMillis();
NucleusLogger.QUERY.debug(Localiser.msg("021044", getLanguage(), getSingleStringQuery()));
}
String from = this.from;
if (type == QueryType.BULK_INSERT) {
// Append artificial alias so the compilation passes
from += " this";
}
JavaQueryCompiler compiler = new JPQLCompiler(ec.getNucleusContext(), ec.getClassLoaderResolver(), from, candidateClass, candidateCollection, this.filter, getParsedImports(), this.ordering, this.result, this.grouping, this.having, explicitParameters, update);
if (getBooleanExtensionProperty(EXTENSION_JPQL_STRICT, false)) {
compiler.setOption(EXTENSION_JPQL_STRICT, "true");
}
compilation = compiler.compile(parameterValues, subqueries);
if (QueryUtils.queryReturnsSingleRow(this)) {
compilation.setReturnsSingleRow();
}
if (resultDistinct) {
compilation.setResultDistinct();
}
if (compilation.getExprResult() == null) {
// If the result was "Object(e)" or "e" then this is meaningless so remove
result = null;
}
if (NucleusLogger.QUERY.isDebugEnabled()) {
NucleusLogger.QUERY.debug(Localiser.msg("021045", getLanguage(), "" + (System.currentTimeMillis() - startTime)));
}
if (subqueries != null) {
// Compile any subqueries
compileSubqueries(subqueries, compilation, compiler, parameterValues);
}
if (NucleusLogger.QUERY.isDebugEnabled()) {
// Log the query compilation
NucleusLogger.QUERY.debug(compilation.toString());
}
if (implicitParameters != null) {
// Make sure any implicit parameters have their values in the compilation
Iterator paramKeyIter = implicitParameters.keySet().iterator();
while (paramKeyIter.hasNext()) {
Object paramKey = paramKeyIter.next();
String paramName = "" + paramKey;
applyImplicitParameterValueToCompilation(paramName, implicitParameters.get(paramName));
}
}
checkParameterTypesAgainstCompilation(parameterValues);
if (useCaching() && queryCacheKey != null) {
// Cache for future reference
queryMgr.addQueryCompilation(getLanguage(), queryCacheKey, compilation);
}
}
use of org.datanucleus.query.compiler.QueryCompilation in project datanucleus-core by datanucleus.
the class QueryManagerImpl method getQueryCompilationForQuery.
/* (non-Javadoc)
* @see org.datanucleus.store.query.QueryManager#getQueryCompilationForQuery(java.lang.String, java.lang.String)
*/
@Override
public QueryCompilation getQueryCompilationForQuery(String language, String query) {
if (queryCompilationCache != null) {
String queryKey = language + ":" + query;
QueryCompilation compilation = queryCompilationCache.get(queryKey);
if (compilation != null) {
if (NucleusLogger.QUERY.isDebugEnabled()) {
NucleusLogger.QUERY.debug(Localiser.msg("021079", query, language));
}
return compilation;
}
}
return null;
}
Aggregations