use of org.datanucleus.store.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.store.query.expression.Expression in project datanucleus-rdbms by datanucleus.
the class QueryToSQLMapper method compileGrouping.
/**
* Method to compile the grouping clause of the query into the SQLStatement.
* @param stmt SELECT statement
*/
protected void compileGrouping(SelectStatement stmt) {
if (compilation.getExprGrouping() != null) {
// Apply any grouping to the statement
compileComponent = CompilationComponent.GROUPING;
Expression[] groupExprs = compilation.getExprGrouping();
for (Expression groupExpr : groupExprs) {
stmt.addGroupingExpression((SQLExpression) groupExpr.evaluate(this));
}
compileComponent = null;
}
}
use of org.datanucleus.store.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.store.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.getTenantId());
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);
}
}
use of org.datanucleus.store.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;
}
}
Aggregations