简体   繁体   中英

Using jOOQ as schema generator (plain sql as output)

I'm trying to use jOOQ as a schema generator to generate DDL statements, and later to generate SQL insert statements.

The underlying data is coming from static CSV files and I basically want to write a static SQL script which includes statements to create the schema and insert the data; merely a dump file.

For example I have this Groovy class containing the table deifnition:


class ContinentTable extends CustomTable<Record> {

    static ContinentTable CONTINENT = new ContinentTable()

    static UniqueKey<Record> CONTINENT_PK = createUniqueKey(CONTINENT, name("continent_pk"), [CONTINENT.ID] as TableField[], true)

    TableField<Record, String> ID = createField(name("id"), VARCHAR(255).nullable(false))
    TableField<Record, String> CODE = createField(name("code"), VARCHAR(2).nullable(false))
    TableField<Record, String> NAME = createField(name("name"), VARCHAR(255).nullable(false))
    TableField<Record, String> DEMONYM = createField(name("demonym"), VARCHAR(255).nullable(false))

    private ContinentTable() {
        super(name("continent"))
    }

    @Override
    Class<? extends Record> getRecordType() {
        return Record
    }

    @Override
    UniqueKey<Record> getPrimaryKey() {
        return CONTINENT_PK
    }
}

It contains the fields and the PK, but when I use jOOQ to create the SQL statement with:

dsl.createTable(ContinentTable.CONTINENT)

it only generates:

create table "continent";

without the columns. Off course I could do something like:

dsl.createTable(ContinentTable.CONTINENT).columns(ContinentTable.CONTINENT.fields())

but it seems a bit of unnecessary as I already pass in the table??

It gets more complex if I want to add primary keys and possible foreign keys, indexes etc.

Is it expected behaviour that these are all not added when calling dsl.createTable(ContinentTable.CONTINENT) ?

Note: I'm creating the ContinentTable manually as I'm not using codegen as I have no source database.

I was using jOOQ's dsl.createTable(ContinentTable.CONTINENT).columns(ContinentTable.CONTINENT.fields())

But jOOQ also has a ddl method which does exactly what I want.

def queries = create.ddl(ContinentTable.CONTINENT)
queries.each { query ->
    println query}
}

which does generate the table including columns, constraints etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM