[jOOQ/jOOQ#8656] Add org.jooq.DDLExportConfiguration
This commit is contained in:
parent
fce9626b13
commit
17df39efbc
79
jOOQ/src/main/java/org/jooq/DDLExportConfiguration.java
Normal file
79
jOOQ/src/main/java/org/jooq/DDLExportConfiguration.java
Normal file
@ -0,0 +1,79 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* A configuration type for use with the various {@link DSLContext#ddl(Catalog)}
|
||||
* methods.
|
||||
*
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
public final class DDLExportConfiguration {
|
||||
|
||||
final EnumSet<DDLFlag> flags;
|
||||
|
||||
public DDLExportConfiguration() {
|
||||
this(
|
||||
EnumSet.allOf(DDLFlag.class)
|
||||
);
|
||||
}
|
||||
|
||||
private DDLExportConfiguration(
|
||||
Set<DDLFlag> flags
|
||||
) {
|
||||
this.flags = EnumSet.copyOf(flags);
|
||||
}
|
||||
|
||||
public final Set<DDLFlag> flags() {
|
||||
return Collections.unmodifiableSet(flags);
|
||||
}
|
||||
|
||||
public final DDLExportConfiguration flags(DDLFlag... newFlags) {
|
||||
return flags(Arrays.asList(newFlags));
|
||||
}
|
||||
|
||||
public final DDLExportConfiguration flags(Collection<DDLFlag> newFlags) {
|
||||
return new DDLExportConfiguration(EnumSet.copyOf(newFlags));
|
||||
}
|
||||
}
|
||||
@ -8264,7 +8264,7 @@ public interface DSLContext extends Scope , AutoCloseable {
|
||||
/**
|
||||
* Generate the complete creation script for the entire catalog.
|
||||
*
|
||||
* @see #ddl(Catalog, DDLFlag...)
|
||||
* @see #ddl(Catalog, DDLExportConfiguration)
|
||||
*/
|
||||
Queries ddl(Catalog catalog);
|
||||
|
||||
@ -8287,12 +8287,19 @@ public interface DSLContext extends Scope , AutoCloseable {
|
||||
* a separate <code>ALTER TABLE .. ADD CONSTRAINT</code> statement.</li>
|
||||
* </ul>
|
||||
*/
|
||||
Queries ddl(Catalog schema, DDLExportConfiguration configuration);
|
||||
|
||||
/**
|
||||
* Generate a partial creation script for the entire catalog.
|
||||
*
|
||||
* @see #ddl(Catalog, DDLExportConfiguration)
|
||||
*/
|
||||
Queries ddl(Catalog schema, DDLFlag... flags);
|
||||
|
||||
/**
|
||||
* Generate the complete creation script for the entire schema.
|
||||
*
|
||||
* @see #ddl(Schema, DDLFlag...)
|
||||
* @see #ddl(Schema, DDLExportConfiguration)
|
||||
*/
|
||||
Queries ddl(Schema schema);
|
||||
|
||||
@ -8313,12 +8320,19 @@ public interface DSLContext extends Scope , AutoCloseable {
|
||||
* a separate <code>ALTER TABLE .. ADD CONSTRAINT</code> statement.</li>
|
||||
* </ul>
|
||||
*/
|
||||
Queries ddl(Schema schema, DDLExportConfiguration configuration);
|
||||
|
||||
/**
|
||||
* Generate a partial creation script for the entire schema.
|
||||
*
|
||||
* @see #ddl(Schema, DDLExportConfiguration)
|
||||
*/
|
||||
Queries ddl(Schema schema, DDLFlag... flags);
|
||||
|
||||
/**
|
||||
* Generate the complete creation script for a table.
|
||||
*
|
||||
* @see #ddl(Table, DDLFlag...)
|
||||
* @see #ddl(Table, DDLExportConfiguration)
|
||||
*/
|
||||
Queries ddl(Table<?> table);
|
||||
|
||||
@ -8339,12 +8353,19 @@ public interface DSLContext extends Scope , AutoCloseable {
|
||||
* </li>
|
||||
* </ul>
|
||||
*/
|
||||
Queries ddl(Table<?> table, DDLExportConfiguration configuration);
|
||||
|
||||
/**
|
||||
* Generate a partial creation script for a table.
|
||||
*
|
||||
* @see #ddl(Table, DDLExportConfiguration)
|
||||
*/
|
||||
Queries ddl(Table<?> table, DDLFlag... flags);
|
||||
|
||||
/**
|
||||
* Generate the complete creation script for tables.
|
||||
*
|
||||
* @see #ddl(Table[], DDLFlag...)
|
||||
* @see #ddl(Table[], DDLExportConfiguration)
|
||||
*/
|
||||
Queries ddl(Table<?>... tables);
|
||||
|
||||
@ -8366,12 +8387,19 @@ public interface DSLContext extends Scope , AutoCloseable {
|
||||
* </ul>
|
||||
* </p>
|
||||
*/
|
||||
Queries ddl(Table<?>[] tables, DDLExportConfiguration configuration);
|
||||
|
||||
/**
|
||||
* Generate the complete creation script for tables.
|
||||
*
|
||||
* @see #ddl(Table[], DDLExportConfiguration)
|
||||
*/
|
||||
Queries ddl(Table<?>[] tables, DDLFlag... flags);
|
||||
|
||||
/**
|
||||
* Generate the complete creation script for tables.
|
||||
*
|
||||
* @see #ddl(Collection, DDLFlag...)
|
||||
* @see #ddl(Collection, DDLExportConfiguration)
|
||||
*/
|
||||
Queries ddl(Collection<? extends Table<?>> tables);
|
||||
|
||||
@ -8395,6 +8423,13 @@ public interface DSLContext extends Scope , AutoCloseable {
|
||||
*/
|
||||
Queries ddl(Collection<? extends Table<?>> tables, DDLFlag... flags);
|
||||
|
||||
/**
|
||||
* Generate the complete creation script for tables.
|
||||
*
|
||||
* @see #ddl(Collection, DDLExportConfiguration)
|
||||
*/
|
||||
Queries ddl(Collection<? extends Table<?>> tables, DDLExportConfiguration configuration);
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// XXX Session Statements
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@ -47,12 +47,11 @@ 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.DDLExportConfiguration;
|
||||
import org.jooq.DSLContext;
|
||||
import org.jooq.Field;
|
||||
import org.jooq.ForeignKey;
|
||||
@ -68,15 +67,12 @@ import org.jooq.tools.StringUtils;
|
||||
*/
|
||||
final class DDL {
|
||||
|
||||
private final DSLContext ctx;
|
||||
private final EnumSet<DDLFlag> flags;
|
||||
private final DSLContext ctx;
|
||||
private final DDLExportConfiguration configuration;
|
||||
|
||||
DDL(DSLContext ctx, DDLFlag... flags) {
|
||||
DDL(DSLContext ctx, DDLExportConfiguration configuration) {
|
||||
this.ctx = ctx;
|
||||
this.flags = EnumSet.noneOf(DDLFlag.class);
|
||||
|
||||
for (DDLFlag flag : flags)
|
||||
this.flags.add(flag);
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
private final Query createTable(Table<?> table) {
|
||||
@ -108,7 +104,7 @@ final class DDL {
|
||||
private List<Constraint> primaryKeys(Table<?> table) {
|
||||
List<Constraint> result = new ArrayList<Constraint>();
|
||||
|
||||
if (flags.contains(PRIMARY_KEY))
|
||||
if (configuration.flags().contains(PRIMARY_KEY))
|
||||
for (UniqueKey<?> key : table.getKeys())
|
||||
if (key.isPrimary())
|
||||
result.add(constraint(key.getName()).primaryKey(key.getFieldsArray()));
|
||||
@ -119,7 +115,7 @@ final class DDL {
|
||||
private List<Constraint> uniqueKeys(Table<?> table) {
|
||||
List<Constraint> result = new ArrayList<Constraint>();
|
||||
|
||||
if (flags.contains(UNIQUE))
|
||||
if (configuration.flags().contains(UNIQUE))
|
||||
for (UniqueKey<?> key : table.getKeys())
|
||||
if (!key.isPrimary())
|
||||
result.add(constraint(key.getName()).unique(key.getFieldsArray()));
|
||||
@ -130,7 +126,7 @@ final class DDL {
|
||||
private List<Constraint> foreignKeys(Table<?> table) {
|
||||
List<Constraint> result = new ArrayList<Constraint>();
|
||||
|
||||
if (flags.contains(FOREIGN_KEY))
|
||||
if (configuration.flags().contains(FOREIGN_KEY))
|
||||
for (ForeignKey<?, ?> key : table.getReferences())
|
||||
result.add(constraint(key.getName()).foreignKey(key.getFieldsArray()).references(key.getKey().getTable(), key.getKey().getFieldsArray()));
|
||||
|
||||
@ -141,7 +137,7 @@ final class DDL {
|
||||
List<Query> queries = new ArrayList<Query>();
|
||||
|
||||
for (Table<?> table : tables) {
|
||||
if (flags.contains(TABLE))
|
||||
if (configuration.flags().contains(TABLE))
|
||||
queries.add(createTable(table));
|
||||
else
|
||||
queries.addAll(alterTableAddConstraints(table));
|
||||
@ -155,7 +151,7 @@ final class DDL {
|
||||
private List<Query> commentOn(Table<?> table) {
|
||||
List<Query> result = new ArrayList<Query>();
|
||||
|
||||
if (flags.contains(COMMENT)) {
|
||||
if (configuration.flags().contains(COMMENT)) {
|
||||
String tComment = table.getComment();
|
||||
|
||||
if (!StringUtils.isEmpty(tComment))
|
||||
@ -175,10 +171,10 @@ final class DDL {
|
||||
final Queries queries(Schema schema) {
|
||||
List<Query> queries = new ArrayList<Query>();
|
||||
|
||||
if (flags.contains(SCHEMA) && !StringUtils.isBlank(schema.getName()))
|
||||
if (configuration.flags().contains(SCHEMA) && !StringUtils.isBlank(schema.getName()))
|
||||
queries.add(ctx.createSchema(schema.getName()));
|
||||
|
||||
if (flags.contains(TABLE)) {
|
||||
if (configuration.flags().contains(TABLE)) {
|
||||
for (Table<?> table : schema.getTables()) {
|
||||
List<Constraint> constraints = new ArrayList<Constraint>();
|
||||
|
||||
@ -193,23 +189,23 @@ final class DDL {
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (flags.contains(PRIMARY_KEY))
|
||||
if (configuration.flags().contains(PRIMARY_KEY))
|
||||
for (Table<?> table : schema.getTables())
|
||||
for (Constraint constraint : primaryKeys(table))
|
||||
queries.add(ctx.alterTable(table).add(constraint));
|
||||
|
||||
if (flags.contains(UNIQUE))
|
||||
if (configuration.flags().contains(UNIQUE))
|
||||
for (Table<?> table : schema.getTables())
|
||||
for (Constraint constraint : uniqueKeys(table))
|
||||
queries.add(ctx.alterTable(table).add(constraint));
|
||||
}
|
||||
|
||||
if (flags.contains(FOREIGN_KEY))
|
||||
if (configuration.flags().contains(FOREIGN_KEY))
|
||||
for (Table<?> table : schema.getTables())
|
||||
for (Constraint constraint : foreignKeys(table))
|
||||
queries.add(ctx.alterTable(table).add(constraint));
|
||||
|
||||
if (flags.contains(COMMENT))
|
||||
if (configuration.flags().contains(COMMENT))
|
||||
for (Table<?> table : schema.getTables())
|
||||
queries.addAll(commentOn(table));
|
||||
|
||||
|
||||
@ -120,6 +120,7 @@ import org.jooq.CreateTableColumnStep;
|
||||
import org.jooq.CreateTypeStep;
|
||||
import org.jooq.CreateViewAsStep;
|
||||
import org.jooq.Cursor;
|
||||
import org.jooq.DDLExportConfiguration;
|
||||
import org.jooq.DDLFlag;
|
||||
import org.jooq.DSLContext;
|
||||
import org.jooq.DataType;
|
||||
@ -2754,22 +2755,32 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri
|
||||
|
||||
@Override
|
||||
public Queries ddl(Catalog catalog) {
|
||||
return ddl(catalog, DDLFlag.values());
|
||||
return ddl(catalog, new DDLExportConfiguration());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Queries ddl(Catalog schema, DDLFlag... flags) {
|
||||
return new DDL(this, flags).queries(schema);
|
||||
public Queries ddl(Catalog catalog, DDLFlag... flags) {
|
||||
return ddl(catalog, new DDLExportConfiguration().flags(flags));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Queries ddl(Catalog catalog, DDLExportConfiguration exportConfiguration) {
|
||||
return new DDL(this, exportConfiguration).queries(catalog);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Queries ddl(Schema schema) {
|
||||
return ddl(schema, DDLFlag.values());
|
||||
return ddl(schema, new DDLExportConfiguration());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Queries ddl(Schema schema, DDLFlag... flags) {
|
||||
return new DDL(this, flags).queries(schema);
|
||||
return ddl(schema, new DDLExportConfiguration().flags(flags));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Queries ddl(Schema schema, DDLExportConfiguration exportConfiguration) {
|
||||
return new DDL(this, exportConfiguration).queries(schema);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -2782,19 +2793,29 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri
|
||||
return ddl(new Table[] { table }, flags);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Queries ddl(Table<?> table, DDLExportConfiguration exportConfiguration) {
|
||||
return ddl(new Table[] { table }, exportConfiguration);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Queries ddl(Table... tables) {
|
||||
return ddl(tables, DDLFlag.values());
|
||||
return ddl(tables, new DDLExportConfiguration());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Queries ddl(Table[] tables, DDLFlag... flags) {
|
||||
return new DDL(this, flags).queries(tables);
|
||||
return ddl(tables, new DDLExportConfiguration().flags(flags));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Queries ddl(Table[] tables, DDLExportConfiguration exportConfiguration) {
|
||||
return new DDL(this, exportConfiguration).queries(tables);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Queries ddl(Collection<? extends Table<?>> tables) {
|
||||
return ddl(tables.toArray(EMPTY_TABLE), DDLFlag.values());
|
||||
return ddl(tables.toArray(EMPTY_TABLE));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -2802,6 +2823,11 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri
|
||||
return ddl(tables.toArray(EMPTY_TABLE), flags);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Queries ddl(Collection<? extends Table<?>> tables, DDLExportConfiguration exportConfiguration) {
|
||||
return ddl(tables.toArray(EMPTY_TABLE), exportConfiguration);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// XXX DDL Statements
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
Loading…
Reference in New Issue
Block a user