提交 9c26bbb3 authored 作者: Thomas Mueller's avatar Thomas Mueller

ALTER TABLE ALTER COLUMN could throw the wrong exception.

上级 96341778
...@@ -18,7 +18,9 @@ Change Log ...@@ -18,7 +18,9 @@ Change Log
<h1>Change Log</h1> <h1>Change Log</h1>
<h2>Next Version (unreleased)</h2> <h2>Next Version (unreleased)</h2>
<ul><li>Updatable result sets: the key columns can now be updated. <ul><li>ALTER TABLE ALTER COLUMN could throw the wrong exception in the last version
(Table not found).
</li><li>Updatable result sets: the key columns can now be updated.
</li><li>The H2DatabaseProvider for ActiveObjects is now included in the tools section. </li><li>The H2DatabaseProvider for ActiveObjects is now included in the tools section.
</li><li>The H2Platform for Oracle Toplink Essential has been improved a bit. </li><li>The H2Platform for Oracle Toplink Essential has been improved a bit.
</li><li>The Windows service to start H2 didn't work in version 1.1. </li><li>The Windows service to start H2 didn't work in version 1.1.
......
...@@ -267,13 +267,7 @@ public class AlterTableAlterColumn extends SchemaCommand { ...@@ -267,13 +267,7 @@ public class AlterTableAlterColumn extends SchemaCommand {
buff.append(" FROM "); buff.append(" FROM ");
buff.append(table.getSQL()); buff.append(table.getSQL());
String newTableSQL = buff.toString(); String newTableSQL = buff.toString();
try {
execute(newTableSQL, true); execute(newTableSQL, true);
} catch (SQLException e) {
unlinkSequences(newTable);
execute("DROP TABLE " + newTable.getSQL(), true);
throw e;
}
newTable = (TableData) newTable.getSchema().getTableOrView(session, newTable.getName()); newTable = (TableData) newTable.getSchema().getTableOrView(session, newTable.getName());
ObjectArray children = table.getChildren(); ObjectArray children = table.getChildren();
ObjectArray triggers = new ObjectArray(); ObjectArray triggers = new ObjectArray();
...@@ -347,19 +341,6 @@ public class AlterTableAlterColumn extends SchemaCommand { ...@@ -347,19 +341,6 @@ public class AlterTableAlterColumn extends SchemaCommand {
} }
} }
private void unlinkSequences(Table table) {
Column[] columns = table.getColumns();
for (int i = 0; i < columns.length; i++) {
// if we don't do that, the sequence is dropped when the table is
// dropped
Sequence seq = columns[i].getSequence();
if (seq != null) {
table.removeSequence(session, seq);
columns[i].setSequence(null);
}
}
}
private void execute(String sql, boolean ddl) throws SQLException { private void execute(String sql, boolean ddl) throws SQLException {
Prepared command = session.prepare(sql); Prepared command = session.prepare(sql);
command.update(); command.update();
......
...@@ -1528,7 +1528,14 @@ public class Database implements DataHandler { ...@@ -1528,7 +1528,14 @@ public class Database implements DataHandler {
removeMeta(session, id); removeMeta(session, id);
} }
private String getDependentObject(SchemaObject obj) { /**
* Get the first table that depends on this object.
*
* @param obj the object to find
* @param except the table to exclude (or null)
* @return the first dependent table, or null
*/
public Table getDependentTable(SchemaObject obj, Table except) {
switch (obj.getType()) { switch (obj.getType()) {
case DbObject.COMMENT: case DbObject.COMMENT:
case DbObject.CONSTRAINT: case DbObject.CONSTRAINT:
...@@ -1543,10 +1550,13 @@ public class Database implements DataHandler { ...@@ -1543,10 +1550,13 @@ public class Database implements DataHandler {
HashSet set = new HashSet(); HashSet set = new HashSet();
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {
Table t = (Table) list.get(i); Table t = (Table) list.get(i);
if (except == t) {
continue;
}
set.clear(); set.clear();
t.addDependencies(set); t.addDependencies(set);
if (set.contains(obj)) { if (set.contains(obj)) {
return t.getSQL(); return t;
} }
} }
return null; return null;
...@@ -1595,7 +1605,8 @@ public class Database implements DataHandler { ...@@ -1595,7 +1605,8 @@ public class Database implements DataHandler {
obj.getSchema().remove(obj); obj.getSchema().remove(obj);
String invalid; String invalid;
if (SysProperties.OPTIMIZE_DROP_DEPENDENCIES) { if (SysProperties.OPTIMIZE_DROP_DEPENDENCIES) {
invalid = getDependentObject(obj); Table t = getDependentTable(obj, null);
invalid = t == null ? null : t.getSQL();
} else { } else {
invalid = getFirstInvalidTable(session); invalid = getFirstInvalidTable(session);
} }
......
...@@ -404,10 +404,14 @@ public abstract class Table extends SchemaObjectBase { ...@@ -404,10 +404,14 @@ public abstract class Table extends SchemaObjectBase {
Sequence sequence = (Sequence) sequences.get(0); Sequence sequence = (Sequence) sequences.get(0);
sequences.remove(0); sequences.remove(0);
if (!getTemporary()) { if (!getTemporary()) {
// only remove if no other table depends on this sequence
// this is possible when calling ALTER TABLE ALTER COLUMN
if (database.getDependentTable(sequence, this) == null) {
database.removeSchemaObject(session, sequence); database.removeSchemaObject(session, sequence);
} }
} }
} }
}
/** /**
* Check that this column is not referenced by a referential constraint or * Check that this column is not referenced by a referential constraint or
......
...@@ -12,6 +12,7 @@ import java.util.Properties; ...@@ -12,6 +12,7 @@ import java.util.Properties;
import org.h2.engine.Constants; import org.h2.engine.Constants;
import org.h2.store.fs.FileSystemDisk; import org.h2.store.fs.FileSystemDisk;
import org.h2.test.bench.TestPerformance; import org.h2.test.bench.TestPerformance;
import org.h2.test.db.TestAlter;
import org.h2.test.db.TestAutoRecompile; import org.h2.test.db.TestAutoRecompile;
import org.h2.test.db.TestBackup; import org.h2.test.db.TestBackup;
import org.h2.test.db.TestBigDb; import org.h2.test.db.TestBigDb;
...@@ -499,6 +500,7 @@ http://www.w3schools.com/sql/ ...@@ -499,6 +500,7 @@ http://www.w3schools.com/sql/
// db // db
new TestScriptSimple().runTest(this); new TestScriptSimple().runTest(this);
new TestScript().runTest(this); new TestScript().runTest(this);
new TestAlter().runTest(this);
new TestAutoRecompile().runTest(this); new TestAutoRecompile().runTest(this);
new TestBackup().runTest(this); new TestBackup().runTest(this);
new TestBigDb().runTest(this); new TestBigDb().runTest(this);
......
/*
* Copyright 2004-2008 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.db;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import org.h2.constant.ErrorCode;
import org.h2.test.TestBase;
/**
* Test ALTER statements.
*/
public class TestAlter extends TestBase {
private Connection conn;
private Statement stat;
/**
* Run just this test.
*
* @param a ignored
*/
public static void main(String[] a) throws Exception {
TestBase.createCaller().init().test();
}
public void test() throws Exception {
deleteDb("alter");
conn = getConnection("alter");
stat = conn.createStatement();
testAlterTableAlterColumn();
conn.close();
deleteDb("alter");
}
private void testAlterTableAlterColumn() throws SQLException {
stat.execute("create table t(x varchar) as select 'x'");
try {
stat.execute("alter table t alter column x int");
} catch (SQLException e) {
assertEquals(ErrorCode.DATA_CONVERSION_ERROR_1, e.getErrorCode());
}
stat.execute("drop table t");
stat.execute("create table t(id identity, x varchar) as select null, 'x'");
try {
stat.execute("alter table t alter column x int");
} catch (SQLException e) {
assertEquals(ErrorCode.DATA_CONVERSION_ERROR_1, e.getErrorCode());
}
stat.execute("drop table t");
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论