Search in sources :

Example 1 with ADMIN_USER

use of org.molgenis.emx2.sql.SqlDatabase.ADMIN_USER 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 2 with ADMIN_USER

use of org.molgenis.emx2.sql.SqlDatabase.ADMIN_USER in project molgenis-emx2 by molgenis.

the class SqlTable method updateBatch.

private static int updateBatch(SqlTable table, List<Row> rows, Set<String> updateColumns) {
    boolean inherit = table.getMetadata().getInherit() != null;
    if (inherit) {
        SqlTable inheritedTable = table.getInheritedTable();
        inheritedTable.updateBatch(inheritedTable, rows, updateColumns);
    }
    // get metadata
    Set<Column> columns = table.getColumnsToBeUpdated(updateColumns);
    List<Column> pkeyFields = table.getMetadata().getPrimaryKeyColumns();
    // check that columns exist for validation
    checkForMissingVariablesColumns(columns);
    // create batch of updates
    List<UpdateConditionStep> list = new ArrayList();
    String user = table.getSchema().getDatabase().getActiveUser();
    if (user == null) {
        user = ADMIN_USER;
    }
    LocalDateTime now = LocalDateTime.now();
    for (Row row : rows) {
        Map values = SqlTypeUtils.getValuesAsMap(row, columns);
        if (!inherit) {
            values.put(MG_UPDATEDBY, user);
            values.put(MG_UPDATEDON, now);
        }
        if (!row.isDraft()) {
            checkRequired(row, columns);
            checkValidation(values, columns);
        }
        list.add(table.getJooq().update(table.getJooqTable()).set(values).where(table.getUpdateCondition(row, pkeyFields)));
    }
    return Arrays.stream(table.getJooq().batch(list).execute()).reduce(Integer::sum).getAsInt();
}
Also used : LocalDateTime(java.time.LocalDateTime) Row(org.molgenis.emx2.Row)

Aggregations

LocalDateTime (java.time.LocalDateTime)2 Row (org.molgenis.emx2.Row)2 StringReader (java.io.StringReader)1 Writer (java.io.Writer)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 MutationType (org.molgenis.emx2.MutationType)1 Query (org.molgenis.emx2.Query)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 SqlTypeUtils.getTypedValue (org.molgenis.emx2.sql.SqlTypeUtils.getTypedValue)1 CopyManager (org.postgresql.copy.CopyManager)1 BaseConnection (org.postgresql.core.BaseConnection)1