提交 7205f150 authored 作者: Thomas Mueller's avatar Thomas Mueller

Renaming tables that have foreign keys with cascade didn't work correctly.

上级 075899ac
......@@ -18,7 +18,8 @@ Change Log
<h1>Change Log</h1>
<h2>Next Version (unreleased)</h2>
<ul><li>The auto-reconnect feature didn't work when using the auto-server mode. Fixed.
<ul><li>Renaming tables that have foreign keys with cascade didn't work correctly.
</li><li>The auto-reconnect feature didn't work when using the auto-server mode. Fixed.
</li><li>Fulltext search: new method FT_DROP_INDEX.
</li><li>The optimization to group using an index didn't work in some cases in version 1.1
(see also system property h2.optimizeGroupSorted).
......
......@@ -118,6 +118,12 @@ public abstract class Constraint extends SchemaObjectBase implements Comparable
*/
public abstract void checkExistingData(Session session) throws SQLException;
/**
* This method is called after a related table has changed
* (the table was renamed, or columns have been renamed).
*/
public abstract void rebuild() throws SQLException;
/**
* Get the unique index used to enforce this constraint, or null if no index
* is used.
......
......@@ -143,4 +143,8 @@ public class ConstraintCheck extends Constraint {
return null;
}
public void rebuild() throws SQLException {
// nothing to do
}
}
......@@ -484,15 +484,22 @@ public class ConstraintReferential extends Constraint {
* @param action the action
*/
public void setDeleteAction(int action) throws SQLException {
if (action == deleteAction) {
if (action == deleteAction && deleteSQL == null) {
return;
}
if (deleteAction != RESTRICT) {
throw Message.getSQLException(ErrorCode.CONSTRAINT_ALREADY_EXISTS_1, "ON DELETE");
}
this.deleteAction = action;
buildDeleteSQL();
}
private void buildDeleteSQL() {
if (deleteAction == RESTRICT) {
return;
}
StringBuffer buff = new StringBuffer();
if (action == CASCADE) {
if (deleteAction == CASCADE) {
buff.append("DELETE FROM ");
buff.append(table.getSQL());
} else {
......@@ -520,19 +527,31 @@ public class ConstraintReferential extends Constraint {
* @param action the action
*/
public void setUpdateAction(int action) throws SQLException {
if (action == updateAction) {
if (action == updateAction && updateSQL == null) {
return;
}
if (updateAction != RESTRICT) {
throw Message.getSQLException(ErrorCode.CONSTRAINT_ALREADY_EXISTS_1, "ON UPDATE");
}
this.updateAction = action;
buildUpdateSQL();
}
private void buildUpdateSQL() {
if (updateAction == RESTRICT) {
return;
}
StringBuffer buff = new StringBuffer();
appendUpdate(buff);
appendWhere(buff);
updateSQL = buff.toString();
}
public void rebuild() throws SQLException {
buildUpdateSQL();
buildDeleteSQL();
}
private Prepared prepare(Session session, String sql, int action) throws SQLException {
Prepared command = session.prepare(sql);
if (action != CASCADE) {
......
......@@ -148,4 +148,8 @@ public class ConstraintUnique extends Constraint {
return index;
}
public void rebuild() throws SQLException {
// nothing to do
}
}
......@@ -98,6 +98,14 @@ public abstract class Table extends SchemaObjectBase {
this.persistent = persistent;
}
public void rename(String newName) throws SQLException {
super.rename(newName);
for (int i = 0; constraints != null && i < constraints.size(); i++) {
Constraint constraint = (Constraint) constraints.get(i);
constraint.rebuild();
}
}
/**
* Lock the table for the given session.
* This method waits until the lock is granted.
......
create table master(id int primary key);
create table detail(id int primary key, x bigint, foreign key(x) references master(id) on delete cascade);
alter table detail alter column x bigint;
insert into master values(0);
insert into detail values(0,0);
delete from master;
drop table master, detail;
drop all objects;
create table test(id int, parent int references test(id) on delete cascade);
insert into test values(0, 0);
alter table test rename to test2;
delete from test2;
drop table test2;
SELECT X FROM dual GROUP BY X HAVING X=AVG(X);
> 1;
create view test_view(id,) as select * from dual;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论