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();
}
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();
}
Aggregations