提交 5e7df467 authored 作者: lukaseder's avatar lukaseder

[#285] Add support for ALTER INDEX IF EXISTS

上级 e8cccd33
......@@ -192,7 +192,7 @@ SHOW TABLES
"
"Commands (DDL)","ALTER INDEX RENAME","
ALTER INDEX indexName RENAME TO newIndexName
ALTER INDEX [ IF EXISTS ] indexName RENAME TO newIndexName
","
Renames an index.
This command commits an open transaction in this connection.
......
......@@ -4852,10 +4852,13 @@ public class Parser {
}
private AlterIndexRename parseAlterIndex() {
boolean ifExists = readIfExists(false);
String indexName = readIdentifierWithSchema();
Schema old = getSchema();
AlterIndexRename command = new AlterIndexRename(session);
command.setOldIndex(getSchema().getIndex(indexName));
command.setOldSchema(old);
command.setOldName(indexName);
command.setIfExists(ifExists);
read("RENAME");
read("TO");
String newName = readIdentifierWithSchema(old.getName());
......
......@@ -20,6 +20,9 @@ import org.h2.schema.Schema;
*/
public class AlterIndexRename extends DefineCommand {
private boolean ifExists;
private Schema oldSchema;
private String oldIndexName;
private Index oldIndex;
private String newIndexName;
......@@ -27,8 +30,16 @@ public class AlterIndexRename extends DefineCommand {
super(session);
}
public void setOldIndex(Index index) {
oldIndex = index;
public void setIfExists(boolean b) {
ifExists = b;
}
public void setOldSchema(Schema old) {
oldSchema = old;
}
public void setOldName(String name) {
oldIndexName = name;
}
public void setNewName(String name) {
......@@ -39,9 +50,17 @@ public class AlterIndexRename extends DefineCommand {
public int update() {
session.commit(true);
Database db = session.getDatabase();
Schema schema = oldIndex.getSchema();
if (schema.findIndex(session, newIndexName) != null ||
newIndexName.equals(oldIndex.getName())) {
oldIndex = oldSchema.findIndex(session, oldIndexName);
if (oldIndex == null) {
if (!ifExists) {
throw DbException.get(ErrorCode.INDEX_NOT_FOUND_1,
newIndexName);
} else {
return 0;
}
}
if (oldSchema.findIndex(session, newIndexName) != null ||
newIndexName.equals(oldIndexName)) {
throw DbException.get(ErrorCode.INDEX_ALREADY_EXISTS_1,
newIndexName);
}
......
......@@ -67,7 +67,7 @@ SHOW { SCHEMAS | TABLES [ FROM schemaName ] |
","
Lists the schemas, tables, or the columns of a table."
"Commands (DDL)","ALTER INDEX RENAME","
ALTER INDEX indexName RENAME TO newIndexName
ALTER INDEX [ IF EXISTS ] indexName RENAME TO newIndexName
","
Renames an index."
"Commands (DDL)","ALTER SCHEMA RENAME","
......
......@@ -4458,7 +4458,13 @@ create index if not exists idx_id on s.test(id);
create index if not exists idx_id on s.test(id);
> ok
alter index s.idx_id rename to s.index_id;
alter index s.idx_id rename to s.x;
> ok
alter index if exists s.idx_id rename to s.x;
> ok
alter index if exists s.x rename to s.index_id;
> ok
alter sequence if exists s.seq restart with 10;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论