From 9105e603d2380c69d8064dc5eb8963d6dcdea553 Mon Sep 17 00:00:00 2001 From: lukaseder Date: Wed, 21 Jan 2015 18:54:37 +0100 Subject: [PATCH] [#3853] Add support for CREATE TEMPORARY TABLE and all related flags --- .../main/java/org/jooq/CreateTableAsStep.java | 2 +- .../java/org/jooq/CreateTableColumnStep.java | 2 +- .../org/jooq/CreateTableOnCommitStep.java | 87 +++++++++++++++++++ jOOQ/src/main/java/org/jooq/DSLContext.java | 34 +++++++- .../java/org/jooq/impl/CreateTableImpl.java | 86 ++++++++++++++---- jOOQ/src/main/java/org/jooq/impl/DSL.java | 40 +++++++++ .../java/org/jooq/impl/DefaultDSLContext.java | 22 ++++- 7 files changed, 251 insertions(+), 22 deletions(-) create mode 100644 jOOQ/src/main/java/org/jooq/CreateTableOnCommitStep.java diff --git a/jOOQ/src/main/java/org/jooq/CreateTableAsStep.java b/jOOQ/src/main/java/org/jooq/CreateTableAsStep.java index ae88d14362..df9d3a5b0e 100644 --- a/jOOQ/src/main/java/org/jooq/CreateTableAsStep.java +++ b/jOOQ/src/main/java/org/jooq/CreateTableAsStep.java @@ -68,7 +68,7 @@ public interface CreateTableAsStep { * Add an AS clause to the CREATE TABLE statement. */ @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) - CreateTableFinalStep as(Select select); + CreateTableOnCommitStep as(Select select); /** * Add a column to the column list of the CREATE TABLE statement. diff --git a/jOOQ/src/main/java/org/jooq/CreateTableColumnStep.java b/jOOQ/src/main/java/org/jooq/CreateTableColumnStep.java index 54f17a67ea..83bcdb6a45 100644 --- a/jOOQ/src/main/java/org/jooq/CreateTableColumnStep.java +++ b/jOOQ/src/main/java/org/jooq/CreateTableColumnStep.java @@ -45,7 +45,7 @@ package org.jooq; * * @author Lukas Eder */ -public interface CreateTableColumnStep extends CreateTableFinalStep { +public interface CreateTableColumnStep extends CreateTableOnCommitStep { /** * Add a column to the column list of the CREATE TABLE statement. diff --git a/jOOQ/src/main/java/org/jooq/CreateTableOnCommitStep.java b/jOOQ/src/main/java/org/jooq/CreateTableOnCommitStep.java new file mode 100644 index 0000000000..733d3fa052 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/CreateTableOnCommitStep.java @@ -0,0 +1,87 @@ +/** + * Copyright (c) 2009-2015, Data Geekery GmbH (http://www.datageekery.com) + * All rights reserved. + * + * This work is dual-licensed + * - under the Apache Software License 2.0 (the "ASL") + * - under the jOOQ License and Maintenance Agreement (the "jOOQ License") + * ============================================================================= + * You may choose which license applies to you: + * + * - If you're using this work with Open Source databases, you may choose + * either ASL or jOOQ License. + * - If you're using this work with at least one commercial database, you must + * choose jOOQ License + * + * For more information, please visit http://www.jooq.org/licenses + * + * Apache Software License 2.0: + * ----------------------------------------------------------------------------- + * 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. + * + * jOOQ License and Maintenance Agreement: + * ----------------------------------------------------------------------------- + * Data Geekery grants the Customer the non-exclusive, timely limited and + * non-transferable license to install and use the Software under the terms of + * the jOOQ License and Maintenance Agreement. + * + * This library is distributed with a LIMITED WARRANTY. See the jOOQ License + * and Maintenance Agreement for more details: http://www.jooq.org/licensing + */ +package org.jooq; + +// ... +import static org.jooq.SQLDialect.POSTGRES; + +import org.jooq.impl.DSL; + +/** + * A {@link Query} that can create tables. + * + * @author Lukas Eder + */ +public interface CreateTableOnCommitStep extends CreateTableFinalStep { + + /** + * Add an ON COMMIT DELETE ROWS clause. + *

+ * This clause will only be rendered when used with a + * GLOBAL TEMPORARY TABLE + * + * @see DSL#createGlobalTemporaryTable(Table) + */ + @Support({ POSTGRES }) + CreateTableFinalStep onCommitDeleteRows(); + + /** + * Add an ON COMMIT PRESERVE ROWS clause. + *

+ * This clause will only be rendered when used with a + * GLOBAL TEMPORARY TABLE + * + * @see DSL#createGlobalTemporaryTable(Table) + */ + @Support({ POSTGRES }) + CreateTableFinalStep onCommitPreserveRows(); + + /** + * Add an ON COMMIT DROP clause. + *

+ * This clause will only be rendered when used with a + * GLOBAL TEMPORARY TABLE + * + * @see DSL#createGlobalTemporaryTable(Table) + */ + @Support({ POSTGRES }) + CreateTableFinalStep onCommitDrop(); +} diff --git a/jOOQ/src/main/java/org/jooq/DSLContext.java b/jOOQ/src/main/java/org/jooq/DSLContext.java index 808be4a109..7b425ba267 100644 --- a/jOOQ/src/main/java/org/jooq/DSLContext.java +++ b/jOOQ/src/main/java/org/jooq/DSLContext.java @@ -4834,13 +4834,45 @@ public interface DSLContext extends Scope { CreateTableAsStep createTable(String tableName); /** - * Create a new DSL CREATE TABLE statement. + * Create a new DSL CREATE TABLE statement. * * @see DSL#createTable(Table) */ @Support({ CUBRID, DERBY, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) CreateTableAsStep createTable(Table table); + /** + * Create a new DSL CREATE TEMPORARY TABLE statement. + * + * @see DSL#createTemporaryTable(String) + */ + @Support({ POSTGRES }) + CreateTableAsStep createTemporaryTable(String tableName); + + /** + * Create a new DSL CREATE TEMPORARY TABLE statement. + * + * @see DSL#createTemporaryTable(Table) + */ + @Support({ POSTGRES }) + CreateTableAsStep createTemporaryTable(Table table); + + /** + * Create a new DSL CREATE GLOBAL TEMPORARY TABLE statement. + * + * @see DSL#createGlobalTemporaryTable(String) + */ + @Support({ POSTGRES }) + CreateTableAsStep createGlobalTemporaryTable(String tableName); + + /** + * Create a new DSL CREATE GLOBAL TEMPORARY TABLE statement. + * + * @see DSL#createGlobalTemporaryTable(Table) + */ + @Support({ POSTGRES }) + CreateTableAsStep createGlobalTemporaryTable(Table table); + /** * Create a new DSL CREATE VIEW statement. * diff --git a/jOOQ/src/main/java/org/jooq/impl/CreateTableImpl.java b/jOOQ/src/main/java/org/jooq/impl/CreateTableImpl.java index d48f76fe5b..3a78ad7b4f 100644 --- a/jOOQ/src/main/java/org/jooq/impl/CreateTableImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/CreateTableImpl.java @@ -48,6 +48,7 @@ import static org.jooq.Clause.CREATE_TABLE_NAME; // ... // ... // ... +import static org.jooq.SQLDialect.POSTGRES; // ... import static org.jooq.impl.DSL.field; import static org.jooq.impl.DSL.name; @@ -62,6 +63,7 @@ import org.jooq.Context; import org.jooq.CreateTableAsStep; import org.jooq.CreateTableColumnStep; import org.jooq.CreateTableFinalStep; +import org.jooq.CreateTableOnCommitStep; import org.jooq.DataType; import org.jooq.Field; import org.jooq.Record; @@ -86,11 +88,14 @@ class CreateTableImpl extends AbstractQuery implements private Select select; private final List> columnFields; private final List> columnTypes; + private final boolean temporary; + private OnCommit onCommit; - CreateTableImpl(Configuration configuration, Table table) { + CreateTableImpl(Configuration configuration, Table table, boolean temporary) { super(configuration); this.table = table; + this.temporary = temporary; this.columnFields = new ArrayList>(); this.columnTypes = new ArrayList>(); } @@ -100,7 +105,7 @@ class CreateTableImpl extends AbstractQuery implements // ------------------------------------------------------------------------ @Override - public final CreateTableFinalStep as(Select s) { + public final CreateTableOnCommitStep as(Select s) { this.select = s; return this; } @@ -119,6 +124,24 @@ class CreateTableImpl extends AbstractQuery implements return this; } + @Override + public final CreateTableFinalStep onCommitDeleteRows() { + onCommit = OnCommit.DELETE_ROWS; + return this; + } + + @Override + public final CreateTableFinalStep onCommitPreserveRows() { + onCommit = OnCommit.PRESERVE_ROWS; + return this; + } + + @Override + public final CreateTableFinalStep onCommitDrop() { + onCommit = OnCommit.DROP; + return this; + } + // ------------------------------------------------------------------------ // XXX: QueryPart API // ------------------------------------------------------------------------ @@ -139,13 +162,9 @@ class CreateTableImpl extends AbstractQuery implements } } else { - ctx.start(CREATE_TABLE) - .start(CREATE_TABLE_NAME) - .keyword("create table") - .sql(" ") - .visit(table) - .end(CREATE_TABLE_NAME) - .start(CREATE_TABLE_COLUMNS) + ctx.start(CREATE_TABLE); + toSQLCreateTableName(ctx); + ctx.start(CREATE_TABLE_COLUMNS) .sql("(") .formatIndentStart() .formatNewLine(); @@ -171,19 +190,17 @@ class CreateTableImpl extends AbstractQuery implements ctx.formatIndentEnd() .formatNewLine() .sql(")") - .end(CREATE_TABLE_COLUMNS) - .end(CREATE_TABLE); + .end(CREATE_TABLE_COLUMNS); + toSQLOnCommit(ctx); + ctx.end(CREATE_TABLE); } } private final void acceptCreateTableAsSelect(Context ctx) { - ctx.start(CREATE_TABLE) - .start(CREATE_TABLE_NAME) - .keyword("create table") - .sql(" ") - .visit(table) - .end(CREATE_TABLE_NAME) - .formatSeparator() + ctx.start(CREATE_TABLE); + toSQLCreateTableName(ctx); + toSQLOnCommit(ctx); + ctx.formatSeparator() .keyword("as"); /* [pro] xx @@ -213,6 +230,33 @@ class CreateTableImpl extends AbstractQuery implements ctx.end(CREATE_TABLE); } + private final void toSQLCreateTableName(Context ctx) { + ctx.start(CREATE_TABLE_NAME) + .keyword("create") + .sql(" "); + + if (temporary) + if (ctx.family() == POSTGRES) + ctx.keyword("temporary").sql(" "); + else + ctx.keyword("global temporary").sql(" "); + + ctx.keyword("table") + .sql(" ") + .visit(table) + .end(CREATE_TABLE_NAME); + } + + private final void toSQLOnCommit(Context ctx) { + if (temporary && onCommit != null) { + switch (onCommit) { + case DELETE_ROWS: ctx.formatSeparator().keyword("on commit delete rows"); break; + case PRESERVE_ROWS: ctx.formatSeparator().keyword("on commit preserve rows"); break; + case DROP: ctx.formatSeparator().keyword("on commit drop"); break; + } + } + } + private final void acceptSelectInto(Context ctx) { ctx.data(DATA_SELECT_INTO_TABLE, table); ctx.visit(select); @@ -223,4 +267,10 @@ class CreateTableImpl extends AbstractQuery implements public final Clause[] clauses(Context ctx) { return null; } + + private enum OnCommit { + DELETE_ROWS, + PRESERVE_ROWS, + DROP; + } } diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 2846d1d3fa..89aa29019c 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -4242,6 +4242,46 @@ public class DSL { return using(new DefaultConfiguration()).createTable(table); } + /** + * Create a new DSL CREATE GLOBAL TEMPORARY TABLE statement. + * + * @see DSLContext#createTemporaryTable(String) + */ + @Support({ POSTGRES }) + public static CreateTableAsStep createTemporaryTable(String tableName) { + return createTemporaryTable(table(name(tableName))); + } + + /** + * Create a new DSL CREATE GLOBAL TEMPORARY TABLE statement. + * + * @see DSLContext#createTemporaryTable(Table) + */ + @Support({ POSTGRES }) + public static CreateTableAsStep createTemporaryTable(Table table) { + return using(new DefaultConfiguration()).createTemporaryTable(table); + } + + /** + * Create a new DSL CREATE GLOBAL TEMPORARY TABLE statement. + * + * @see DSLContext#createGlobalTemporaryTable(String) + */ + @Support({ POSTGRES }) + public static CreateTableAsStep createGlobalTemporaryTable(String tableName) { + return createGlobalTemporaryTable(table(name(tableName))); + } + + /** + * Create a new DSL CREATE GLOBAL TEMPORARY TABLE statement. + * + * @see DSLContext#createGlobalTemporaryTable(Table) + */ + @Support({ POSTGRES }) + public static CreateTableAsStep createGlobalTemporaryTable(Table table) { + return using(new DefaultConfiguration()).createGlobalTemporaryTable(table); + } + /** * Create a new DSL CREATE VIEW statement. * diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java index 910abd8929..d722c637d8 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java @@ -1604,7 +1604,27 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri @Override public CreateTableAsStep createTable(Table table) { - return new CreateTableImpl(configuration(), table); + return new CreateTableImpl(configuration(), table, false); + } + + @Override + public CreateTableAsStep createTemporaryTable(String tableName) { + return createTemporaryTable(table(name(tableName))); + } + + @Override + public CreateTableAsStep createTemporaryTable(Table table) { + return new CreateTableImpl(configuration(), table, true); + } + + @Override + public CreateTableAsStep createGlobalTemporaryTable(String tableName) { + return createGlobalTemporaryTable(table(name(tableName))); + } + + @Override + public CreateTableAsStep createGlobalTemporaryTable(Table table) { + return new CreateTableImpl(configuration(), table, true); } @Override