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

IF [NOT] EXISTS is supported for named constraints.

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