提交 db9406e2 authored 作者: plus33's avatar plus33

Support drop-columns using brackets (Oracle syntax style)

E.g. 
  alter table test drop (a, b) 
works same as  
  alter table test drop column a, b 
上级 6888e92a
This source diff could not be displayed because it is too large. You can view the blob instead.
/* /*
* Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html). * and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group * Initial Developer: H2 Group
*/ */
package org.h2.test.db; package org.h2.test.db;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DatabaseMetaData; import java.sql.DatabaseMetaData;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.test.TestBase; import org.h2.test.TestBase;
/** /**
* Test ALTER statements. * Test ALTER statements.
*/ */
public class TestAlter extends TestBase { public class TestAlter extends TestBase {
private Connection conn; private Connection conn;
private Statement stat; private Statement stat;
/** /**
* Run just this test. * Run just this test.
* *
* @param a ignored * @param a ignored
*/ */
public static void main(String... a) throws Exception { public static void main(String... a) throws Exception {
TestBase.createCaller().init().test(); TestBase.createCaller().init().test();
} }
@Override @Override
public void test() throws Exception { public void test() throws Exception {
deleteDb(getTestName()); deleteDb(getTestName());
conn = getConnection(getTestName()); conn = getConnection(getTestName());
stat = conn.createStatement(); stat = conn.createStatement();
testAlterTableRenameConstraint(); testAlterTableRenameConstraint();
testAlterTableAlterColumnAsSelfColumn(); testAlterTableAlterColumnAsSelfColumn();
testAlterTableDropColumnWithReferences(); testAlterTableDropColumnWithReferences();
testAlterTableDropMultipleColumns(); testAlterTableDropMultipleColumns();
testAlterTableAlterColumnWithConstraint(); testAlterTableAlterColumnWithConstraint();
testAlterTableAlterColumn(); testAlterTableAlterColumn();
testAlterTableAddColumnIdentity(); testAlterTableAddColumnIdentity();
testAlterTableDropIdentityColumn(); testAlterTableDropIdentityColumn();
testAlterTableAddColumnIfNotExists(); testAlterTableAddColumnIfNotExists();
testAlterTableAddMultipleColumns(); testAlterTableAddMultipleColumns();
testAlterTableAlterColumn2(); testAlterTableAlterColumn2();
testAlterTableAddColumnBefore(); testAlterTableAddColumnBefore();
testAlterTableAddColumnAfter(); testAlterTableAddColumnAfter();
testAlterTableAddMultipleColumnsBefore(); testAlterTableAddMultipleColumnsBefore();
testAlterTableAddMultipleColumnsAfter(); testAlterTableAddMultipleColumnsAfter();
testAlterTableModifyColumn(); testAlterTableModifyColumn();
testAlterTableModifyColumnSetNull(); testAlterTableModifyColumnSetNull();
conn.close(); conn.close();
deleteDb(getTestName()); deleteDb(getTestName());
} }
private void testAlterTableAlterColumnAsSelfColumn() throws SQLException { private void testAlterTableAlterColumnAsSelfColumn() throws SQLException {
stat.execute("create table test(id int, name varchar)"); stat.execute("create table test(id int, name varchar)");
stat.execute("alter table test alter column id int as id+1"); stat.execute("alter table test alter column id int as id+1");
stat.execute("insert into test values(1, 'Hello')"); stat.execute("insert into test values(1, 'Hello')");
stat.execute("update test set name='World'"); stat.execute("update test set name='World'");
ResultSet rs = stat.executeQuery("select * from test"); ResultSet rs = stat.executeQuery("select * from test");
rs.next(); rs.next();
assertEquals(3, rs.getInt(1)); assertEquals(3, rs.getInt(1));
stat.execute("drop table test"); stat.execute("drop table test");
} }
private void testAlterTableDropColumnWithReferences() throws SQLException { private void testAlterTableDropColumnWithReferences() throws SQLException {
stat.execute("create table parent(id int, b int)"); stat.execute("create table parent(id int, b int)");
stat.execute("create table child(p int primary key)"); stat.execute("create table child(p int primary key)");
stat.execute("alter table child add foreign key(p) references parent(id)"); stat.execute("alter table child add foreign key(p) references parent(id)");
stat.execute("alter table parent drop column id"); stat.execute("alter table parent drop column id");
stat.execute("drop table parent"); stat.execute("drop table parent");
stat.execute("drop table child"); stat.execute("drop table child");
stat.execute("create table test(id int, name varchar(255))"); stat.execute("create table test(id int, name varchar(255))");
stat.execute("alter table test add constraint x check (id > name)"); stat.execute("alter table test add constraint x check (id > name)");
// the constraint references multiple columns // the constraint references multiple columns
assertThrows(ErrorCode.COLUMN_IS_REFERENCED_1, stat). assertThrows(ErrorCode.COLUMN_IS_REFERENCED_1, stat).
execute("alter table test drop column id"); execute("alter table test drop column id");
stat.execute("drop table test"); stat.execute("drop table test");
stat.execute("create table test(id int, name varchar(255))"); stat.execute("create table test(id int, name varchar(255))");
stat.execute("alter table test add constraint x unique(id, name)"); stat.execute("alter table test add constraint x unique(id, name)");
// the constraint references multiple columns // the constraint references multiple columns
assertThrows(ErrorCode.COLUMN_IS_REFERENCED_1, stat). assertThrows(ErrorCode.COLUMN_IS_REFERENCED_1, stat).
execute("alter table test drop column id"); execute("alter table test drop column id");
stat.execute("drop table test"); stat.execute("drop table test");
stat.execute("create table test(id int, name varchar(255))"); stat.execute("create table test(id int, name varchar(255))");
stat.execute("alter table test add constraint x check (id > 1)"); stat.execute("alter table test add constraint x check (id > 1)");
stat.execute("alter table test drop column id"); stat.execute("alter table test drop column id");
stat.execute("drop table test"); stat.execute("drop table test");
stat.execute("create table test(id int, name varchar(255))"); stat.execute("create table test(id int, name varchar(255))");
stat.execute("alter table test add constraint x check (name > 'TEST.ID')"); stat.execute("alter table test add constraint x check (name > 'TEST.ID')");
// previous versions of H2 used sql.indexOf(columnName) // previous versions of H2 used sql.indexOf(columnName)
// to check if the column is referenced // to check if the column is referenced
stat.execute("alter table test drop column id"); stat.execute("alter table test drop column id");
stat.execute("drop table test"); stat.execute("drop table test");
stat.execute("create table test(id int, name varchar(255))"); stat.execute("create table test(id int, name varchar(255))");
stat.execute("alter table test add constraint x unique(id)"); stat.execute("alter table test add constraint x unique(id)");
stat.execute("alter table test drop column id"); stat.execute("alter table test drop column id");
stat.execute("drop table test"); stat.execute("drop table test");
} }
private void testAlterTableDropMultipleColumns() throws SQLException { private void testAlterTableDropMultipleColumns() throws SQLException {
stat.execute("create table test(id int, name varchar, name2 varchar)"); stat.execute("create table test(id int, name varchar, name2 varchar)");
stat.execute("alter table test drop column name, name2"); stat.execute("alter table test drop column name, name2");
stat.execute("drop table test"); stat.execute("drop table test");
// Test-Case: Same as above but using brackets (Oracle style)
stat.execute("create table test(id int, name varchar, name2 varchar)"); stat.execute("create table test(id int, name varchar, name2 varchar)");
assertThrows(ErrorCode.CANNOT_DROP_LAST_COLUMN, stat). stat.execute("alter table test drop column (name, name2)");
execute("alter table test drop column id, name, name2"); assertThrows(ErrorCode.COLUMN_NOT_FOUND_1, stat).
stat.execute("drop table test"); execute("alter table test drop column name");
} stat.execute("drop table test");
// Test-Case: Error if dropping all columns
/** stat.execute("create table test(id int, name varchar, name2 varchar)");
* Tests a bug we used to have where altering the name of a column that had assertThrows(ErrorCode.CANNOT_DROP_LAST_COLUMN, stat).
* a check constraint that referenced itself would result in not being able execute("alter table test drop column id, name, name2");
* to re-open the DB. stat.execute("drop table test");
*/ }
private void testAlterTableAlterColumnWithConstraint() throws SQLException {
if (config.memory) { /**
return; * Tests a bug we used to have where altering the name of a column that had
} * a check constraint that referenced itself would result in not being able
stat.execute("create table test(id int check(id in (1,2)) )"); * to re-open the DB.
stat.execute("alter table test alter id rename to id2"); */
// disconnect and reconnect private void testAlterTableAlterColumnWithConstraint() throws SQLException {
conn.close(); if (config.memory) {
conn = getConnection(getTestName()); return;
stat = conn.createStatement(); }
stat.execute("insert into test values(1)"); stat.execute("create table test(id int check(id in (1,2)) )");
assertThrows(ErrorCode.CHECK_CONSTRAINT_VIOLATED_1, stat). stat.execute("alter table test alter id rename to id2");
execute("insert into test values(3)"); // disconnect and reconnect
stat.execute("drop table test"); conn.close();
} conn = getConnection(getTestName());
stat = conn.createStatement();
private void testAlterTableRenameConstraint() throws SQLException { stat.execute("insert into test values(1)");
stat.execute("create table test(id int, name varchar(255))"); assertThrows(ErrorCode.CHECK_CONSTRAINT_VIOLATED_1, stat).
stat.execute("alter table test add constraint x check (id > name)"); execute("insert into test values(3)");
stat.execute("alter table test rename constraint x to x2"); stat.execute("drop table test");
stat.execute("drop table test"); }
}
private void testAlterTableRenameConstraint() throws SQLException {
private void testAlterTableDropIdentityColumn() throws SQLException { stat.execute("create table test(id int, name varchar(255))");
stat.execute("create table test(id int auto_increment, name varchar)"); stat.execute("alter table test add constraint x check (id > name)");
stat.execute("alter table test drop column id"); stat.execute("alter table test rename constraint x to x2");
ResultSet rs = stat.executeQuery("select * from INFORMATION_SCHEMA.SEQUENCES"); stat.execute("drop table test");
assertFalse(rs.next()); }
stat.execute("drop table test");
private void testAlterTableDropIdentityColumn() throws SQLException {
stat.execute("create table test(id int auto_increment, name varchar)"); stat.execute("create table test(id int auto_increment, name varchar)");
stat.execute("alter table test drop column name"); stat.execute("alter table test drop column id");
rs = stat.executeQuery("select * from INFORMATION_SCHEMA.SEQUENCES"); ResultSet rs = stat.executeQuery("select * from INFORMATION_SCHEMA.SEQUENCES");
assertTrue(rs.next()); assertFalse(rs.next());
stat.execute("drop table test"); stat.execute("drop table test");
}
stat.execute("create table test(id int auto_increment, name varchar)");
private void testAlterTableAlterColumn() throws SQLException { stat.execute("alter table test drop column name");
stat.execute("create table t(x varchar) as select 'x'"); rs = stat.executeQuery("select * from INFORMATION_SCHEMA.SEQUENCES");
assertThrows(ErrorCode.DATA_CONVERSION_ERROR_1, stat). assertTrue(rs.next());
execute("alter table t alter column x int"); stat.execute("drop table test");
stat.execute("drop table t"); }
stat.execute("create table t(id identity, x varchar) as select null, 'x'");
assertThrows(ErrorCode.DATA_CONVERSION_ERROR_1, stat). private void testAlterTableAlterColumn() throws SQLException {
execute("alter table t alter column x int"); stat.execute("create table t(x varchar) as select 'x'");
stat.execute("drop table t"); assertThrows(ErrorCode.DATA_CONVERSION_ERROR_1, stat).
} execute("alter table t alter column x int");
stat.execute("drop table t");
private void testAlterTableAddColumnIdentity() throws SQLException { stat.execute("create table t(id identity, x varchar) as select null, 'x'");
stat.execute("create table t(x varchar)"); assertThrows(ErrorCode.DATA_CONVERSION_ERROR_1, stat).
stat.execute("alter table t add id bigint identity(5, 5) not null"); execute("alter table t alter column x int");
stat.execute("insert into t values (null, null)"); stat.execute("drop table t");
stat.execute("insert into t values (null, null)"); }
ResultSet rs = stat.executeQuery("select id from t order by id");
assertTrue(rs.next()); private void testAlterTableAddColumnIdentity() throws SQLException {
assertEquals(5, rs.getInt(1)); stat.execute("create table t(x varchar)");
assertTrue(rs.next()); stat.execute("alter table t add id bigint identity(5, 5) not null");
assertEquals(10, rs.getInt(1)); stat.execute("insert into t values (null, null)");
assertFalse(rs.next()); stat.execute("insert into t values (null, null)");
stat.execute("drop table t"); ResultSet rs = stat.executeQuery("select id from t order by id");
} assertTrue(rs.next());
assertEquals(5, rs.getInt(1));
private void testAlterTableAddColumnIfNotExists() throws SQLException { assertTrue(rs.next());
stat.execute("create table t(x varchar) as select 'x'"); assertEquals(10, rs.getInt(1));
stat.execute("alter table t add if not exists x int"); assertFalse(rs.next());
stat.execute("drop table t"); stat.execute("drop table t");
stat.execute("create table t(x varchar) as select 'x'"); }
stat.execute("alter table t add if not exists y int");
stat.execute("select x, y from t"); private void testAlterTableAddColumnIfNotExists() throws SQLException {
stat.execute("drop table t"); stat.execute("create table t(x varchar) as select 'x'");
} stat.execute("alter table t add if not exists x int");
stat.execute("drop table t");
private void testAlterTableAddMultipleColumns() throws SQLException { stat.execute("create table t(x varchar) as select 'x'");
stat.execute("create table t(x varchar) as select 'x'"); stat.execute("alter table t add if not exists y int");
stat.execute("alter table t add (y int, z varchar)"); stat.execute("select x, y from t");
stat.execute("drop table t"); stat.execute("drop table t");
stat.execute("create table t(x varchar) as select 'x'"); }
stat.execute("alter table t add (y int)");
stat.execute("drop table t"); private void testAlterTableAddMultipleColumns() throws SQLException {
} stat.execute("create table t(x varchar) as select 'x'");
stat.execute("alter table t add (y int, z varchar)");
// column and field names must be upper-case due to getMetaData sensitivity stat.execute("drop table t");
private void testAlterTableAddMultipleColumnsBefore() throws SQLException { stat.execute("create table t(x varchar) as select 'x'");
stat.execute("create table T(X varchar)"); stat.execute("alter table t add (y int)");
stat.execute("alter table T add (Y int, Z int) before X"); stat.execute("drop table t");
DatabaseMetaData dbMeta = conn.getMetaData(); }
ResultSet rs = dbMeta.getColumns(null, null, "T", null);
assertTrue(rs.next());
assertEquals("Y", rs.getString("COLUMN_NAME"));
assertTrue(rs.next()); // column and field names must be upper-case due to getMetaData sensitivity
assertEquals("Z", rs.getString("COLUMN_NAME")); private void testAlterTableAddMultipleColumnsBefore() throws SQLException {
assertTrue(rs.next()); stat.execute("create table T(X varchar)");
assertEquals("X", rs.getString("COLUMN_NAME")); stat.execute("alter table T add (Y int, Z int) before X");
assertFalse(rs.next()); DatabaseMetaData dbMeta = conn.getMetaData();
stat.execute("drop table T"); ResultSet rs = dbMeta.getColumns(null, null, "T", null);
} assertTrue(rs.next());
assertEquals("Y", rs.getString("COLUMN_NAME"));
// column and field names must be upper-case due to getMetaData sensitivity assertTrue(rs.next());
private void testAlterTableAddMultipleColumnsAfter() throws SQLException { assertEquals("Z", rs.getString("COLUMN_NAME"));
stat.execute("create table T(X varchar)"); assertTrue(rs.next());
stat.execute("alter table T add (Y int, Z int) after X"); assertEquals("X", rs.getString("COLUMN_NAME"));
DatabaseMetaData dbMeta = conn.getMetaData(); assertFalse(rs.next());
ResultSet rs = dbMeta.getColumns(null, null, "T", null); stat.execute("drop table T");
assertTrue(rs.next()); }
assertEquals("X", rs.getString("COLUMN_NAME"));
assertTrue(rs.next()); // column and field names must be upper-case due to getMetaData sensitivity
assertEquals("Y", rs.getString("COLUMN_NAME")); private void testAlterTableAddMultipleColumnsAfter() throws SQLException {
assertTrue(rs.next()); stat.execute("create table T(X varchar)");
assertEquals("Z", rs.getString("COLUMN_NAME")); stat.execute("alter table T add (Y int, Z int) after X");
assertFalse(rs.next()); DatabaseMetaData dbMeta = conn.getMetaData();
stat.execute("drop table T"); ResultSet rs = dbMeta.getColumns(null, null, "T", null);
} assertTrue(rs.next());
assertEquals("X", rs.getString("COLUMN_NAME"));
// column and field names must be upper-case due to getMetaData sensitivity assertTrue(rs.next());
private void testAlterTableAddColumnBefore() throws SQLException { assertEquals("Y", rs.getString("COLUMN_NAME"));
stat.execute("create table T(X varchar)"); assertTrue(rs.next());
stat.execute("alter table T add Y int before X"); assertEquals("Z", rs.getString("COLUMN_NAME"));
DatabaseMetaData dbMeta = conn.getMetaData(); assertFalse(rs.next());
ResultSet rs = dbMeta.getColumns(null, null, "T", null); stat.execute("drop table T");
assertTrue(rs.next()); }
assertEquals("Y", rs.getString("COLUMN_NAME"));
assertTrue(rs.next()); // column and field names must be upper-case due to getMetaData sensitivity
assertEquals("X", rs.getString("COLUMN_NAME")); private void testAlterTableAddColumnBefore() throws SQLException {
assertFalse(rs.next()); stat.execute("create table T(X varchar)");
stat.execute("drop table T"); stat.execute("alter table T add Y int before X");
} DatabaseMetaData dbMeta = conn.getMetaData();
ResultSet rs = dbMeta.getColumns(null, null, "T", null);
// column and field names must be upper-case due to getMetaData sensitivity assertTrue(rs.next());
private void testAlterTableAddColumnAfter() throws SQLException { assertEquals("Y", rs.getString("COLUMN_NAME"));
stat.execute("create table T(X varchar)"); assertTrue(rs.next());
stat.execute("alter table T add Y int after X"); assertEquals("X", rs.getString("COLUMN_NAME"));
DatabaseMetaData dbMeta = conn.getMetaData(); assertFalse(rs.next());
ResultSet rs = dbMeta.getColumns(null, null, "T", null); stat.execute("drop table T");
assertTrue(rs.next()); }
assertEquals("X", rs.getString("COLUMN_NAME"));
assertTrue(rs.next()); // column and field names must be upper-case due to getMetaData sensitivity
assertEquals("Y", rs.getString("COLUMN_NAME")); private void testAlterTableAddColumnAfter() throws SQLException {
assertFalse(rs.next()); stat.execute("create table T(X varchar)");
stat.execute("drop table T"); stat.execute("alter table T add Y int after X");
} DatabaseMetaData dbMeta = conn.getMetaData();
ResultSet rs = dbMeta.getColumns(null, null, "T", null);
private void testAlterTableAlterColumn2() throws SQLException { assertTrue(rs.next());
// ensure that increasing a VARCHAR columns length takes effect because assertEquals("X", rs.getString("COLUMN_NAME"));
// we optimize this case assertTrue(rs.next());
stat.execute("create table t(x varchar(2)) as select 'x'"); assertEquals("Y", rs.getString("COLUMN_NAME"));
stat.execute("alter table t alter column x varchar(20)"); assertFalse(rs.next());
stat.execute("insert into t values('Hello')"); stat.execute("drop table T");
stat.execute("drop table t"); }
}
private void testAlterTableAlterColumn2() throws SQLException {
private void testAlterTableModifyColumn() throws SQLException { // ensure that increasing a VARCHAR columns length takes effect because
stat.execute("create table t(x int)"); // we optimize this case
stat.execute("alter table t modify column x varchar(20)"); stat.execute("create table t(x varchar(2)) as select 'x'");
stat.execute("insert into t values('Hello')"); stat.execute("alter table t alter column x varchar(20)");
stat.execute("drop table t"); stat.execute("insert into t values('Hello')");
} stat.execute("drop table t");
}
/**
* Test for fix "Change not-null / null -constraint to existing column" private void testAlterTableModifyColumn() throws SQLException {
* (MySql/ORACLE - SQL style) that failed silently corrupting the changed stat.execute("create table t(x int)");
* column.<br/> stat.execute("alter table t modify column x varchar(20)");
* Before the change (added after v1.4.196) following was observed: stat.execute("insert into t values('Hello')");
* <pre> stat.execute("drop table t");
* alter table T modify C int null; -- Worked as expected }
* alter table T modify C null; -- Silently corrupted column C
* </pre> /**
*/ * Test for fix "Change not-null / null -constraint to existing column"
private void testAlterTableModifyColumnSetNull() throws SQLException { * (MySql/ORACLE - SQL style) that failed silently corrupting the changed
// This worked in v1.4.196 * column.<br/>
stat.execute("create table T (C varchar not null)"); * Before the change (added after v1.4.196) following was observed:
stat.execute("alter table T modify C int null"); * <pre>
stat.execute("insert into T values(null)"); * alter table T modify C int null; -- Worked as expected
stat.execute("drop table T"); * alter table T modify C null; -- Silently corrupted column C
// This failed in v1.4.196 * </pre>
stat.execute("create table T (C int not null)"); */
stat.execute("alter table T modify C null"); // Silently corrupted column C private void testAlterTableModifyColumnSetNull() throws SQLException {
stat.execute("insert into T values(null)"); // <- Fixed in v1.4.196 - NULL is allowed // This worked in v1.4.196
stat.execute("drop table T"); stat.execute("create table T (C varchar not null)");
} stat.execute("alter table T modify C int null");
} stat.execute("insert into T values(null)");
stat.execute("drop table T");
// This failed in v1.4.196
stat.execute("create table T (C int not null)");
stat.execute("alter table T modify C null"); // Silently corrupted column C
stat.execute("insert into T values(null)"); // <- Fixed in v1.4.196 - NULL is allowed
stat.execute("drop table T");
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论