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

[#285] Add support for ALTER INDEX IF EXISTS

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