[#6240] Add support for SQL Server's ALTER TABLE .. RENAME INDEX emulation

This commit is contained in:
lukaseder 2018-01-04 11:37:02 +01:00
parent 6333877662
commit 70c8dfc6da
5 changed files with 172 additions and 28 deletions

View File

@ -0,0 +1,94 @@
/*
* 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 static org.jooq.SQLDialect.H2;
import static org.jooq.SQLDialect.HSQLDB;
// ...
import static org.jooq.SQLDialect.POSTGRES;
// ...
/**
* The step in the <code>ALTER INDEX</code> where the table can be specified for
* the index.
*
* @author Lukas Eder
*/
public interface AlterIndexOnStep extends AlterIndexStep {
/**
* Specify the table expression on which to alter an index.
* <p>
* {@link SQLDialect#MYSQL}, {@link SQLDialect#MARIADB}, and
* {@link SQLDialect#SQLSERVER} use table-scoped index names, not
* schema-scoped names. This means that in these databases, the
* <code>ON</code> clause is mandatory in order to unambiguously identify an
* index. In all other databases, the <code>ON</code> clause will simply be
* ignored for compatibility reasons.
*/
@Support({ H2, HSQLDB, POSTGRES })
AlterIndexStep on(Table<?> table);
/**
* Specify the table expression on which to alter an index.
* <p>
* {@link SQLDialect#MYSQL}, {@link SQLDialect#MARIADB}, and
* {@link SQLDialect#SQLSERVER} use table-scoped index names, not
* schema-scoped names. This means that in these databases, the
* <code>ON</code> clause is mandatory in order to unambiguously identify an
* index. In all other databases, the <code>ON</code> clause will simply be
* ignored for compatibility reasons.
*/
@Support({ H2, HSQLDB, POSTGRES })
AlterIndexStep on(String tableName);
/**
* Specify the table expression on which to alter an index.
* <p>
* {@link SQLDialect#MYSQL}, {@link SQLDialect#MARIADB}, and
* {@link SQLDialect#SQLSERVER} use table-scoped index names, not
* schema-scoped names. This means that in these databases, the
* <code>ON</code> clause is mandatory in order to unambiguously identify an
* index. In all other databases, the <code>ON</code> clause will simply be
* ignored for compatibility reasons.
*/
@Support({ H2, HSQLDB, POSTGRES })
AlterIndexStep on(Name tableName);
}

View File

@ -61,6 +61,7 @@ import static org.jooq.SQLDialect.SQLITE;
// ...
// ...
// ...
// ...
import java.math.BigInteger;
import java.sql.Connection;
@ -9117,7 +9118,7 @@ public interface DSLContext extends Scope , AutoCloseable {
* @see DSL#alterIndex(String)
*/
@Support({ H2, HSQLDB, POSTGRES })
AlterIndexStep alterIndex(String index);
AlterIndexOnStep alterIndex(String index);
/**
* Create a new DSL <code>ALTER INDEX</code> statement.
@ -9125,7 +9126,7 @@ public interface DSLContext extends Scope , AutoCloseable {
* @see DSL#alterIndex(Name)
*/
@Support({ H2, HSQLDB, POSTGRES })
AlterIndexStep alterIndex(Name index);
AlterIndexOnStep alterIndex(Name index);
/**
* Create a new DSL <code>ALTER INDEX</code> statement.
@ -9133,7 +9134,7 @@ public interface DSLContext extends Scope , AutoCloseable {
* @see DSL#alterIndex(Name)
*/
@Support({ H2, HSQLDB, POSTGRES })
AlterIndexStep alterIndex(Index index);
AlterIndexOnStep alterIndex(Index index);
/**
* Create a new DSL <code>ALTER INDEX</code> statement.

View File

@ -49,8 +49,11 @@ import static org.jooq.SQLDialect.FIREBIRD;
// ...
// ...
import static org.jooq.impl.DSL.index;
import static org.jooq.impl.DSL.inline;
import static org.jooq.impl.DSL.name;
import static org.jooq.impl.DSL.table;
import static org.jooq.impl.Keywords.K_ALTER_INDEX;
import static org.jooq.impl.Keywords.K_EXEC;
import static org.jooq.impl.Keywords.K_IF_EXISTS;
import static org.jooq.impl.Keywords.K_RENAME_INDEX;
import static org.jooq.impl.Keywords.K_RENAME_TO;
@ -61,13 +64,14 @@ import static org.jooq.impl.Tools.endTryCatch;
import java.util.EnumSet;
import org.jooq.AlterIndexFinalStep;
import org.jooq.AlterIndexStep;
import org.jooq.AlterIndexOnStep;
import org.jooq.Clause;
import org.jooq.Configuration;
import org.jooq.Context;
import org.jooq.Index;
import org.jooq.Name;
import org.jooq.SQLDialect;
import org.jooq.Table;
/**
* @author Lukas Eder
@ -75,7 +79,7 @@ import org.jooq.SQLDialect;
final class AlterIndexImpl extends AbstractQuery implements
// Cascading interface implementations for ALTER INDEX behaviour
AlterIndexStep,
AlterIndexOnStep,
AlterIndexFinalStep {
/**
@ -88,6 +92,7 @@ final class AlterIndexImpl extends AbstractQuery implements
private final Index index;
private final boolean ifExists;
private Table<?> on;
private Index renameTo;
AlterIndexImpl(Configuration configuration, Index index) {
@ -105,6 +110,22 @@ final class AlterIndexImpl extends AbstractQuery implements
// XXX: DSL API
// ------------------------------------------------------------------------
@Override
public final AlterIndexImpl on(Table<?> table) {
this.on = table;
return this;
}
@Override
public final AlterIndexImpl on(String tableName) {
return on(name(tableName));
}
@Override
public final AlterIndexImpl on(Name tableName) {
return on(table(tableName));
}
@Override
public final AlterIndexImpl renameTo(String newName) {
return renameTo(name(newName));
@ -143,29 +164,54 @@ final class AlterIndexImpl extends AbstractQuery implements
private final void accept0(Context<?> ctx) {
boolean renameIndex = SUPPORT_RENAME_INDEX.contains(ctx.family());
boolean qualify = ctx.qualify();
ctx.start(ALTER_INDEX_INDEX)
.visit(renameIndex ? K_RENAME_INDEX : K_ALTER_INDEX);
switch (ctx.family()) {
if (ifExists && supportsIfExists(ctx))
ctx.sql(' ').visit(K_IF_EXISTS);
ctx.sql(' ').visit(index)
.end(ALTER_INDEX_INDEX)
.formatIndentStart()
.formatSeparator();
if (renameTo != null) {
boolean qualify = ctx.qualify();
ctx.start(ALTER_INDEX_RENAME)
.qualify(false)
.visit(renameIndex ? K_TO : K_RENAME_TO).sql(' ').visit(renameTo)
.qualify(qualify)
.end(ALTER_INDEX_RENAME);
default: {
ctx.start(ALTER_INDEX_INDEX)
.visit(renameIndex ? K_RENAME_INDEX : K_ALTER_INDEX);
if (ifExists && supportsIfExists(ctx))
ctx.sql(' ').visit(K_IF_EXISTS);
ctx.sql(' ').visit(index)
.end(ALTER_INDEX_INDEX)
.formatIndentStart()
.formatSeparator();
if (renameTo != null)
ctx.start(ALTER_INDEX_RENAME)
.qualify(false)
.visit(renameIndex ? K_TO : K_RENAME_TO).sql(' ').visit(renameTo)
.qualify(qualify)
.end(ALTER_INDEX_RENAME);
ctx.formatIndentEnd();
break;
}
}
ctx.formatIndentEnd();
}
@Override

View File

@ -69,6 +69,7 @@ import static org.jooq.SQLDialect.SQLITE;
// ...
// ...
// ...
// ...
import static org.jooq.impl.Term.ROW_NUMBER;
import static org.jooq.impl.Tools.EMPTY_FIELD;
import static org.jooq.impl.Tools.EMPTY_QUERYPART;
@ -103,6 +104,7 @@ import javax.annotation.Generated;
import javax.sql.DataSource;
import org.jooq.AggregateFunction;
import org.jooq.AlterIndexOnStep;
import org.jooq.AlterIndexStep;
import org.jooq.AlterSchemaStep;
import org.jooq.AlterSequenceStep;
@ -7267,7 +7269,7 @@ public class DSL {
* @see DSLContext#alterIndex(String)
*/
@Support({ H2, HSQLDB, POSTGRES })
public static AlterIndexStep alterIndex(String index) {
public static AlterIndexOnStep alterIndex(String index) {
return using(new DefaultConfiguration()).alterIndex(index);
}
@ -7277,7 +7279,7 @@ public class DSL {
* @see DSLContext#alterIndex(Name)
*/
@Support({ H2, HSQLDB, POSTGRES })
public static AlterIndexStep alterIndex(Name index) {
public static AlterIndexOnStep alterIndex(Name index) {
return using(new DefaultConfiguration()).alterIndex(index);
}
@ -7287,7 +7289,7 @@ public class DSL {
* @see DSLContext#alterIndex(Index)
*/
@Support({ H2, HSQLDB, POSTGRES })
public static AlterIndexStep alterIndex(Index index) {
public static AlterIndexOnStep alterIndex(Index index) {
return using(new DefaultConfiguration()).alterIndex(index);
}

View File

@ -87,6 +87,7 @@ import java.util.stream.Stream;
import javax.annotation.Generated;
import javax.sql.DataSource;
import org.jooq.AlterIndexOnStep;
import org.jooq.AlterIndexStep;
import org.jooq.AlterSchemaStep;
import org.jooq.AlterSequenceStep;
@ -3248,17 +3249,17 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri
}
@Override
public AlterIndexStep alterIndex(String index) {
public AlterIndexOnStep alterIndex(String index) {
return alterIndex(name(index));
}
@Override
public AlterIndexStep alterIndex(Name index) {
public AlterIndexOnStep alterIndex(Name index) {
return alterIndex(index(index));
}
@Override
public AlterIndexStep alterIndex(Index index) {
public AlterIndexOnStep alterIndex(Index index) {
return new AlterIndexImpl(configuration(), index);
}