Search in sources :

Example 1 with Step

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);
        }
    }
}
Also used : Task(org.molgenis.emx2.tasks.Task) Step(org.molgenis.emx2.tasks.Step)

Example 2 with 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();
}
Also used : EvaluateExpressions.checkValidation(org.molgenis.emx2.sql.EvaluateExpressions.checkValidation) java.util(java.util) DSL(org.jooq.impl.DSL) Logger(org.slf4j.Logger) LocalDateTime(java.time.LocalDateTime) LoggerFactory(org.slf4j.LoggerFactory) Constants(org.molgenis.emx2.Constants) org.molgenis.emx2(org.molgenis.emx2) Collectors(java.util.stream.Collectors) EvaluateExpressions.checkForMissingVariablesColumns(org.molgenis.emx2.sql.EvaluateExpressions.checkForMissingVariablesColumns) ADMIN_USER(org.molgenis.emx2.sql.SqlDatabase.ADMIN_USER) Query(org.molgenis.emx2.Query) MutationType(org.molgenis.emx2.MutationType) StringReader(java.io.StringReader) Table(org.molgenis.emx2.Table) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) org.jooq(org.jooq) Writer(java.io.Writer) BaseConnection(org.postgresql.core.BaseConnection) CopyManager(org.postgresql.copy.CopyManager) SqlTypeUtils.getTypedValue(org.molgenis.emx2.sql.SqlTypeUtils.getTypedValue) Row(org.molgenis.emx2.Row) LocalDateTime(java.time.LocalDateTime) Row(org.molgenis.emx2.Row)

Example 3 with Step

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;
}
Also used : MolgenisException(org.molgenis.emx2.MolgenisException) GraphQLError(graphql.GraphQLError) ExecutionResult(graphql.ExecutionResult)

Aggregations

ExecutionResult (graphql.ExecutionResult)1 GraphQLError (graphql.GraphQLError)1 StringReader (java.io.StringReader)1 Writer (java.io.Writer)1 LocalDateTime (java.time.LocalDateTime)1 java.util (java.util)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Collectors (java.util.stream.Collectors)1 org.jooq (org.jooq)1 DSL (org.jooq.impl.DSL)1 org.molgenis.emx2 (org.molgenis.emx2)1 Constants (org.molgenis.emx2.Constants)1 MolgenisException (org.molgenis.emx2.MolgenisException)1 MutationType (org.molgenis.emx2.MutationType)1 Query (org.molgenis.emx2.Query)1 Row (org.molgenis.emx2.Row)1 Table (org.molgenis.emx2.Table)1 EvaluateExpressions.checkForMissingVariablesColumns (org.molgenis.emx2.sql.EvaluateExpressions.checkForMissingVariablesColumns)1 EvaluateExpressions.checkValidation (org.molgenis.emx2.sql.EvaluateExpressions.checkValidation)1 ADMIN_USER (org.molgenis.emx2.sql.SqlDatabase.ADMIN_USER)1