提交 9d53c2d0 authored 作者: noelgrandin's avatar noelgrandin

support ALTER TABLE ADD ... AFTER.

Patch from Andrew Gaul argaul@gmail.com
Fixes issue 401.
上级 9ab2a6b4
......@@ -24,6 +24,7 @@ Change Log
</li><li>MVStore: unified exception handling; the version is included in the messages.
</li><li>MVStore: old data is now retained for 45 seconds by default.
</ul><li>MVStore: compress is now disabled by default, and can be enabled on request.
</ul><li>Support ALTER TABLE ADD ... AFTER. Patch from Andrew Gaul argaul@gmail.com. Fixes issue 401.
</li></ul>
<h2>Version 1.3.170 (2012-11-30)</h2>
......
......@@ -4987,6 +4987,8 @@ public class Parser {
columnsToAdd.add(column);
if (readIf("BEFORE")) {
command.setAddBefore(readColumnIdentifier());
} else if (readIf("AFTER")) {
command.setAddAfter(readColumnIdentifier());
}
}
command.setNewColumns(columnsToAdd);
......
......@@ -54,6 +54,7 @@ public class AlterTableAlterColumn extends SchemaCommand {
private Expression defaultExpression;
private Expression newSelectivity;
private String addBefore;
private String addAfter;
private boolean ifNotExists;
private ArrayList<Column> columnsToAdd;
......@@ -73,6 +74,10 @@ public class AlterTableAlterColumn extends SchemaCommand {
this.addBefore = before;
}
public void setAddAfter(String after) {
this.addAfter = after;
}
public int update() {
session.commit(true);
Database db = session.getDatabase();
......@@ -270,10 +275,12 @@ public class AlterTableAlterColumn extends SchemaCommand {
newColumns.remove(position);
} else if (type == CommandInterface.ALTER_TABLE_ADD_COLUMN) {
int position;
if (addBefore == null) {
position = columns.length;
} else {
if (addBefore != null) {
position = table.getColumn(addBefore).getColumnId();
} else if (addAfter != null) {
position = table.getColumn(addAfter).getColumnId() + 1;
} else {
position = columns.length;
}
for (Column column : columnsToAdd) {
newColumns.add(position++, column);
......
......@@ -80,7 +80,7 @@ ALTER SEQUENCE sequenceName [ RESTART WITH long ] [ INCREMENT BY long ]
Changes the next value and the increment of a sequence."
"Commands (DDL)","ALTER TABLE ADD","
ALTER TABLE tableName ADD [ COLUMN ]
{ [ IF NOT EXISTS ] columnDefinition [ BEFORE columnName ]
{ [ IF NOT EXISTS ] columnDefinition [ BEFORE columnName | AFTER columnName ]
| ( { columnDefinition } [,...] ) }
","
Adds a new column to a table."
......
......@@ -7,6 +7,7 @@
package org.h2.test.db;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
......@@ -42,6 +43,8 @@ public class TestAlter extends TestBase {
testAlterTableAddColumnIfNotExists();
testAlterTableAddMultipleColumns();
testAlterTableAlterColumn2();
testAlterTableAddColumnBefore();
testAlterTableAddColumnAfter();
conn.close();
deleteDb("alter");
}
......@@ -146,6 +149,34 @@ public class TestAlter extends TestBase {
stat.execute("drop table t");
}
// column and field names must be upper-case due to getMetaData sensitivity
private void testAlterTableAddColumnBefore() throws SQLException {
stat.execute("create table T(X varchar)");
stat.execute("alter table T add Y 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("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 testAlterTableAddColumnAfter() throws SQLException {
stat.execute("create table T(X varchar)");
stat.execute("alter table T add Y 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"));
assertFalse(rs.next());
stat.execute("drop table T");
}
private void testAlterTableAlterColumn2() throws SQLException {
// ensure that increasing a VARCHAR columns length takes effect because
// we optimize this case
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论