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

Fixed CR/LF issue with Git

上级 d4a99ea1
/* /*
* 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, b varchar, c int, d int)");
stat.execute("alter table test drop column name, name2"); stat.execute("alter table test drop column b, c");
stat.execute("drop table test"); stat.execute("alter table test drop d");
// Test-Case: Same as above but using brackets (Oracle style) stat.execute("drop table test");
stat.execute("create table test(id int, b varchar, c int, d int)"); // Test-Case: Same as above but using brackets (Oracle style)
stat.execute("alter table test drop column (b, c)"); stat.execute("create table test(id int, b varchar, c int, d int)");
assertThrows(ErrorCode.COLUMN_NOT_FOUND_1, stat). stat.execute("alter table test drop column (b, c)");
execute("alter table test drop column b"); assertThrows(ErrorCode.COLUMN_NOT_FOUND_1, stat).
stat.execute("alter table test drop (d)"); execute("alter table test drop column b");
stat.execute("drop table test"); stat.execute("alter table test drop (d)");
// Test-Case: Error if dropping all columns stat.execute("drop table test");
stat.execute("create table test(id int, name varchar, name2 varchar)"); // Test-Case: Error if dropping all columns
assertThrows(ErrorCode.CANNOT_DROP_LAST_COLUMN, stat). stat.execute("create table test(id int, name varchar, name2 varchar)");
execute("alter table test drop column id, name, name2"); assertThrows(ErrorCode.CANNOT_DROP_LAST_COLUMN, stat).
stat.execute("drop table test"); execute("alter table test drop column id, name, name2");
} stat.execute("drop table test");
}
/**
* 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 * Tests a bug we used to have where altering the name of a column that had
* to re-open the DB. * a check constraint that referenced itself would result in not being able
*/ * to re-open the DB.
private void testAlterTableAlterColumnWithConstraint() throws SQLException { */
if (config.memory) { private void testAlterTableAlterColumnWithConstraint() throws SQLException {
return; if (config.memory) {
} return;
stat.execute("create table test(id int check(id in (1,2)) )"); }
stat.execute("alter table test alter id rename to id2"); stat.execute("create table test(id int check(id in (1,2)) )");
// disconnect and reconnect stat.execute("alter table test alter id rename to id2");
conn.close(); // disconnect and reconnect
conn = getConnection(getTestName()); conn.close();
stat = conn.createStatement(); conn = getConnection(getTestName());
stat.execute("insert into test values(1)"); stat = conn.createStatement();
assertThrows(ErrorCode.CHECK_CONSTRAINT_VIOLATED_1, stat). stat.execute("insert into test values(1)");
execute("insert into test values(3)"); assertThrows(ErrorCode.CHECK_CONSTRAINT_VIOLATED_1, stat).
stat.execute("drop table test"); execute("insert into test values(3)");
} stat.execute("drop table test");
}
private void testAlterTableRenameConstraint() throws SQLException {
stat.execute("create table test(id int, name varchar(255))"); private void testAlterTableRenameConstraint() throws SQLException {
stat.execute("alter table test add constraint x check (id > name)"); stat.execute("create table test(id int, name varchar(255))");
stat.execute("alter table test rename constraint x to x2"); stat.execute("alter table test add constraint x check (id > name)");
stat.execute("drop table test"); stat.execute("alter table test rename constraint x to x2");
} stat.execute("drop table test");
}
private void testAlterTableDropIdentityColumn() throws SQLException {
stat.execute("create table test(id int auto_increment, name varchar)"); private void testAlterTableDropIdentityColumn() throws SQLException {
stat.execute("alter table test drop column id"); stat.execute("create table test(id int auto_increment, name varchar)");
ResultSet rs = stat.executeQuery("select * from INFORMATION_SCHEMA.SEQUENCES"); stat.execute("alter table test drop column id");
assertFalse(rs.next()); ResultSet rs = stat.executeQuery("select * from INFORMATION_SCHEMA.SEQUENCES");
stat.execute("drop table test"); assertFalse(rs.next());
stat.execute("drop table test");
stat.execute("create table test(id int auto_increment, name varchar)");
stat.execute("alter table test drop column name"); stat.execute("create table test(id int auto_increment, name varchar)");
rs = stat.executeQuery("select * from INFORMATION_SCHEMA.SEQUENCES"); stat.execute("alter table test drop column name");
assertTrue(rs.next()); rs = stat.executeQuery("select * from INFORMATION_SCHEMA.SEQUENCES");
stat.execute("drop table test"); assertTrue(rs.next());
} stat.execute("drop table test");
}
private void testAlterTableAlterColumn() throws SQLException {
stat.execute("create table t(x varchar) as select 'x'"); private void testAlterTableAlterColumn() throws SQLException {
assertThrows(ErrorCode.DATA_CONVERSION_ERROR_1, stat). stat.execute("create table t(x varchar) as select 'x'");
execute("alter table t alter column x int"); assertThrows(ErrorCode.DATA_CONVERSION_ERROR_1, stat).
stat.execute("drop table t"); execute("alter table t alter column x int");
stat.execute("create table t(id identity, x varchar) as select null, 'x'"); stat.execute("drop table t");
assertThrows(ErrorCode.DATA_CONVERSION_ERROR_1, stat). stat.execute("create table t(id identity, x varchar) as select null, 'x'");
execute("alter table t alter column x int"); assertThrows(ErrorCode.DATA_CONVERSION_ERROR_1, stat).
stat.execute("drop table t"); execute("alter table t alter column x int");
} stat.execute("drop table t");
}
private void testAlterTableAddColumnIdentity() throws SQLException {
stat.execute("create table t(x varchar)"); private void testAlterTableAddColumnIdentity() throws SQLException {
stat.execute("alter table t add id bigint identity(5, 5) not null"); stat.execute("create table t(x varchar)");
stat.execute("insert into t values (null, null)"); stat.execute("alter table t add id bigint identity(5, 5) not null");
stat.execute("insert into t values (null, null)"); stat.execute("insert into t values (null, null)");
ResultSet rs = stat.executeQuery("select id from t order by id"); stat.execute("insert into t values (null, null)");
assertTrue(rs.next()); ResultSet rs = stat.executeQuery("select id from t order by id");
assertEquals(5, rs.getInt(1)); assertTrue(rs.next());
assertTrue(rs.next()); assertEquals(5, rs.getInt(1));
assertEquals(10, rs.getInt(1)); assertTrue(rs.next());
assertFalse(rs.next()); assertEquals(10, rs.getInt(1));
stat.execute("drop table t"); assertFalse(rs.next());
} stat.execute("drop table t");
}
private void testAlterTableAddColumnIfNotExists() throws SQLException {
stat.execute("create table t(x varchar) as select 'x'"); private void testAlterTableAddColumnIfNotExists() throws SQLException {
stat.execute("alter table t add if not exists x int"); stat.execute("create table t(x varchar) as select 'x'");
stat.execute("drop table t"); stat.execute("alter table t add if not exists x int");
stat.execute("create table t(x varchar) as select 'x'"); stat.execute("drop table t");
stat.execute("alter table t add if not exists y int"); stat.execute("create table t(x varchar) as select 'x'");
stat.execute("select x, y from t"); stat.execute("alter table t add if not exists y int");
stat.execute("drop table t"); stat.execute("select x, y from t");
} stat.execute("drop table t");
}
private void testAlterTableAddMultipleColumns() throws SQLException {
stat.execute("create table t(x varchar) as select 'x'"); private void testAlterTableAddMultipleColumns() throws SQLException {
stat.execute("alter table t add (y int, z varchar)"); stat.execute("create table t(x varchar) as select 'x'");
stat.execute("drop table t"); stat.execute("alter table t add (y int, z varchar)");
stat.execute("create table t(x varchar) as select 'x'"); stat.execute("drop table t");
stat.execute("alter table t add (y int)"); stat.execute("create table t(x varchar) as select 'x'");
stat.execute("drop table t"); stat.execute("alter table t add (y int)");
} stat.execute("drop table t");
}
// column and field names must be upper-case due to getMetaData sensitivity
private void testAlterTableAddMultipleColumnsBefore() throws SQLException { // column and field names must be upper-case due to getMetaData sensitivity
stat.execute("create table T(X varchar)"); private void testAlterTableAddMultipleColumnsBefore() throws SQLException {
stat.execute("alter table T add (Y int, Z int) before X"); stat.execute("create table T(X varchar)");
DatabaseMetaData dbMeta = conn.getMetaData(); stat.execute("alter table T add (Y int, Z int) before X");
ResultSet rs = dbMeta.getColumns(null, null, "T", null); DatabaseMetaData dbMeta = conn.getMetaData();
assertTrue(rs.next()); ResultSet rs = dbMeta.getColumns(null, null, "T", null);
assertEquals("Y", rs.getString("COLUMN_NAME")); assertTrue(rs.next());
assertTrue(rs.next()); assertEquals("Y", rs.getString("COLUMN_NAME"));
assertEquals("Z", rs.getString("COLUMN_NAME")); assertTrue(rs.next());
assertTrue(rs.next()); assertEquals("Z", rs.getString("COLUMN_NAME"));
assertEquals("X", rs.getString("COLUMN_NAME")); assertTrue(rs.next());
assertFalse(rs.next()); assertEquals("X", rs.getString("COLUMN_NAME"));
stat.execute("drop table T"); 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 { // column and field names must be upper-case due to getMetaData sensitivity
stat.execute("create table T(X varchar)"); private void testAlterTableAddMultipleColumnsAfter() throws SQLException {
stat.execute("alter table T add (Y int, Z int) after X"); stat.execute("create table T(X varchar)");
DatabaseMetaData dbMeta = conn.getMetaData(); stat.execute("alter table T add (Y int, Z int) after X");
ResultSet rs = dbMeta.getColumns(null, null, "T", null); DatabaseMetaData dbMeta = conn.getMetaData();
assertTrue(rs.next()); ResultSet rs = dbMeta.getColumns(null, null, "T", null);
assertEquals("X", rs.getString("COLUMN_NAME")); assertTrue(rs.next());
assertTrue(rs.next()); assertEquals("X", rs.getString("COLUMN_NAME"));
assertEquals("Y", rs.getString("COLUMN_NAME")); assertTrue(rs.next());
assertTrue(rs.next()); assertEquals("Y", rs.getString("COLUMN_NAME"));
assertEquals("Z", rs.getString("COLUMN_NAME")); assertTrue(rs.next());
assertFalse(rs.next()); assertEquals("Z", rs.getString("COLUMN_NAME"));
stat.execute("drop table T"); assertFalse(rs.next());
} stat.execute("drop table T");
}
// column and field names must be upper-case due to getMetaData sensitivity
private void testAlterTableAddColumnBefore() throws SQLException { // column and field names must be upper-case due to getMetaData sensitivity
stat.execute("create table T(X varchar)"); private void testAlterTableAddColumnBefore() throws SQLException {
stat.execute("alter table T add Y int before X"); stat.execute("create table T(X varchar)");
DatabaseMetaData dbMeta = conn.getMetaData(); stat.execute("alter table T add Y int before X");
ResultSet rs = dbMeta.getColumns(null, null, "T", null); DatabaseMetaData dbMeta = conn.getMetaData();
assertTrue(rs.next()); ResultSet rs = dbMeta.getColumns(null, null, "T", null);
assertEquals("Y", rs.getString("COLUMN_NAME")); assertTrue(rs.next());
assertTrue(rs.next()); assertEquals("Y", rs.getString("COLUMN_NAME"));
assertEquals("X", rs.getString("COLUMN_NAME")); assertTrue(rs.next());
assertFalse(rs.next()); assertEquals("X", rs.getString("COLUMN_NAME"));
stat.execute("drop table T"); 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 { // column and field names must be upper-case due to getMetaData sensitivity
stat.execute("create table T(X varchar)"); private void testAlterTableAddColumnAfter() throws SQLException {
stat.execute("alter table T add Y int after X"); stat.execute("create table T(X varchar)");
DatabaseMetaData dbMeta = conn.getMetaData(); stat.execute("alter table T add Y int after X");
ResultSet rs = dbMeta.getColumns(null, null, "T", null); DatabaseMetaData dbMeta = conn.getMetaData();
assertTrue(rs.next()); ResultSet rs = dbMeta.getColumns(null, null, "T", null);
assertEquals("X", rs.getString("COLUMN_NAME")); assertTrue(rs.next());
assertTrue(rs.next()); assertEquals("X", rs.getString("COLUMN_NAME"));
assertEquals("Y", rs.getString("COLUMN_NAME")); assertTrue(rs.next());
assertFalse(rs.next()); assertEquals("Y", rs.getString("COLUMN_NAME"));
stat.execute("drop table T"); assertFalse(rs.next());
} stat.execute("drop table T");
}
private void testAlterTableAlterColumn2() throws SQLException {
// ensure that increasing a VARCHAR columns length takes effect because private void testAlterTableAlterColumn2() throws SQLException {
// we optimize this case // ensure that increasing a VARCHAR columns length takes effect because
stat.execute("create table t(x varchar(2)) as select 'x'"); // we optimize this case
stat.execute("alter table t alter 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");
}
private void testAlterTableModifyColumn() throws SQLException {
stat.execute("create table t(x int)"); private void testAlterTableModifyColumn() throws SQLException {
stat.execute("alter table t modify column x varchar(20)"); stat.execute("create table t(x int)");
stat.execute("insert into t values('Hello')"); stat.execute("alter table t modify 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" /**
* (MySql/ORACLE - SQL style) that failed silently corrupting the changed * Test for fix "Change not-null / null -constraint to existing column"
* column.<br/> * (MySql/ORACLE - SQL style) that failed silently corrupting the changed
* Before the change (added after v1.4.196) following was observed: * column.<br/>
* <pre> * Before the change (added after v1.4.196) following was observed:
* alter table T modify C int null; -- Worked as expected * <pre>
* alter table T modify C null; -- Silently corrupted column C * alter table T modify C int null; -- Worked as expected
* </pre> * alter table T modify C null; -- Silently corrupted column C
*/ * </pre>
private void testAlterTableModifyColumnSetNull() throws SQLException { */
// This worked in v1.4.196 private void testAlterTableModifyColumnSetNull() throws SQLException {
stat.execute("create table T (C varchar not null)"); // This worked in v1.4.196
stat.execute("alter table T modify C int null"); stat.execute("create table T (C varchar not null)");
stat.execute("insert into T values(null)"); stat.execute("alter table T modify C int null");
stat.execute("drop table T"); stat.execute("insert into T values(null)");
// This failed in v1.4.196 stat.execute("drop table T");
stat.execute("create table T (C int not null)"); // This failed in v1.4.196
stat.execute("alter table T modify C null"); // Silently corrupted column C stat.execute("create table T (C int not null)");
stat.execute("insert into T values(null)"); // <- Fixed in v1.4.196 - NULL is allowed stat.execute("alter table T modify C null"); // Silently corrupted column C
stat.execute("drop table T"); stat.execute("insert into T values(null)"); // <- Fixed in v1.4.196 - NULL is allowed
} stat.execute("drop table T");
} }
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论