public class QueryToSQLMapper extends org.datanucleus.query.expression.AbstractExpressionEvaluator implements QueryGenerator
Supports the following extensions currently :-
TODO This class currently takes in an SQLStatement and updates it with filter, result, from etc. If the input statement is a UNION of statements it tries to update the statement by applying the filter, result etc across all UNIONs. This can cause problems in some situations, particularly around looking up tables when using COMPLETE_TABLE inheritance, or with TYPE/instanceOf operations. We currently work around these by some fixes to getSQLTableMappingForPrimaryExpression, and by manually going around the UNIONs. What may be a better long term solution would be to run this class on each of the UNIONed statements separately and merge them.
| Modifier and Type | Field and Description |
|---|---|
static String |
MAP_KEY_ALIAS_SUFFIX |
static String |
MAP_VALUE_ALIAS_SUFFIX |
static String |
OPTION_BULK_UPDATE_VERSION |
static String |
OPTION_CASE_INSENSITIVE |
static String |
OPTION_EXPLICIT_JOINS |
static String |
OPTION_NON_DISTINCT_IMPLICIT_JOINS |
static String |
OPTION_NULL_PARAM_USE_IS_NULL |
static String |
OPTION_SELECT_CANDIDATE_ID_ONLY |
QueryToSQLMapper |
parentMapper
Parent mapper if we are processing a subquery.
|
protected boolean |
processingCase |
| Constructor and Description |
|---|
QueryToSQLMapper(SQLStatement stmt,
org.datanucleus.query.compiler.QueryCompilation compilation,
Map parameters,
StatementClassMapping resultDefForClass,
StatementResultMapping resultDef,
org.datanucleus.metadata.AbstractClassMetaData cmd,
boolean subclasses,
org.datanucleus.FetchPlan fetchPlan,
org.datanucleus.ExecutionContext ec,
org.datanucleus.util.Imports importsDefinition,
Set<String> options,
Map<String,Object> extensions)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
void |
bindParameter(String paramName,
Class type)
Method to bind the specified parameter to the defined type.
|
void |
bindVariable(String varName,
org.datanucleus.metadata.AbstractClassMetaData cmd,
SQLTable sqlTbl,
JavaTypeMapping mapping)
Method to bind the specified variable to the table and mapping.
|
SQLExpression |
bindVariable(UnboundExpression expr,
Class type)
Method to bind the specified unbound variable (as cross join) on the assumption that the type is a persistable class.
|
void |
compile()
Method to update the supplied SQLStatement with the components of the specified query.
|
protected void |
compileFilter()
Method to compile the WHERE clause of the query into the SQLStatement.
|
protected void |
compileFrom()
Method to compile the FROM clause of the query into the SQLStatement.
|
protected void |
compileFromClassExpression(org.datanucleus.query.expression.ClassExpression clsExpr)
Method to take a ClassExpression (in a FROM clause) and process the candidate and any
linked JoinExpression(s), adding joins to the SQLStatement as required.
|
protected void |
compileGrouping(SelectStatement stmt)
Method to compile the grouping clause of the query into the SQLStatement.
|
protected void |
compileHaving(SelectStatement stmt)
Method to compile the having clause of the query into the SQLStatement.
|
protected void |
compileOrdering(SelectStatement stmt)
Method to compile the ordering clause of the query into the SQLStatement.
|
protected void |
compileResult(SelectStatement stmt)
Method to compile the result clause of the query into the SQLStatement.
|
protected void |
compileUpdate(UpdateStatement stmt)
Method to compile the result clause of the query into the SQLStatement.
|
String |
getAliasForSQLTable(SQLTable tbl)
Returns the alias used for the specified SQLTable.
|
String |
getAliasForSQLTableMapping(org.datanucleus.store.rdbms.query.QueryToSQLMapper.SQLTableMapping tblMapping) |
protected BooleanExpression |
getBooleanExpressionForUseInFilter(BooleanExpression expr)
Convenience method to return a boolean expression suitable for using in a filter.
|
org.datanucleus.ClassLoaderResolver |
getClassLoaderResolver()
Accessor for the ClassLoader resolver to use when looking up classes.
|
org.datanucleus.query.compiler.CompilationComponent |
getCompilationComponent()
Accessor for the current query component being compiled.
|
org.datanucleus.ExecutionContext |
getExecutionContext()
Accessor for the ExecutionContext for this query.
|
protected SQLExpression |
getInvokedSqlExpressionForInvokeExpression(org.datanucleus.query.expression.InvokeExpression expr) |
Map<Integer,String> |
getParameterNameByPosition()
Convenience accessor for a map of the parameter name keyed by its position.
|
Object |
getProperty(String name)
Accessor for a property affecting the query compilation.
|
String |
getQueryLanguage()
Accessor for the query language that this query pertains to.
|
SQLJoin.JoinType |
getRequiredJoinTypeForAlias(String alias)
Convenience method to return the required join type for the specified alias.
|
protected SQLExpression |
getSQLLiteralForLiteralValue(Object litValue) |
protected org.datanucleus.store.rdbms.query.QueryToSQLMapper.SQLTableMapping |
getSQLTableMappingForAlias(String alias) |
protected StatementNewObjectMapping |
getStatementMappingForNewObjectExpression(NewObjectExpression expr,
SelectStatement stmt)
Convenience method to convert a NewObjectExpression into a StatementNewObjectMapping.
|
Class |
getTypeOfVariable(String varName)
Accessor for the type of a variable if already known (declared?).
|
Object |
getValueForExtension(String key)
Accessor for the value of the specified query extension (or null if not defined).
|
protected Object |
getValueForObjectField(Object obj,
String fieldName)
Convenience method to return the value of a field of the supplied object.
|
boolean |
hasExplicitJoins()
Accessor for whether the query has explicit variables.
|
boolean |
hasExtension(String key)
Accessor for whether the query being generated has the specified extension.
|
protected boolean |
hasSQLTableMappingForAlias(String alias) |
boolean |
isPrecompilable()
Accessor for whether the query is precompilable (doesn't need to know parameter values
to be able to compile it).
|
protected Object |
processAddExpression(org.datanucleus.query.expression.Expression expr) |
protected Object |
processAndExpression(org.datanucleus.query.expression.Expression expr) |
protected Object |
processBitAndExpression(org.datanucleus.query.expression.Expression expr) |
protected Object |
processBitOrExpression(org.datanucleus.query.expression.Expression expr) |
protected Object |
processBitXorExpression(org.datanucleus.query.expression.Expression expr) |
protected Object |
processCaseExpression(org.datanucleus.query.expression.CaseExpression expr) |
protected Object |
processCaseExpression(org.datanucleus.query.expression.CaseExpression expr,
SQLExpression typeExpr) |
protected Object |
processCastExpression(org.datanucleus.query.expression.Expression expr) |
protected Object |
processComExpression(org.datanucleus.query.expression.Expression expr) |
protected Object |
processCreatorExpression(org.datanucleus.query.expression.CreatorExpression expr) |
protected Object |
processDistinctExpression(org.datanucleus.query.expression.Expression expr) |
protected Object |
processDivExpression(org.datanucleus.query.expression.Expression expr) |
protected Object |
processEqExpression(org.datanucleus.query.expression.Expression expr) |
protected void |
processFromClauseSubquery(org.datanucleus.query.expression.ClassExpression clsExpr,
SQLTable candSqlTbl,
org.datanucleus.metadata.MetaDataManager mmgr)
Method to process a ClassExpression where it represents a subquery.
|
protected Object |
processGteqExpression(org.datanucleus.query.expression.Expression expr) |
protected Object |
processGtExpression(org.datanucleus.query.expression.Expression expr) |
protected Object |
processInExpression(org.datanucleus.query.expression.Expression expr) |
boolean |
processingOnClause() |
protected Object |
processInvokeExpression(org.datanucleus.query.expression.InvokeExpression expr) |
protected SQLExpression |
processInvokeExpression(org.datanucleus.query.expression.InvokeExpression expr,
SQLExpression invokedSqlExpr)
Internal method to handle the processing of an InvokeExpression.
|
protected Object |
processIsExpression(org.datanucleus.query.expression.Expression expr) |
protected Object |
processIsnotExpression(org.datanucleus.query.expression.Expression expr) |
protected Object |
processLikeExpression(org.datanucleus.query.expression.Expression expr) |
protected Object |
processLiteral(org.datanucleus.query.expression.Literal expr) |
protected Object |
processLteqExpression(org.datanucleus.query.expression.Expression expr) |
protected Object |
processLtExpression(org.datanucleus.query.expression.Expression expr) |
protected Object |
processModExpression(org.datanucleus.query.expression.Expression expr) |
protected Object |
processMulExpression(org.datanucleus.query.expression.Expression expr) |
protected Object |
processNegExpression(org.datanucleus.query.expression.Expression expr) |
protected Object |
processNoteqExpression(org.datanucleus.query.expression.Expression expr) |
protected Object |
processNotExpression(org.datanucleus.query.expression.Expression expr) |
protected Object |
processNotInExpression(org.datanucleus.query.expression.Expression expr) |
protected Object |
processOrExpression(org.datanucleus.query.expression.Expression expr) |
protected Object |
processParameterExpression(org.datanucleus.query.expression.ParameterExpression expr) |
protected Object |
processParameterExpression(org.datanucleus.query.expression.ParameterExpression expr,
boolean asLiteral)
Method to process a parameter expression.
|
protected Object |
processPrimaryExpression(org.datanucleus.query.expression.PrimaryExpression expr) |
protected Object |
processSubExpression(org.datanucleus.query.expression.Expression expr) |
protected Object |
processSubqueryExpression(org.datanucleus.query.expression.SubqueryExpression expr) |
protected SQLExpression |
processUnboundExpression(UnboundExpression expr) |
protected Object |
processVariableExpression(org.datanucleus.query.expression.VariableExpression expr) |
protected SQLExpression |
replaceParameterLiteral(ParameterLiteral paramLit,
JavaTypeMapping mapping)
Convenience method to return a parameter-based literal using the supplied mapping to replace
the provided ParameterLiteral (generated before its type was known).
|
Class |
resolveClass(String className)
Convenience method to resolve a class name.
|
protected void |
selectFetchPlanForCandidate(SelectStatement stmt,
StatementClassMapping resultClassMapping,
int maxFetchDepth) |
protected void |
setNotPrecompilable() |
protected void |
setSQLTableMappingForAlias(String alias,
org.datanucleus.store.rdbms.query.QueryToSQLMapper.SQLTableMapping mapping) |
void |
useParameterExpressionAsLiteral(SQLLiteral paramLiteral)
Method to instruct the generator to convert the provided parameter expression to just be
a literal using the value of the parameter (hence the statement cannot be precompilable since
the value needs to be known).
|
protected void |
validateExpressionForResult(SQLExpression sqlExpr)
Method that validates that the specified expression is valid for use in a result clause.
|
public static final String OPTION_CASE_INSENSITIVE
public static final String OPTION_EXPLICIT_JOINS
public static final String OPTION_NON_DISTINCT_IMPLICIT_JOINS
public static final String OPTION_BULK_UPDATE_VERSION
public static final String OPTION_SELECT_CANDIDATE_ID_ONLY
public static final String OPTION_NULL_PARAM_USE_IS_NULL
public static final String MAP_KEY_ALIAS_SUFFIX
public static final String MAP_VALUE_ALIAS_SUFFIX
public QueryToSQLMapper parentMapper
protected boolean processingCase
public QueryToSQLMapper(SQLStatement stmt, org.datanucleus.query.compiler.QueryCompilation compilation, Map parameters, StatementClassMapping resultDefForClass, StatementResultMapping resultDef, org.datanucleus.metadata.AbstractClassMetaData cmd, boolean subclasses, org.datanucleus.FetchPlan fetchPlan, org.datanucleus.ExecutionContext ec, org.datanucleus.util.Imports importsDefinition, Set<String> options, Map<String,Object> extensions)
stmt - SQL Statement to update with the query contents.compilation - The generic query compilationparameters - Parameters neededresultDefForClass - Definition of results for the statement (populated here)resultDef - Definition of results if we have a result clause (populated here)cmd - Metadata for the candidate classsubclasses - Whether to include subclassesfetchPlan - Fetch Plan to applyec - execution contextimportsDefinition - Import definition to use in class lookups (optional)options - Options controlling behaviourextensions - Any query extensions that may define compilation behaviourpublic String getQueryLanguage()
getQueryLanguage in interface QueryGeneratorpublic org.datanucleus.ClassLoaderResolver getClassLoaderResolver()
QueryGeneratorgetClassLoaderResolver in interface QueryGeneratorpublic org.datanucleus.query.compiler.CompilationComponent getCompilationComponent()
QueryGeneratorgetCompilationComponent in interface QueryGeneratorpublic org.datanucleus.ExecutionContext getExecutionContext()
QueryGeneratorgetExecutionContext in interface QueryGeneratorpublic Object getProperty(String name)
QueryGeneratorgetProperty in interface QueryGeneratorname - The property namepublic boolean isPrecompilable()
protected void setNotPrecompilable()
public Map<Integer,String> getParameterNameByPosition()
compileis called.
public void compile()
protected void compileFrom()
protected void compileFilter()
protected void compileResult(SelectStatement stmt)
stmt - SELECT statementprotected void selectFetchPlanForCandidate(SelectStatement stmt, StatementClassMapping resultClassMapping, int maxFetchDepth)
protected StatementNewObjectMapping getStatementMappingForNewObjectExpression(NewObjectExpression expr, SelectStatement stmt)
expr - The NewObjectExpressionstmt - SelectStatementprotected void compileUpdate(UpdateStatement stmt)
stmt - UPDATE statementprotected void validateExpressionForResult(SQLExpression sqlExpr)
sqlExpr - The SQLExpressionprotected void compileGrouping(SelectStatement stmt)
stmt - SELECT statementprotected void compileHaving(SelectStatement stmt)
stmt - SELECT statementprotected void compileOrdering(SelectStatement stmt)
stmt - SELECT statementprotected void compileFromClassExpression(org.datanucleus.query.expression.ClassExpression clsExpr)
clsExpr - The ClassExpressionpublic boolean processingOnClause()
processingOnClause in interface QueryGeneratorprotected void processFromClauseSubquery(org.datanucleus.query.expression.ClassExpression clsExpr,
SQLTable candSqlTbl,
org.datanucleus.metadata.MetaDataManager mmgr)
clsExpr - The ClassExpressioncandSqlTbl - Candidate SQL Tablemmgr - MetaData Managerprotected Object processAndExpression(org.datanucleus.query.expression.Expression expr)
processAndExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processOrExpression(org.datanucleus.query.expression.Expression expr)
processOrExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processBitAndExpression(org.datanucleus.query.expression.Expression expr)
processBitAndExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processBitOrExpression(org.datanucleus.query.expression.Expression expr)
processBitOrExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processBitXorExpression(org.datanucleus.query.expression.Expression expr)
processBitXorExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processEqExpression(org.datanucleus.query.expression.Expression expr)
processEqExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processNoteqExpression(org.datanucleus.query.expression.Expression expr)
processNoteqExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processGteqExpression(org.datanucleus.query.expression.Expression expr)
processGteqExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processGtExpression(org.datanucleus.query.expression.Expression expr)
processGtExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processLteqExpression(org.datanucleus.query.expression.Expression expr)
processLteqExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processLtExpression(org.datanucleus.query.expression.Expression expr)
processLtExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processLiteral(org.datanucleus.query.expression.Literal expr)
processLiteral in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected SQLExpression getSQLLiteralForLiteralValue(Object litValue)
protected Object processPrimaryExpression(org.datanucleus.query.expression.PrimaryExpression expr)
processPrimaryExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processParameterExpression(org.datanucleus.query.expression.ParameterExpression expr)
processParameterExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processParameterExpression(org.datanucleus.query.expression.ParameterExpression expr, boolean asLiteral)
expr - The ParameterExpressionasLiteral - Whether to create a SQLLiteral rather than a parameter literalprotected SQLExpression getInvokedSqlExpressionForInvokeExpression(org.datanucleus.query.expression.InvokeExpression expr)
protected Object processInvokeExpression(org.datanucleus.query.expression.InvokeExpression expr)
processInvokeExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected SQLExpression processInvokeExpression(org.datanucleus.query.expression.InvokeExpression expr, SQLExpression invokedSqlExpr)
expr - The InvokeExpressioninvokedSqlExpr - The SQLExpression that we are invoking the method on.protected Object processSubqueryExpression(org.datanucleus.query.expression.SubqueryExpression expr)
processSubqueryExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processAddExpression(org.datanucleus.query.expression.Expression expr)
processAddExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processDivExpression(org.datanucleus.query.expression.Expression expr)
processDivExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processMulExpression(org.datanucleus.query.expression.Expression expr)
processMulExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processSubExpression(org.datanucleus.query.expression.Expression expr)
processSubExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processDistinctExpression(org.datanucleus.query.expression.Expression expr)
processDistinctExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processComExpression(org.datanucleus.query.expression.Expression expr)
processComExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processModExpression(org.datanucleus.query.expression.Expression expr)
processModExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processNegExpression(org.datanucleus.query.expression.Expression expr)
processNegExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processNotExpression(org.datanucleus.query.expression.Expression expr)
processNotExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processCastExpression(org.datanucleus.query.expression.Expression expr)
processCastExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processCaseExpression(org.datanucleus.query.expression.CaseExpression expr)
processCaseExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processCaseExpression(org.datanucleus.query.expression.CaseExpression expr, SQLExpression typeExpr)
protected Object processIsExpression(org.datanucleus.query.expression.Expression expr)
processIsExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processIsnotExpression(org.datanucleus.query.expression.Expression expr)
processIsnotExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processInExpression(org.datanucleus.query.expression.Expression expr)
processInExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processNotInExpression(org.datanucleus.query.expression.Expression expr)
processNotInExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processCreatorExpression(org.datanucleus.query.expression.CreatorExpression expr)
processCreatorExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processLikeExpression(org.datanucleus.query.expression.Expression expr)
processLikeExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected Object processVariableExpression(org.datanucleus.query.expression.VariableExpression expr)
processVariableExpression in class org.datanucleus.query.expression.AbstractExpressionEvaluatorprotected SQLExpression processUnboundExpression(UnboundExpression expr)
protected SQLExpression replaceParameterLiteral(ParameterLiteral paramLit, JavaTypeMapping mapping)
paramLit - The parameter literalmapping - Mapping to usepublic void useParameterExpressionAsLiteral(SQLLiteral paramLiteral)
QueryGeneratoruseParameterExpressionAsLiteral in interface QueryGeneratorparamLiteral - The parameter expressionpublic boolean hasExtension(String key)
QueryGeneratorhasExtension in interface QueryGeneratorkey - Extension namepublic Object getValueForExtension(String key)
QueryGeneratorgetValueForExtension in interface QueryGeneratorkey - Extension namepublic SQLJoin.JoinType getRequiredJoinTypeForAlias(String alias)
alias - The aliasprotected Object getValueForObjectField(Object obj, String fieldName)
obj - The objectfieldName - The field nameprotected org.datanucleus.store.rdbms.query.QueryToSQLMapper.SQLTableMapping getSQLTableMappingForAlias(String alias)
public String getAliasForSQLTableMapping(org.datanucleus.store.rdbms.query.QueryToSQLMapper.SQLTableMapping tblMapping)
public String getAliasForSQLTable(SQLTable tbl)
tbl - The tableprotected void setSQLTableMappingForAlias(String alias, org.datanucleus.store.rdbms.query.QueryToSQLMapper.SQLTableMapping mapping)
protected boolean hasSQLTableMappingForAlias(String alias)
public void bindVariable(String varName, org.datanucleus.metadata.AbstractClassMetaData cmd, SQLTable sqlTbl, JavaTypeMapping mapping)
bindVariable in interface QueryGeneratorvarName - Variable namecmd - Metadata for this variable typesqlTbl - Table for this variablemapping - The mapping of this variable in the tablepublic SQLExpression bindVariable(UnboundExpression expr, Class type)
bindVariable in interface QueryGeneratorexpr - Unbound expressiontype - The type to bind aspublic void bindParameter(String paramName, Class type)
bindParameter in interface QueryGeneratorparamName - Name of the parametertype - The type (or subclass)public Class getTypeOfVariable(String varName)
getTypeOfVariable in interface QueryGeneratorvarName - Name of the variablepublic boolean hasExplicitJoins()
hasExplicitJoins in interface QueryGeneratorprotected BooleanExpression getBooleanExpressionForUseInFilter(BooleanExpression expr)
myBoolField. In that case we return a boolean like
myBoolField == TRUE.
expr - The expression to checkpublic Class resolveClass(String className)
resolveClass in interface QueryGeneratorclassName - The class nameCopyright © 2019. All rights reserved.