private void updateWorkflowInstanceWithCTE(WorkflowInstance instance, final WorkflowInstanceAction action,
Map<String, String> changedStateVariables) {
int executorId = executorInfo.getExecutorId();
StringBuilder sqlb = new StringBuilder(256);
sqlb.append("with wf as (").append(updateWorkflowInstanceSql()).append(" returning id), ");
sqlb.append("act as (").append(insertWorkflowActionSql()).append(" select wf.id, ?, ").append(sqlVariants.actionType())
.append(", ?, ?, ?, ?, ? from wf returning id)");
Timestamp nextActivation = toTimestamp(instance.nextActivation);
Object[] fixedValues = new Object[] { instance.status.name(), instance.state,
abbreviate(instance.stateText, instanceStateTextLength), nextActivation, nextActivation, nextActivation,
instance.status == executing ? executorId : null, instance.retries, instance.id, executorId, action.type.name(),
action.state, abbreviate(action.stateText, actionStateTextLength), action.retryNo, toTimestamp(action.executionStart),
toTimestamp(action.executionEnd) };
int pos = fixedValues.length;
Object[] args = Arrays.copyOf(fixedValues, pos + changedStateVariables.size() * 2);
for (Entry<String, String> var : changedStateVariables.entrySet()) {
sqlb.append(", ins").append(pos).append(" as (").append(insertWorkflowInstanceStateSql())
.append(" select wf.id,act.id,?,? from wf,act)");
args[pos++] = var.getKey();
args[pos++] = var.getValue();
}
sqlb.append(" select act.id from act");
jdbc.queryForObject(sqlb.toString(), Integer.class, args);
}