public List<Integer> listArchivableWorkflows(DateTime before, int maxRows) { return jdbc.query( "select w.id id from nflow_workflow w, " + "(" + " select parent.id from nflow_workflow parent " + " where parent.next_activation is null and parent.modified <= ? " + " and parent.root_workflow_id is null " + " and not exists(" + " select 1 from nflow_workflow child where child.root_workflow_id = parent.id " + " and (child.modified > ? or child.next_activation is not null)" + " )" + " order by modified asc " + " limit " + maxRows + ") as archivable_parent " + "where archivable_parent.id = w.id or archivable_parent.id = w.root_workflow_id", new ArchivableWorkflowsRowMapper(), toTimestamp(before), toTimestamp(before)); }
public boolean updateNotRunningWorkflowInstance(WorkflowInstance instance) { List<String> vars = new ArrayList<>(); List<Object> args = new ArrayList<>(); if (instance.state != null) { vars.add("state = ?, retries = 0"); args.add(instance.state); } if (instance.stateText != null) { vars.add("state_text = ?"); args.add(instance.stateText); } if (instance.nextActivation != null) { vars.add("next_activation = ?"); args.add(toTimestamp(instance.nextActivation)); } if (instance.status != null) { vars.add("status = " + sqlVariants.workflowStatus()); args.add(instance.status.name()); } String sql = "update nflow_workflow set " + join(vars, ", ") + " where id = ? and executor_id is null"; args.add(instance.id); return jdbc.update(sql, args.toArray()) == 1; }
@Override @SuppressFBWarnings(value = { "OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE", "SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING" }, justification = "findbugs does not trust jdbctemplate, sql string is practically constant") public PreparedStatement createPreparedStatement(Connection con) throws SQLException { PreparedStatement p = con.prepareStatement( insertWorkflowActionSql() + " values (?, ?, " + sqlVariants.actionType() + ", ?, ?, ?, ?, ?)", new String[] { "id" }); int field = 1; p.setInt(field++, action.workflowInstanceId); p.setInt(field++, executorInfo.getExecutorId()); p.setString(field++, action.type.name()); p.setString(field++, action.state); p.setString(field++, abbreviate(action.stateText, actionStateTextLength)); p.setInt(field++, action.retryNo); p.setTimestamp(field++, toTimestamp(action.executionStart)); p.setTimestamp(field++, toTimestamp(action.executionEnd)); return p; } }, keyHolder);
private int insertWorkflowInstanceWithCte(WorkflowInstance instance) { try { StringBuilder sqlb = new StringBuilder(256); sqlb.append("with wf as (" + insertWorkflowInstanceSql() + " returning id)"); Object[] instanceValues = new Object[] { instance.type, instance.rootWorkflowId, instance.parentWorkflowId, instance.parentActionId, instance.businessKey, instance.externalId, executorInfo.getExecutorGroup(), instance.status.name(), instance.state, abbreviate(instance.stateText, instanceStateTextLength), toTimestamp(instance.nextActivation) }; int pos = instanceValues.length; Object[] args = Arrays.copyOf(instanceValues, pos + instance.stateVariables.size() * 2); for (Entry<String, String> var : instance.stateVariables.entrySet()) { sqlb.append(", ins").append(pos).append(" as (").append(insertWorkflowInstanceStateSql()) .append(" select wf.id,0,?,? from wf)"); args[pos++] = var.getKey(); args[pos++] = var.getValue(); } sqlb.append(" select wf.id from wf"); return jdbc.queryForObject(sqlb.toString(), Integer.class, args); } catch (DuplicateKeyException e) { logger.warn("Failed to insert workflow instance", e); return -1; } }
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)"); // using sqlVariants.nextActivationUpdate() requires that nextActivation is added 3 times 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); }
public int updateWorkflowInstance(WorkflowInstance instance) { // using sqlVariants.nextActivationUpdate() requires that nextActivation is used 3 times Timestamp nextActivation = toTimestamp(instance.nextActivation); return jdbc.update(updateWorkflowInstanceSql(), instance.status.name(), instance.state, abbreviate(instance.stateText, instanceStateTextLength), nextActivation, nextActivation, nextActivation, instance.status == executing ? executorInfo.getExecutorId() : null, instance.retries, instance.id); }