From 58b9bb3770c2764e321013a8d3d1e9dbc2ae5759 Mon Sep 17 00:00:00 2001
From: Timur Shaidullin
Date: Tue, 5 Dec 2017 21:19:08 +0300
Subject: [PATCH] [#6867] Added a possibility to export several DDL of tables
---
jOOQ/src/main/java/org/jooq/DSLContext.java | 56 +++++++++++++++++++
jOOQ/src/main/java/org/jooq/impl/DDL.java | 22 ++++++--
.../java/org/jooq/impl/DefaultDSLContext.java | 21 +++++++
jOOQ/src/main/java/org/jooq/impl/Tools.java | 1 +
4 files changed, 95 insertions(+), 5 deletions(-)
diff --git a/jOOQ/src/main/java/org/jooq/DSLContext.java b/jOOQ/src/main/java/org/jooq/DSLContext.java
index e6767cd031..d48f5b216d 100644
--- a/jOOQ/src/main/java/org/jooq/DSLContext.java
+++ b/jOOQ/src/main/java/org/jooq/DSLContext.java
@@ -8362,6 +8362,62 @@ public interface DSLContext extends Scope , AutoCloseable {
*/
Queries ddl(Table> table, DDLFlag... flags);
+ /**
+ * Generate the complete creation script for tables.
+ *
+ * @see #ddl(Table[], DDLFlag...)
+ */
+ Queries ddl(Table[] tables);
+
+ /**
+ * Generate the complete creation script for tables.
+ *
+ *
+ * The following {@link DDLFlag} can be set:
+ *
+ * - {@link DDLFlag#TABLE}: If not set, this will generate nothing at all.
+ *
+ * - {@link DDLFlag#PRIMARY_KEY}: If set, a potential
+ *
PRIMARY KEY constraint is specified inline with the table.
+ *
+ * - {@link DDLFlag#UNIQUE}: If set, any potential
UNIQUE
+ * constraint is specified inline with the table.
+ * - {@link DDLFlag#FOREIGN_KEY}: If set, any potential
+ *
FOREIGN KEY constraint is specified inline with the table.
+ *
+ *
+ *
+ */
+ Queries ddl(Table[] tables, DDLFlag... flags);
+
+ /**
+ * Generate the complete creation script for tables.
+ *
+ * @see #ddl(Collection, DDLFlag...)
+ */
+ Queries ddl(Collection extends Table>> tables);
+
+ /**
+ * Generate the complete creation script for tables.
+ *
+ *
+ * The following {@link DDLFlag} can be set:
+ *
+ * - {@link DDLFlag#TABLE}: If not set, this will generate nothing at all.
+ *
+ * - {@link DDLFlag#PRIMARY_KEY}: If set, a potential
+ *
PRIMARY KEY constraint is specified inline with the table.
+ *
+ * - {@link DDLFlag#UNIQUE}: If set, any potential
UNIQUE
+ * constraint is specified inline with the table.
+ * - {@link DDLFlag#FOREIGN_KEY}: If set, any potential
+ *
FOREIGN KEY constraint is specified inline with the table.
+ *
+ *
+ *
+ */
+ Queries ddl(Collection extends Table>> tables, DDLFlag... flags);
+
// -------------------------------------------------------------------------
// XXX DDL Statements
// -------------------------------------------------------------------------
diff --git a/jOOQ/src/main/java/org/jooq/impl/DDL.java b/jOOQ/src/main/java/org/jooq/impl/DDL.java
index 84a8f72c00..a271c8fb2b 100644
--- a/jOOQ/src/main/java/org/jooq/impl/DDL.java
+++ b/jOOQ/src/main/java/org/jooq/impl/DDL.java
@@ -78,6 +78,10 @@ final class DDL {
}
final Queries queries(Table> table) {
+ return ctx.queries(createTable(table));
+ }
+
+ private final Query createTable(Table> table) {
List constraints = new ArrayList();
if (flags.contains(TABLE)) {
@@ -96,11 +100,19 @@ final class DDL {
constraints.add(constraint(key.getName()).foreignKey(key.getFieldsArray()).references(key.getKey().getTable(), key.getKey().getFieldsArray()));
}
- return ctx.queries(
- ctx.createTable(table)
- .columns(table.fields())
- .constraints(constraints)
- );
+ return ctx.createTable(table)
+ .columns(table.fields())
+ .constraints(constraints);
+ }
+
+ final Queries queries(Table[] tables) {
+ List queries = new ArrayList();
+
+ for (Table table : tables) {
+ queries.add(createTable(table));
+ }
+
+ return ctx.queries(queries);
}
final Queries queries(Schema schema) {
diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java
index 2412959122..64b2d5599c 100644
--- a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java
+++ b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java
@@ -52,6 +52,7 @@ import static org.jooq.impl.DSL.sql;
import static org.jooq.impl.DSL.table;
import static org.jooq.impl.DSL.zero;
import static org.jooq.impl.Tools.EMPTY_QUERY;
+import static org.jooq.impl.Tools.EMPTY_TABLE;
import static org.jooq.impl.Tools.EMPTY_TABLE_RECORD;
import static org.jooq.impl.Tools.EMPTY_UPDATABLE_RECORD;
import static org.jooq.impl.Tools.blocking;
@@ -2817,6 +2818,26 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri
return new DDL(this, flags).queries(table);
}
+ @Override
+ public Queries ddl(Table[] tables) {
+ return ddl(tables, DDLFlag.values());
+ }
+
+ @Override
+ public Queries ddl(Table[] tables, DDLFlag... flags) {
+ return new DDL(this, flags).queries(tables);
+ }
+
+ @Override
+ public Queries ddl(Collection extends Table>> tables) {
+ return ddl(tables.toArray(EMPTY_TABLE), DDLFlag.values());
+ }
+
+ @Override
+ public Queries ddl(Collection extends Table>> tables, DDLFlag... flags) {
+ return ddl(tables.toArray(EMPTY_TABLE), flags);
+ }
+
// -------------------------------------------------------------------------
// XXX DDL Statements
// -------------------------------------------------------------------------
diff --git a/jOOQ/src/main/java/org/jooq/impl/Tools.java b/jOOQ/src/main/java/org/jooq/impl/Tools.java
index b761bbefa3..3d77619ab7 100644
--- a/jOOQ/src/main/java/org/jooq/impl/Tools.java
+++ b/jOOQ/src/main/java/org/jooq/impl/Tools.java
@@ -262,6 +262,7 @@ final class Tools {
static final RowN[] EMPTY_ROWN = {};
static final SortField>[] EMPTY_SORTFIELD = {};
static final String[] EMPTY_STRING = {};
+ static final Table>[] EMPTY_TABLE = {};
static final TableRecord>[] EMPTY_TABLE_RECORD = {};
static final UpdatableRecord>[] EMPTY_UPDATABLE_RECORD = {};