Unverified 提交 6ad9d310 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov 提交者: GitHub

Merge pull request #1335 from katzyn/ddl

Prefer explicitly defined primary key
...@@ -393,6 +393,10 @@ public class AlterTableAddConstraint extends SchemaCommand { ...@@ -393,6 +393,10 @@ public class AlterTableAddConstraint extends SchemaCommand {
this.constraintName = constraintName; this.constraintName = constraintName;
} }
public String getConstraintName() {
return constraintName;
}
public void setType(int type) { public void setType(int type) {
this.type = type; this.type = type;
} }
......
...@@ -21,7 +21,7 @@ public abstract class CommandWithColumns extends SchemaCommand { ...@@ -21,7 +21,7 @@ public abstract class CommandWithColumns extends SchemaCommand {
private ArrayList<DefineCommand> constraintCommands; private ArrayList<DefineCommand> constraintCommands;
private IndexColumn[] pkColumns; private AlterTableAddConstraint primaryKey;
protected CommandWithColumns(Session session, Schema schema) { protected CommandWithColumns(Session session, Schema schema) {
super(session, schema); super(session, schema);
...@@ -49,7 +49,7 @@ public abstract class CommandWithColumns extends SchemaCommand { ...@@ -49,7 +49,7 @@ public abstract class CommandWithColumns extends SchemaCommand {
AlterTableAddConstraint con = (AlterTableAddConstraint) command; AlterTableAddConstraint con = (AlterTableAddConstraint) command;
boolean alreadySet; boolean alreadySet;
if (con.getType() == CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_PRIMARY_KEY) { if (con.getType() == CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_PRIMARY_KEY) {
alreadySet = setPrimaryKeyColumns(con.getIndexColumns()); alreadySet = setPrimaryKey(con);
} else { } else {
alreadySet = false; alreadySet = false;
} }
...@@ -66,7 +66,8 @@ public abstract class CommandWithColumns extends SchemaCommand { ...@@ -66,7 +66,8 @@ public abstract class CommandWithColumns extends SchemaCommand {
* @param columns the list of columns * @param columns the list of columns
*/ */
protected void changePrimaryKeysToNotNull(ArrayList<Column> columns) { protected void changePrimaryKeysToNotNull(ArrayList<Column> columns) {
if (pkColumns != null) { if (primaryKey != null) {
IndexColumn[] pkColumns = primaryKey.getIndexColumns();
for (Column c : columns) { for (Column c : columns) {
for (IndexColumn idxCol : pkColumns) { for (IndexColumn idxCol : pkColumns) {
if (c.getName().equals(idxCol.columnName)) { if (c.getName().equals(idxCol.columnName)) {
...@@ -126,27 +127,39 @@ public abstract class CommandWithColumns extends SchemaCommand { ...@@ -126,27 +127,39 @@ public abstract class CommandWithColumns extends SchemaCommand {
} }
/** /**
* Sets the primary key columns, but also check if a primary key with different * Set the primary key, but also check if a primary key with different
* columns is already defined. * columns is already defined.
* <p>
* If an unnamed primary key with the same columns is already defined it is
* removed from the list of constraints and this method returns
* {@code false}.
* </p>
* *
* @param columns * @param primaryKey
* the primary key columns * the primary key
* @return true if the same primary key columns where already set * @return whether another primary key with the same columns was already set
* and the specified primary key should be ignored
*/ */
private boolean setPrimaryKeyColumns(IndexColumn[] columns) { private boolean setPrimaryKey(AlterTableAddConstraint primaryKey) {
if (pkColumns != null) { if (this.primaryKey != null) {
int len = columns.length; IndexColumn[] oldColumns = this.primaryKey.getIndexColumns();
if (len != pkColumns.length) { IndexColumn[] newColumns = primaryKey.getIndexColumns();
int len = newColumns.length;
if (len != oldColumns.length) {
throw DbException.get(ErrorCode.SECOND_PRIMARY_KEY); throw DbException.get(ErrorCode.SECOND_PRIMARY_KEY);
} }
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
if (!columns[i].columnName.equals(pkColumns[i].columnName)) { if (!newColumns[i].columnName.equals(oldColumns[i].columnName)) {
throw DbException.get(ErrorCode.SECOND_PRIMARY_KEY); throw DbException.get(ErrorCode.SECOND_PRIMARY_KEY);
} }
} }
return true; if (this.primaryKey.getConstraintName() != null) {
return true;
}
// Remove unnamed primary key
constraintCommands.remove(this.primaryKey);
} }
this.pkColumns = columns; this.primaryKey = primaryKey;
return false; return false;
} }
......
...@@ -15,6 +15,18 @@ SELECT CONSTRAINT_NAME, CONSTRAINT_TYPE FROM INFORMATION_SCHEMA.TABLE_CONSTRAINT ...@@ -15,6 +15,18 @@ SELECT CONSTRAINT_NAME, CONSTRAINT_TYPE FROM INFORMATION_SCHEMA.TABLE_CONSTRAINT
DROP TABLE TEST; DROP TABLE TEST;
> ok > ok
CREATE TABLE TEST(ID IDENTITY, CONSTRAINT PK_1 PRIMARY KEY(ID));
> ok
SELECT CONSTRAINT_NAME, CONSTRAINT_TYPE FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS;
> CONSTRAINT_NAME CONSTRAINT_TYPE
> --------------- ---------------
> PK_1 PRIMARY KEY
> rows: 1
DROP TABLE TEST;
> ok
CREATE TABLE T1(ID INT PRIMARY KEY, COL2 INT); CREATE TABLE T1(ID INT PRIMARY KEY, COL2 INT);
> ok > ok
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论