use of org.datanucleus.query.expression.Expression in project datanucleus-rdbms by datanucleus.
the class QueryToSQLMapper method compileUpdate.
/**
* Method to compile the result clause of the query into the SQLStatement.
* @param stmt UPDATE statement
*/
protected void compileUpdate(UpdateStatement stmt) {
if (compilation.getExprUpdate() != null) {
// Update statement, so generate update expression(s)
compileComponent = CompilationComponent.UPDATE;
Expression[] updateExprs = compilation.getExprUpdate();
SQLExpression[] updateSqlExprs = new SQLExpression[updateExprs.length];
// TODO If the field being set is in a different table omit it
boolean performingUpdate = false;
for (int i = 0; i < updateExprs.length; i++) {
// "field = value"
DyadicExpression updateExpr = (DyadicExpression) updateExprs[i];
// Left-side has to be PrimaryExpression
SQLExpression leftSqlExpr = null;
if (updateExpr.getLeft() instanceof PrimaryExpression) {
processPrimaryExpression((PrimaryExpression) updateExpr.getLeft());
leftSqlExpr = stack.pop();
if (leftSqlExpr.getSQLTable() != stmt.getPrimaryTable()) {
// Set left to null to signify that it is not applicable to the table of this UPDATE statement
leftSqlExpr = null;
}
} else {
throw new NucleusException("Dont currently support update clause containing left expression of type " + updateExpr.getLeft());
}
if (leftSqlExpr != null) {
if (!stmt.getDatastoreAdapter().supportsOption(DatastoreAdapter.UPDATE_STATEMENT_ALLOW_TABLE_ALIAS_IN_SET_CLAUSE)) {
// This datastore doesn't allow table alias in UPDATE SET clause, so just use column name
for (int j = 0; j < leftSqlExpr.getNumberOfSubExpressions(); j++) {
ColumnExpression colExpr = leftSqlExpr.getSubExpression(j);
colExpr.setOmitTableFromString(true);
}
}
performingUpdate = true;
SQLExpression rightSqlExpr = null;
if (updateExpr.getRight() instanceof Literal) {
processLiteral((Literal) updateExpr.getRight());
rightSqlExpr = stack.pop();
} else if (updateExpr.getRight() instanceof ParameterExpression) {
ParameterExpression paramExpr = (ParameterExpression) updateExpr.getRight();
paramMappingForName.put(paramExpr.getId(), leftSqlExpr.getJavaTypeMapping());
processParameterExpression(paramExpr);
rightSqlExpr = stack.pop();
} else if (updateExpr.getRight() instanceof PrimaryExpression) {
processPrimaryExpression((PrimaryExpression) updateExpr.getRight());
rightSqlExpr = stack.pop();
} else if (updateExpr.getRight() instanceof DyadicExpression) {
updateExpr.getRight().evaluate(this);
rightSqlExpr = stack.pop();
} else if (updateExpr.getRight() instanceof CaseExpression) {
CaseExpression caseExpr = (CaseExpression) updateExpr.getRight();
processCaseExpression(caseExpr, leftSqlExpr);
rightSqlExpr = stack.pop();
} else if (updateExpr.getRight() instanceof VariableExpression) {
// Subquery?
processVariableExpression((VariableExpression) updateExpr.getRight());
rightSqlExpr = stack.pop();
if (rightSqlExpr instanceof UnboundExpression) {
// TODO Support whatever this is
throw new NucleusException("Found UnboundExpression in UPDATE clause!");
}
} else {
throw new NucleusException("Dont currently support update clause containing right expression of type " + updateExpr.getRight());
}
if (rightSqlExpr != null) {
updateSqlExprs[i] = leftSqlExpr.eq(rightSqlExpr);
}
}
}
if (candidateCmd.isVersioned() && options.contains(OPTION_BULK_UPDATE_VERSION)) {
SQLExpression updateSqlExpr = null;
ClassTable table = (ClassTable) stmt.getPrimaryTable().getTable();
JavaTypeMapping verMapping = table.getSurrogateMapping(SurrogateColumnType.VERSION, true);
ClassTable verTable = table.getTableManagingMapping(verMapping);
if (verTable == stmt.getPrimaryTable().getTable()) {
VersionMetaData vermd = candidateCmd.getVersionMetaDataForClass();
if (vermd.getVersionStrategy() == VersionStrategy.VERSION_NUMBER) {
// Increment the version
SQLTable verSqlTbl = stmt.getTable(verTable, stmt.getPrimaryTable().getGroupName());
SQLExpression verExpr = new NumericExpression(stmt, verSqlTbl, verMapping);
SQLExpression incrExpr = verExpr.add(new IntegerLiteral(stmt, exprFactory.getMappingForType(Integer.class, false), Integer.valueOf(1), null));
updateSqlExpr = verExpr.eq(incrExpr);
SQLExpression[] oldArray = updateSqlExprs;
updateSqlExprs = new SQLExpression[oldArray.length + 1];
System.arraycopy(oldArray, 0, updateSqlExprs, 0, oldArray.length);
updateSqlExprs[oldArray.length] = updateSqlExpr;
performingUpdate = true;
} else if (vermd.getVersionStrategy() == VersionStrategy.DATE_TIME) {
// Set version to the time of update
SQLTable verSqlTbl = stmt.getTable(verTable, stmt.getPrimaryTable().getGroupName());
SQLExpression verExpr = new NumericExpression(stmt, verSqlTbl, verMapping);
Object newVersion = ec.getLockManager().getNextVersion(vermd, null);
JavaTypeMapping valMapping = exprFactory.getMappingForType(newVersion.getClass(), false);
SQLExpression valExpr = new TemporalLiteral(stmt, valMapping, newVersion, null);
updateSqlExpr = verExpr.eq(valExpr);
SQLExpression[] oldArray = updateSqlExprs;
updateSqlExprs = new SQLExpression[oldArray.length + 1];
System.arraycopy(oldArray, 0, updateSqlExprs, 0, oldArray.length);
updateSqlExprs[oldArray.length] = updateSqlExpr;
performingUpdate = true;
}
}
}
if (performingUpdate) {
// Only set the updates component of the SQLStatement if anything to update in this table
stmt.setUpdates(updateSqlExprs);
}
}
compileComponent = null;
}
use of org.datanucleus.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.query.expression.Expression in project datanucleus-core by datanucleus.
the class ObjectGetClassMethod method evaluate.
/* (non-Javadoc)
* @see org.datanucleus.query.evaluator.memory.InvocationEvaluator#evaluate(org.datanucleus.query.expression.InvokeExpression, org.datanucleus.query.evaluator.memory.InMemoryExpressionEvaluator)
*/
public Object evaluate(InvokeExpression expr, Object invokedValue, InMemoryExpressionEvaluator eval) {
Expression argExpr = expr.getArguments().get(0);
if (argExpr instanceof ParameterExpression) {
ParameterExpression paramExpr = (ParameterExpression) argExpr;
Object value = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
return value.getClass();
}
return eval.getValueForPrimaryExpression((PrimaryExpression) argExpr).getClass();
}
use of org.datanucleus.query.expression.Expression in project datanucleus-core by datanucleus.
the class StringConcatMethod method evaluate.
/* (non-Javadoc)
* @see org.datanucleus.query.evaluator.memory.InvocationEvaluator#evaluate(org.datanucleus.query.expression.InvokeExpression, org.datanucleus.query.evaluator.memory.InMemoryExpressionEvaluator)
*/
public Object evaluate(InvokeExpression expr, Object invokedValue, InMemoryExpressionEvaluator eval) {
String method = expr.getOperation();
if (invokedValue == null) {
return null;
}
if (!(invokedValue instanceof String)) {
throw new NucleusException(Localiser.msg("021011", method, invokedValue.getClass().getName()));
}
Expression arg0Expr = expr.getArguments().get(0);
Object arg0Val = null;
if (arg0Expr instanceof PrimaryExpression) {
arg0Val = eval.getValueForPrimaryExpression((PrimaryExpression) arg0Expr);
} else if (arg0Expr instanceof ParameterExpression) {
arg0Val = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), (ParameterExpression) arg0Expr);
} else if (arg0Expr instanceof Literal) {
arg0Val = ((Literal) arg0Expr).getLiteral();
} else if (arg0Expr instanceof DyadicExpression) {
arg0Val = ((DyadicExpression) arg0Expr).evaluate(eval);
}
if (!(arg0Val instanceof String)) {
throw new NucleusException(method + "(param1) : param1 must be String");
}
return ((String) invokedValue).concat((String) arg0Val);
}
use of org.datanucleus.query.expression.Expression in project datanucleus-core by datanucleus.
the class StringSubstringMethod method evaluate.
/* (non-Javadoc)
* @see org.datanucleus.query.evaluator.memory.InvocationEvaluator#evaluate(org.datanucleus.query.expression.InvokeExpression, org.datanucleus.query.evaluator.memory.InMemoryExpressionEvaluator)
*/
public Object evaluate(InvokeExpression expr, Object invokedValue, InMemoryExpressionEvaluator eval) {
String method = expr.getOperation();
if (invokedValue == null) {
return null;
}
if (!(invokedValue instanceof String)) {
throw new NucleusException(Localiser.msg("021011", method, invokedValue.getClass().getName()));
}
int arg0 = -1;
Expression arg0Expr = expr.getArguments().get(0);
Object arg0Val = null;
if (arg0Expr instanceof PrimaryExpression) {
arg0Val = eval.getValueForPrimaryExpression((PrimaryExpression) arg0Expr);
} else if (arg0Expr instanceof ParameterExpression) {
arg0Val = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), (ParameterExpression) arg0Expr);
} else if (arg0Expr instanceof Literal) {
arg0Val = ((Literal) arg0Expr).getLiteral();
} else if (arg0Expr instanceof DyadicExpression) {
arg0Val = ((DyadicExpression) arg0Expr).evaluate(eval);
}
if (!(arg0Val instanceof Number)) {
throw new NucleusException(method + "(param1[,param2]) : param1 must be numeric");
}
arg0 = ((Number) arg0Val).intValue();
String result = null;
if (expr.getArguments().size() == 2) {
int arg1 = -1;
Expression arg1Expr = expr.getArguments().get(1);
Object arg1Val = null;
if (arg1Expr instanceof PrimaryExpression) {
arg1Val = eval.getValueForPrimaryExpression((PrimaryExpression) arg1Expr);
} else if (arg1Expr instanceof ParameterExpression) {
arg1Val = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), (ParameterExpression) arg1Expr);
} else if (arg1Expr instanceof Literal) {
arg1Val = ((Literal) arg1Expr).getLiteral();
} else if (arg0Expr instanceof DyadicExpression) {
arg1Val = ((DyadicExpression) arg1Expr).evaluate(eval);
}
if (!(arg1Val instanceof Number)) {
throw new NucleusException(method + "(param1,param2) : param2 must be numeric");
}
arg1 = ((Number) arg1Val).intValue();
if (((String) invokedValue).length() < arg1) {
if (((String) invokedValue).length() < arg0) {
return null;
}
return ((String) invokedValue).substring(arg0);
}
result = ((String) invokedValue).substring(arg0, arg1);
} else {
if (((String) invokedValue).length() < arg0) {
return null;
}
result = ((String) invokedValue).substring(arg0);
}
return result;
}
Aggregations