提交 110e1596 authored 作者: noelgrandin's avatar noelgrandin

Improve error message when check constraint is broken, test case from Gili (cowwoc)

上级 c4a0d51f
...@@ -32,6 +32,7 @@ Change Log ...@@ -32,6 +32,7 @@ Change Log
</li><li>Issue 474: H2 Mysql Compatibility code fails to ignore "COMMENT" in CREATE TABLE, patch from Aaron Azeckoski </li><li>Issue 474: H2 Mysql Compatibility code fails to ignore "COMMENT" in CREATE TABLE, patch from Aaron Azeckoski
</li><li>Issue 476: Broken link in jaqu.html </li><li>Issue 476: Broken link in jaqu.html
</li><li>Fix potential UTF8 encoding issue in org.h2.store.FileStore, reported by Juerg Spiess </li><li>Fix potential UTF8 encoding issue in org.h2.store.FileStore, reported by Juerg Spiess
</li><li>Improve error message when check constraint is broken, test case from Gili (cowwoc)
</li></ul> </li></ul>
<h2>Version 1.3.172 (2013-05-25)</h2> <h2>Version 1.3.172 (2013-05-25)</h2>
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
23506=Referential integrity constraint violation: {0} 23506=Referential integrity constraint violation: {0}
23507=No default value is set for column {0} 23507=No default value is set for column {0}
23513=Check constraint violation: {0} 23513=Check constraint violation: {0}
23514=Check constraint invalid: {0}
28000=Wrong user name or password 28000=Wrong user name or password
40001=Deadlock detected. The current transaction was rolled back. Details: {0} 40001=Deadlock detected. The current transaction was rolled back. Details: {0}
42000=Syntax error in SQL statement {0} 42000=Syntax error in SQL statement {0}
......
...@@ -224,6 +224,12 @@ public class ErrorCode { ...@@ -224,6 +224,12 @@ public class ErrorCode {
*/ */
public static final int CHECK_CONSTRAINT_VIOLATED_1 = 23513; public static final int CHECK_CONSTRAINT_VIOLATED_1 = 23513;
/**
* The error with code <code>23514</code> is thrown during evaluation of a check constraint
* if the SQL generates an error.
*/
public static final int CHECK_CONSTRAINT_INVALID = 23514;
// 28: invalid authorization specification // 28: invalid authorization specification
/** /**
......
...@@ -93,8 +93,14 @@ public class ConstraintCheck extends Constraint { ...@@ -93,8 +93,14 @@ public class ConstraintCheck extends Constraint {
return; return;
} }
filter.set(newRow); filter.set(newRow);
Boolean b;
try {
b = expr.getValue(session).getBoolean();
} catch (DbException ex) {
throw DbException.get(ErrorCode.CHECK_CONSTRAINT_INVALID, ex, getShortDescription());
}
// Both TRUE and NULL are ok // Both TRUE and NULL are ok
if (Boolean.FALSE.equals(expr.getValue(session).getBoolean())) { if (Boolean.FALSE.equals(b)) {
throw DbException.get(ErrorCode.CHECK_CONSTRAINT_VIOLATED_1, getShortDescription()); throw DbException.get(ErrorCode.CHECK_CONSTRAINT_VIOLATED_1, getShortDescription());
} }
} }
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
23506=Referential integrity constraint violation: {0} 23506=Referential integrity constraint violation: {0}
23507=No default value is set for column {0} 23507=No default value is set for column {0}
23513=Check constraint violation: {0} 23513=Check constraint violation: {0}
23514=Check constraint invalid: {0}
28000=Wrong user name or password 28000=Wrong user name or password
40001=Deadlock detected. The current transaction was rolled back. Details: {0} 40001=Deadlock detected. The current transaction was rolled back. Details: {0}
42000=Syntax error in SQL statement {0} 42000=Syntax error in SQL statement {0}
......
...@@ -49,6 +49,7 @@ public class TestTriggersConstraints extends TestBase implements Trigger { ...@@ -49,6 +49,7 @@ public class TestTriggersConstraints extends TestBase implements Trigger {
testTriggerAlterTable(); testTriggerAlterTable();
testTriggers(); testTriggers();
testConstraints(); testConstraints();
testCheckConstraintErrorMessage();
testMultiPartForeignKeys(); testMultiPartForeignKeys();
deleteDb("trigger"); deleteDb("trigger");
} }
...@@ -403,6 +404,36 @@ public class TestTriggersConstraints extends TestBase implements Trigger { ...@@ -403,6 +404,36 @@ public class TestTriggersConstraints extends TestBase implements Trigger {
conn.close(); conn.close();
} }
private void testCheckConstraintErrorMessage() throws SQLException {
Connection conn = getConnection("trigger");
Statement stat = conn.createStatement();
stat.execute("create table companies(id identity)");
stat.execute("create table departments(id identity, "
+ "company_id int not null, "
+ "foreign key(company_id) references companies(id))");
stat.execute("create table connections (id identity, company_id int not null, "
+ "first int not null, second int not null, "
+ "foreign key (company_id) references companies(id), "
+ "foreign key (first) references departments(id), "
+ "foreign key (second) references departments(id), "
+ "check (select departments.company_id from departments, companies where "
+ " departments.id in (first, second)) = company_id)");
stat.execute("insert into companies(id) values(1)");
stat.execute("insert into departments(id, company_id) "
+ "values(10, 1)");
stat.execute("insert into departments(id, company_id) "
+ "values(20, 1)");
assertThrows(ErrorCode.CHECK_CONSTRAINT_INVALID, stat)
.execute("insert into connections(id, company_id, first, second) "
+ "values(100, 1, 10, 20)");
stat.execute("drop table connections");
stat.execute("drop table departments");
stat.execute("drop table companies");
conn.close();
}
/** /**
* Regression test: we had a bug where the AlterTableAddConstraint class * Regression test: we had a bug where the AlterTableAddConstraint class
* used to sometimes pick the wrong unique index for a foreign key. * used to sometimes pick the wrong unique index for a foreign key.
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论