提交 1eb9a748 authored 作者: Noel Grandin's avatar Noel Grandin

Add support for ALTER TABLE ... RENAME CONSTRAINT .. TO ...

上级 ad857502
...@@ -246,6 +246,15 @@ This command commits an open transaction in this connection. ...@@ -246,6 +246,15 @@ This command commits an open transaction in this connection.
ALTER TABLE TEST ADD CONSTRAINT NAME_UNIQUE UNIQUE(NAME) ALTER TABLE TEST ADD CONSTRAINT NAME_UNIQUE UNIQUE(NAME)
" "
"Commands (DDL)","ALTER TABLE RENAME CONSTRAINT","
ALTER TABLE tableName RENAME oldConstraintName TO newConstraintName
","
Renames a constraint.
This command commits an open transaction in this connection.
","
ALTER TABLE TEST RENAME CONSTRAINT FOO TO BAR
"
"Commands (DDL)","ALTER TABLE ALTER COLUMN"," "Commands (DDL)","ALTER TABLE ALTER COLUMN","
ALTER TABLE tableName ALTER COLUMN columnName ALTER TABLE tableName ALTER COLUMN columnName
{ { dataType [ DEFAULT expression ] [ [ NOT ] NULL ] [ AUTO_INCREMENT | IDENTITY ] } { { dataType [ DEFAULT expression ] [ [ NOT ] NULL ] [ AUTO_INCREMENT | IDENTITY ] }
......
...@@ -21,6 +21,8 @@ Change Log ...@@ -21,6 +21,8 @@ Change Log
<h2>Next Version (unreleased)</h2> <h2>Next Version (unreleased)</h2>
<ul> <ul>
<li>Add support for ALTER TABLE ... RENAME CONSTRAINT .. TO ...
</li>
<li>Add support for PostgreSQL ALTER TABLE ... RENAME COLUMN .. TO ... <li>Add support for PostgreSQL ALTER TABLE ... RENAME COLUMN .. TO ...
</li> </li>
<li>Improve performance of cleaning up temp tables - patch from Eric Faulhaber. <li>Improve performance of cleaning up temp tables - patch from Eric Faulhaber.
......
...@@ -451,6 +451,11 @@ public interface CommandInterface { ...@@ -451,6 +451,11 @@ public interface CommandInterface {
*/ */
int SHUTDOWN_DEFRAG = 84; int SHUTDOWN_DEFRAG = 84;
/**
* The type of a ALTER TABLE RENAME CONSTRAINT statement.
*/
int ALTER_TABLE_RENAME_CONSTRAINT = 85;
/** /**
* Get command type. * Get command type.
* *
......
...@@ -25,6 +25,7 @@ import org.h2.command.ddl.AlterTableAlterColumn; ...@@ -25,6 +25,7 @@ import org.h2.command.ddl.AlterTableAlterColumn;
import org.h2.command.ddl.AlterTableDropConstraint; import org.h2.command.ddl.AlterTableDropConstraint;
import org.h2.command.ddl.AlterTableRename; import org.h2.command.ddl.AlterTableRename;
import org.h2.command.ddl.AlterTableRenameColumn; import org.h2.command.ddl.AlterTableRenameColumn;
import org.h2.command.ddl.AlterTableRenameConstraint;
import org.h2.command.ddl.AlterUser; import org.h2.command.ddl.AlterUser;
import org.h2.command.ddl.AlterView; import org.h2.command.ddl.AlterView;
import org.h2.command.ddl.Analyze; import org.h2.command.ddl.Analyze;
...@@ -5434,6 +5435,17 @@ public class Parser { ...@@ -5434,6 +5435,17 @@ public class Parser {
String newName = readColumnIdentifier(); String newName = readColumnIdentifier();
command.setNewColumnName(newName); command.setNewColumnName(newName);
return command; return command;
} else if (readIf("CONSTRAINT")) {
String constraintName = readIdentifierWithSchema(table
.getSchema().getName());
checkSchema(table.getSchema());
read("TO");
AlterTableRenameConstraint command = new AlterTableRenameConstraint(
session, table.getSchema());
command.setConstraintName(constraintName);
String newName = readColumnIdentifier();
command.setNewConstraintName(newName);
return command;
} else { } else {
read("TO"); read("TO");
String newName = readIdentifierWithSchema(table.getSchema() String newName = readIdentifierWithSchema(table.getSchema()
......
/*
* Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.command.ddl;
import org.h2.api.ErrorCode;
import org.h2.command.CommandInterface;
import org.h2.constraint.Constraint;
import org.h2.engine.Right;
import org.h2.engine.Session;
import org.h2.message.DbException;
import org.h2.schema.Schema;
/**
* This class represents the statement
* ALTER TABLE RENAME CONSTRAINT
*/
public class AlterTableRenameConstraint extends SchemaCommand {
private String constraintName;
private String newConstraintName;
public AlterTableRenameConstraint(Session session, Schema schema) {
super(session, schema);
}
public void setConstraintName(String string) {
constraintName = string;
}
public void setNewConstraintName(String newName) {
this.newConstraintName = newName;
}
@Override
public int update() {
session.commit(true);
Constraint constraint = getSchema().findConstraint(session, constraintName);
if (constraint == null) {
throw DbException.get(ErrorCode.CONSTRAINT_NOT_FOUND_1, constraintName);
}
if (getSchema().findConstraint(session, newConstraintName) != null ||
newConstraintName.equals(constraintName)) {
throw DbException.get(ErrorCode.CONSTRAINT_ALREADY_EXISTS_1,
newConstraintName);
}
session.getUser().checkRight(constraint.getTable(), Right.ALL);
session.getUser().checkRight(constraint.getRefTable(), Right.ALL);
session.getDatabase().renameSchemaObject(session, constraint, newConstraintName);
return 0;
}
@Override
public int getType() {
return CommandInterface.ALTER_TABLE_RENAME_CONSTRAINT;
}
}
...@@ -35,6 +35,7 @@ public class TestAlter extends TestBase { ...@@ -35,6 +35,7 @@ public class TestAlter extends TestBase {
deleteDb(getTestName()); deleteDb(getTestName());
conn = getConnection(getTestName()); conn = getConnection(getTestName());
stat = conn.createStatement(); stat = conn.createStatement();
testAlterTableRenameConstraint();
testAlterTableAlterColumnAsSelfColumn(); testAlterTableAlterColumnAsSelfColumn();
testAlterTableDropColumnWithReferences(); testAlterTableDropColumnWithReferences();
testAlterTableDropMultipleColumns(); testAlterTableDropMultipleColumns();
...@@ -140,6 +141,13 @@ public class TestAlter extends TestBase { ...@@ -140,6 +141,13 @@ public class TestAlter extends TestBase {
stat.execute("drop table test"); stat.execute("drop table test");
} }
private void testAlterTableRenameConstraint() throws SQLException {
stat.execute("create table test(id int, name varchar(255))");
stat.execute("alter table test add constraint x check (id > name)");
stat.execute("alter table test rename constraint x to x2");
stat.execute("drop table test");
}
private void testAlterTableDropIdentityColumn() throws SQLException { private void testAlterTableDropIdentityColumn() throws SQLException {
stat.execute("create table test(id int auto_increment, name varchar)"); stat.execute("create table test(id int auto_increment, name varchar)");
stat.execute("alter table test drop column id"); stat.execute("alter table test drop column id");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论