{
// "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();
}
else
{
throw new NucleusException("Dont currently support update clause containing left expression of type " + updateExpr.getLeft());
}
// Right-side can be Literal, or Parameter, or PrimaryExpression
SQLExpression rightSqlExpr = null;
if (updateExpr.getRight() instanceof Literal)
{
processLiteral((Literal)updateExpr.getRight());
rightSqlExpr = stack.pop();
}
else if (updateExpr.getRight() instanceof ParameterExpression)
{
processParameterExpression((ParameterExpression)updateExpr.getRight());
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)
{
updateExpr.getRight().evaluate(this);
rightSqlExpr = stack.pop();
}
else
{
throw new NucleusException("Dont currently support update clause containing right expression of type " + updateExpr.getRight());
}
if (leftSqlExpr != null && rightSqlExpr != null)
{
updateSqlExprs[i] = leftSqlExpr.eq(rightSqlExpr);
}
}
if (candidateCmd.hasVersionStrategy() && options.contains(OPTION_BULK_UPDATE_VERSION))
{
SQLExpression updateSqlExpr = null;
ClassTable table = (ClassTable)stmt.getPrimaryTable().getTable();
JavaTypeMapping verMapping = table.getVersionMapping(true);
ClassTable verTable = table.getTableManagingMapping(verMapping);
VersionMetaData vermd = candidateCmd.getVersionMetaData();
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,
stmt.getSQLExpressionFactory().getMappingForType(Integer.class, false), new Integer(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;
}
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 = VersionHelper.getNextVersion(vermd.getVersionStrategy(), null);
JavaTypeMapping valMapping = stmt.getSQLExpressionFactory().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;
}
}
stmt.setUpdates(updateSqlExprs);
}
else if (compilation.getExprResult() != null)
{
compileComponent = CompilationComponent.RESULT;
// Select any result expressions
Expression[] resultExprs = compilation.getExprResult();
for (int i=0;i<resultExprs.length;i++)
{
String alias = resultExprs[i].getAlias();
if (resultExprs[i] instanceof InvokeExpression)
{
processInvokeExpression((InvokeExpression)resultExprs[i]);
SQLExpression sqlExpr = stack.pop();
validateExpressionForResult(sqlExpr);
int[] cols = stmt.select(sqlExpr, alias);
StatementMappingIndex idx = new StatementMappingIndex(sqlExpr.getJavaTypeMapping());
idx.setColumnPositions(cols);
if (alias != null)
{
idx.setColumnAlias(alias);
}
resultDefinition.addMappingForResultExpression(i, idx);
}
else if (resultExprs[i] instanceof PrimaryExpression)
{
PrimaryExpression primExpr = (PrimaryExpression)resultExprs[i];
if (primExpr.getId().equals(candidateAlias))
{
// "this", so select fetch plan fields
StatementClassMapping map = new StatementClassMapping(candidateCmd.getFullClassName(), null);
SQLStatementHelper.selectFetchPlanOfCandidateInStatement(stmt, map,
candidateCmd, fetchPlan, 1);
resultDefinition.addMappingForResultExpression(i, map);
}
else
{
processPrimaryExpression(primExpr);
SQLExpression sqlExpr = stack.pop();
validateExpressionForResult(sqlExpr);
if (sqlExpr instanceof SQLLiteral)
{
int[] cols = stmt.select(sqlExpr, alias);
StatementMappingIndex idx = new StatementMappingIndex(sqlExpr.getJavaTypeMapping());
idx.setColumnPositions(cols);
if (alias != null)
{
idx.setColumnAlias(alias);
}
resultDefinition.addMappingForResultExpression(i, idx);
}
else
{
int[] cols = stmt.select(sqlExpr.getSQLTable(), sqlExpr.getJavaTypeMapping(), alias);
StatementMappingIndex idx = new StatementMappingIndex(sqlExpr.getJavaTypeMapping());
idx.setColumnPositions(cols);
if (alias != null)
{
idx.setColumnAlias(alias);
}
resultDefinition.addMappingForResultExpression(i, idx);
}
}
}
else if (resultExprs[i] instanceof ParameterExpression)
{
processParameterExpression((ParameterExpression)resultExprs[i], true); // Params are literals in result
SQLExpression sqlExpr = stack.pop();
validateExpressionForResult(sqlExpr);
int[] cols = stmt.select(sqlExpr, alias);
StatementMappingIndex idx = new StatementMappingIndex(sqlExpr.getJavaTypeMapping());
idx.setColumnPositions(cols);
if (alias != null)
{
idx.setColumnAlias(alias);
}
resultDefinition.addMappingForResultExpression(i, idx);
}
else if (resultExprs[i] instanceof VariableExpression)
{
processVariableExpression((VariableExpression)resultExprs[i]);
SQLExpression sqlExpr = stack.pop();
validateExpressionForResult(sqlExpr);
if (sqlExpr instanceof UnboundExpression)
{
// Variable wasn't bound in the compilation so far, so handle as cross-join
processUnboundExpression((UnboundExpression) sqlExpr);
sqlExpr = stack.pop();
NucleusLogger.QUERY.debug(">> QueryToSQL.exprResult variable was still unbound, so binding via cross-join");
}
StatementMappingIndex idx = new StatementMappingIndex(sqlExpr.getJavaTypeMapping());
int[] cols = stmt.select(sqlExpr, alias);
idx.setColumnPositions(cols);
if (alias != null)
{
idx.setColumnAlias(alias);
}
resultDefinition.addMappingForResultExpression(i, idx);
}
else if (resultExprs[i] instanceof Literal)
{
processLiteral((Literal)resultExprs[i]);
SQLExpression sqlExpr = stack.pop();
validateExpressionForResult(sqlExpr);
int[] cols = stmt.select(sqlExpr, alias);
StatementMappingIndex idx = new StatementMappingIndex(sqlExpr.getJavaTypeMapping());
idx.setColumnPositions(cols);
if (alias != null)
{
idx.setColumnAlias(alias);
}
resultDefinition.addMappingForResultExpression(i, idx);
}
else if (resultExprs[i] instanceof CreatorExpression)
{
processCreatorExpression((CreatorExpression)resultExprs[i]);
NewObjectExpression sqlExpr = (NewObjectExpression)stack.pop();
StatementNewObjectMapping stmtMap = getStatementMappingForNewObjectExpression(sqlExpr);
resultDefinition.addMappingForResultExpression(i, stmtMap);
}
else if (resultExprs[i] instanceof DyadicExpression)
{
// Something like {a+b} perhaps. Maybe we should check for invalid expressions?
resultExprs[i].evaluate(this);
SQLExpression sqlExpr = stack.pop();
int[] cols = stmt.select(sqlExpr, alias);
StatementMappingIndex idx = new StatementMappingIndex(sqlExpr.getJavaTypeMapping());
idx.setColumnPositions(cols);
if (alias != null)
{
idx.setColumnAlias(alias);
}
resultDefinition.addMappingForResultExpression(i, idx);
}
else if (resultExprs[i] instanceof CaseExpression)
{
resultExprs[i].evaluate(this);
SQLExpression sqlExpr = stack.pop();
int[] cols = stmt.select(sqlExpr, alias);
StatementMappingIndex idx = new StatementMappingIndex(sqlExpr.getJavaTypeMapping());
idx.setColumnPositions(cols);
if (alias != null)
{
idx.setColumnAlias(alias);
}