[#3160] Add Queries DSLContext.ddl(Schema), ddl(Table) and others to re-generate known artefacts
This commit is contained in:
parent
1b7796bbb2
commit
efc50afd2c
67
jOOQ/src/main/java/org/jooq/DDLFlag.java
Normal file
67
jOOQ/src/main/java/org/jooq/DDLFlag.java
Normal file
@ -0,0 +1,67 @@
|
||||
/**
|
||||
* Copyright (c) 2009-2016, Data Geekery GmbH (http://www.datageekery.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Other licenses:
|
||||
* -----------------------------------------------------------------------------
|
||||
* Commercial licenses for this work are available. These replace the above
|
||||
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
|
||||
* database integrations.
|
||||
*
|
||||
* For more information, please visit: http://www.jooq.org/licenses
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
package org.jooq;
|
||||
|
||||
/**
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
public enum DDLFlag {
|
||||
|
||||
/**
|
||||
* Whether <code>CREATE TABLE</code> statements should be generated.
|
||||
*/
|
||||
TABLE,
|
||||
|
||||
/**
|
||||
* Whether <code>PRIMARY KEY</code> constraints should be generated.
|
||||
*/
|
||||
PRIMARY_KEY,
|
||||
|
||||
/**
|
||||
* Whether <code>UNIQUE</code> constraints should be generated.
|
||||
*/
|
||||
UNIQUE,
|
||||
|
||||
/**
|
||||
* Whether <code>FOREIGN KEY</code> constraints should be generated.
|
||||
*/
|
||||
FOREIGN_KEY,
|
||||
}
|
||||
@ -6834,6 +6834,62 @@ public interface DSLContext extends Scope , AutoCloseable {
|
||||
@Support
|
||||
Batch batchDelete(Collection<? extends UpdatableRecord<?>> records);
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// XXX DDL Statements from existing meta data
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Generate the complete creation script for the entire schema.
|
||||
*
|
||||
* @see #ddl(Schema, DDLFlag...)
|
||||
*/
|
||||
Queries ddl(Schema schema);
|
||||
|
||||
/**
|
||||
* Generate a partial creation script for the entire schema.
|
||||
* <p>
|
||||
* The following {@link DDLFlag} can be set:
|
||||
* <ul>
|
||||
* <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(Schema schema, DDLFlag... flags);
|
||||
|
||||
/**
|
||||
* Generate the complete creation script for a table.
|
||||
*
|
||||
* @see #ddl(Table, DDLFlag...)
|
||||
*/
|
||||
Queries ddl(Table<?> table);
|
||||
|
||||
/**
|
||||
* Generate a partial creation script for a table.
|
||||
* <p>
|
||||
* The following {@link DDLFlag} can be set:
|
||||
* <ul>
|
||||
* <li>{@link DDLFlag#TABLE}: If not set, this will generate nothing at all.
|
||||
* </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 inline with the table.
|
||||
* </li>
|
||||
* </ul>
|
||||
*/
|
||||
Queries ddl(Table<?> table, DDLFlag... flags);
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// XXX DDL Statements
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@ -40,12 +40,18 @@
|
||||
*/
|
||||
package org.jooq.impl;
|
||||
|
||||
import static org.jooq.DDLFlag.FOREIGN_KEY;
|
||||
import static org.jooq.DDLFlag.PRIMARY_KEY;
|
||||
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.EnumSet;
|
||||
import java.util.List;
|
||||
|
||||
import org.jooq.Constraint;
|
||||
import org.jooq.DDLFlag;
|
||||
import org.jooq.DSLContext;
|
||||
import org.jooq.ForeignKey;
|
||||
import org.jooq.Queries;
|
||||
@ -57,25 +63,37 @@ import org.jooq.UniqueKey;
|
||||
/**
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
public class DDL {
|
||||
class DDL {
|
||||
|
||||
private final DSLContext ctx;
|
||||
private final DSLContext ctx;
|
||||
private final EnumSet<DDLFlag> flags;
|
||||
|
||||
public DDL(DSLContext ctx) {
|
||||
DDL(DSLContext ctx, DDLFlag... flags) {
|
||||
this.ctx = ctx;
|
||||
this.flags = EnumSet.noneOf(DDLFlag.class);
|
||||
|
||||
for (DDLFlag flag : flags)
|
||||
this.flags.add(flag);
|
||||
}
|
||||
|
||||
public Queries queries(Table<?> table) {
|
||||
final Queries queries(Table<?> table) {
|
||||
List<Constraint> constraints = new ArrayList<Constraint>();
|
||||
|
||||
for (UniqueKey<?> key : table.getKeys())
|
||||
if (key.isPrimary())
|
||||
constraints.add(constraint(key.getName()).primaryKey(key.getFieldsArray()));
|
||||
else
|
||||
constraints.add(constraint(key.getName()).unique(key.getFieldsArray()));
|
||||
if (flags.contains(TABLE)) {
|
||||
if (flags.contains(PRIMARY_KEY))
|
||||
for (UniqueKey<?> key : table.getKeys())
|
||||
if (key.isPrimary())
|
||||
constraints.add(constraint(key.getName()).primaryKey(key.getFieldsArray()));
|
||||
|
||||
for (ForeignKey<?, ?> key : table.getReferences())
|
||||
constraints.add(constraint(key.getName()).foreignKey(key.getFieldsArray()).references(key.getKey().getTable(), key.getKey().getFieldsArray()));
|
||||
if (flags.contains(UNIQUE))
|
||||
for (UniqueKey<?> key : table.getKeys())
|
||||
if (!key.isPrimary())
|
||||
constraints.add(constraint(key.getName()).unique(key.getFieldsArray()));
|
||||
|
||||
if (flags.contains(FOREIGN_KEY))
|
||||
for (ForeignKey<?, ?> key : table.getReferences())
|
||||
constraints.add(constraint(key.getName()).foreignKey(key.getFieldsArray()).references(key.getKey().getTable(), key.getKey().getFieldsArray()));
|
||||
}
|
||||
|
||||
return DSL.queries(
|
||||
ctx.createTable(table)
|
||||
@ -84,29 +102,36 @@ public class DDL {
|
||||
);
|
||||
}
|
||||
|
||||
public Queries queries(Schema schema) {
|
||||
final Queries queries(Schema schema) {
|
||||
List<Query> queries = new ArrayList<Query>();
|
||||
|
||||
for (Table<?> table : schema.getTables()) {
|
||||
List<Constraint> constraints = new ArrayList<Constraint>();
|
||||
if (flags.contains(TABLE)) {
|
||||
for (Table<?> table : schema.getTables()) {
|
||||
List<Constraint> constraints = new ArrayList<Constraint>();
|
||||
|
||||
for (UniqueKey<?> key : table.getKeys())
|
||||
if (key.isPrimary())
|
||||
constraints.add(constraint(key.getName()).primaryKey(key.getFieldsArray()));
|
||||
else
|
||||
constraints.add(constraint(key.getName()).unique(key.getFieldsArray()));
|
||||
if (flags.contains(PRIMARY_KEY))
|
||||
for (UniqueKey<?> key : table.getKeys())
|
||||
if (key.isPrimary())
|
||||
constraints.add(constraint(key.getName()).primaryKey(key.getFieldsArray()));
|
||||
|
||||
queries.add(
|
||||
ctx.createTable(table)
|
||||
.columns(table.fields())
|
||||
.constraints(constraints)
|
||||
);
|
||||
if (flags.contains(UNIQUE))
|
||||
for (UniqueKey<?> key : table.getKeys())
|
||||
if (!key.isPrimary())
|
||||
constraints.add(constraint(key.getName()).unique(key.getFieldsArray()));
|
||||
|
||||
queries.add(
|
||||
ctx.createTable(table)
|
||||
.columns(table.fields())
|
||||
.constraints(constraints)
|
||||
);
|
||||
}
|
||||
|
||||
if (flags.contains(FOREIGN_KEY))
|
||||
for (Table<?> table : schema.getTables())
|
||||
for (ForeignKey<?, ?> key : table.getReferences())
|
||||
queries.add(ctx.alterTable(table).add(constraint(key.getName()).foreignKey(key.getFieldsArray()).references(key.getKey().getTable(), key.getKey().getFieldsArray())));
|
||||
}
|
||||
|
||||
for (Table<?> table : schema.getTables())
|
||||
for (ForeignKey<?, ?> key : table.getReferences())
|
||||
queries.add(ctx.alterTable(table).add(constraint(key.getName()).foreignKey(key.getFieldsArray()).references(key.getKey().getTable(), key.getKey().getFieldsArray())));
|
||||
|
||||
return DSL.queries(queries);
|
||||
}
|
||||
}
|
||||
|
||||
@ -96,6 +96,7 @@ import org.jooq.CreateSequenceFinalStep;
|
||||
import org.jooq.CreateTableAsStep;
|
||||
import org.jooq.CreateViewAsStep;
|
||||
import org.jooq.Cursor;
|
||||
import org.jooq.DDLFlag;
|
||||
import org.jooq.DSLContext;
|
||||
import org.jooq.DataType;
|
||||
import org.jooq.DeleteQuery;
|
||||
@ -2287,6 +2288,30 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri
|
||||
return batchDelete(records.toArray(new UpdatableRecord[records.size()]));
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// XXX DDL Statements from existing meta data
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@Override
|
||||
public Queries ddl(Schema schema) {
|
||||
return ddl(schema, DDLFlag.values());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Queries ddl(Schema schema, DDLFlag... flags) {
|
||||
return new DDL(this, flags).queries(schema);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Queries ddl(Table<?> table) {
|
||||
return ddl(table, DDLFlag.values());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Queries ddl(Table<?> table, DDLFlag... flags) {
|
||||
return new DDL(this, flags).queries(table);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// XXX DDL Statements
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
Loading…
Reference in New Issue
Block a user