1

Are there any Java libraries that allow me to generate DDL from Java code using a typesafe builder pattern, or similar?

Something along the lines of:

String sql = DDLGenerator.createTable("MY_TABLE")
                         .withPrimaryKey("my_id").ofNumberType().withPrecision(2, 2)
                         .withColumn("name").ofVarchar2Type().withLength(30).notNull()
                         .withIndexOn("name")
                         .buildSql(Dialect.ORACLE);

I'm not looking for an ORM solution such as Hibernate nor a database migration solution such as Liquibase or Flyway, nor a tool that works with an existing schema.

1 Answer 1

4

jOOQ

jOOQ provides a DSL for creating tables:

Connection conn = ... //code that creates a DB connection
DSLContext create = DSL.using(conn, SQLDialect.ORACLE);
create.createTable("MY_TABLE")
    .column("my_id", SQLDataType.DECIMAL.precision(2, 2))
    .column("name", SQLDataType.VARCHAR.length(30).nullable(false))
    .withPrimaryKey("my_id").ofNumberType().withPrecision(2, 2)
    .constraints(
        constraint("PK_MY_TABLE").primaryKey("my_id")
    )
    .execute();
create.createIndex("I_MY_TABLE_NAME")
    .on("MY_TABLE", "name")
    .execute();

See Wikipedia.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.