use of org.datanucleus.query.expression.Expression in project datanucleus-rdbms by datanucleus.
the class QueryToSQLMapper method processCaseExpression.
protected Object processCaseExpression(CaseExpression expr, SQLExpression typeExpr) {
processingCase = true;
try {
List<ExpressionPair> conditions = expr.getConditions();
Iterator<ExpressionPair> whenExprIter = conditions.iterator();
SQLExpression[] whenSqlExprs = new SQLExpression[conditions.size()];
SQLExpression[] actionSqlExprs = new SQLExpression[conditions.size()];
boolean numericCase = false;
boolean booleanCase = false;
boolean stringCase = false;
boolean typeSet = false;
if (typeExpr != null) {
if (typeExpr instanceof NumericExpression) {
numericCase = true;
typeSet = true;
} else if (typeExpr instanceof BooleanExpression) {
booleanCase = true;
typeSet = true;
} else if (typeExpr instanceof StringExpression) {
stringCase = true;
typeSet = true;
}
}
int i = 0;
while (whenExprIter.hasNext()) {
ExpressionPair pair = whenExprIter.next();
Expression whenExpr = pair.getWhenExpression();
whenExpr.evaluate(this);
whenSqlExprs[i] = stack.pop();
if (!(whenSqlExprs[i] instanceof BooleanExpression)) {
throw new QueryCompilerSyntaxException("IF/ELSE conditional expression should return boolean but doesn't : " + expr);
}
Expression actionExpr = pair.getActionExpression();
actionExpr.evaluate(this);
actionSqlExprs[i] = stack.pop();
if (!typeSet) {
if (actionSqlExprs[i] instanceof NumericExpression) {
numericCase = true;
typeSet = true;
} else if (actionSqlExprs[i] instanceof BooleanExpression) {
booleanCase = true;
typeSet = true;
} else if (actionSqlExprs[i] instanceof StringExpression) {
stringCase = true;
typeSet = true;
}
}
i++;
}
Expression elseExpr = expr.getElseExpression();
elseExpr.evaluate(this);
SQLExpression elseActionSqlExpr = stack.pop();
// Check that all action sql expressions are consistent
for (int j = 1; j < actionSqlExprs.length; j++) {
if (!checkCaseExpressionsConsistent(actionSqlExprs[0], actionSqlExprs[j])) {
throw new QueryCompilerSyntaxException("IF/ELSE action expression " + actionSqlExprs[j] + " is of different type to first action " + actionSqlExprs[0] + " - must be consistent");
}
}
if (!checkCaseExpressionsConsistent(actionSqlExprs[0], elseActionSqlExpr)) {
throw new QueryCompilerSyntaxException("IF/ELSE action expression " + elseActionSqlExpr + " is of different type to first action " + actionSqlExprs[0] + " - must be consistent");
}
SQLExpression caseSqlExpr = null;
if (numericCase) {
caseSqlExpr = new org.datanucleus.store.rdbms.sql.expression.CaseNumericExpression(whenSqlExprs, actionSqlExprs, elseActionSqlExpr);
} else if (booleanCase) {
caseSqlExpr = new org.datanucleus.store.rdbms.sql.expression.CaseBooleanExpression(whenSqlExprs, actionSqlExprs, elseActionSqlExpr);
} else if (stringCase) {
caseSqlExpr = new org.datanucleus.store.rdbms.sql.expression.CaseStringExpression(whenSqlExprs, actionSqlExprs, elseActionSqlExpr);
} else {
caseSqlExpr = new org.datanucleus.store.rdbms.sql.expression.CaseExpression(whenSqlExprs, actionSqlExprs, elseActionSqlExpr);
}
stack.push(caseSqlExpr);
return caseSqlExpr;
} finally {
processingCase = false;
}
}
use of org.datanucleus.query.expression.Expression in project datanucleus-rdbms by datanucleus.
the class QueryToSQLMapper method compileFrom.
/**
* Method to compile the FROM clause of the query into the SQLStatement.
*/
protected void compileFrom() {
if (compilation.getExprFrom() != null) {
// Process all ClassExpression(s) in the FROM, adding joins to the statement as required
compileComponent = CompilationComponent.FROM;
Expression[] fromExprs = compilation.getExprFrom();
for (Expression fromExpr : fromExprs) {
compileFromClassExpression((ClassExpression) fromExpr);
}
compileComponent = null;
}
}
use of org.datanucleus.query.expression.Expression in project datanucleus-rdbms by datanucleus.
the class QueryToSQLMapper method getInvokedSqlExpressionForInvokeExpression.
protected SQLExpression getInvokedSqlExpressionForInvokeExpression(InvokeExpression expr) {
Expression invokedExpr = expr.getLeft();
SQLExpression invokedSqlExpr = null;
if (invokedExpr == null) {
// Static method
} else if (invokedExpr instanceof PrimaryExpression) {
processPrimaryExpression((PrimaryExpression) invokedExpr);
invokedSqlExpr = stack.pop();
} else if (invokedExpr instanceof Literal) {
processLiteral((Literal) invokedExpr);
invokedSqlExpr = stack.pop();
} else if (invokedExpr instanceof ParameterExpression) {
// TODO May be needed to set the second parameter to "false" here and then if the method
// being invoked needs the parameters as a normal SQLLiteral then allow it to convert it itself
processParameterExpression((ParameterExpression) invokedExpr, true);
invokedSqlExpr = stack.pop();
} else if (invokedExpr instanceof InvokeExpression) {
processInvokeExpression((InvokeExpression) invokedExpr);
invokedSqlExpr = stack.pop();
} else if (invokedExpr instanceof VariableExpression) {
processVariableExpression((VariableExpression) invokedExpr);
invokedSqlExpr = stack.pop();
} else if (invokedExpr instanceof ArrayExpression) {
ArrayExpression arrExpr = (ArrayExpression) invokedExpr;
SQLExpression[] arrSqlExprs = new SQLExpression[arrExpr.getArraySize()];
for (int i = 0; i < arrExpr.getArraySize(); i++) {
Expression arrElemExpr = arrExpr.getElement(i);
arrElemExpr.evaluate(this);
arrSqlExprs[i] = stack.pop();
}
JavaTypeMapping m = exprFactory.getMappingForType(Object[].class, false);
invokedSqlExpr = new org.datanucleus.store.rdbms.sql.expression.ArrayExpression(stmt, m, arrSqlExprs);
} else if (invokedExpr instanceof DyadicExpression) {
DyadicExpression dyExpr = (DyadicExpression) invokedExpr;
dyExpr.evaluate(this);
invokedSqlExpr = stack.pop();
} else {
throw new NucleusException("Dont currently support invoke expression " + invokedExpr);
}
return invokedSqlExpr;
}
use of org.datanucleus.query.expression.Expression in project datanucleus-rdbms by datanucleus.
the class QueryToSQLMapper method compileOrdering.
/**
* Method to compile the ordering clause of the query into the SQLStatement.
* @param stmt SELECT statement
*/
protected void compileOrdering(SelectStatement stmt) {
if (compilation.getExprOrdering() != null) {
compileComponent = CompilationComponent.ORDERING;
Expression[] orderingExpr = compilation.getExprOrdering();
SQLExpression[] orderSqlExprs = new SQLExpression[orderingExpr.length];
boolean[] directions = new boolean[orderingExpr.length];
NullOrderingType[] nullOrders = new NullOrderingType[orderingExpr.length];
for (int i = 0; i < orderingExpr.length; i++) {
OrderExpression orderExpr = (OrderExpression) orderingExpr[i];
Expression expr = orderExpr.getLeft();
if (expr instanceof PrimaryExpression) {
PrimaryExpression orderPrimExpr = (PrimaryExpression) expr;
if (orderPrimExpr.getTuples().size() == 1 && resultAliases != null) {
if (resultAliases.contains(orderPrimExpr.getId().toLowerCase())) {
// Order by a result alias
orderSqlExprs[i] = new ResultAliasExpression(stmt, orderPrimExpr.getId());
}
}
}
if (orderSqlExprs[i] == null) {
orderSqlExprs[i] = (SQLExpression) orderExpr.getLeft().evaluate(this);
}
String orderDir = orderExpr.getSortOrder();
directions[i] = ((orderDir == null || orderDir.equals("ascending")) ? false : true);
nullOrders[i] = orderExpr.getNullOrder();
}
stmt.setOrdering(orderSqlExprs, directions, nullOrders);
compileComponent = null;
}
}
use of org.datanucleus.query.expression.Expression in project datanucleus-rdbms by datanucleus.
the class JDOQLQuery method compileQueryUpdate.
/**
* Method to compile the query for RDBMS for a bulk update.
* @param parameterValues The parameter values (if any)
* @param candidateCmd Meta-data for the candidate class
*/
protected void compileQueryUpdate(Map parameterValues, AbstractClassMetaData candidateCmd) {
Expression[] updateExprs = compilation.getExprUpdate();
if (updateExprs == null || updateExprs.length == 0) {
// Nothing to update
return;
}
// Generate statement for candidate
RDBMSStoreManager storeMgr = (RDBMSStoreManager) getStoreManager();
DatastoreClass candidateTbl = storeMgr.getDatastoreClass(candidateCmd.getFullClassName(), clr);
if (candidateTbl == null) {
throw new NucleusDataStoreException("Bulk update of " + candidateCmd.getFullClassName() + " not supported since candidate has no table of its own");
}
List<BulkTable> tables = new ArrayList<>();
tables.add(new BulkTable(candidateTbl, true));
if (candidateTbl.getSuperDatastoreClass() != null) {
DatastoreClass tbl = candidateTbl;
while (tbl.getSuperDatastoreClass() != null) {
tbl = tbl.getSuperDatastoreClass();
tables.add(new BulkTable(tbl, false));
}
}
List<SQLStatement> stmts = new ArrayList<>();
List<Boolean> stmtCountFlags = new ArrayList<>();
for (BulkTable bulkTable : tables) {
// Generate statement for candidate
DatastoreClass table = bulkTable.table;
Map<String, Object> extensions = null;
if (!storeMgr.getDatastoreAdapter().supportsOption(DatastoreAdapter.UPDATE_DELETE_STATEMENT_ALLOW_TABLE_ALIAS_IN_WHERE_CLAUSE)) {
extensions = new HashMap<>();
extensions.put(SQLStatement.EXTENSION_SQL_TABLE_NAMING_STRATEGY, "table-name");
}
UpdateStatement stmt = new UpdateStatement(storeMgr, table, null, null, extensions);
stmt.setClassLoaderResolver(clr);
stmt.setCandidateClassName(candidateCmd.getFullClassName());
JavaTypeMapping multitenancyMapping = table.getSurrogateMapping(SurrogateColumnType.MULTITENANCY, false);
if (multitenancyMapping != null) {
// Multi-tenancy restriction
SQLExpression tenantExpr = stmt.getSQLExpressionFactory().newExpression(stmt, stmt.getPrimaryTable(), multitenancyMapping);
SQLExpression tenantVal = stmt.getSQLExpressionFactory().newLiteral(stmt, multitenancyMapping, ec.getNucleusContext().getMultiTenancyId(ec, candidateCmd));
stmt.whereAnd(tenantExpr.eq(tenantVal), true);
}
// TODO Discriminator restriction?
JavaTypeMapping softDeleteMapping = table.getSurrogateMapping(SurrogateColumnType.SOFTDELETE, false);
if (softDeleteMapping != null) {
// Soft-delete restriction
SQLExpression softDeleteExpr = stmt.getSQLExpressionFactory().newExpression(stmt, stmt.getPrimaryTable(), softDeleteMapping);
SQLExpression softDeleteVal = stmt.getSQLExpressionFactory().newLiteral(stmt, softDeleteMapping, Boolean.FALSE);
stmt.whereAnd(softDeleteExpr.eq(softDeleteVal), true);
}
Set<String> options = new HashSet<>();
if (getBooleanExtensionProperty(EXTENSION_USE_IS_NULL_WHEN_EQUALS_NULL_PARAM, true)) {
options.add(QueryToSQLMapper.OPTION_NULL_PARAM_USE_IS_NULL);
}
QueryToSQLMapper sqlMapper = new QueryToSQLMapper(stmt, compilation, parameterValues, null, null, candidateCmd, subclasses, getFetchPlan(), ec, null, options, extensions);
setMapperJoinTypes(sqlMapper);
sqlMapper.compile();
if (stmt.hasUpdates()) {
stmts.add(stmt);
stmtCountFlags.add(bulkTable.useInCount);
datastoreCompilation.setStatementParameters(stmt.getSQLText().getParametersForStatement());
datastoreCompilation.setPrecompilable(sqlMapper.isPrecompilable());
}
}
datastoreCompilation.clearStatements();
Iterator<SQLStatement> stmtIter = stmts.iterator();
Iterator<Boolean> stmtCountFlagsIter = stmtCountFlags.iterator();
while (stmtIter.hasNext()) {
SQLStatement stmt = stmtIter.next();
Boolean useInCount = stmtCountFlagsIter.next();
if (stmts.size() == 1) {
useInCount = true;
}
datastoreCompilation.addStatement(stmt, stmt.getSQLText().toSQL(), useInCount);
}
}
Aggregations