use of org.molgenis.emx2.tasks.Step in project molgenis-emx2 by molgenis.
the class ImportSchemaTask method rollback.
private void rollback(Task task) {
for (Step step : task.getSteps()) {
if (step.getStatus().equals(COMPLETED)) {
step.setStatus(SKIPPED);
step.setDescription("Rolled back: " + step.getDescription());
}
if (step instanceof Task) {
this.rollback((Task) step);
}
}
}
use of org.molgenis.emx2.tasks.Step in project molgenis-emx2 by molgenis.
the class SqlTable method insertBatch.
private static int insertBatch(SqlTable table, List<Row> rows, boolean updateOnConflict, Set<String> updateColumns) {
boolean inherit = table.getMetadata().getInherit() != null;
if (inherit) {
SqlTable inheritedTable = table.getInheritedTable();
inheritedTable.insertBatch(inheritedTable, rows, updateOnConflict, updateColumns);
}
// get metadata
Set<Column> columns = table.getColumnsToBeUpdated(updateColumns);
// check that columns exist for validation
checkForMissingVariablesColumns(columns);
List<Column> allColumns = table.getMetadata().getMutationColumns();
List<Field> insertFields = columns.stream().map(c -> c.getJooqField()).collect(Collectors.toList());
if (!inherit) {
insertFields.add(field(name(MG_INSERTEDBY)));
insertFields.add(field(name(MG_INSERTEDON)));
insertFields.add(field(name(MG_UPDATEDBY)));
insertFields.add(field(name(MG_UPDATEDON)));
}
// define the insert step
InsertValuesStepN<org.jooq.Record> step = table.getJooq().insertInto(table.getJooqTable(), insertFields.toArray(new Field[0]));
// add all the rows as steps
String user = table.getSchema().getDatabase().getActiveUser();
if (user == null) {
user = ADMIN_USER;
}
LocalDateTime now = LocalDateTime.now();
for (Row row : rows) {
// get values
Map values = SqlTypeUtils.getValuesAsMap(row, columns);
if (!inherit) {
values.put(MG_INSERTEDBY, user);
values.put(MG_INSERTEDON, now);
values.put(MG_UPDATEDBY, user);
values.put(MG_UPDATEDON, now);
}
// when insert, we should include all columns, not only 'updateColumns'
if (!row.isDraft()) {
checkRequired(row, allColumns);
checkValidation(values, columns);
}
step.values(values.values());
}
// optionally, add conflict clause
if (updateOnConflict) {
InsertOnDuplicateSetStep<org.jooq.Record> step2 = step.onConflict(table.getMetadata().getPrimaryKeyFields().toArray(new Field[0])).doUpdate();
for (Column column : columns) {
step2.set(column.getJooqField(), (Object) field(unquotedName("excluded.\"" + column.getName() + "\"")));
}
if (!inherit) {
step2.set(field(name(MG_UPDATEDBY)), user);
step2.set(field(name(MG_UPDATEDON)), now);
}
}
return step.execute();
}
use of org.molgenis.emx2.tasks.Step in project molgenis-emx2 by molgenis.
the class GraphqlApi method executeQuery.
private static String executeQuery(GraphQL g, Request request) throws IOException {
String query = getQueryFromRequest(request);
Map<String, Object> variables = getVariablesFromRequest(request);
long start = System.currentTimeMillis();
// we don't log password calls
if (logger.isInfoEnabled()) {
if (query.contains("password")) {
logger.info("query: obfuscated because contains parameter with name 'password'");
} else {
logger.info("query: {}", query.replaceAll("[\n|\r|\t]", "").replaceAll(" +", " "));
}
}
// tests show overhead of this step is about 20ms (jooq takes the rest)
ExecutionResult executionResult = null;
if (variables != null) {
executionResult = g.execute(ExecutionInput.newExecutionInput(query).variables(variables));
} else {
executionResult = g.execute(query);
}
String result = GraphqlApiFactory.convertExecutionResultToJson(executionResult);
for (GraphQLError err : executionResult.getErrors()) {
if (logger.isErrorEnabled()) {
logger.error(err.getMessage());
}
}
if (executionResult.getErrors().size() > 0) {
throw new MolgenisException("Error", executionResult.getErrors().get(0).getMessage());
}
if (logger.isInfoEnabled())
logger.info("graphql request completed in {}ms", +(System.currentTimeMillis() - start));
return result;
}
Aggregations