提交 e66811e5 authored 作者: Thomas Mueller Graf's avatar Thomas Mueller Graf

Merge branch 'master' of https://github.com/h2database/h2database

......@@ -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.
......@@ -201,7 +201,7 @@ ALTER INDEX IDXNAME RENAME TO IDX_TEST_NAME
"
"Commands (DDL)","ALTER SCHEMA RENAME","
ALTER SCHEMA schema RENAME TO newSchemaName
ALTER SCHEMA [ IF EXISTS ] schema RENAME TO newSchemaName
","
Renames a schema.
This command commits an open transaction in this connection.
......@@ -210,7 +210,7 @@ ALTER SCHEMA TEST RENAME TO PRODUCTION
"
"Commands (DDL)","ALTER SEQUENCE","
ALTER SEQUENCE sequenceName [ RESTART WITH long ] [ INCREMENT BY long ]
ALTER SEQUENCE [ IF EXISTS ] sequenceName [ RESTART WITH long ] [ INCREMENT BY long ]
[ MINVALUE long | NOMINVALUE | NO MINVALUE ]
[ MAXVALUE long | NOMAXVALUE | NO MAXVALUE ]
[ CYCLE long | NOCYCLE | NO CYCLE ]
......@@ -224,7 +224,7 @@ ALTER SEQUENCE SEQ_ID RESTART WITH 1000
"
"Commands (DDL)","ALTER TABLE ADD","
ALTER TABLE tableName ADD [ COLUMN ]
ALTER TABLE [ IF EXISTS ] tableName ADD [ COLUMN ]
{ [ IF NOT EXISTS ] columnDefinition [ { BEFORE | AFTER } columnName ]
| ( { columnDefinition } [,...] ) }
","
......@@ -235,7 +235,7 @@ ALTER TABLE TEST ADD CREATEDATE TIMESTAMP
"
"Commands (DDL)","ALTER TABLE ADD CONSTRAINT","
ALTER TABLE tableName ADD constraint [ CHECK | NOCHECK ]
ALTER TABLE [ IF EXISTS ] tableName ADD constraint [ CHECK | NOCHECK ]
","
Adds a constraint to a table. If NOCHECK is specified, existing rows are not
checked for consistency (the default is to check consistency for existing rows).
......@@ -247,7 +247,7 @@ ALTER TABLE TEST ADD CONSTRAINT NAME_UNIQUE UNIQUE(NAME)
"
"Commands (DDL)","ALTER TABLE RENAME CONSTRAINT","
ALTER TABLE tableName RENAME oldConstraintName TO newConstraintName
ALTER TABLE [ IF EXISTS ] tableName RENAME oldConstraintName TO newConstraintName
","
Renames a constraint.
This command commits an open transaction in this connection.
......@@ -256,7 +256,7 @@ ALTER TABLE TEST RENAME CONSTRAINT FOO TO BAR
"
"Commands (DDL)","ALTER TABLE ALTER COLUMN","
ALTER TABLE tableName ALTER COLUMN columnName
ALTER TABLE [ IF EXISTS ] tableName ALTER COLUMN columnName
{ { dataType [ DEFAULT expression ] [ [ NOT ] NULL ] [ AUTO_INCREMENT | IDENTITY ] }
| { RENAME TO name }
| { RESTART WITH long }
......@@ -298,7 +298,7 @@ ALTER TABLE TEST ALTER COLUMN NAME SET NULL;
"
"Commands (DDL)","ALTER TABLE DROP COLUMN","
ALTER TABLE tableName DROP COLUMN [ IF EXISTS ] ( columnName [,...] )
ALTER TABLE [ IF EXISTS ] tableName DROP COLUMN [ IF EXISTS ] ( columnName [,...] )
","
Removes column(s) from a table.
This command commits an open transaction in this connection.
......@@ -308,7 +308,7 @@ ALTER TABLE TEST DROP COLUMN NAME1, NAME2
"
"Commands (DDL)","ALTER TABLE DROP CONSTRAINT","
ALTER TABLE tableName DROP { CONSTRAINT [ IF EXISTS ] constraintName | PRIMARY KEY }
ALTER TABLE [ IF EXISTS ] tableName DROP { CONSTRAINT [ IF EXISTS ] constraintName | PRIMARY KEY }
","
Removes a constraint or a primary key from a table.
This command commits an open transaction in this connection.
......@@ -317,7 +317,7 @@ ALTER TABLE TEST DROP CONSTRAINT UNIQUE_NAME
"
"Commands (DDL)","ALTER TABLE SET","
ALTER TABLE tableName SET REFERENTIAL_INTEGRITY
ALTER TABLE [ IF EXISTS ] tableName SET REFERENTIAL_INTEGRITY
{ FALSE | TRUE [ CHECK | NOCHECK ] }
","
Disables or enables referential integrity checking for a table. This command can
......@@ -332,7 +332,7 @@ ALTER TABLE TEST SET REFERENTIAL_INTEGRITY FALSE
"
"Commands (DDL)","ALTER TABLE RENAME","
ALTER TABLE tableName RENAME TO newName
ALTER TABLE [ IF EXISTS ] tableName RENAME TO newName
","
Renames a table.
This command commits an open transaction in this connection.
......@@ -380,7 +380,7 @@ ALTER USER SA SET PASSWORD 'rioyxlgt'
"
"Commands (DDL)","ALTER VIEW","
ALTER VIEW viewName RECOMPILE
ALTER VIEW [ IF EXISTS ] viewName RECOMPILE
","
Recompiles a view after the underlying tables have been changed or created.
This command is used for views created using CREATE FORCE VIEW.
......
......@@ -48,6 +48,16 @@ table with an LOB column.
</li>
<li>Add support for PostgreSQL ALTER TABLE ... RENAME COLUMN .. TO ...
</li>
<li>Add support for ALTER SCHEMA [ IF EXISTS ]
</li>
<li>Add support for ALTER TABLE [ IF EXISTS ]
</li>
<li>Add support for ALTER VIEW [ IF EXISTS ]
</li>
<li>Add support for ALTER INDE [ IF EXISTS ]
</li>
<li>Add support for ALTER SEQUENCE [ IF EXISTS ]
</li>
<li>Improve performance of cleaning up temp tables - patch from Eric Faulhaber.
</li>
<li>Fix bug where table locks were not dropped when the connection closed
......
......@@ -652,7 +652,7 @@ public class Parser {
return command;
}
private Schema getSchema(String schemaName) {
private Schema findSchema(String schemaName) {
if (schemaName == null) {
return null;
}
......@@ -664,13 +664,22 @@ public class Parser {
} else if (database.getMode().sysDummy1 &&
"SYSIBM".equals(schemaName)) {
// IBM DB2 and Apache Derby compatibility: SYSIBM.SYSDUMMY1
} else {
throw DbException.get(ErrorCode.SCHEMA_NOT_FOUND_1, schemaName);
}
}
return schema;
}
private Schema getSchema(String schemaName) {
if (schemaName == null) {
return null;
}
Schema schema = findSchema(schemaName);
if (schema == null) {
throw DbException.get(ErrorCode.SCHEMA_NOT_FOUND_1, schemaName);
}
return schema;
}
private Schema getSchema() {
return getSchema(schemaName);
}
......@@ -4238,7 +4247,7 @@ public class Parser {
}
primaryKey = true;
if (!isToken("ON")) {
ifNotExists = readIfNoExists();
ifNotExists = readIfNotExists();
indexName = readIdentifierWithSchema(null);
oldSchema = getSchema();
}
......@@ -4254,7 +4263,7 @@ public class Parser {
}
if (readIf("INDEX")) {
if (!isToken("ON")) {
ifNotExists = readIfNoExists();
ifNotExists = readIfNotExists();
indexName = readIdentifierWithSchema(null);
oldSchema = getSchema();
}
......@@ -4470,14 +4479,14 @@ public class Parser {
private CreateRole parseCreateRole() {
CreateRole command = new CreateRole(session);
command.setIfNotExists(readIfNoExists());
command.setIfNotExists(readIfNotExists());
command.setRoleName(readUniqueIdentifier());
return command;
}
private CreateSchema parseCreateSchema() {
CreateSchema command = new CreateSchema(session);
command.setIfNotExists(readIfNoExists());
command.setIfNotExists(readIfNotExists());
command.setSchemaName(readUniqueIdentifier());
if (readIf("AUTHORIZATION")) {
command.setAuthorization(readUniqueIdentifier());
......@@ -4488,7 +4497,7 @@ public class Parser {
}
private CreateSequence parseCreateSequence() {
boolean ifNotExists = readIfNoExists();
boolean ifNotExists = readIfNotExists();
String sequenceName = readIdentifierWithSchema();
CreateSequence command = new CreateSequence(session, getSchema());
command.setIfNotExists(ifNotExists);
......@@ -4537,7 +4546,7 @@ public class Parser {
return command;
}
private boolean readIfNoExists() {
private boolean readIfNotExists() {
if (readIf("IF")) {
read("NOT");
read("EXISTS");
......@@ -4547,7 +4556,7 @@ public class Parser {
}
private CreateConstant parseCreateConstant() {
boolean ifNotExists = readIfNoExists();
boolean ifNotExists = readIfNotExists();
String constantName = readIdentifierWithSchema();
Schema schema = getSchema();
if (isKeyword(constantName)) {
......@@ -4564,7 +4573,7 @@ public class Parser {
}
private CreateAggregate parseCreateAggregate(boolean force) {
boolean ifNotExists = readIfNoExists();
boolean ifNotExists = readIfNotExists();
CreateAggregate command = new CreateAggregate(session);
command.setForce(force);
String name = readIdentifierWithSchema();
......@@ -4582,7 +4591,7 @@ public class Parser {
}
private CreateUserDataType parseCreateUserDataType() {
boolean ifNotExists = readIfNoExists();
boolean ifNotExists = readIfNotExists();
CreateUserDataType command = new CreateUserDataType(session);
command.setTypeName(readUniqueIdentifier());
read("AS");
......@@ -4598,7 +4607,7 @@ public class Parser {
}
private CreateTrigger parseCreateTrigger(boolean force) {
boolean ifNotExists = readIfNoExists();
boolean ifNotExists = readIfNotExists();
String triggerName = readIdentifierWithSchema(null);
Schema schema = getSchema();
boolean insteadOf, isBefore;
......@@ -4665,7 +4674,7 @@ public class Parser {
private CreateUser parseCreateUser() {
CreateUser command = new CreateUser(session);
command.setIfNotExists(readIfNoExists());
command.setIfNotExists(readIfNotExists());
command.setUserName(readUniqueIdentifier());
command.setComment(readCommentIf());
if (readIf("PASSWORD")) {
......@@ -4689,7 +4698,7 @@ public class Parser {
}
private CreateFunctionAlias parseCreateFunctionAlias(boolean force) {
boolean ifNotExists = readIfNoExists();
boolean ifNotExists = readIfNotExists();
String aliasName = readIdentifierWithSchema();
if (isKeyword(aliasName) ||
Function.getFunction(database, aliasName) != null ||
......@@ -4777,7 +4786,7 @@ public class Parser {
}
private CreateView parseCreateView(boolean force, boolean orReplace) {
boolean ifNotExists = readIfNoExists();
boolean ifNotExists = readIfNotExists();
String viewName = readIdentifierWithSchema();
CreateView command = new CreateView(session, getSchema());
this.createView = command;
......@@ -4852,10 +4861,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());
......@@ -4866,9 +4878,11 @@ public class Parser {
private AlterView parseAlterView() {
AlterView command = new AlterView(session);
boolean ifExists = readIfExists(false);
command.setIfExists(ifExists);
String viewName = readIdentifierWithSchema();
Table tableView = getSchema().findTableOrView(session, viewName);
if (!(tableView instanceof TableView)) {
if (!(tableView instanceof TableView) && !ifExists) {
throw DbException.get(ErrorCode.VIEW_NOT_FOUND_1, viewName);
}
TableView view = (TableView) tableView;
......@@ -4877,24 +4891,35 @@ public class Parser {
return command;
}
private AlterSchemaRename parseAlterSchema() {
private Prepared parseAlterSchema() {
boolean ifExists = readIfExists(false);
String schemaName = readIdentifierWithSchema();
Schema old = getSchema();
AlterSchemaRename command = new AlterSchemaRename(session);
command.setOldSchema(getSchema(schemaName));
read("RENAME");
read("TO");
String newName = readIdentifierWithSchema(old.getName());
checkSchema(old);
command.setNewName(newName);
return command;
Schema schema = findSchema(schemaName);
if (schema == null) {
if (ifExists) {
return new NoOperation(session);
} else {
throw DbException.get(ErrorCode.SCHEMA_NOT_FOUND_1, schemaName);
}
} else {
AlterSchemaRename command = new AlterSchemaRename(session);
command.setOldSchema(schema);
checkSchema(old);
command.setNewName(newName);
return command;
}
}
private AlterSequence parseAlterSequence() {
boolean ifExists = readIfExists(false);
String sequenceName = readIdentifierWithSchema();
Sequence sequence = getSchema().getSequence(sequenceName);
AlterSequence command = new AlterSequence(session, sequence.getSchema());
command.setSequence(sequence);
AlterSequence command = new AlterSequence(session, getSchema());
command.setSequenceName(sequenceName);
command.setIfExists(ifExists);
while (true) {
if (readIf("RESTART")) {
read("WITH");
......@@ -5415,21 +5440,24 @@ public class Parser {
}
private Prepared parseAlterTable() {
Table table = readTableOrView();
boolean ifTableExists = readIfExists(false);
String tableName = readIdentifierWithSchema();
Schema schema = getSchema();
if (readIf("ADD")) {
Prepared command = parseAlterTableAddConstraintIf(table.getName(),
table.getSchema());
Prepared command = parseAlterTableAddConstraintIf(tableName,
schema, ifTableExists);
if (command != null) {
return command;
}
return parseAlterTableAddColumn(table);
return parseAlterTableAddColumn(tableName, schema, ifTableExists);
} else if (readIf("SET")) {
read("REFERENTIAL_INTEGRITY");
int type = CommandInterface.ALTER_TABLE_SET_REFERENTIAL_INTEGRITY;
boolean value = readBooleanSetting();
AlterTableSet command = new AlterTableSet(session,
table.getSchema(), type, value);
command.setTableName(table.getName());
schema, type, value);
command.setTableName(tableName);
command.setIfTableExists(ifTableExists);
if (readIf("CHECK")) {
command.setCheckExisting(true);
} else if (readIf("NOCHECK")) {
......@@ -5440,87 +5468,93 @@ public class Parser {
if (readIf("COLUMN")) {
// PostgreSQL syntax
String columnName = readColumnIdentifier();
Column column = table.getColumn(columnName);
read("TO");
AlterTableRenameColumn command = new AlterTableRenameColumn(
session);
command.setTable(table);
command.setColumn(column);
session, schema);
command.setTableName(tableName);
command.setIfTableExists(ifTableExists);
command.setOldColumnName(columnName);
String newName = readColumnIdentifier();
command.setNewColumnName(newName);
return command;
} else if (readIf("CONSTRAINT")) {
String constraintName = readIdentifierWithSchema(table
.getSchema().getName());
checkSchema(table.getSchema());
String constraintName = readIdentifierWithSchema(schema.getName());
checkSchema(schema);
read("TO");
AlterTableRenameConstraint command = new AlterTableRenameConstraint(
session, table.getSchema());
session, schema);
command.setConstraintName(constraintName);
String newName = readColumnIdentifier();
command.setNewConstraintName(newName);
return command;
return commandIfTableExists(schema, tableName, ifTableExists, command);
} else {
read("TO");
String newName = readIdentifierWithSchema(table.getSchema()
.getName());
checkSchema(table.getSchema());
String newName = readIdentifierWithSchema(schema.getName());
checkSchema(schema);
AlterTableRename command = new AlterTableRename(session,
getSchema());
command.setOldTable(table);
command.setOldTableName(tableName);
command.setNewTableName(newName);
command.setIfTableExists(ifTableExists);
command.setHidden(readIf("HIDDEN"));
return command;
}
} else if (readIf("DROP")) {
if (readIf("CONSTRAINT")) {
boolean ifExists = readIfExists(false);
String constraintName = readIdentifierWithSchema(table
.getSchema().getName());
String constraintName = readIdentifierWithSchema(schema.getName());
ifExists = readIfExists(ifExists);
checkSchema(table.getSchema());
checkSchema(schema);
AlterTableDropConstraint command = new AlterTableDropConstraint(
session, getSchema(), ifExists);
command.setConstraintName(constraintName);
return command;
return commandIfTableExists(schema, tableName, ifTableExists, command);
} else if (readIf("FOREIGN")) {
// MySQL compatibility
read("KEY");
String constraintName = readIdentifierWithSchema(table
.getSchema().getName());
checkSchema(table.getSchema());
String constraintName = readIdentifierWithSchema(schema.getName());
checkSchema(schema);
AlterTableDropConstraint command = new AlterTableDropConstraint(
session, getSchema(), false);
command.setConstraintName(constraintName);
return command;
return commandIfTableExists(schema, tableName, ifTableExists, command);
} else if (readIf("INDEX")) {
// MySQL compatibility
String indexName = readIdentifierWithSchema();
DropIndex command = new DropIndex(session, getSchema());
command.setIndexName(indexName);
return command;
return commandIfTableExists(schema, tableName, ifTableExists, command);
} else if (readIf("PRIMARY")) {
read("KEY");
Table table = tableIfTableExists(schema, tableName, ifTableExists);
if (table == null) {
return new NoOperation(session);
}
Index idx = table.getPrimaryKey();
DropIndex command = new DropIndex(session, table.getSchema());
DropIndex command = new DropIndex(session, schema);
command.setIndexName(idx.getName());
return command;
} else {
readIf("COLUMN");
boolean ifExists = readIfExists(false);
AlterTableAlterColumn command = new AlterTableAlterColumn(
session, table.getSchema());
session, schema);
command.setType(CommandInterface.ALTER_TABLE_DROP_COLUMN);
ArrayList<Column> columnsToRemove = New.arrayList();
Table table = tableIfTableExists(schema, tableName, ifTableExists);
do {
String columnName = readColumnIdentifier();
if (table == null) {
return new NoOperation(session);
}
if (ifExists && !table.doesColumnExist(columnName)) {
return new NoOperation(session);
}
Column column = table.getColumn(columnName);
columnsToRemove.add(column);
} while (readIf(","));
command.setTable(table);
command.setTableName(tableName);
command.setIfTableExists(ifTableExists);
command.setColumnsToRemove(columnsToRemove);
return command;
}
......@@ -5528,32 +5562,34 @@ public class Parser {
// MySQL compatibility
readIf("COLUMN");
String columnName = readColumnIdentifier();
Column column = table.getColumn(columnName);
String newColumnName = readColumnIdentifier();
Column column = columnIfTableExists(schema, tableName, columnName, ifTableExists);
boolean nullable = column == null ? true : column.isNullable();
// new column type ignored. RENAME and MODIFY are
// a single command in MySQL but two different commands in H2.
parseColumnForTable(newColumnName, column.isNullable());
AlterTableRenameColumn command = new AlterTableRenameColumn(session);
command.setTable(table);
command.setColumn(column);
parseColumnForTable(newColumnName, nullable);
AlterTableRenameColumn command = new AlterTableRenameColumn(session, schema);
command.setTableName(tableName);
command.setIfTableExists(ifTableExists);
command.setOldColumnName(columnName);
command.setNewColumnName(newColumnName);
return command;
} else if (readIf("MODIFY")) {
// MySQL compatibility
readIf("COLUMN");
String columnName = readColumnIdentifier();
Column column = table.getColumn(columnName);
return parseAlterTableAlterColumnType(table, columnName, column);
return parseAlterTableAlterColumnType(schema, tableName, columnName, ifTableExists);
} else if (readIf("ALTER")) {
readIf("COLUMN");
String columnName = readColumnIdentifier();
Column column = table.getColumn(columnName);
Column column = columnIfTableExists(schema, tableName, columnName, ifTableExists);
if (readIf("RENAME")) {
read("TO");
AlterTableRenameColumn command = new AlterTableRenameColumn(
session);
command.setTable(table);
command.setColumn(column);
session, schema);
command.setTableName(tableName);
command.setIfTableExists(ifTableExists);
command.setOldColumnName(columnName);
String newName = readColumnIdentifier();
command.setNewColumnName(newName);
return command;
......@@ -5561,8 +5597,9 @@ public class Parser {
// PostgreSQL compatibility
if (readIf("DEFAULT")) {
AlterTableAlterColumn command = new AlterTableAlterColumn(
session, table.getSchema());
command.setTable(table);
session, schema);
command.setTableName(tableName);
command.setIfTableExists(ifTableExists);
command.setOldColumn(column);
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_DEFAULT);
command.setDefaultExpression(null);
......@@ -5571,24 +5608,26 @@ public class Parser {
read("NOT");
read("NULL");
AlterTableAlterColumn command = new AlterTableAlterColumn(
session, table.getSchema());
command.setTable(table);
session, schema);
command.setTableName(tableName);
command.setIfTableExists(ifTableExists);
command.setOldColumn(column);
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_NULL);
return command;
} else if (readIf("TYPE")) {
// PostgreSQL compatibility
return parseAlterTableAlterColumnType(table, columnName, column);
return parseAlterTableAlterColumnType(schema, tableName, columnName, ifTableExists);
} else if (readIf("SET")) {
if (readIf("DATA")) {
// Derby compatibility
read("TYPE");
return parseAlterTableAlterColumnType(table, columnName,
column);
return parseAlterTableAlterColumnType(schema, tableName, columnName,
ifTableExists);
}
AlterTableAlterColumn command = new AlterTableAlterColumn(
session, table.getSchema());
command.setTable(table);
session, schema);
command.setTableName(tableName);
command.setIfTableExists(ifTableExists);
command.setOldColumn(column);
if (readIf("NULL")) {
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_NULL);
......@@ -5606,45 +5645,66 @@ public class Parser {
} else if (readIf("RESTART")) {
readIf("WITH");
Expression start = readExpression();
AlterSequence command = new AlterSequence(session,
table.getSchema());
AlterSequence command = new AlterSequence(session, schema);
command.setColumn(column);
command.setStartWith(start);
return command;
return commandIfTableExists(schema, tableName, ifTableExists, command);
} else if (readIf("SELECTIVITY")) {
AlterTableAlterColumn command = new AlterTableAlterColumn(
session, table.getSchema());
command.setTable(table);
session, schema);
command.setTableName(tableName);
command.setIfTableExists(ifTableExists);
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_SELECTIVITY);
command.setOldColumn(column);
command.setSelectivity(readExpression());
return command;
} else {
return parseAlterTableAlterColumnType(table, columnName, column);
return parseAlterTableAlterColumnType(schema, tableName, columnName, ifTableExists);
}
}
throw getSyntaxError();
}
private AlterTableAlterColumn parseAlterTableAlterColumnType(Table table,
String columnName, Column column) {
Column newColumn = parseColumnForTable(columnName, column.isNullable());
private Table tableIfTableExists(Schema schema, String tableName, boolean ifTableExists) {
Table table = schema.findTableOrView(session, tableName);
if (table == null && !ifTableExists) {
throw DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, tableName);
}
return table;
}
private Column columnIfTableExists(Schema schema, String tableName, String columnName, boolean ifTableExists) {
Table table = tableIfTableExists(schema, tableName, ifTableExists);
return table == null ? null : table.getColumn(columnName);
}
private Prepared commandIfTableExists(Schema schema, String tableName, boolean ifTableExists, Prepared commandIfTableExists) {
return tableIfTableExists(schema, tableName, ifTableExists) == null
? new NoOperation(session)
: commandIfTableExists;
}
private AlterTableAlterColumn parseAlterTableAlterColumnType(Schema schema, String tableName,
String columnName, boolean ifTableExists) {
Column oldColumn = columnIfTableExists(schema, tableName, columnName, ifTableExists);
Column newColumn = parseColumnForTable(columnName, oldColumn == null ? true : oldColumn.isNullable());
AlterTableAlterColumn command = new AlterTableAlterColumn(session,
table.getSchema());
command.setTable(table);
schema);
command.setTableName(tableName);
command.setIfTableExists(ifTableExists);
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_CHANGE_TYPE);
command.setOldColumn(column);
command.setOldColumn(oldColumn);
command.setNewColumn(newColumn);
return command;
}
private AlterTableAlterColumn parseAlterTableAddColumn(Table table) {
private AlterTableAlterColumn parseAlterTableAddColumn(String tableName, Schema schema, boolean ifTableExists) {
readIf("COLUMN");
Schema schema = table.getSchema();
AlterTableAlterColumn command = new AlterTableAlterColumn(session,
schema);
command.setType(CommandInterface.ALTER_TABLE_ADD_COLUMN);
command.setTable(table);
command.setTableName(tableName);
command.setIfTableExists(ifTableExists);
ArrayList<Column> columnsToAdd = New.arrayList();
if (readIf("(")) {
command.setIfNotExists(false);
......@@ -5656,7 +5716,7 @@ public class Parser {
read(")");
command.setNewColumns(columnsToAdd);
} else {
boolean ifNotExists = readIfNoExists();
boolean ifNotExists = readIfNotExists();
command.setIfNotExists(ifNotExists);
String columnName = readColumnIdentifier();
Column column = parseColumnForTable(columnName, true);
......@@ -5699,12 +5759,12 @@ public class Parser {
}
private DefineCommand parseAlterTableAddConstraintIf(String tableName,
Schema schema) {
Schema schema, boolean ifTableExists) {
String constraintName = null, comment = null;
boolean ifNotExists = false;
boolean allowIndexDefinition = database.getMode().indexDefinitionInCreateTable;
if (readIf("CONSTRAINT")) {
ifNotExists = readIfNoExists();
ifNotExists = readIfNotExists();
constraintName = readIdentifierWithSchema(schema.getName());
checkSchema(schema);
comment = readCommentIf();
......@@ -5718,6 +5778,7 @@ public class Parser {
command.setComment(comment);
command.setConstraintName(constraintName);
command.setTableName(tableName);
command.setIfTableExists(ifTableExists);
if (readIf("HASH")) {
command.setPrimaryKeyHash(true);
}
......@@ -5742,6 +5803,7 @@ public class Parser {
CreateIndex command = new CreateIndex(session, schema);
command.setComment(comment);
command.setTableName(tableName);
command.setIfTableExists(ifTableExists);
if (!readIf("(")) {
command.setIndexName(readUniqueIdentifier());
read("(");
......@@ -5801,6 +5863,7 @@ public class Parser {
command.setCheckExisting(true);
}
command.setTableName(tableName);
command.setIfTableExists(ifTableExists);
command.setConstraintName(constraintName);
command.setComment(comment);
return command;
......@@ -5840,7 +5903,7 @@ public class Parser {
private CreateLinkedTable parseCreateLinkedTable(boolean temp,
boolean globalTemp, boolean force) {
read("TABLE");
boolean ifNotExists = readIfNoExists();
boolean ifNotExists = readIfNotExists();
String tableName = readIdentifierWithSchema();
CreateLinkedTable command = new CreateLinkedTable(session, getSchema());
command.setTemporary(temp);
......@@ -5876,7 +5939,7 @@ public class Parser {
private CreateTable parseCreateTable(boolean temp, boolean globalTemp,
boolean persistIndexes) {
boolean ifNotExists = readIfNoExists();
boolean ifNotExists = readIfNotExists();
String tableName = readIdentifierWithSchema();
if (temp && globalTemp && equalsToken("SESSION", schemaName)) {
// support weird syntax: declare global temporary table session.xy
......@@ -5896,7 +5959,7 @@ public class Parser {
if (!readIf(")")) {
do {
DefineCommand c = parseAlterTableAddConstraintIf(tableName,
schema);
schema, false);
if (c != null) {
command.addConstraintCommand(c);
} else {
......
......@@ -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);
}
......
......@@ -49,6 +49,7 @@ public class AlterTableAddConstraint extends SchemaCommand {
private String comment;
private boolean checkExisting;
private boolean primaryKeyHash;
private boolean ifTableExists;
private final boolean ifNotExists;
private ArrayList<Index> createdIndexes = New.arrayList();
......@@ -58,6 +59,10 @@ public class AlterTableAddConstraint extends SchemaCommand {
this.ifNotExists = ifNotExists;
}
public void setIfTableExists(boolean b) {
ifTableExists = b;
}
private String generateConstraintName(Table table) {
if (constraintName == null) {
constraintName = getSchema().getUniqueConstraintName(
......@@ -90,7 +95,13 @@ public class AlterTableAddConstraint extends SchemaCommand {
session.commit(true);
}
Database db = session.getDatabase();
Table table = getSchema().getTableOrView(session, tableName);
Table table = getSchema().findTableOrView(session, tableName);
if (table == null) {
if (ifTableExists) {
return 0;
}
throw DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, tableName);
}
if (getSchema().findConstraint(session, constraintName) != null) {
if (ifNotExists) {
return 0;
......
......@@ -48,7 +48,7 @@ import org.h2.util.New;
*/
public class AlterTableAlterColumn extends SchemaCommand {
private Table table;
private String tableName;
private Column oldColumn;
private Column newColumn;
private int type;
......@@ -56,6 +56,7 @@ public class AlterTableAlterColumn extends SchemaCommand {
private Expression newSelectivity;
private String addBefore;
private String addAfter;
private boolean ifTableExists;
private boolean ifNotExists;
private ArrayList<Column> columnsToAdd;
private ArrayList<Column> columnsToRemove;
......@@ -64,8 +65,12 @@ public class AlterTableAlterColumn extends SchemaCommand {
super(session, schema);
}
public void setTable(Table table) {
this.table = table;
public void setIfTableExists(boolean b) {
ifTableExists = b;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public void setOldColumn(Column oldColumn) {
......@@ -84,16 +89,23 @@ public class AlterTableAlterColumn extends SchemaCommand {
public int update() {
session.commit(true);
Database db = session.getDatabase();
Table table = getSchema().findTableOrView(session, tableName);
if (table == null) {
if (ifTableExists) {
return 0;
}
throw DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, tableName);
}
session.getUser().checkRight(table, Right.ALL);
table.checkSupportAlter();
table.lock(session, true, true);
if (newColumn != null) {
checkDefaultReferencesTable(newColumn.getDefaultExpression());
checkDefaultReferencesTable(table, newColumn.getDefaultExpression());
checkClustering(newColumn);
}
if (columnsToAdd != null) {
for (Column column : columnsToAdd) {
checkDefaultReferencesTable(column.getDefaultExpression());
checkDefaultReferencesTable(table, column.getDefaultExpression());
checkClustering(column);
}
}
......@@ -103,7 +115,7 @@ public class AlterTableAlterColumn extends SchemaCommand {
// no change
break;
}
checkNoNullValues();
checkNoNullValues(table);
oldColumn.setNullable(false);
db.updateMeta(session, table);
break;
......@@ -113,17 +125,17 @@ public class AlterTableAlterColumn extends SchemaCommand {
// no change
break;
}
checkNullable();
checkNullable(table);
oldColumn.setNullable(true);
db.updateMeta(session, table);
break;
}
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_DEFAULT: {
Sequence sequence = oldColumn == null ? null : oldColumn.getSequence();
checkDefaultReferencesTable(defaultExpression);
checkDefaultReferencesTable(table, defaultExpression);
oldColumn.setSequence(null);
oldColumn.setDefaultExpression(session, defaultExpression);
removeSequence(sequence);
removeSequence(table, sequence);
db.updateMeta(session, table);
break;
}
......@@ -132,7 +144,7 @@ public class AlterTableAlterColumn extends SchemaCommand {
// need to copy the table because the length is only a constraint,
// and does not affect the storage structure.
if (oldColumn.isWideningConversion(newColumn)) {
convertAutoIncrementColumn(newColumn);
convertAutoIncrementColumn(table, newColumn);
oldColumn.copy(newColumn);
db.updateMeta(session, table);
} else {
......@@ -140,12 +152,12 @@ public class AlterTableAlterColumn extends SchemaCommand {
oldColumn.setDefaultExpression(session, null);
oldColumn.setConvertNullToDefault(false);
if (oldColumn.isNullable() && !newColumn.isNullable()) {
checkNoNullValues();
checkNoNullValues(table);
} else if (!oldColumn.isNullable() && newColumn.isNullable()) {
checkNullable();
checkNullable(table);
}
convertAutoIncrementColumn(newColumn);
copyData();
convertAutoIncrementColumn(table, newColumn);
copyData(table);
}
break;
}
......@@ -162,7 +174,7 @@ public class AlterTableAlterColumn extends SchemaCommand {
table.isTemporary());
}
}
copyData();
copyData(table);
break;
}
case CommandInterface.ALTER_TABLE_DROP_COLUMN: {
......@@ -171,7 +183,7 @@ public class AlterTableAlterColumn extends SchemaCommand {
columnsToRemove.get(0).getSQL());
}
table.dropMultipleColumnsConstraintsAndIndexes(session, columnsToRemove);
copyData();
copyData(table);
break;
}
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_SELECTIVITY: {
......@@ -186,7 +198,7 @@ public class AlterTableAlterColumn extends SchemaCommand {
return 0;
}
private void checkDefaultReferencesTable(Expression defaultExpression) {
private void checkDefaultReferencesTable(Table table, Expression defaultExpression) {
if (defaultExpression == null) {
return;
}
......@@ -209,7 +221,7 @@ public class AlterTableAlterColumn extends SchemaCommand {
}
}
private void convertAutoIncrementColumn(Column c) {
private void convertAutoIncrementColumn(Table table, Column c) {
if (c.isAutoIncrement()) {
if (c.isPrimaryKey()) {
c.setOriginalSQL("IDENTITY");
......@@ -221,7 +233,7 @@ public class AlterTableAlterColumn extends SchemaCommand {
}
}
private void removeSequence(Sequence sequence) {
private void removeSequence(Table table, Sequence sequence) {
if (sequence != null) {
table.removeSequence(sequence);
sequence.setBelongsToTable(false);
......@@ -230,7 +242,7 @@ public class AlterTableAlterColumn extends SchemaCommand {
}
}
private void copyData() {
private void copyData(Table table) {
if (table.isTemporary()) {
throw DbException.getUnsupportedException("TEMP TABLE");
}
......@@ -239,7 +251,7 @@ public class AlterTableAlterColumn extends SchemaCommand {
String tempName = db.getTempTableName(baseName, session);
Column[] columns = table.getColumns();
ArrayList<Column> newColumns = New.arrayList();
Table newTable = cloneTableStructure(columns, db, tempName, newColumns);
Table newTable = cloneTableStructure(table, columns, db, tempName, newColumns);
try {
// check if a view would become invalid
// (because the column to drop is referenced or so)
......@@ -289,7 +301,7 @@ public class AlterTableAlterColumn extends SchemaCommand {
}
}
private Table cloneTableStructure(Column[] columns, Database db,
private Table cloneTableStructure(Table table, Column[] columns, Database db,
String tempName, ArrayList<Column> newColumns) {
for (Column col : columns) {
newColumns.add(col.getClone());
......@@ -479,7 +491,7 @@ public class AlterTableAlterColumn extends SchemaCommand {
}
}
private void checkNullable() {
private void checkNullable(Table table) {
for (Index index : table.getIndexes()) {
if (index.getColumnIndex(oldColumn) < 0) {
continue;
......@@ -492,7 +504,7 @@ public class AlterTableAlterColumn extends SchemaCommand {
}
}
private void checkNoNullValues() {
private void checkNoNullValues(Table table) {
String sql = "SELECT COUNT(*) FROM " +
table.getSQL() + " WHERE " +
oldColumn.getSQL() + " IS NULL";
......
......@@ -20,7 +20,8 @@ import org.h2.table.Table;
*/
public class AlterTableRename extends SchemaCommand {
private Table oldTable;
private boolean ifTableExists;
private String oldTableName;
private String newTableName;
private boolean hidden;
......@@ -28,8 +29,12 @@ public class AlterTableRename extends SchemaCommand {
super(session, schema);
}
public void setOldTable(Table table) {
oldTable = table;
public void setIfTableExists(boolean b) {
ifTableExists = b;
}
public void setOldTableName(String name) {
oldTableName = name;
}
public void setNewTableName(String name) {
......@@ -40,6 +45,13 @@ public class AlterTableRename extends SchemaCommand {
public int update() {
session.commit(true);
Database db = session.getDatabase();
Table oldTable = getSchema().findTableOrView(session, oldTableName);
if (oldTable == null) {
if (ifTableExists) {
return 0;
}
throw DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, oldTableName);
}
session.getUser().checkRight(oldTable, Right.ALL);
Table t = getSchema().findTableOrView(session, newTableName);
if (t != null && hidden && newTableName.equals(oldTable.getName())) {
......
......@@ -5,12 +5,15 @@
*/
package org.h2.command.ddl;
import org.h2.api.ErrorCode;
import org.h2.command.CommandInterface;
import org.h2.engine.Database;
import org.h2.engine.DbObject;
import org.h2.engine.Right;
import org.h2.engine.Session;
import org.h2.expression.Expression;
import org.h2.message.DbException;
import org.h2.schema.Schema;
import org.h2.table.Column;
import org.h2.table.Table;
......@@ -18,22 +21,27 @@ import org.h2.table.Table;
* This class represents the statement
* ALTER TABLE ALTER COLUMN RENAME
*/
public class AlterTableRenameColumn extends DefineCommand {
public class AlterTableRenameColumn extends SchemaCommand {
private Table table;
private Column column;
private boolean ifTableExists;
private String tableName;
private String oldName;
private String newName;
public AlterTableRenameColumn(Session session) {
super(session);
public AlterTableRenameColumn(Session session, Schema schema) {
super(session, schema);
}
public void setTable(Table table) {
this.table = table;
public void setIfTableExists(boolean b) {
this.ifTableExists = b;
}
public void setColumn(Column column) {
this.column = column;
public void setTableName(String tableName) {
this.tableName = tableName;
}
public void setOldColumnName(String oldName) {
this.oldName = oldName;
}
public void setNewColumnName(String newName) {
......@@ -44,6 +52,14 @@ public class AlterTableRenameColumn extends DefineCommand {
public int update() {
session.commit(true);
Database db = session.getDatabase();
Table table = getSchema().findTableOrView(session, tableName);
if (table == null) {
if (ifTableExists) {
return 0;
}
throw DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, tableName);
}
Column column = table.getColumn(oldName);
session.getUser().checkRight(table, Right.ALL);
table.checkSupportAlter();
// we need to update CHECK constraint
......
......@@ -17,12 +17,17 @@ import org.h2.table.TableView;
*/
public class AlterView extends DefineCommand {
private boolean ifExists;
private TableView view;
public AlterView(Session session) {
super(session);
}
public void setIfExists(boolean b) {
ifExists = b;
}
public void setView(TableView view) {
this.view = view;
}
......@@ -30,6 +35,9 @@ public class AlterView extends DefineCommand {
@Override
public int update() {
session.commit(true);
if (view == null && ifExists) {
return 0;
}
session.getUser().checkRight(view, Right.ALL);
DbException e = view.recompile(session, false, true);
if (e != null) {
......
......@@ -27,6 +27,7 @@ public class CreateIndex extends SchemaCommand {
private String indexName;
private IndexColumn[] indexColumns;
private boolean primaryKey, unique, hash, spatial;
private boolean ifTableExists;
private boolean ifNotExists;
private String comment;
......@@ -34,6 +35,10 @@ public class CreateIndex extends SchemaCommand {
super(session, schema);
}
public void setIfTableExists(boolean b) {
this.ifTableExists = b;
}
public void setIfNotExists(boolean ifNotExists) {
this.ifNotExists = ifNotExists;
}
......@@ -57,7 +62,13 @@ public class CreateIndex extends SchemaCommand {
}
Database db = session.getDatabase();
boolean persistent = db.isPersistent();
Table table = getSchema().getTableOrView(session, tableName);
Table table = getSchema().findTableOrView(session, tableName);
if (table == null) {
if (ifTableExists) {
return 0;
}
throw DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, tableName);
}
if (getSchema().findIndex(session, indexName) != null) {
if (ifNotExists) {
return 0;
......
......@@ -24,7 +24,9 @@ import org.h2.table.Table;
*/
public class AlterSequence extends SchemaCommand {
private boolean ifExists;
private Table table;
private String sequenceName;
private Sequence sequence;
private Expression start;
private Expression increment;
......@@ -37,8 +39,12 @@ public class AlterSequence extends SchemaCommand {
super(session, schema);
}
public void setSequence(Sequence sequence) {
this.sequence = sequence;
public void setIfExists(boolean b) {
ifExists = b;
}
public void setSequenceName(String sequenceName) {
this.sequenceName = sequenceName;
}
@Override
......@@ -49,7 +55,7 @@ public class AlterSequence extends SchemaCommand {
public void setColumn(Column column) {
table = column.getTable();
sequence = column.getSequence();
if (sequence == null) {
if (sequence == null && !ifExists) {
throw DbException.get(ErrorCode.SEQUENCE_NOT_FOUND_1, column.getSQL());
}
}
......@@ -81,6 +87,16 @@ public class AlterSequence extends SchemaCommand {
@Override
public int update() {
Database db = session.getDatabase();
if (sequence == null) {
sequence = getSchema().findSequence(sequenceName);
if (sequence == null) {
if (!ifExists) {
throw DbException.get(ErrorCode.SEQUENCE_NOT_FOUND_1, sequenceName);
} else {
return 0;
}
}
}
if (table != null) {
session.getUser().checkRight(table, Right.ALL);
}
......
......@@ -5,6 +5,7 @@
*/
package org.h2.command.dml;
import org.h2.api.ErrorCode;
import org.h2.command.CommandInterface;
import org.h2.command.ddl.SchemaCommand;
import org.h2.engine.Right;
......@@ -19,6 +20,7 @@ import org.h2.table.Table;
*/
public class AlterTableSet extends SchemaCommand {
private boolean ifTableExists;
private String tableName;
private final int type;
......@@ -40,13 +42,23 @@ public class AlterTableSet extends SchemaCommand {
return true;
}
public void setIfTableExists(boolean b) {
this.ifTableExists = b;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
@Override
public int update() {
Table table = getSchema().getTableOrView(session, tableName);
Table table = getSchema().findTableOrView(session, tableName);
if (table == null) {
if (ifTableExists) {
return 0;
}
throw DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, tableName);
}
session.getUser().checkRight(table, Right.ALL);
table.lock(session, true, true);
switch (type) {
......
......@@ -67,15 +67,15 @@ 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","
ALTER SCHEMA schema RENAME TO newSchemaName
ALTER SCHEMA [ IF EXISTS ] schema RENAME TO newSchemaName
","
Renames a schema."
"Commands (DDL)","ALTER SEQUENCE","
ALTER SEQUENCE sequenceName [ RESTART WITH long ] [ INCREMENT BY long ]
ALTER SEQUENCE [ IF EXISTS ] sequenceName [ RESTART WITH long ] [ INCREMENT BY long ]
[ MINVALUE long | NOMINVALUE | NO MINVALUE ]
[ MAXVALUE long | NOMAXVALUE | NO MAXVALUE ]
[ CYCLE long | NOCYCLE | NO CYCLE ]
......@@ -83,21 +83,21 @@ ALTER SEQUENCE sequenceName [ RESTART WITH long ] [ INCREMENT BY long ]
","
Changes the parameters of a sequence."
"Commands (DDL)","ALTER TABLE ADD","
ALTER TABLE tableName ADD [ COLUMN ]
ALTER TABLE [ IF EXISTS ] tableName ADD [ COLUMN ]
{ [ IF NOT EXISTS ] columnDefinition [ { BEFORE | AFTER } columnName ]
| ( { columnDefinition } [,...] ) }
","
Adds a new column to a table."
"Commands (DDL)","ALTER TABLE ADD CONSTRAINT","
ALTER TABLE tableName ADD constraint [ CHECK | NOCHECK ]
ALTER TABLE [ IF EXISTS ] tableName ADD constraint [ CHECK | NOCHECK ]
","
Adds a constraint to a table."
"Commands (DDL)","ALTER TABLE RENAME CONSTRAINT","
ALTER TABLE tableName RENAME oldConstraintName TO newConstraintName
ALTER TABLE [ IF EXISTS ] tableName RENAME oldConstraintName TO newConstraintName
","
Renames a constraint."
"Commands (DDL)","ALTER TABLE ALTER COLUMN","
ALTER TABLE tableName ALTER COLUMN columnName
ALTER TABLE [ IF EXISTS ] tableName ALTER COLUMN columnName
{ { dataType [ DEFAULT expression ] [ [ NOT ] NULL ] [ AUTO_INCREMENT | IDENTITY ] }
| { RENAME TO name }
| { RESTART WITH long }
......@@ -109,20 +109,20 @@ ALTER TABLE tableName ALTER COLUMN columnName
Changes the data type of a column, rename a column,
change the identity value, or change the selectivity."
"Commands (DDL)","ALTER TABLE DROP COLUMN","
ALTER TABLE tableName DROP COLUMN [ IF EXISTS ] ( columnName [,...] )
ALTER TABLE [ IF EXISTS ] tableName DROP COLUMN [ IF EXISTS ] ( columnName [,...] )
","
Removes column(s) from a table."
"Commands (DDL)","ALTER TABLE DROP CONSTRAINT","
ALTER TABLE tableName DROP { CONSTRAINT [ IF EXISTS ] constraintName | PRIMARY KEY }
ALTER TABLE [ IF EXISTS ] tableName DROP { CONSTRAINT [ IF EXISTS ] constraintName | PRIMARY KEY }
","
Removes a constraint or a primary key from a table."
"Commands (DDL)","ALTER TABLE SET","
ALTER TABLE tableName SET REFERENTIAL_INTEGRITY
ALTER TABLE [ IF EXISTS ] tableName SET REFERENTIAL_INTEGRITY
{ FALSE | TRUE [ CHECK | NOCHECK ] }
","
Disables or enables referential integrity checking for a table."
"Commands (DDL)","ALTER TABLE RENAME","
ALTER TABLE tableName RENAME TO newName
ALTER TABLE [ IF EXISTS ] tableName RENAME TO newName
","
Renames a table."
"Commands (DDL)","ALTER USER ADMIN","
......@@ -138,7 +138,7 @@ ALTER USER userName SET { PASSWORD string | SALT bytes HASH bytes }
","
Changes the password of a user."
"Commands (DDL)","ALTER VIEW","
ALTER VIEW viewName RECOMPILE
ALTER VIEW [ IF EXISTS ] viewName RECOMPILE
","
Recompiles a view after the underlying tables have been changed or created."
"Commands (DDL)","ANALYZE","
......
......@@ -2991,6 +2991,12 @@ CREATE memory TABLE ADDRESS(ID INT);
alter view address_view recompile;
> ok
alter view if exists address_view recompile;
> ok
alter view if exists doesnt_exist recompile;
> ok
select * from ADDRESS_VIEW;
> ID
> --
......@@ -4452,7 +4458,31 @@ 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;
> ok
create sequence s.seq cache 0;
> ok
alter sequence if exists s.seq restart with 3;
> ok
select s.seq.nextval as x;
> X
> -
> 3
> rows: 1
drop sequence s.seq;
> ok
create sequence s.seq cache 0;
......@@ -10197,3 +10227,225 @@ select 0 from ((
> rows: 0
};
> update count: 0
create table x(id int not null);
> ok
alter table if exists y add column a varchar;
> ok
alter table if exists x add column a varchar;
> ok
alter table if exists x add column a varchar;
> exception
alter table if exists y alter column a rename to b;
> ok
alter table if exists x alter column a rename to b;
> ok
alter table if exists x alter column a rename to b;
> exception
alter table if exists y alter column b set default 'a';
> ok
alter table if exists x alter column b set default 'a';
> ok
insert into x(id) values(1);
> update count: 1
select b from x;
> B
> -
> a
> rows: 1
delete from x;
> update count: 1
alter table if exists y alter column b drop default;
> ok
alter table if exists x alter column b drop default;
> ok
alter table if exists y alter column b set not null;
> ok
alter table if exists x alter column b set not null;
> ok
insert into x(id) values(1);
> exception
alter table if exists y alter column b drop not null;
> ok
alter table if exists x alter column b drop not null;
> ok
insert into x(id) values(1);
> update count: 1
select b from x;
> B
> ----
> null
> rows: 1
delete from x;
> update count: 1
alter table if exists y add constraint x_pk primary key (id);
> ok
alter table if exists x add constraint x_pk primary key (id);
> ok
alter table if exists x add constraint x_pk primary key (id);
> exception
insert into x(id) values(1);
> update count: 1
insert into x(id) values(1);
> exception
delete from x;
> update count: 1
alter table if exists y add constraint x_chk check (b = 'a');
> ok
alter table if exists x add constraint x_chk check (b = 'a');
> ok
alter table if exists x add constraint x_chk check (b = 'a');
> exception
insert into x(id, b) values(1, 'b');
> exception
alter table if exists y rename constraint x_chk to x_chk1;
> ok
alter table if exists x rename constraint x_chk to x_chk1;
> ok
alter table if exists x rename constraint x_chk to x_chk1;
> exception
alter table if exists y drop constraint x_chk1;
> ok
alter table if exists x drop constraint x_chk1;
> ok
alter table if exists y rename to z;
> ok
alter table if exists x rename to z;
> ok
alter table if exists x rename to z;
> ok
insert into z(id, b) values(1, 'b');
> update count: 1
delete from z;
> update count: 1
alter table if exists y add constraint z_uk unique (b);
> ok
alter table if exists z add constraint z_uk unique (b);
> ok
alter table if exists z add constraint z_uk unique (b);
> exception
insert into z(id, b) values(1, 'b');
> update count: 1
insert into z(id, b) values(1, 'b');
> exception
delete from z;
> update count: 1
alter table if exists y drop column b;
> ok
alter table if exists z drop column b;
> ok
alter table if exists z drop column b;
> exception
alter table if exists y drop primary key;
> ok
alter table if exists z drop primary key;
> ok
alter table if exists z drop primary key;
> exception
create table x (id int not null primary key);
> ok
alter table if exists y add constraint z_fk foreign key (id) references x (id);
> ok
alter table if exists z add constraint z_fk foreign key (id) references x (id);
> ok
alter table if exists z add constraint z_fk foreign key (id) references x (id);
> exception
insert into z (id) values (1);
> exception
alter table if exists y drop foreign key z_fk;
> ok
alter table if exists z drop foreign key z_fk;
> ok
alter table if exists z drop foreign key z_fk;
> exception
insert into z (id) values (1);
> update count: 1
delete from z;
> update count: 1
drop table x;
> ok
drop table z;
> ok
create schema x;
> ok
alter schema if exists y rename to z;
> ok
alter schema if exists x rename to z;
> ok
alter schema if exists x rename to z;
> ok
create table z.z (id int);
> ok
drop schema z;
> ok
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论