Unverified 提交 cc021a06 authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #911 from katzyn/ddl

Add support for MySQL-style ALTER TABLE ADD ... FIRST
...@@ -285,7 +285,7 @@ ALTER SEQUENCE SEQ_ID RESTART WITH 1000 ...@@ -285,7 +285,7 @@ ALTER SEQUENCE SEQ_ID RESTART WITH 1000
"Commands (DDL)","ALTER TABLE ADD"," "Commands (DDL)","ALTER TABLE ADD","
ALTER TABLE [ IF EXISTS ] tableName ADD [ COLUMN ] ALTER TABLE [ IF EXISTS ] tableName ADD [ COLUMN ]
{ [ IF NOT EXISTS ] columnName columnDefinition | ( { columnName columnDefinition } [,...] ) } { [ IF NOT EXISTS ] columnName columnDefinition | ( { columnName columnDefinition } [,...] ) }
[ { BEFORE | AFTER } columnName ] [ { { BEFORE | AFTER } columnName } | FIRST ]
"," ","
Adds a new column to a table. Adds a new column to a table.
This command commits an open transaction in this connection. This command commits an open transaction in this connection.
......
...@@ -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;
......
...@@ -86,8 +86,6 @@ public class TestScript extends TestBase { ...@@ -86,8 +86,6 @@ public class TestScript extends TestBase {
testScript("joins.sql"); testScript("joins.sql");
testScript("altertable-index-reuse.sql"); testScript("altertable-index-reuse.sql");
testScript("query-optimisations.sql"); testScript("query-optimisations.sql");
testScript("commands-dml-script.sql");
testScript("commands-dml-create-view.sql");
String decimal2; String decimal2;
if (SysProperties.BIG_DECIMAL_IS_DECIMAL) { if (SysProperties.BIG_DECIMAL_IS_DECIMAL) {
decimal2 = "decimal_decimal"; decimal2 = "decimal_decimal";
...@@ -102,6 +100,12 @@ public class TestScript extends TestBase { ...@@ -102,6 +100,12 @@ 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[] { "alterTableAdd", "createView" }) {
testScript("ddl/" + s + ".sql");
}
for (String s : new String[] { "insertIgnore", "mergeUsing", "script", "with" }) {
testScript("dml/" + s + ".sql");
}
for (String s : new String[] { "avg", "bit-and", "bit-or", "count", for (String s : new String[] { "avg", "bit-and", "bit-or", "count",
"group-concat", "max", "median", "min", "selectivity", "stddev-pop", "group-concat", "max", "median", "min", "selectivity", "stddev-pop",
"stddev-samp", "sum", "var-pop", "var-samp" }) { "stddev-samp", "sum", "var-pop", "var-samp" }) {
...@@ -145,9 +149,6 @@ public class TestScript extends TestBase { ...@@ -145,9 +149,6 @@ public class TestScript extends TestBase {
"parsedatetime", "quarter", "second", "week", "year" }) { "parsedatetime", "quarter", "second", "week", "year" }) {
testScript("functions/timeanddate/" + s + ".sql"); testScript("functions/timeanddate/" + s + ".sql");
} }
for (String s : new String[] { "insertIgnore", "mergeUsing", "with" }) {
testScript("dml/" + s + ".sql");
}
deleteDb("script"); deleteDb("script");
System.out.flush(); System.out.flush();
} }
......
-- 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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论