提交 595c4dd3 authored 作者: Thomas Mueller's avatar Thomas Mueller

There was a way to prevent a database from being re-opened, by creating a column…

There was a way to prevent a database from being re-opened, by creating a column constraint that references a table with a higher id.
上级 58c1f264
......@@ -7,12 +7,14 @@
package org.h2.command.ddl;
import java.util.ArrayList;
import java.util.HashSet;
import org.h2.api.ErrorCode;
import org.h2.command.CommandInterface;
import org.h2.command.dml.Insert;
import org.h2.command.dml.Query;
import org.h2.engine.Database;
import org.h2.engine.DbObject;
import org.h2.engine.Session;
import org.h2.expression.Expression;
import org.h2.message.DbException;
......@@ -186,6 +188,28 @@ public class CreateTable extends SchemaCommand {
session.setUndoLogEnabled(old);
}
}
HashSet<DbObject> set = New.hashSet();
set.clear();
table.addDependencies(set);
for (DbObject obj : set) {
if (obj == table) {
continue;
}
if (obj.getType() == DbObject.TABLE_OR_VIEW) {
if (obj instanceof Table) {
Table t = (Table) obj;
if (t.getId() > table.getId()) {
throw DbException.get(
ErrorCode.FEATURE_NOT_SUPPORTED_1,
"Table depends on another table " +
"with a higher ID: " + t +
", this is currently not supported, " +
"as it would prevent the database from " +
"beeing re-opened");
}
}
}
}
} catch (DbException e) {
db.checkPowerOff();
db.removeSchemaObject(session, table);
......
......@@ -41,6 +41,7 @@ public class TestCases extends TestBase {
@Override
public void test() throws Exception {
testReferenceLaterTable();
testAutoCommitInDatabaseURL();
testReferenceableIndexUsage();
testClearSyntaxException();
......@@ -107,6 +108,23 @@ public class TestCases extends TestBase {
deleteDb("cases");
}
private void testReferenceLaterTable() throws SQLException {
deleteDb("cases");
Connection conn = getConnection("cases");
Statement stat = conn.createStatement();
stat.execute("create table a(id int)");
stat.execute("create table b(id int)");
stat.execute("drop table a");
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, stat).
execute("create table a(id int check id < select max(id) from b)");
stat.execute("drop table b");
stat.execute("create table b(id int)");
stat.execute("create table a(id int check id < select max(id) from b)");
conn.close();
conn = getConnection("cases");
conn.close();
}
private void testAutoCommitInDatabaseURL() throws SQLException {
Connection conn = getConnection("cases;autocommit=false");
assertFalse(conn.getAutoCommit());
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论