提交 42086958 authored 作者: Thomas Mueller's avatar Thomas Mueller

IF [NOT] EXISTS is supported for named constraints.

上级 7345f4fc
......@@ -4271,9 +4271,11 @@ public class Parser {
return command;
} else if (readIf("DROP")) {
if (readIf("CONSTRAINT")) {
boolean ifExists = readIfExists(false);
String constraintName = readIdentifierWithSchema(table.getSchema().getName());
ifExists = readIfExists(ifExists);
checkSchema(table.getSchema());
AlterTableDropConstraint command = new AlterTableDropConstraint(session, getSchema());
AlterTableDropConstraint command = new AlterTableDropConstraint(session, getSchema(), ifExists);
command.setConstraintName(constraintName);
return command;
} else if (readIf("PRIMARY")) {
......@@ -4393,14 +4395,16 @@ public class Parser {
private Prepared parseAlterTableAddConstraintIf(String tableName, Schema schema) throws SQLException {
String constraintName = null, comment = null;
boolean ifNotExists = false;
if (readIf("CONSTRAINT")) {
ifNotExists = readIfNoExists();
constraintName = readIdentifierWithSchema(schema.getName());
checkSchema(schema);
comment = readCommentIf();
}
if (readIf("PRIMARY")) {
read("KEY");
AlterTableAddConstraint command = new AlterTableAddConstraint(session, schema);
AlterTableAddConstraint command = new AlterTableAddConstraint(session, schema, ifNotExists);
command.setType(AlterTableAddConstraint.PRIMARY_KEY);
command.setComment(comment);
command.setConstraintName(constraintName);
......@@ -4429,12 +4433,12 @@ public class Parser {
}
AlterTableAddConstraint command;
if (readIf("CHECK")) {
command = new AlterTableAddConstraint(session, schema);
command = new AlterTableAddConstraint(session, schema, ifNotExists);
command.setType(AlterTableAddConstraint.CHECK);
command.setCheckExpression(readExpression());
} else if (readIf("UNIQUE")) {
readIf("INDEX");
command = new AlterTableAddConstraint(session, schema);
command = new AlterTableAddConstraint(session, schema, ifNotExists);
command.setType(AlterTableAddConstraint.UNIQUE);
if (!readIf("(")) {
constraintName = readUniqueIdentifier();
......@@ -4446,7 +4450,7 @@ public class Parser {
command.setIndex(getSchema().findIndex(session, indexName));
}
} else if (readIf("FOREIGN")) {
command = new AlterTableAddConstraint(session, schema);
command = new AlterTableAddConstraint(session, schema, ifNotExists);
command.setType(AlterTableAddConstraint.REFERENTIAL);
read("KEY");
read("(");
......@@ -4574,7 +4578,7 @@ public class Parser {
column.setPrimaryKey(false);
IndexColumn[] cols = new IndexColumn[]{new IndexColumn()};
cols[0].columnName = column.getName();
AlterTableAddConstraint pk = new AlterTableAddConstraint(session, schema);
AlterTableAddConstraint pk = new AlterTableAddConstraint(session, schema, false);
pk.setType(AlterTableAddConstraint.PRIMARY_KEY);
pk.setTableName(tableName);
pk.setIndexColumns(cols);
......@@ -4590,7 +4594,7 @@ public class Parser {
boolean hash = readIf("HASH");
IndexColumn[] cols = new IndexColumn[]{new IndexColumn()};
cols[0].columnName = column.getName();
AlterTableAddConstraint pk = new AlterTableAddConstraint(session, schema);
AlterTableAddConstraint pk = new AlterTableAddConstraint(session, schema, false);
pk.setPrimaryKeyHash(hash);
pk.setType(AlterTableAddConstraint.PRIMARY_KEY);
pk.setTableName(tableName);
......@@ -4600,7 +4604,7 @@ public class Parser {
parseAutoIncrement(column);
}
} else if (readIf("UNIQUE")) {
AlterTableAddConstraint unique = new AlterTableAddConstraint(session, schema);
AlterTableAddConstraint unique = new AlterTableAddConstraint(session, schema, false);
unique.setConstraintName(constraintName);
unique.setType(AlterTableAddConstraint.UNIQUE);
IndexColumn[] cols = new IndexColumn[]{new IndexColumn()};
......@@ -4614,7 +4618,7 @@ public class Parser {
column.addCheckConstraint(session, expr);
}
if (readIf("REFERENCES")) {
AlterTableAddConstraint ref = new AlterTableAddConstraint(session, schema);
AlterTableAddConstraint ref = new AlterTableAddConstraint(session, schema, false);
ref.setConstraintName(constraintName);
ref.setType(AlterTableAddConstraint.REFERENTIAL);
IndexColumn[] cols = new IndexColumn[]{new IndexColumn()};
......
......@@ -69,9 +69,11 @@ public class AlterTableAddConstraint extends SchemaCommand {
private String comment;
private boolean checkExisting;
private boolean primaryKeyHash;
private boolean ifNotExists;
public AlterTableAddConstraint(Session session, Schema schema) {
public AlterTableAddConstraint(Session session, Schema schema, boolean ifNotExists) {
super(session, schema);
this.ifNotExists = ifNotExists;
}
private String generateConstraintName(Table table) {
......@@ -99,6 +101,9 @@ public class AlterTableAddConstraint extends SchemaCommand {
Database db = session.getDatabase();
Table table = getSchema().getTableOrView(session, tableName);
if (getSchema().findConstraint(constraintName) != null) {
if (ifNotExists) {
return 0;
}
throw Message.getSQLException(ErrorCode.CONSTRAINT_ALREADY_EXISTS_1, constraintName);
}
session.getUser().checkRight(table, Right.ALL);
......
......@@ -8,9 +8,11 @@ package org.h2.command.ddl;
import java.sql.SQLException;
import org.h2.constant.ErrorCode;
import org.h2.constraint.Constraint;
import org.h2.engine.Right;
import org.h2.engine.Session;
import org.h2.message.Message;
import org.h2.schema.Schema;
/**
......@@ -20,9 +22,11 @@ import org.h2.schema.Schema;
public class AlterTableDropConstraint extends SchemaCommand {
private String constraintName;
private boolean ifExists;
public AlterTableDropConstraint(Session session, Schema schema) {
public AlterTableDropConstraint(Session session, Schema schema, boolean ifExists) {
super(session, schema);
this.ifExists = ifExists;
}
public void setConstraintName(String string) {
......@@ -31,10 +35,16 @@ public class AlterTableDropConstraint extends SchemaCommand {
public int update() throws SQLException {
session.commit(true);
Constraint constraint = getSchema().getConstraint(constraintName);
Constraint constraint = getSchema().findConstraint(constraintName);
if (constraint == null) {
if (!ifExists) {
throw Message.getSQLException(ErrorCode.CONSTRAINT_NOT_FOUND_1, constraintName);
}
} else {
session.getUser().checkRight(constraint.getTable(), Right.ALL);
session.getUser().checkRight(constraint.getRefTable(), Right.ALL);
session.getDatabase().removeSchemaObject(session, constraint);
}
return 0;
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论