提交 39baefe5 authored 作者: Thomas Mueller's avatar Thomas Mueller

Primary key violations threw a strange exception message when using a single…

Primary key violations threw a strange exception message when using a single column INT or BIGINT key.
上级 9a0977bd
...@@ -129,7 +129,7 @@ public class PageDataIndex extends PageIndex { ...@@ -129,7 +129,7 @@ public class PageDataIndex extends PageIndex {
throw e; throw e;
} }
if (!retry) { if (!retry) {
throw super.getDuplicateKeyException(); throw getNewDuplicateKeyException();
} }
if (add == 0) { if (add == 0) {
// in the first re-try add a small random number, // in the first re-try add a small random number,
...@@ -146,6 +146,11 @@ public class PageDataIndex extends PageIndex { ...@@ -146,6 +146,11 @@ public class PageDataIndex extends PageIndex {
lastKey = Math.max(lastKey, row.getKey() + 1); lastKey = Math.max(lastKey, row.getKey() + 1);
} }
public DbException getNewDuplicateKeyException() {
String sql = "PRIMARY KEY ON " + table.getSQL() + "(" + indexColumns[mainIndexColumn].getSQL() + ")";
return DbException.get(ErrorCode.DUPLICATE_KEY_1, sql);
}
private void addTry(Session session, Row row) { private void addTry(Session session, Row row) {
while (true) { while (true) {
PageData root = getPage(rootPageId, 0); PageData root = getPage(rootPageId, 0);
......
...@@ -38,6 +38,7 @@ public class TestIndex extends TestBase { ...@@ -38,6 +38,7 @@ public class TestIndex extends TestBase {
public void test() throws SQLException { public void test() throws SQLException {
deleteDb("index"); deleteDb("index");
testErrorMessage();
testNonUniqueHashIndex(); testNonUniqueHashIndex();
testRenamePrimaryKey(); testRenamePrimaryKey();
testRandomized(); testRandomized();
...@@ -89,6 +90,38 @@ public class TestIndex extends TestBase { ...@@ -89,6 +90,38 @@ public class TestIndex extends TestBase {
deleteDb("index"); deleteDb("index");
} }
private void testErrorMessage() throws SQLException {
reconnect();
stat.execute("create table test(id int primary key, name varchar)");
testErrorMessage("PRIMARY KEY ON PUBLIC.TEST(ID)");
stat.execute("create table test(id int, name varchar primary key)");
testErrorMessage("PRIMARY_KEY_2 ON PUBLIC.TEST(NAME)");
stat.execute("create table test(id int, name varchar, primary key(id, name))");
testErrorMessage("PRIMARY_KEY_2 ON PUBLIC.TEST(ID, NAME)");
stat.execute("create table test(id int, name varchar, primary key(name, id))");
testErrorMessage("PRIMARY_KEY_2 ON PUBLIC.TEST(NAME, ID)");
stat.execute("create table test(id int, name int primary key)");
testErrorMessage("PRIMARY KEY ON PUBLIC.TEST(NAME)");
stat.execute("create table test(id int, name int, unique(name))");
testErrorMessage("CONSTRAINT_INDEX_2 ON PUBLIC.TEST(NAME)");
stat.execute("create table test(id int, name int, constraint abc unique(name, id))");
testErrorMessage("ABC_INDEX_2 ON PUBLIC.TEST(NAME, ID)");
}
private void testErrorMessage(String expected) throws SQLException {
try {
stat.execute("INSERT INTO TEST VALUES(1, 1)");
stat.execute("INSERT INTO TEST VALUES(1, 1)");
fail();
} catch (SQLException e) {
String m = e.getMessage();
int start = m.indexOf('\"'), end = m.indexOf('\"', start + 1);
String s = m.substring(start + 1, end);
assertEquals(expected, s);
}
stat.execute("drop table test");
}
private void testNonUniqueHashIndex() throws SQLException { private void testNonUniqueHashIndex() throws SQLException {
reconnect(); reconnect();
stat.execute("create memory table test(id bigint, data bigint)"); stat.execute("create memory table test(id bigint, data bigint)");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论