提交 92169c95 authored 作者: Thomas Mueller's avatar Thomas Mueller

Issue 271: ResultSet.getConcurrency() did not correctly detect that the result…

Issue 271: ResultSet.getConcurrency() did not correctly detect that the result set is not updatable.
上级 bb983f8b
...@@ -90,16 +90,44 @@ public class UpdatableRow { ...@@ -90,16 +90,44 @@ public class UpdatableRow {
String c = rs.getString("COLUMN_NAME"); String c = rs.getString("COLUMN_NAME");
key.add(toUpper ? StringUtils.toUpperEnglish(c) : c); key.add(toUpper ? StringUtils.toUpperEnglish(c) : c);
} }
if (key.size() == 0) { if (isIndexUsable(key)) {
isUpdatable = true;
return;
}
key.clear();
rs = meta.getIndexInfo(null, rs = meta.getIndexInfo(null,
JdbcUtils.escapeMetaDataPattern(schemaName), JdbcUtils.escapeMetaDataPattern(schemaName),
tableName, true, true); tableName, true, true);
while (rs.next()) { while (rs.next()) {
int pos = rs.getShort("ORDINAL_POSITION");
if (pos == 1) {
// check the last key if there was any
if (isIndexUsable(key)) {
isUpdatable = true;
return;
}
key.clear();
}
String c = rs.getString("COLUMN_NAME"); String c = rs.getString("COLUMN_NAME");
key.add(toUpper ? StringUtils.toUpperEnglish(c) : c); key.add(toUpper ? StringUtils.toUpperEnglish(c) : c);
} }
if (isIndexUsable(key)) {
isUpdatable = true;
return;
}
key = null;
}
private boolean isIndexUsable(ArrayList<String> indexColumns) {
if (indexColumns.size() == 0) {
return false;
}
for (String c : indexColumns) {
if (findColumnIndex(c) < 0) {
return false;
} }
isUpdatable = key.size() > 0; }
return true;
} }
/** /**
...@@ -111,15 +139,23 @@ public class UpdatableRow { ...@@ -111,15 +139,23 @@ public class UpdatableRow {
return isUpdatable; return isUpdatable;
} }
private int getColumnIndex(String columnName) { private int findColumnIndex(String columnName) {
for (int i = 0; i < columnCount; i++) { for (int i = 0; i < columnCount; i++) {
String col = result.getColumnName(i); String col = result.getColumnName(i);
if (col.equals(columnName)) { if (col.equals(columnName)) {
return i; return i;
} }
} }
return -1;
}
private int getColumnIndex(String columnName) {
int index = findColumnIndex(columnName);
if (index < 0) {
throw DbException.get(ErrorCode.COLUMN_NOT_FOUND_1, columnName); throw DbException.get(ErrorCode.COLUMN_NOT_FOUND_1, columnName);
} }
return index;
}
private void appendColumnList(StatementBuilder buff, boolean set) { private void appendColumnList(StatementBuilder buff, boolean set) {
buff.resetCount(); buff.resetCount();
......
...@@ -38,6 +38,7 @@ public class TestUpdatableResultSet extends TestBase { ...@@ -38,6 +38,7 @@ public class TestUpdatableResultSet extends TestBase {
} }
public void test() throws SQLException { public void test() throws SQLException {
testDetectUpdatable();
testUpdateLob(); testUpdateLob();
testScroll(); testScroll();
testUpdateDeleteInsert(); testUpdateDeleteInsert();
...@@ -46,6 +47,75 @@ public class TestUpdatableResultSet extends TestBase { ...@@ -46,6 +47,75 @@ public class TestUpdatableResultSet extends TestBase {
deleteDb("updatableResultSet"); deleteDb("updatableResultSet");
} }
private void testDetectUpdatable() throws SQLException {
deleteDb("updatableResultSet");
Connection conn = getConnection("updatableResultSet");
Statement stat;
ResultSet rs;
stat = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
stat.execute("create table test(id int primary key, name varchar)");
rs = stat.executeQuery("select * from test");
assertEquals(ResultSet.CONCUR_UPDATABLE, rs.getConcurrency());
rs = stat.executeQuery("select name from test");
assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
stat.execute("drop table test");
stat.execute("create table test(a int, b int, name varchar, primary key(a, b))");
rs = stat.executeQuery("select * from test");
assertEquals(ResultSet.CONCUR_UPDATABLE, rs.getConcurrency());
rs = stat.executeQuery("select a, name from test");
assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
rs = stat.executeQuery("select b, name from test");
assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
rs = stat.executeQuery("select b, name, a from test");
assertEquals(ResultSet.CONCUR_UPDATABLE, rs.getConcurrency());
stat.execute("drop table test");
stat.execute("create table test(a int, b int, name varchar)");
stat.execute("create unique index on test(b, a)");
rs = stat.executeQuery("select * from test");
assertEquals(ResultSet.CONCUR_UPDATABLE, rs.getConcurrency());
rs = stat.executeQuery("select a, name from test");
assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
rs = stat.executeQuery("select b, name from test");
assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
rs = stat.executeQuery("select b, name, a from test");
assertEquals(ResultSet.CONCUR_UPDATABLE, rs.getConcurrency());
stat.execute("drop table test");
stat.execute("create table test(a int, b int, c int unique, name varchar, primary key(a, b))");
rs = stat.executeQuery("select * from test");
assertEquals(ResultSet.CONCUR_UPDATABLE, rs.getConcurrency());
rs = stat.executeQuery("select a, name, c from test");
assertEquals(ResultSet.CONCUR_UPDATABLE, rs.getConcurrency());
rs = stat.executeQuery("select b, a, name, c from test");
assertEquals(ResultSet.CONCUR_UPDATABLE, rs.getConcurrency());
stat.execute("drop table test");
stat.execute("create table test(id int primary key, a int, b int, i int, j int, k int, name varchar)");
stat.execute("create unique index on test(b, a)");
stat.execute("create unique index on test(i, j)");
stat.execute("create unique index on test(a, j)");
rs = stat.executeQuery("select * from test");
assertEquals(ResultSet.CONCUR_UPDATABLE, rs.getConcurrency());
rs = stat.executeQuery("select a, name, b from test");
assertEquals(ResultSet.CONCUR_UPDATABLE, rs.getConcurrency());
rs = stat.executeQuery("select a, name, b from test");
assertEquals(ResultSet.CONCUR_UPDATABLE, rs.getConcurrency());
rs = stat.executeQuery("select i, b, k, name from test");
assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
rs = stat.executeQuery("select a, i, name from test");
assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
rs = stat.executeQuery("select b, i, k, name from test");
assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
rs = stat.executeQuery("select a, k, j, name from test");
assertEquals(ResultSet.CONCUR_UPDATABLE, rs.getConcurrency());
stat.execute("drop table test");
conn.close();
}
private void testUpdateLob() throws SQLException { private void testUpdateLob() throws SQLException {
deleteDb("updatableResultSet"); deleteDb("updatableResultSet");
Connection conn = getConnection("updatableResultSet"); Connection conn = getConnection("updatableResultSet");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论