[#5461] Add DSLContext.ddl(Catalog)
This commit is contained in:
parent
a402026561
commit
325aaf7d3f
@ -45,6 +45,11 @@ package org.jooq;
|
||||
*/
|
||||
public enum DDLFlag {
|
||||
|
||||
/**
|
||||
* Whether <code>CREATE SCHEMA</code> statements should be generated.
|
||||
*/
|
||||
SCHEMA,
|
||||
|
||||
/**
|
||||
* Whether <code>CREATE TABLE</code> statements should be generated.
|
||||
*/
|
||||
|
||||
@ -6916,6 +6916,34 @@ public interface DSLContext extends Scope , AutoCloseable {
|
||||
// XXX DDL Statements from existing meta data
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Generate the complete creation script for the entire catalog.
|
||||
*
|
||||
* @see #ddl(Catalog, DDLFlag...)
|
||||
*/
|
||||
Queries ddl(Catalog catalog);
|
||||
|
||||
/**
|
||||
* Generate a partial creation script for the entire catalog.
|
||||
* <p>
|
||||
* The following {@link DDLFlag} can be set:
|
||||
* <ul>
|
||||
* <li>{@link DDLFlag#SCHEMA}: If set, the catalog's <code>SCHEMA</code>
|
||||
* specification will be generated.</li>
|
||||
* <li>{@link DDLFlag#TABLE}: If set, the schema's <code>TABLE</code>
|
||||
* specification will be generated.</li>
|
||||
* <li>{@link DDLFlag#PRIMARY_KEY}: If set, a potential
|
||||
* <code>PRIMARY KEY</code> constraint is specified inline with the table.
|
||||
* </li>
|
||||
* <li>{@link DDLFlag#UNIQUE}: If set, any potential <code>UNIQUE</code>
|
||||
* constraint is specified inline with the table.</li>
|
||||
* <li>{@link DDLFlag#FOREIGN_KEY}: If set, any potential
|
||||
* <code>FOREIGN KEY</code> constraint is specified after all the tables, as
|
||||
* a separate <code>ALTER TABLE .. ADD CONSTRAINT</code> statement.</li>
|
||||
* </ul>
|
||||
*/
|
||||
Queries ddl(Catalog schema, DDLFlag... flags);
|
||||
|
||||
/**
|
||||
* Generate the complete creation script for the entire schema.
|
||||
*
|
||||
|
||||
@ -42,14 +42,17 @@ package org.jooq.impl;
|
||||
|
||||
import static org.jooq.DDLFlag.FOREIGN_KEY;
|
||||
import static org.jooq.DDLFlag.PRIMARY_KEY;
|
||||
import static org.jooq.DDLFlag.SCHEMA;
|
||||
import static org.jooq.DDLFlag.TABLE;
|
||||
import static org.jooq.DDLFlag.UNIQUE;
|
||||
import static org.jooq.impl.DSL.constraint;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
|
||||
import org.jooq.Catalog;
|
||||
import org.jooq.Constraint;
|
||||
import org.jooq.DDLFlag;
|
||||
import org.jooq.DSLContext;
|
||||
@ -105,6 +108,9 @@ final class DDL {
|
||||
final Queries queries(Schema schema) {
|
||||
List<Query> queries = new ArrayList<Query>();
|
||||
|
||||
if (flags.contains(SCHEMA))
|
||||
queries.add(ctx.createSchema(schema.getName()));
|
||||
|
||||
if (flags.contains(TABLE)) {
|
||||
for (Table<?> table : schema.getTables()) {
|
||||
List<Constraint> constraints = new ArrayList<Constraint>();
|
||||
@ -134,4 +140,13 @@ final class DDL {
|
||||
|
||||
return DSL.queries(queries);
|
||||
}
|
||||
|
||||
final Queries queries(Catalog catalog) {
|
||||
List<Query> queries = new ArrayList<Query>();
|
||||
|
||||
for (Schema schema : catalog.getSchemas())
|
||||
queries.addAll(Arrays.asList(queries(schema).queries()));
|
||||
|
||||
return DSL.queries(queries);
|
||||
}
|
||||
}
|
||||
|
||||
@ -92,6 +92,7 @@ import org.jooq.Attachable;
|
||||
import org.jooq.Batch;
|
||||
import org.jooq.BatchBindStep;
|
||||
import org.jooq.BindContext;
|
||||
import org.jooq.Catalog;
|
||||
import org.jooq.CommonTableExpression;
|
||||
import org.jooq.Condition;
|
||||
import org.jooq.Configuration;
|
||||
@ -2406,6 +2407,16 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri
|
||||
// XXX DDL Statements from existing meta data
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@Override
|
||||
public Queries ddl(Catalog catalog) {
|
||||
return ddl(catalog, DDLFlag.values());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Queries ddl(Catalog schema, DDLFlag... flags) {
|
||||
return new DDL(this, flags).queries(schema);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Queries ddl(Schema schema) {
|
||||
return ddl(schema, DDLFlag.values());
|
||||
|
||||
Loading…
Reference in New Issue
Block a user