提交 4459b0b2 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Add support for MySQL-style ALTER TABLE ADD ... FIRST

上级 620f2cd0
...@@ -6289,6 +6289,8 @@ public class Parser { ...@@ -6289,6 +6289,8 @@ public class Parser {
command.setAddBefore(readColumnIdentifier()); command.setAddBefore(readColumnIdentifier());
} else if (readIf("AFTER")) { } else if (readIf("AFTER")) {
command.setAddAfter(readColumnIdentifier()); command.setAddAfter(readColumnIdentifier());
} else if (readIf("FIRST")) {
command.setAddFirst();
} }
command.setNewColumns(columnsToAdd); command.setNewColumns(columnsToAdd);
return command; return command;
......
...@@ -56,6 +56,7 @@ public class AlterTableAlterColumn extends SchemaCommand { ...@@ -56,6 +56,7 @@ public class AlterTableAlterColumn extends SchemaCommand {
private int type; private int type;
private Expression defaultExpression; private Expression defaultExpression;
private Expression newSelectivity; private Expression newSelectivity;
private boolean addFirst;
private String addBefore; private String addBefore;
private String addAfter; private String addAfter;
private boolean ifTableExists; private boolean ifTableExists;
...@@ -80,6 +81,10 @@ public class AlterTableAlterColumn extends SchemaCommand { ...@@ -80,6 +81,10 @@ public class AlterTableAlterColumn extends SchemaCommand {
this.oldColumn = oldColumn; this.oldColumn = oldColumn;
} }
public void setAddFirst() {
addFirst = true;
}
public void setAddBefore(String before) { public void setAddBefore(String before) {
this.addBefore = before; this.addBefore = before;
} }
...@@ -331,7 +336,9 @@ public class AlterTableAlterColumn extends SchemaCommand { ...@@ -331,7 +336,9 @@ public class AlterTableAlterColumn extends SchemaCommand {
} }
} else if (type == CommandInterface.ALTER_TABLE_ADD_COLUMN) { } else if (type == CommandInterface.ALTER_TABLE_ADD_COLUMN) {
int position; int position;
if (addBefore != null) { if (addFirst) {
position = 0;
} else if (addBefore != null) {
position = table.getColumn(addBefore).getColumnId(); position = table.getColumn(addBefore).getColumnId();
} else if (addAfter != null) { } else if (addAfter != null) {
position = table.getColumn(addAfter).getColumnId() + 1; position = table.getColumn(addAfter).getColumnId() + 1;
......
...@@ -100,7 +100,7 @@ public class TestScript extends TestBase { ...@@ -100,7 +100,7 @@ public class TestScript extends TestBase {
"uuid", "varchar", "varchar-ignorecase" }) { "uuid", "varchar", "varchar-ignorecase" }) {
testScript("datatypes/" + s + ".sql"); testScript("datatypes/" + s + ".sql");
} }
for (String s : new String[] { "createView" }) { for (String s : new String[] { "alterTableAdd", "createView" }) {
testScript("ddl/" + s + ".sql"); testScript("ddl/" + s + ".sql");
} }
for (String s : new String[] { "insertIgnore", "mergeUsing", "script", "with" }) { for (String s : new String[] { "insertIgnore", "mergeUsing", "script", "with" }) {
......
-- Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
-- and the EPL 1.0 (http://h2database.com/html/license.html).
-- Initial Developer: H2 Group
--
CREATE TABLE TEST(B INT);
> ok
ALTER TABLE TEST ADD C INT;
> ok
ALTER TABLE TEST ADD COLUMN D INT;
> ok
ALTER TABLE TEST ADD IF NOT EXISTS B INT;
> ok
ALTER TABLE TEST ADD IF NOT EXISTS E INT;
> ok
ALTER TABLE IF EXISTS TEST2 ADD COLUMN B INT;
> ok
ALTER TABLE TEST ADD B1 INT AFTER B;
> ok
ALTER TABLE TEST ADD B2 INT BEFORE C;
> ok
ALTER TABLE TEST ADD (C1 INT, C2 INT) AFTER C;
> ok
ALTER TABLE TEST ADD (C3 INT, C4 INT) BEFORE D;
> ok
ALTER TABLE TEST ADD A2 INT FIRST;
> ok
ALTER TABLE TEST ADD (A INT, A1 INT) FIRST;
> ok
SELECT * FROM TEST;
> A A1 A2 B B1 B2 C C1 C2 C3 C4 D E
> - -- -- - -- -- - -- -- -- -- - -
> rows: 0
DROP TABLE TEST;
> ok
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论