提交 c593e188 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Add ALTER VIEW RENAME command for better compatibilty

上级 7e3ef0c6
...@@ -468,7 +468,7 @@ This command commits an open transaction in this connection. ...@@ -468,7 +468,7 @@ This command commits an open transaction in this connection.
ALTER USER SA SET PASSWORD 'rioyxlgt' ALTER USER SA SET PASSWORD 'rioyxlgt'
" "
"Commands (DDL)","ALTER VIEW"," "Commands (DDL)","ALTER VIEW RECOMPILE","
ALTER VIEW [ IF EXISTS ] viewName RECOMPILE ALTER VIEW [ IF EXISTS ] viewName RECOMPILE
"," ","
Recompiles a view after the underlying tables have been changed or created. Recompiles a view after the underlying tables have been changed or created.
...@@ -478,6 +478,15 @@ This command commits an open transaction in this connection. ...@@ -478,6 +478,15 @@ This command commits an open transaction in this connection.
ALTER VIEW ADDRESS_VIEW RECOMPILE ALTER VIEW ADDRESS_VIEW RECOMPILE
" "
"Commands (DDL)","ALTER VIEW RENAME","
ALTER VIEW [ IF EXISTS ] viewName RENAME TO newName
","
Renames a view.
This command commits an open transaction in this connection.
","
ALTER VIEW TEST RENAME TO MY_VIEW
"
"Commands (DDL)","ANALYZE"," "Commands (DDL)","ANALYZE","
ANALYZE [ TABLE tableName ] [ SAMPLE_SIZE rowCountInt ] ANALYZE [ TABLE tableName ] [ SAMPLE_SIZE rowCountInt ]
"," ","
......
...@@ -5767,19 +5767,31 @@ public class Parser { ...@@ -5767,19 +5767,31 @@ public class Parser {
return command; return command;
} }
private AlterView parseAlterView() { private DefineCommand parseAlterView() {
AlterView command = new AlterView(session);
boolean ifExists = readIfExists(false); boolean ifExists = readIfExists(false);
command.setIfExists(ifExists);
String viewName = readIdentifierWithSchema(); String viewName = readIdentifierWithSchema();
Table tableView = getSchema().findTableOrView(session, viewName); Schema schema = getSchema();
Table tableView = schema.findTableOrView(session, viewName);
if (!(tableView instanceof TableView) && !ifExists) { if (!(tableView instanceof TableView) && !ifExists) {
throw DbException.get(ErrorCode.VIEW_NOT_FOUND_1, viewName); throw DbException.get(ErrorCode.VIEW_NOT_FOUND_1, viewName);
} }
TableView view = (TableView) tableView; if (readIf("RENAME")) {
command.setView(view); read("TO");
read("RECOMPILE"); String newName = readIdentifierWithSchema(schema.getName());
return command; checkSchema(schema);
AlterTableRename command = new AlterTableRename(session, getSchema());
command.setOldTableName(viewName);
command.setNewTableName(newName);
command.setIfTableExists(ifExists);
return command;
} else {
read("RECOMPILE");
TableView view = (TableView) tableView;
AlterView command = new AlterView(session);
command.setIfExists(ifExists);
command.setView(view);
return command;
}
} }
private Prepared parseAlterSchema() { private Prepared parseAlterSchema() {
......
...@@ -123,8 +123,8 @@ public class TestScript extends TestDb { ...@@ -123,8 +123,8 @@ public class TestScript extends TestDb {
"uuid", "varchar", "varchar-ignorecase" }) { "uuid", "varchar", "varchar-ignorecase" }) {
testScript("datatypes/" + s + ".sql"); testScript("datatypes/" + s + ".sql");
} }
for (String s : new String[] { "alterTableAdd", "alterTableDropColumn", for (String s : new String[] { "alterTableAdd", "alterTableDropColumn", "alterTableRename",
"createAlias", "createSynonym", "createView", "createTable", "createTrigger", "createAlias", "createSynonym", "createTable", "createTrigger", "createView",
"dropDomain", "dropIndex", "dropSchema", "truncateTable" }) { "dropDomain", "dropIndex", "dropSchema", "truncateTable" }) {
testScript("ddl/" + s + ".sql"); testScript("ddl/" + s + ".sql");
} }
......
-- Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
-- and the EPL 1.0 (http://h2database.com/html/license.html).
-- Initial Developer: H2 Group
--
-- Test for ALTER TABLE RENAME and ALTER VIEW RENAME
CREATE TABLE TABLE1A(ID INT);
> ok
INSERT INTO TABLE1A VALUES (1);
> update count: 1
-- ALTER TABLE RENAME
ALTER TABLE TABLE1A RENAME TO TABLE1B;
> ok
SELECT * FROM TABLE1B;
>> 1
ALTER TABLE IF EXISTS TABLE1B RENAME TO TABLE1C;
> ok
SELECT * FROM TABLE1C;
>> 1
ALTER TABLE BAD RENAME TO SMTH;
> exception TABLE_OR_VIEW_NOT_FOUND_1
ALTER TABLE IF EXISTS BAD RENAME TO SMTH;
> ok
-- ALTER VIEW RENAME
CREATE VIEW VIEW1A AS SELECT * FROM TABLE1C;
> ok
ALTER VIEW VIEW1A RENAME TO VIEW1B;
> ok
SELECT * FROM VIEW1B;
>> 1
ALTER TABLE IF EXISTS VIEW1B RENAME TO VIEW1C;
> ok
SELECT * FROM VIEW1C;
>> 1
ALTER VIEW BAD RENAME TO SMTH;
> exception VIEW_NOT_FOUND_1
ALTER VIEW IF EXISTS BAD RENAME TO SMTH;
> ok
SELECT * FROM VIEW1C;
>> 1
DROP TABLE TABLE1C CASCADE;
> ok
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论