提交 440e5139 authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #356 from hendriks73/Add_before_after_to_add_multiple_columns

Support for BEFORE and AFTER clauses when using multiple columns in A…
...@@ -226,8 +226,8 @@ ALTER SEQUENCE SEQ_ID RESTART WITH 1000 ...@@ -226,8 +226,8 @@ 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 ] columnDefinition [ { BEFORE | AFTER } columnName ] { [ IF NOT EXISTS ] columnDefinition | ( { columnDefinition } [,...] ) }
| ( { columnDefinition } [,...] ) } [ { BEFORE | AFTER } columnName ]
"," ","
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.
......
...@@ -5719,7 +5719,11 @@ public class Parser { ...@@ -5719,7 +5719,11 @@ public class Parser {
columnsToAdd.add(column); columnsToAdd.add(column);
} while (readIf(",")); } while (readIf(","));
read(")"); read(")");
command.setNewColumns(columnsToAdd); if (readIf("BEFORE")) {
command.setAddBefore(readColumnIdentifier());
} else if (readIf("AFTER")) {
command.setAddAfter(readColumnIdentifier());
}
} else { } else {
boolean ifNotExists = readIfNotExists(); boolean ifNotExists = readIfNotExists();
command.setIfNotExists(ifNotExists); command.setIfNotExists(ifNotExists);
......
...@@ -84,8 +84,8 @@ ALTER SEQUENCE [ IF EXISTS ] sequenceName [ RESTART WITH long ] [ INCREMENT BY l ...@@ -84,8 +84,8 @@ ALTER SEQUENCE [ IF EXISTS ] sequenceName [ RESTART WITH long ] [ INCREMENT BY l
Changes the parameters of a sequence." Changes the parameters of a sequence."
"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 ] columnDefinition [ { BEFORE | AFTER } columnName ] { [ IF NOT EXISTS ] columnDefinition | ( { columnDefinition } [,...] ) }
| ( { columnDefinition } [,...] ) } [ { BEFORE | AFTER } columnName ]
"," ","
Adds a new column to a table." Adds a new column to a table."
"Commands (DDL)","ALTER TABLE ADD CONSTRAINT"," "Commands (DDL)","ALTER TABLE ADD CONSTRAINT","
......
...@@ -48,6 +48,8 @@ public class TestAlter extends TestBase { ...@@ -48,6 +48,8 @@ public class TestAlter extends TestBase {
testAlterTableAlterColumn2(); testAlterTableAlterColumn2();
testAlterTableAddColumnBefore(); testAlterTableAddColumnBefore();
testAlterTableAddColumnAfter(); testAlterTableAddColumnAfter();
testAlterTableAddMultipleColumnsBefore();
testAlterTableAddMultipleColumnsAfter();
testAlterTableModifyColumn(); testAlterTableModifyColumn();
conn.close(); conn.close();
deleteDb(getTestName()); deleteDb(getTestName());
...@@ -206,6 +208,38 @@ public class TestAlter extends TestBase { ...@@ -206,6 +208,38 @@ public class TestAlter extends TestBase {
stat.execute("drop table t"); stat.execute("drop table t");
} }
// column and field names must be upper-case due to getMetaData sensitivity
private void testAlterTableAddMultipleColumnsBefore() throws SQLException {
stat.execute("create table T(X varchar)");
stat.execute("alter table T add (Y int, Z int) before X");
DatabaseMetaData dbMeta = conn.getMetaData();
ResultSet rs = dbMeta.getColumns(null, null, "T", null);
assertTrue(rs.next());
assertEquals("Y", rs.getString("COLUMN_NAME"));
assertTrue(rs.next());
assertEquals("Z", rs.getString("COLUMN_NAME"));
assertTrue(rs.next());
assertEquals("X", rs.getString("COLUMN_NAME"));
assertFalse(rs.next());
stat.execute("drop table T");
}
// column and field names must be upper-case due to getMetaData sensitivity
private void testAlterTableAddMultipleColumnsAfter() throws SQLException {
stat.execute("create table T(X varchar)");
stat.execute("alter table T add (Y int, Z int) after X");
DatabaseMetaData dbMeta = conn.getMetaData();
ResultSet rs = dbMeta.getColumns(null, null, "T", null);
assertTrue(rs.next());
assertEquals("X", rs.getString("COLUMN_NAME"));
assertTrue(rs.next());
assertEquals("Y", rs.getString("COLUMN_NAME"));
assertTrue(rs.next());
assertEquals("Z", rs.getString("COLUMN_NAME"));
assertFalse(rs.next());
stat.execute("drop table T");
}
// column and field names must be upper-case due to getMetaData sensitivity // column and field names must be upper-case due to getMetaData sensitivity
private void testAlterTableAddColumnBefore() throws SQLException { private void testAlterTableAddColumnBefore() throws SQLException {
stat.execute("create table T(X varchar)"); stat.execute("create table T(X varchar)");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论