[#3853] Add support for CREATE TEMPORARY TABLE and all related flags
This commit is contained in:
parent
759daa2a55
commit
9105e603d2
@ -68,7 +68,7 @@ public interface CreateTableAsStep<R extends Record> {
|
||||
* Add an <code>AS</code> clause to the <code>CREATE TABLE</code> statement.
|
||||
*/
|
||||
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
|
||||
CreateTableFinalStep as(Select<? extends R> select);
|
||||
CreateTableOnCommitStep as(Select<? extends R> select);
|
||||
|
||||
/**
|
||||
* Add a column to the column list of the <code>CREATE TABLE</code> statement.
|
||||
|
||||
@ -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 <code>CREATE TABLE</code> statement.
|
||||
|
||||
87
jOOQ/src/main/java/org/jooq/CreateTableOnCommitStep.java
Normal file
87
jOOQ/src/main/java/org/jooq/CreateTableOnCommitStep.java
Normal file
@ -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 <code>ON COMMIT DELETE ROWS</code> clause.
|
||||
* <p>
|
||||
* This clause will only be rendered when used with a
|
||||
* <code>GLOBAL TEMPORARY TABLE</code>
|
||||
*
|
||||
* @see DSL#createGlobalTemporaryTable(Table)
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
CreateTableFinalStep onCommitDeleteRows();
|
||||
|
||||
/**
|
||||
* Add an <code>ON COMMIT PRESERVE ROWS</code> clause.
|
||||
* <p>
|
||||
* This clause will only be rendered when used with a
|
||||
* <code>GLOBAL TEMPORARY TABLE</code>
|
||||
*
|
||||
* @see DSL#createGlobalTemporaryTable(Table)
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
CreateTableFinalStep onCommitPreserveRows();
|
||||
|
||||
/**
|
||||
* Add an <code>ON COMMIT DROP</code> clause.
|
||||
* <p>
|
||||
* This clause will only be rendered when used with a
|
||||
* <code>GLOBAL TEMPORARY TABLE</code>
|
||||
*
|
||||
* @see DSL#createGlobalTemporaryTable(Table)
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
CreateTableFinalStep onCommitDrop();
|
||||
}
|
||||
@ -4834,13 +4834,45 @@ public interface DSLContext extends Scope {
|
||||
CreateTableAsStep<Record> createTable(String tableName);
|
||||
|
||||
/**
|
||||
* Create a new DSL <code>CREATE TABLE</code> statement.
|
||||
* Create a new DSL <code>CREATE TABLE</code> statement.
|
||||
*
|
||||
* @see DSL#createTable(Table)
|
||||
*/
|
||||
@Support({ CUBRID, DERBY, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
|
||||
CreateTableAsStep<Record> createTable(Table<?> table);
|
||||
|
||||
/**
|
||||
* Create a new DSL <code>CREATE TEMPORARY TABLE</code> statement.
|
||||
*
|
||||
* @see DSL#createTemporaryTable(String)
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
CreateTableAsStep<Record> createTemporaryTable(String tableName);
|
||||
|
||||
/**
|
||||
* Create a new DSL <code>CREATE TEMPORARY TABLE</code> statement.
|
||||
*
|
||||
* @see DSL#createTemporaryTable(Table)
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
CreateTableAsStep<Record> createTemporaryTable(Table<?> table);
|
||||
|
||||
/**
|
||||
* Create a new DSL <code>CREATE GLOBAL TEMPORARY TABLE</code> statement.
|
||||
*
|
||||
* @see DSL#createGlobalTemporaryTable(String)
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
CreateTableAsStep<Record> createGlobalTemporaryTable(String tableName);
|
||||
|
||||
/**
|
||||
* Create a new DSL <code>CREATE GLOBAL TEMPORARY TABLE</code> statement.
|
||||
*
|
||||
* @see DSL#createGlobalTemporaryTable(Table)
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
CreateTableAsStep<Record> createGlobalTemporaryTable(Table<?> table);
|
||||
|
||||
/**
|
||||
* Create a new DSL <code>CREATE VIEW</code> statement.
|
||||
*
|
||||
|
||||
@ -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<R extends Record> extends AbstractQuery implements
|
||||
private Select<?> select;
|
||||
private final List<Field<?>> columnFields;
|
||||
private final List<DataType<?>> 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<Field<?>>();
|
||||
this.columnTypes = new ArrayList<DataType<?>>();
|
||||
}
|
||||
@ -100,7 +105,7 @@ class CreateTableImpl<R extends Record> extends AbstractQuery implements
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
@Override
|
||||
public final CreateTableFinalStep as(Select<? extends R> s) {
|
||||
public final CreateTableOnCommitStep as(Select<? extends R> s) {
|
||||
this.select = s;
|
||||
return this;
|
||||
}
|
||||
@ -119,6 +124,24 @@ class CreateTableImpl<R extends Record> 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<R extends Record> 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<R extends Record> 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<R extends Record> 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<R extends Record> extends AbstractQuery implements
|
||||
public final Clause[] clauses(Context<?> ctx) {
|
||||
return null;
|
||||
}
|
||||
|
||||
private enum OnCommit {
|
||||
DELETE_ROWS,
|
||||
PRESERVE_ROWS,
|
||||
DROP;
|
||||
}
|
||||
}
|
||||
|
||||
@ -4242,6 +4242,46 @@ public class DSL {
|
||||
return using(new DefaultConfiguration()).createTable(table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new DSL <code>CREATE GLOBAL TEMPORARY TABLE</code> statement.
|
||||
*
|
||||
* @see DSLContext#createTemporaryTable(String)
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
public static CreateTableAsStep<Record> createTemporaryTable(String tableName) {
|
||||
return createTemporaryTable(table(name(tableName)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new DSL <code>CREATE GLOBAL TEMPORARY TABLE</code> statement.
|
||||
*
|
||||
* @see DSLContext#createTemporaryTable(Table)
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
public static CreateTableAsStep<Record> createTemporaryTable(Table<?> table) {
|
||||
return using(new DefaultConfiguration()).createTemporaryTable(table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new DSL <code>CREATE GLOBAL TEMPORARY TABLE</code> statement.
|
||||
*
|
||||
* @see DSLContext#createGlobalTemporaryTable(String)
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
public static CreateTableAsStep<Record> createGlobalTemporaryTable(String tableName) {
|
||||
return createGlobalTemporaryTable(table(name(tableName)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new DSL <code>CREATE GLOBAL TEMPORARY TABLE</code> statement.
|
||||
*
|
||||
* @see DSLContext#createGlobalTemporaryTable(Table)
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
public static CreateTableAsStep<Record> createGlobalTemporaryTable(Table<?> table) {
|
||||
return using(new DefaultConfiguration()).createGlobalTemporaryTable(table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new DSL <code>CREATE VIEW</code> statement.
|
||||
*
|
||||
|
||||
@ -1604,7 +1604,27 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri
|
||||
|
||||
@Override
|
||||
public CreateTableAsStep<Record> createTable(Table<?> table) {
|
||||
return new CreateTableImpl<Record>(configuration(), table);
|
||||
return new CreateTableImpl<Record>(configuration(), table, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CreateTableAsStep<Record> createTemporaryTable(String tableName) {
|
||||
return createTemporaryTable(table(name(tableName)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CreateTableAsStep<Record> createTemporaryTable(Table<?> table) {
|
||||
return new CreateTableImpl<Record>(configuration(), table, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CreateTableAsStep<Record> createGlobalTemporaryTable(String tableName) {
|
||||
return createGlobalTemporaryTable(table(name(tableName)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CreateTableAsStep<Record> createGlobalTemporaryTable(Table<?> table) {
|
||||
return new CreateTableImpl<Record>(configuration(), table, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Loading…
Reference in New Issue
Block a user