All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kenshoo.jooq.DataTableUtils Maven / Gradle / Ivy

Go to download

A Java persistence layer based on JOOQ for high performance and business flow support.

There is a newer version: 0.1.121-jooq-3.16.3
Show newest version
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