提交 469c5639 authored 作者: Thomas Mueller's avatar Thomas Mueller

Issue 355: ResultSet.findColumn returns the last column index for ambiguous column names.

上级 876a9574
......@@ -2856,25 +2856,23 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
if (columnCount >= SysProperties.MIN_COLUMN_NAME_MAP) {
if (columnLabelMap == null) {
HashMap<String, Integer> map = New.hashMap(columnCount);
// first column names
// column labels have higher priority
for (int i = 0; i < columnCount; i++) {
String c = StringUtils.toUpperEnglish(result.getAlias(i));
mapColumn(map, c, i);
}
for (int i = 0; i < columnCount; i++) {
String colName = result.getColumnName(i);
if (colName != null) {
colName = StringUtils.toUpperEnglish(colName);
map.put(colName, i);
mapColumn(map, colName, i);
String tabName = result.getTableName(i);
if (tabName != null) {
colName = StringUtils.toUpperEnglish(tabName) + "." + colName;
map.put(colName, i);
mapColumn(map, colName, i);
}
}
}
// column labels have higher priority
// column names with the same name are replaced
for (int i = 0; i < columnCount; i++) {
String c = StringUtils.toUpperEnglish(result.getAlias(i));
map.put(c, i);
}
// assign at the end so concurrent access is supported
columnLabelMap = map;
}
......@@ -2908,6 +2906,16 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
throw DbException.get(ErrorCode.COLUMN_NOT_FOUND_1, columnLabel);
}
private void mapColumn(HashMap<String, Integer> map, String label, int index) {
// put the index (usually that's the only operation)
Integer old = map.put(label, index);
if (old != null) {
// if there was a clash (which is seldom),
// put the old one back
map.put(label, old);
}
}
private void checkColumnIndex(int columnIndex) {
checkClosed();
if (columnIndex < 1 || columnIndex > columnCount) {
......
......@@ -55,6 +55,7 @@ public class TestResultSet extends TestBase {
stat = conn.createStatement();
testAmbiguousColumnNames();
testInsertRowWithUpdateableResultSetDefault();
testBeforeFirstAfterLast();
testParseSpecialValues();
......@@ -91,6 +92,15 @@ public class TestResultSet extends TestBase {
}
private void testAmbiguousColumnNames() throws SQLException {
stat.execute("create table test(id int)");
stat.execute("insert into test values(1)");
ResultSet rs = stat.executeQuery("select 1 x, 2 x, 3 x, 4 x, 5 x, 6 x from test");
rs.next();
assertEquals(1, rs.getInt("x"));
stat.execute("drop table test");
}
private void testInsertRowWithUpdateableResultSetDefault() throws SQLException {
stat.execute("create table test(id int primary key, data varchar(255) default 'Hello')");
PreparedStatement prep = conn.prepareStatement("select * from test",
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论