[#5724] Add support for MySQL's ALTER TABLE .. RENAME INDEX
This commit is contained in:
parent
9fb1258326
commit
dbadeab220
@ -50,6 +50,11 @@ public interface AlterIndexStep {
|
||||
/**
|
||||
* Add a <code>RENAME TO</code> clause to the <code>ALTER INDEX</code>
|
||||
* statement.
|
||||
* <p>
|
||||
* Note that in some databases, including MySQL and SQL Server, the index
|
||||
* namespace is tied to a table, not a schema. In those databases, it is
|
||||
* recommended to call {@link DSLContext#alterTable(Name)} with
|
||||
* {@link AlterTableStep#renameIndex(Name)} instead.
|
||||
*/
|
||||
@Support({ H2, HSQLDB, POSTGRES })
|
||||
AlterIndexFinalStep renameTo(Name newName);
|
||||
@ -57,6 +62,11 @@ public interface AlterIndexStep {
|
||||
/**
|
||||
* Add a <code>RENAME TO</code> clause to the <code>ALTER INDEX</code>
|
||||
* statement.
|
||||
* <p>
|
||||
* Note that in some databases, including MySQL and SQL Server, the index
|
||||
* namespace is tied to a table, not a schema. In those databases, it is
|
||||
* recommended to call {@link DSLContext#alterTable(String)} with
|
||||
* {@link AlterTableStep#renameIndex(String)} instead.
|
||||
*/
|
||||
@Support({ H2, HSQLDB, POSTGRES })
|
||||
AlterIndexFinalStep renameTo(String newName);
|
||||
|
||||
63
jOOQ/src/main/java/org/jooq/AlterTableRenameIndexToStep.java
Normal file
63
jOOQ/src/main/java/org/jooq/AlterTableRenameIndexToStep.java
Normal file
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* 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.MYSQL;
|
||||
// ...
|
||||
import static org.jooq.SQLDialect.POSTGRES;
|
||||
|
||||
/**
|
||||
* The step in the <code>ALTER TABLE</code> DSL used to <code>RENAME</code>
|
||||
* indexes.
|
||||
*
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
public interface AlterTableRenameIndexToStep {
|
||||
|
||||
/**
|
||||
* Specify a new index name.
|
||||
*/
|
||||
@Support({ H2, HSQLDB, MYSQL, POSTGRES })
|
||||
AlterTableFinalStep to(Name newName);
|
||||
|
||||
/**
|
||||
* Specify a new index name.
|
||||
*/
|
||||
@Support({ H2, HSQLDB, MYSQL, POSTGRES })
|
||||
AlterTableFinalStep to(String newName);
|
||||
}
|
||||
@ -107,6 +107,20 @@ public interface AlterTableStep {
|
||||
@Support
|
||||
AlterTableRenameColumnToStep renameColumn(String oldName);
|
||||
|
||||
/**
|
||||
* Add a <code>RENAME INDEX</code> clause to the <code>ALTER TABLE</code>
|
||||
* statement.
|
||||
*/
|
||||
@Support({ H2, HSQLDB, MYSQL, POSTGRES })
|
||||
AlterTableRenameIndexToStep renameIndex(Name oldName);
|
||||
|
||||
/**
|
||||
* Add a <code>RENAME INDEX</code> clause to the <code>ALTER TABLE</code>
|
||||
* statement.
|
||||
*/
|
||||
@Support({ H2, HSQLDB, MYSQL, POSTGRES })
|
||||
AlterTableRenameIndexToStep renameIndex(String oldName);
|
||||
|
||||
/**
|
||||
* Add a <code>RENAME CONSTRAINT</code> clause to the <code>ALTER TABLE</code>
|
||||
* statement.
|
||||
|
||||
@ -1006,6 +1006,20 @@ public enum Clause {
|
||||
*/
|
||||
ALTER_TABLE_RENAME_COLUMN,
|
||||
|
||||
/**
|
||||
* A <code>RENAME INDEX</code> clause within an {@link #ALTER_TABLE}
|
||||
* statement.
|
||||
* <p>
|
||||
* This clause surrounds
|
||||
* <ul>
|
||||
* <li>the <code>RENAME TO</code> keywords</li>
|
||||
* <li>the old index name</li>
|
||||
* <li>the <code>TO</code> keyword</li>
|
||||
* <li>the new index name</li>
|
||||
* </ul>
|
||||
*/
|
||||
ALTER_TABLE_RENAME_INDEX,
|
||||
|
||||
/**
|
||||
* A <code>RENAME CONSTRAINT</code> clause within an {@link #ALTER_TABLE}
|
||||
* statement.
|
||||
|
||||
@ -43,6 +43,7 @@ import static org.jooq.Clause.ALTER_TABLE_DROP;
|
||||
import static org.jooq.Clause.ALTER_TABLE_RENAME;
|
||||
import static org.jooq.Clause.ALTER_TABLE_RENAME_COLUMN;
|
||||
import static org.jooq.Clause.ALTER_TABLE_RENAME_CONSTRAINT;
|
||||
import static org.jooq.Clause.ALTER_TABLE_RENAME_INDEX;
|
||||
import static org.jooq.Clause.ALTER_TABLE_TABLE;
|
||||
// ...
|
||||
// ...
|
||||
@ -76,6 +77,7 @@ import static org.jooq.impl.Keywords.K_NOT_NULL;
|
||||
import static org.jooq.impl.Keywords.K_NULL;
|
||||
import static org.jooq.impl.Keywords.K_RENAME_COLUMN;
|
||||
import static org.jooq.impl.Keywords.K_RENAME_CONSTRAINT;
|
||||
import static org.jooq.impl.Keywords.K_RENAME_INDEX;
|
||||
import static org.jooq.impl.Keywords.K_RENAME_TABLE;
|
||||
import static org.jooq.impl.Keywords.K_RENAME_TO;
|
||||
import static org.jooq.impl.Keywords.K_SET_DATA_TYPE;
|
||||
@ -91,6 +93,7 @@ import org.jooq.AlterTableDropStep;
|
||||
import org.jooq.AlterTableFinalStep;
|
||||
import org.jooq.AlterTableRenameColumnToStep;
|
||||
import org.jooq.AlterTableRenameConstraintToStep;
|
||||
import org.jooq.AlterTableRenameIndexToStep;
|
||||
import org.jooq.AlterTableStep;
|
||||
import org.jooq.AlterTableUsingIndexStep;
|
||||
import org.jooq.Clause;
|
||||
@ -116,6 +119,7 @@ final class AlterTableImpl extends AbstractQuery implements
|
||||
AlterTableAlterStep,
|
||||
AlterTableUsingIndexStep,
|
||||
AlterTableRenameColumnToStep,
|
||||
AlterTableRenameIndexToStep,
|
||||
AlterTableRenameConstraintToStep {
|
||||
|
||||
/**
|
||||
@ -129,6 +133,8 @@ final class AlterTableImpl extends AbstractQuery implements
|
||||
private Table<?> renameTo;
|
||||
private Field<?> renameColumn;
|
||||
private Field<?> renameColumnTo;
|
||||
private Name renameIndex;
|
||||
private Name renameIndexTo;
|
||||
private Constraint renameConstraint;
|
||||
private Constraint renameConstraintTo;
|
||||
private Field<?> addColumn;
|
||||
@ -197,6 +203,17 @@ final class AlterTableImpl extends AbstractQuery implements
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl renameIndex(Name oldName) {
|
||||
renameIndex = oldName;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl renameIndex(String oldName) {
|
||||
return renameIndex(name(oldName));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl renameConstraint(Name oldName) {
|
||||
return renameConstraint(constraint(oldName));
|
||||
@ -233,6 +250,10 @@ final class AlterTableImpl extends AbstractQuery implements
|
||||
return to(field(newName));
|
||||
else if (renameConstraint != null)
|
||||
return to(constraint(newName));
|
||||
else if (renameIndex != null) {
|
||||
renameIndexTo = newName;
|
||||
return this;
|
||||
}
|
||||
else
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
@ -442,6 +463,23 @@ final class AlterTableImpl extends AbstractQuery implements
|
||||
|
||||
|
||||
|
||||
|
||||
if (renameIndexTo != null) {
|
||||
switch (family) {
|
||||
|
||||
// [#5724] These databases use table-scoped index names
|
||||
|
||||
|
||||
|
||||
case MYSQL:
|
||||
break;
|
||||
|
||||
// [#5724] Most databases use schema-scoped index names: Ignore the table.
|
||||
default:
|
||||
ctx.visit(DSL.alterIndex(renameIndex).renameTo(renameIndexTo));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
accept1(ctx);
|
||||
}
|
||||
@ -503,6 +541,19 @@ final class AlterTableImpl extends AbstractQuery implements
|
||||
ctx.qualify(qualify)
|
||||
.end(ALTER_TABLE_RENAME_COLUMN);
|
||||
}
|
||||
else if (renameIndex != null) {
|
||||
boolean qualify = ctx.qualify();
|
||||
|
||||
ctx.start(ALTER_TABLE_RENAME_INDEX)
|
||||
.qualify(false)
|
||||
.visit(K_RENAME_INDEX).sql(' ')
|
||||
.visit(renameIndex)
|
||||
.formatSeparator()
|
||||
.visit(K_TO).sql(' ')
|
||||
.visit(renameIndexTo)
|
||||
.qualify(qualify)
|
||||
.end(ALTER_TABLE_RENAME_INDEX);
|
||||
}
|
||||
else if (renameConstraint != null) {
|
||||
boolean qualify = ctx.qualify();
|
||||
|
||||
|
||||
@ -1682,6 +1682,13 @@ class ParserImpl implements Parser {
|
||||
|
||||
return s1.renameColumn(oldName).to(newName);
|
||||
}
|
||||
else if (parseKeywordIf(ctx, "INDEX")) {
|
||||
Name oldName = parseIdentifier(ctx);
|
||||
parseKeyword(ctx, "TO");
|
||||
Name newName = parseIdentifier(ctx);
|
||||
|
||||
return s1.renameIndex(oldName).to(newName);
|
||||
}
|
||||
else if (parseKeywordIf(ctx, "CONSTRAINT")) {
|
||||
Name oldName = parseIdentifier(ctx);
|
||||
parseKeyword(ctx, "TO");
|
||||
|
||||
Loading…
Reference in New Issue
Block a user