use of org.datanucleus.store.query.compiler.QueryCompilation in project datanucleus-rdbms by datanucleus.
the class QueryToSQLMapper method processVariableExpression.
/* (non-Javadoc)
* @see org.datanucleus.query.evaluator.AbstractExpressionEvaluator#processVariableExpression(org.datanucleus.query.expression.VariableExpression)
*/
@Override
protected Object processVariableExpression(VariableExpression expr) {
String varName = expr.getId();
Symbol varSym = expr.getSymbol();
if (varSym != null) {
// Use name from symbol if possible
varName = varSym.getQualifiedName();
}
if (hasSQLTableMappingForAlias(varName)) {
// Variable already found
SQLTableMapping tblMapping = getSQLTableMappingForAlias(varName);
SQLExpression sqlExpr = exprFactory.newExpression(tblMapping.table.getSQLStatement(), tblMapping.table, tblMapping.mapping);
stack.push(sqlExpr);
return sqlExpr;
} else if (compilation.getCompilationForSubquery(varName) != null) {
// Subquery variable
QueryCompilation subCompilation = compilation.getCompilationForSubquery(varName);
AbstractClassMetaData subCmd = ec.getMetaDataManager().getMetaDataForClass(subCompilation.getCandidateClass(), ec.getClassLoaderResolver());
// Create subquery statement, using any provided alias if possible
String subAlias = null;
if (subCompilation.getCandidateAlias() != null && !subCompilation.getCandidateAlias().equals(candidateAlias)) {
subAlias = subCompilation.getCandidateAlias();
}
StatementResultMapping subqueryResultMapping = new StatementResultMapping();
// TODO Fix "avg(something)" arg - not essential but is a hack right now
SQLStatement subStmt = RDBMSQueryUtils.getStatementForCandidates(storeMgr, stmt, subCmd, null, ec, subCompilation.getCandidateClass(), true, "avg(something)", subAlias, null, null);
QueryToSQLMapper sqlMapper = new QueryToSQLMapper(subStmt, subCompilation, parameters, null, subqueryResultMapping, subCmd, true, fetchPlan, ec, importsDefinition, options, extensionsByName);
sqlMapper.setDefaultJoinType(defaultJoinType);
sqlMapper.setDefaultJoinTypeFilter(defaultJoinTypeFilter);
sqlMapper.setParentMapper(this);
sqlMapper.compile();
if (subqueryResultMapping.getNumberOfResultExpressions() > 1) {
throw new NucleusUserException("Number of result expressions in subquery should be 1");
}
SQLExpression subExpr = null;
// TODO Cater for subquery select of its own candidate
if (subqueryResultMapping.getNumberOfResultExpressions() == 0) {
subExpr = new org.datanucleus.store.rdbms.sql.expression.SubqueryExpression(stmt, subStmt);
} else {
JavaTypeMapping subMapping = ((StatementMappingIndex) subqueryResultMapping.getMappingForResultExpression(0)).getMapping();
if (subMapping instanceof TemporalMapping) {
subExpr = new TemporalSubqueryExpression(stmt, subStmt);
} else if (subMapping instanceof StringMapping) {
subExpr = new StringSubqueryExpression(stmt, subStmt);
} else {
subExpr = new NumericSubqueryExpression(stmt, subStmt);
}
if (subExpr.getJavaTypeMapping() == null) {
subExpr.setJavaTypeMapping(subMapping);
}
}
stack.push(subExpr);
return subExpr;
} else if (stmt.getParentStatement() != null && parentMapper != null && parentMapper.candidateAlias != null && parentMapper.candidateAlias.equals(varName)) {
// Variable in subquery linking back to parent query
SQLExpression varExpr = exprFactory.newExpression(stmt.getParentStatement(), stmt.getParentStatement().getPrimaryTable(), stmt.getParentStatement().getPrimaryTable().getTable().getIdMapping());
stack.push(varExpr);
return varExpr;
} else {
// Variable never met before, so return as UnboundExpression - process later if needing binding
NucleusLogger.QUERY.debug("QueryToSQL.processVariable (unbound) variable=" + varName + " is not yet bound so returning UnboundExpression");
UnboundExpression unbExpr = new UnboundExpression(stmt, varName);
stack.push(unbExpr);
return unbExpr;
}
}
Aggregations