From 2f517565b4b8b259e2620ce202e9e0a78b916be3 Mon Sep 17 00:00:00 2001 From: lukaseder Date: Thu, 19 Oct 2017 13:33:33 +0200 Subject: [PATCH] [#6131] Add plain SQL storage() clause to CREATE TABLE statements --- .../org/jooq/CreateTableOnCommitStep.java | 8 +- .../java/org/jooq/CreateTableStorageStep.java | 189 ++++++++++++++++++ .../java/org/jooq/impl/CreateTableImpl.java | 48 ++++- 3 files changed, 232 insertions(+), 13 deletions(-) create mode 100644 jOOQ/src/main/java/org/jooq/CreateTableStorageStep.java diff --git a/jOOQ/src/main/java/org/jooq/CreateTableOnCommitStep.java b/jOOQ/src/main/java/org/jooq/CreateTableOnCommitStep.java index eada98ac9d..d689f6a34f 100644 --- a/jOOQ/src/main/java/org/jooq/CreateTableOnCommitStep.java +++ b/jOOQ/src/main/java/org/jooq/CreateTableOnCommitStep.java @@ -44,7 +44,7 @@ * * @author Lukas Eder */ -public interface CreateTableOnCommitStep extends CreateTableFinalStep { +public interface CreateTableOnCommitStep extends CreateTableStorageStep { /** * Add an ON COMMIT DELETE ROWS clause. @@ -55,7 +55,7 @@ public interface CreateTableOnCommitStep extends CreateTableFinalStep { * @see DSL#createGlobalTemporaryTable(Table) */ @Support({ POSTGRES }) - CreateTableFinalStep onCommitDeleteRows(); + CreateTableStorageStep onCommitDeleteRows(); /** * Add an ON COMMIT PRESERVE ROWS clause. @@ -66,7 +66,7 @@ public interface CreateTableOnCommitStep extends CreateTableFinalStep { * @see DSL#createGlobalTemporaryTable(Table) */ @Support({ POSTGRES }) - CreateTableFinalStep onCommitPreserveRows(); + CreateTableStorageStep onCommitPreserveRows(); /** * Add an ON COMMIT DROP clause. @@ -77,5 +77,5 @@ public interface CreateTableOnCommitStep extends CreateTableFinalStep { * @see DSL#createGlobalTemporaryTable(Table) */ @Support({ POSTGRES }) - CreateTableFinalStep onCommitDrop(); + CreateTableStorageStep onCommitDrop(); } diff --git a/jOOQ/src/main/java/org/jooq/CreateTableStorageStep.java b/jOOQ/src/main/java/org/jooq/CreateTableStorageStep.java new file mode 100644 index 0000000000..d366ea2035 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/CreateTableStorageStep.java @@ -0,0 +1,189 @@ +/* + * 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 org.jooq.impl.DSL; + +/** + * A {@link Query} that can create tables. + * + * @author Lukas Eder + */ +public interface CreateTableStorageStep extends CreateTableFinalStep { + + /** + * Add vendor-specific storage clauses to the CREATE TABLE statement. + *

+ * Storage clauses will always be appended to the end of everything + * else that jOOQ renders, including possibly other storage clauses, such as + * {@link CreateTableOnCommitStep#onCommitDeleteRows()} or similar clauses. + * If custom storage clauses should be mixed with jOOQ-provided storage + * clauses, it is recommended not to use the jOOQ API and use the custom + * clause API for all storage clauses instead. + *

+ * Storage clauses will be separated from previous elements by a separator + * (whitespace or newline) to ensure syntactic integrity. + *

+ * Example usage: + *

+ *

+     * DSL.using(configuration)
+     *    .createTable("t")
+     *    .column(field("i", SQLDataType.INTEGER))
+     *    .storage("TABLESPACE my_tablespace")
+     *    .execute();
+     * 
+ *

+ * NOTE: When inserting plain SQL into jOOQ objects, you must + * guarantee syntax integrity. You may also create the possibility of + * malicious SQL injection. Be sure to properly use bind variables and/or + * escape literals when concatenated into SQL clauses! One way to escape + * literals is to use {@link DSL#name(String...)} and similar methods + * + * @param sql The SQL + */ + @Support + @PlainSQL + CreateTableFinalStep storage(SQL sql); + + /** + * Add vendor-specific storage clauses to the CREATE TABLE statement. + *

+ * Storage clauses will always be appended to the end of everything + * else that jOOQ renders, including possibly other storage clauses, such as + * {@link CreateTableOnCommitStep#onCommitDeleteRows()} or similar clauses. + * If custom storage clauses should be mixed with jOOQ-provided storage + * clauses, it is recommended not to use the jOOQ API and use the custom + * clause API for all storage clauses instead. + *

+ * Storage clauses will be separated from previous elements by a separator + * (whitespace or newline) to ensure syntactic integrity. + *

+ * Example usage: + *

+ *

+     * DSL.using(configuration)
+     *    .createTable("t")
+     *    .column(field("i", SQLDataType.INTEGER))
+     *    .storage("TABLESPACE my_tablespace")
+     *    .execute();
+     * 
+ *

+ * NOTE: When inserting plain SQL into jOOQ objects, you must + * guarantee syntax integrity. You may also create the possibility of + * malicious SQL injection. Be sure to properly use bind variables and/or + * escape literals when concatenated into SQL clauses! One way to escape + * literals is to use {@link DSL#name(String...)} and similar methods + * + * @param sql The SQL + */ + @Support + @PlainSQL + CreateTableFinalStep storage(String sql); + + /** + * Add vendor-specific storage clauses to the CREATE TABLE statement. + *

+ * Storage clauses will always be appended to the end of everything + * else that jOOQ renders, including possibly other storage clauses, such as + * {@link CreateTableOnCommitStep#onCommitDeleteRows()} or similar clauses. + * If custom storage clauses should be mixed with jOOQ-provided storage + * clauses, it is recommended not to use the jOOQ API and use the custom + * clause API for all storage clauses instead. + *

+ * Storage clauses will be separated from previous elements by a separator + * (whitespace or newline) to ensure syntactic integrity. + *

+ * Example usage: + *

+ *

+     * DSL.using(configuration)
+     *    .createTable("t")
+     *    .column(field("i", SQLDataType.INTEGER))
+     *    .storage("TABLESPACE my_tablespace")
+     *    .execute();
+     * 
+ *

+ * NOTE: When inserting plain SQL into jOOQ objects, you must + * guarantee syntax integrity. You may also create the possibility of + * malicious SQL injection. Be sure to properly use bind variables and/or + * escape literals when concatenated into SQL clauses! One way to escape + * literals is to use {@link DSL#name(String...)} and similar methods + * + * @param sql The SQL + * @param bindings The bindings + */ + @Support + @PlainSQL + CreateTableFinalStep storage(String sql, Object... bindings); + + /** + * Add vendor-specific storage clauses to the CREATE TABLE + * statement. + *

+ * Storage clauses will always be appended to the end of everything + * else that jOOQ renders, including possibly other storage clauses, such as + * {@link CreateTableOnCommitStep#onCommitDeleteRows()} or similar clauses. + * If custom storage clauses should be mixed with jOOQ-provided storage + * clauses, it is recommended not to use the jOOQ API and use the custom + * clause API for all storage clauses instead. + *

+ * Storage clauses will be separated from previous elements by a separator + * (whitespace or newline) to ensure syntactic integrity. + *

+ * Example usage: + *

+ *

+     * DSL.using(configuration)
+     *    .createTable("t")
+     *    .column(field("i", SQLDataType.INTEGER))
+     *    .storage("TABLESPACE my_tablespace")
+     *    .execute();
+     * 
+ *

+ * NOTE: When inserting plain SQL into jOOQ objects, you must + * guarantee syntax integrity. You may also create the possibility of + * malicious SQL injection. Be sure to properly use bind variables and/or + * escape literals when concatenated into SQL clauses! One way to escape + * literals is to use {@link DSL#name(String...)} and similar methods + * + * @param sql The SQL + * @param parts The {@link QueryPart} objects that are rendered at the + * {numbered placeholder} locations + */ + @Support + @PlainSQL + CreateTableFinalStep storage(String sql, QueryPart... parts); +} diff --git a/jOOQ/src/main/java/org/jooq/impl/CreateTableImpl.java b/jOOQ/src/main/java/org/jooq/impl/CreateTableImpl.java index c96791a9cb..5654548a7c 100644 --- a/jOOQ/src/main/java/org/jooq/impl/CreateTableImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/CreateTableImpl.java @@ -55,6 +55,7 @@ import static org.jooq.impl.DSL.field; import static org.jooq.impl.DSL.insertInto; import static org.jooq.impl.DSL.name; +import static org.jooq.impl.DSL.sql; import static org.jooq.impl.Keywords.K_AS; import static org.jooq.impl.Keywords.K_CREATE; import static org.jooq.impl.Keywords.K_GLOBAL_TEMPORARY; @@ -87,10 +88,13 @@ import org.jooq.CreateTableConstraintStep; import org.jooq.CreateTableFinalStep; import org.jooq.CreateTableOnCommitStep; +import org.jooq.CreateTableStorageStep; import org.jooq.DataType; import org.jooq.Field; import org.jooq.Name; +import org.jooq.QueryPart; import org.jooq.Record; +import org.jooq.SQL; import org.jooq.SQLDialect; import org.jooq.Select; import org.jooq.Table; @@ -102,7 +106,8 @@ final class CreateTableImpl extends AbstractQuery implements // Cascading interface implementations for CREATE TABLE behaviour CreateTableAsStep, - CreateTableColumnStep { + CreateTableColumnStep, + CreateTableStorageStep { /** * Generated UID @@ -123,6 +128,7 @@ final class CreateTableImpl extends AbstractQuery implements private final boolean temporary; private final boolean ifNotExists; private OnCommit onCommit; + private SQL storage; CreateTableImpl(Configuration configuration, Table table, boolean temporary, boolean ifNotExists) { super(configuration); @@ -200,23 +206,44 @@ public final CreateTableConstraintStep constraints(Collection ctx) { private final void accept0(Context ctx) { + ctx.start(CREATE_TABLE); + if (select != null) { @@ -251,7 +280,6 @@ private final void accept0(Context ctx) { acceptCreateTableAsSelect(ctx); } else { - ctx.start(CREATE_TABLE); toSQLCreateTableName(ctx); ctx.sql('(') .start(CREATE_TABLE_COLUMNS) @@ -288,12 +316,16 @@ private final void accept0(Context ctx) { .sql(')'); toSQLOnCommit(ctx); - ctx.end(CREATE_TABLE); } + + if (storage != null) + ctx.formatSeparator() + .visit(storage); + + ctx.end(CREATE_TABLE); } private final void acceptCreateTableAsSelect(Context ctx) { - ctx.start(CREATE_TABLE); toSQLCreateTableName(ctx); toSQLOnCommit(ctx); ctx.formatSeparator() @@ -319,8 +351,6 @@ private final void acceptCreateTableAsSelect(Context ctx) { ctx.sql(' ') .visit(K_WITH_DATA); } - - ctx.end(CREATE_TABLE); }