com.kenshoo.jooq.DataTableUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of persistence-layer Show documentation
Show all versions of persistence-layer Show documentation
A Java persistence layer based on JOOQ for high performance and business flow support.
package com.kenshoo.jooq;
import com.google.common.collect.Lists;
import org.jooq.*;
import org.jooq.lambda.Seq;
import java.util.List;
import java.util.stream.Collectors;
import static org.jooq.impl.DSL.constraint;
public class DataTableUtils {
public static void createTable(final DSLContext dslContext, final Table table) {
dslContext.dropTableIfExists(table).execute();
final CreateTableColumnStep createTableAsStep = dslContext.createTable(table)
.columns(table.fields());
final UniqueKey primaryKey = table.getPrimaryKey();
if (primaryKey != null) {
createTableAsStep.constraints(constraint("").primaryKey(primaryKey.getFieldsArray()))
.execute();
} else {
createTableAsStep.execute();
}
}
public static void populateTable(DSLContext dslContext, Table table, Object[][] data) {
populate(dslContext, table, Lists.newArrayList(table.fields()), data);
}
public static void populateTableWithoutAutoIncFields(DSLContext dslContext, Table table, Object[][] data) {
var fields = Seq.of(table.fields()).filter(field -> !field.getDataType().identity()).collect(Collectors.toList());
populate(dslContext, table,fields, data);
}
private static void populate(DSLContext dslContext, Table table, List> fields, Object[][] data) {
InsertValuesStepN insert = dslContext.insertInto(table, fields).values(new Object[fields.size()]);
BatchBindStep batch = dslContext.batch(insert);
for (Object[] values : data) {
batch.bind(values);
}
batch.execute();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy