Pipsqweek
Pipsqweek

Reputation: 404

JOOQ Create Table Based on Existing Class?

As per the startup instructions I was able to successfully generate table classes in JOOQ that end up looking like so:

public class AgencyMeta extends TableImpl<AgencyMetaRecord> {

    private static final long serialVersionUID = 214852552;

    /**
     * The reference instance of <code>PUBLIC.AGENCY_META</code>
     */
    public static final AgencyMeta AGENCY_META = new AgencyMeta();

    /**
     * The class holding records for this type
     */
    @Override
    public Class<AgencyMetaRecord> getRecordType() {
        return AgencyMetaRecord.class;
    }

    /**
     * The column <code>PUBLIC.AGENCY_META.ID</code>.
     */
    public final TableField<AgencyMetaRecord, Long> ID = createField("ID", org.jooq.impl.SQLDataType.BIGINT.nullable(false).defaulted(true), this, "");

    /**
     * The column <code>PUBLIC.AGENCY_META.AGENCY_ID</code>.
     */
    public final TableField<AgencyMetaRecord, Long> AGENCY_ID = createField("AGENCY_ID", org.jooq.impl.SQLDataType.BIGINT.nullable(false), this, "");

    /**
     * The column <code>PUBLIC.AGENCY_META.KEY</code>.
     */
    public final TableField<AgencyMetaRecord, String> KEY = createField("KEY", org.jooq.impl.SQLDataType.VARCHAR.length(255).nullable(false), this, "");

    /**
     * The column <code>PUBLIC.AGENCY_META.VALUE</code>.
     */
    public final TableField<AgencyMetaRecord, String> VALUE = createField("VALUE", org.jooq.impl.SQLDataType.CLOB, this, "");

    /**
     * Create a <code>PUBLIC.AGENCY_META</code> table reference
     */
    public AgencyMeta() {
        this("AGENCY_META", null);
    }

    /**
     * Create an aliased <code>PUBLIC.AGENCY_META</code> table reference
     */
    public AgencyMeta(String alias) {
        this(alias, AGENCY_META);
    }

    private AgencyMeta(String alias, Table<AgencyMetaRecord> aliased) {
        this(alias, aliased, null);
    }

    private AgencyMeta(String alias, Table<AgencyMetaRecord> aliased, Field<?>[] parameters) {
        super(alias, Public.PUBLIC, aliased, parameters, "");
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Identity<AgencyMetaRecord, Long> getIdentity() {
        return Keys.IDENTITY_AGENCY_META;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public UniqueKey<AgencyMetaRecord> getPrimaryKey() {
        return Keys.CONSTRAINT_A0;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<UniqueKey<AgencyMetaRecord>> getKeys() {
        return Arrays.<UniqueKey<AgencyMetaRecord>>asList(Keys.CONSTRAINT_A0, Keys.CONSTRAINT_A0F);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<ForeignKey<AgencyMetaRecord, ?>> getReferences() {
        return Arrays.<ForeignKey<AgencyMetaRecord, ?>>asList(Keys.CONSTRAINT_A0FC);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public AgencyMeta as(String alias) {
        return new AgencyMeta(alias, this);
    }

    /**
     * Rename this table
     */
    public AgencyMeta rename(String name) {
        return new AgencyMeta(name, null);
    }
}

Apparently it has all the ingredients to create a table, and in part I can do so like this:

create.createTable(PRODUCT).column(PRODUCT.ID, SQLDataType.BIGINT.nullable(false).defaulted(true)).execute();

And so forth...

When the program starts up for the first time, I would like to build the h2 database on the spot.

Is there a way to create a table or even the whole database in a one-shot command? It would seem there should be based on what is provided.

Upvotes: 4

Views: 1979

Answers (1)

Maciej Stępyra
Maciej Stępyra

Reputation: 320

As far as i know, there is only posibility to issue DDL commands manually, as described in documentation: ddl statements

The documentation describes that:

jOOQ's DDL support is currently still very limited. In the long run, jOOQ will support the most important statement types for frequent informal database migrations, though. Note that jOOQ will not aim to replace existing database migration frameworks. At Data Geekery, we usually recommend using Flyway for migrations. See also the tutorial about using jOOQ with Flyway for more information.

Upvotes: 3

Related Questions