Skip to content

Commit

Permalink
[#6131] Add plain SQL storage() clause to CREATE TABLE statements
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaseder committed Oct 19, 2017
1 parent d280225 commit 2f51756
Show file tree
Hide file tree
Showing 3 changed files with 232 additions and 13 deletions.
8 changes: 4 additions & 4 deletions jOOQ/src/main/java/org/jooq/CreateTableOnCommitStep.java
Expand Up @@ -44,7 +44,7 @@
* *
* @author Lukas Eder * @author Lukas Eder
*/ */
public interface CreateTableOnCommitStep extends CreateTableFinalStep { public interface CreateTableOnCommitStep extends CreateTableStorageStep {


/** /**
* Add an <code>ON COMMIT DELETE ROWS</code> clause. * Add an <code>ON COMMIT DELETE ROWS</code> clause.
Expand All @@ -55,7 +55,7 @@ public interface CreateTableOnCommitStep extends CreateTableFinalStep {
* @see DSL#createGlobalTemporaryTable(Table) * @see DSL#createGlobalTemporaryTable(Table)
*/ */
@Support({ POSTGRES }) @Support({ POSTGRES })
CreateTableFinalStep onCommitDeleteRows(); CreateTableStorageStep onCommitDeleteRows();


/** /**
* Add an <code>ON COMMIT PRESERVE ROWS</code> clause. * Add an <code>ON COMMIT PRESERVE ROWS</code> clause.
Expand All @@ -66,7 +66,7 @@ public interface CreateTableOnCommitStep extends CreateTableFinalStep {
* @see DSL#createGlobalTemporaryTable(Table) * @see DSL#createGlobalTemporaryTable(Table)
*/ */
@Support({ POSTGRES }) @Support({ POSTGRES })
CreateTableFinalStep onCommitPreserveRows(); CreateTableStorageStep onCommitPreserveRows();


/** /**
* Add an <code>ON COMMIT DROP</code> clause. * Add an <code>ON COMMIT DROP</code> clause.
Expand All @@ -77,5 +77,5 @@ public interface CreateTableOnCommitStep extends CreateTableFinalStep {
* @see DSL#createGlobalTemporaryTable(Table) * @see DSL#createGlobalTemporaryTable(Table)
*/ */
@Support({ POSTGRES }) @Support({ POSTGRES })
CreateTableFinalStep onCommitDrop(); CreateTableStorageStep onCommitDrop();
} }
189 changes: 189 additions & 0 deletions 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 <code>CREATE TABLE</code> statement.
* <p>
* Storage clauses will always be appended to the <em>end</em> 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.
* <p>
* Storage clauses will be separated from previous elements by a separator
* (whitespace or newline) to ensure syntactic integrity.
* <p>
* Example usage:
* <p>
* <code><pre>
* DSL.using(configuration)
* .createTable("t")
* .column(field("i", SQLDataType.INTEGER))
* .storage("TABLESPACE my_tablespace")
* .execute();
* </pre></code>
* <p>
* <b>NOTE</b>: 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 <code>CREATE TABLE</code> statement.
* <p>
* Storage clauses will always be appended to the <em>end</em> 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.
* <p>
* Storage clauses will be separated from previous elements by a separator
* (whitespace or newline) to ensure syntactic integrity.
* <p>
* Example usage:
* <p>
* <code><pre>
* DSL.using(configuration)
* .createTable("t")
* .column(field("i", SQLDataType.INTEGER))
* .storage("TABLESPACE my_tablespace")
* .execute();
* </pre></code>
* <p>
* <b>NOTE</b>: 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 <code>CREATE TABLE</code> statement.
* <p>
* Storage clauses will always be appended to the <em>end</em> 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.
* <p>
* Storage clauses will be separated from previous elements by a separator
* (whitespace or newline) to ensure syntactic integrity.
* <p>
* Example usage:
* <p>
* <code><pre>
* DSL.using(configuration)
* .createTable("t")
* .column(field("i", SQLDataType.INTEGER))
* .storage("TABLESPACE my_tablespace")
* .execute();
* </pre></code>
* <p>
* <b>NOTE</b>: 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 <code>CREATE TABLE</code>
* statement.
* <p>
* Storage clauses will always be appended to the <em>end</em> 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.
* <p>
* Storage clauses will be separated from previous elements by a separator
* (whitespace or newline) to ensure syntactic integrity.
* <p>
* Example usage:
* <p>
* <code><pre>
* DSL.using(configuration)
* .createTable("t")
* .column(field("i", SQLDataType.INTEGER))
* .storage("TABLESPACE my_tablespace")
* .execute();
* </pre></code>
* <p>
* <b>NOTE</b>: 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);
}
48 changes: 39 additions & 9 deletions jOOQ/src/main/java/org/jooq/impl/CreateTableImpl.java
Expand Up @@ -55,6 +55,7 @@
import static org.jooq.impl.DSL.field; import static org.jooq.impl.DSL.field;
import static org.jooq.impl.DSL.insertInto; import static org.jooq.impl.DSL.insertInto;
import static org.jooq.impl.DSL.name; 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_AS;
import static org.jooq.impl.Keywords.K_CREATE; import static org.jooq.impl.Keywords.K_CREATE;
import static org.jooq.impl.Keywords.K_GLOBAL_TEMPORARY; import static org.jooq.impl.Keywords.K_GLOBAL_TEMPORARY;
Expand Down Expand Up @@ -87,10 +88,13 @@
import org.jooq.CreateTableConstraintStep; import org.jooq.CreateTableConstraintStep;
import org.jooq.CreateTableFinalStep; import org.jooq.CreateTableFinalStep;
import org.jooq.CreateTableOnCommitStep; import org.jooq.CreateTableOnCommitStep;
import org.jooq.CreateTableStorageStep;
import org.jooq.DataType; import org.jooq.DataType;
import org.jooq.Field; import org.jooq.Field;
import org.jooq.Name; import org.jooq.Name;
import org.jooq.QueryPart;
import org.jooq.Record; import org.jooq.Record;
import org.jooq.SQL;
import org.jooq.SQLDialect; import org.jooq.SQLDialect;
import org.jooq.Select; import org.jooq.Select;
import org.jooq.Table; import org.jooq.Table;
Expand All @@ -102,7 +106,8 @@ final class CreateTableImpl<R extends Record> extends AbstractQuery implements


// Cascading interface implementations for CREATE TABLE behaviour // Cascading interface implementations for CREATE TABLE behaviour
CreateTableAsStep<R>, CreateTableAsStep<R>,
CreateTableColumnStep { CreateTableColumnStep,
CreateTableStorageStep {


/** /**
* Generated UID * Generated UID
Expand All @@ -123,6 +128,7 @@ final class CreateTableImpl<R extends Record> extends AbstractQuery implements
private final boolean temporary; private final boolean temporary;
private final boolean ifNotExists; private final boolean ifNotExists;
private OnCommit onCommit; private OnCommit onCommit;
private SQL storage;


CreateTableImpl(Configuration configuration, Table<?> table, boolean temporary, boolean ifNotExists) { CreateTableImpl(Configuration configuration, Table<?> table, boolean temporary, boolean ifNotExists) {
super(configuration); super(configuration);
Expand Down Expand Up @@ -200,23 +206,44 @@ public final CreateTableConstraintStep constraints(Collection<? extends Constrai
} }


@Override @Override
public final CreateTableFinalStep onCommitDeleteRows() { public final CreateTableStorageStep onCommitDeleteRows() {
onCommit = OnCommit.DELETE_ROWS; onCommit = OnCommit.DELETE_ROWS;
return this; return this;
} }


@Override @Override
public final CreateTableFinalStep onCommitPreserveRows() { public final CreateTableStorageStep onCommitPreserveRows() {
onCommit = OnCommit.PRESERVE_ROWS; onCommit = OnCommit.PRESERVE_ROWS;
return this; return this;
} }


@Override @Override
public final CreateTableFinalStep onCommitDrop() { public final CreateTableStorageStep onCommitDrop() {
onCommit = OnCommit.DROP; onCommit = OnCommit.DROP;
return this; return this;
} }


@Override
public final CreateTableFinalStep storage(SQL sql) {
storage = sql;
return this;
}

@Override
public final CreateTableFinalStep storage(String sql) {
return storage(sql(sql));
}

@Override
public final CreateTableFinalStep storage(String sql, Object... bindings) {
return storage(sql(sql, bindings));
}

@Override
public final CreateTableFinalStep storage(String sql, QueryPart... parts) {
return storage(sql(sql, parts));
}

// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
// XXX: QueryPart API // XXX: QueryPart API
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
Expand All @@ -239,6 +266,8 @@ public final void accept(Context<?> ctx) {




private final void accept0(Context<?> ctx) { private final void accept0(Context<?> ctx) {
ctx.start(CREATE_TABLE);

if (select != null) { if (select != null) {




Expand All @@ -251,7 +280,6 @@ private final void accept0(Context<?> ctx) {
acceptCreateTableAsSelect(ctx); acceptCreateTableAsSelect(ctx);
} }
else { else {
ctx.start(CREATE_TABLE);
toSQLCreateTableName(ctx); toSQLCreateTableName(ctx);
ctx.sql('(') ctx.sql('(')
.start(CREATE_TABLE_COLUMNS) .start(CREATE_TABLE_COLUMNS)
Expand Down Expand Up @@ -288,12 +316,16 @@ private final void accept0(Context<?> ctx) {
.sql(')'); .sql(')');


toSQLOnCommit(ctx); toSQLOnCommit(ctx);
ctx.end(CREATE_TABLE);
} }

if (storage != null)
ctx.formatSeparator()
.visit(storage);

ctx.end(CREATE_TABLE);
} }


private final void acceptCreateTableAsSelect(Context<?> ctx) { private final void acceptCreateTableAsSelect(Context<?> ctx) {
ctx.start(CREATE_TABLE);
toSQLCreateTableName(ctx); toSQLCreateTableName(ctx);
toSQLOnCommit(ctx); toSQLOnCommit(ctx);
ctx.formatSeparator() ctx.formatSeparator()
Expand All @@ -319,8 +351,6 @@ private final void acceptCreateTableAsSelect(Context<?> ctx) {
ctx.sql(' ') ctx.sql(' ')
.visit(K_WITH_DATA); .visit(K_WITH_DATA);
} }

ctx.end(CREATE_TABLE);
} }




Expand Down

0 comments on commit 2f51756

Please sign in to comment.