Skip to content
Permalink
Browse files

[CONJ-669] No such column exception when reading result-set when empt…

…y column name

(cherry picked from commit d6fbdc4)
  • Loading branch information...
rusher committed Jan 7, 2019
1 parent 2e3110a commit d4a7bd868e1d542af0ec6fc56e862f4020cac864
@@ -52,94 +52,94 @@

package org.mariadb.jdbc.internal.com.read.dao;

import org.mariadb.jdbc.internal.com.read.resultset.ColumnInformation;
import org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.mariadb.jdbc.internal.com.read.resultset.ColumnInformation;
import org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper;


public class ColumnNameMap {
private Map<String, Integer> originalMap;
private Map<String, Integer> aliasMap;
private final ColumnInformation[] columnInfo;

public ColumnNameMap(ColumnInformation[] columnInformations) {
this.columnInfo = columnInformations;
}
private final ColumnInformation[] columnInfo;
private Map<String, Integer> originalMap;
private Map<String, Integer> aliasMap;

/**
* Get column index by name.
*
* @param name column name
* @return index.
* @throws SQLException if no column info exists, or column is unknown
*/
public int getIndex(String name) throws SQLException {
if (name == null) throw new SQLException("Column name cannot be null");
String lowerName = name.toLowerCase(Locale.ROOT);
// The specs in JDBC 4.0 specify that ResultSet.findColumn and
// ResultSet.getXXX(String name) should use column alias (AS in the query). If label is not found, we use
// original table name.
if (aliasMap == null) {
aliasMap = new HashMap<String, Integer>();
int counter = 0;
for (ColumnInformation ci : columnInfo) {
String columnAlias = ci.getName();
if (columnAlias != null && !columnAlias.isEmpty()) {
columnAlias = columnAlias.toLowerCase(Locale.ROOT);
if (!aliasMap.containsKey(columnAlias)) {
aliasMap.put(columnAlias, counter);
}
public ColumnNameMap(ColumnInformation[] columnInformations) {
this.columnInfo = columnInformations;
}

String tableName = ci.getTable();
if (tableName != null && !tableName.isEmpty()) {
tableName = tableName.toLowerCase(Locale.ROOT);
if (!aliasMap.containsKey(tableName + "." + columnAlias)) {
aliasMap.put(tableName + "." + columnAlias, counter);
}
}
}
counter++;
/**
* Get column index by name.
*
* @param name column name
* @return index.
* @throws SQLException if no column info exists, or column is unknown
*/
public int getIndex(String name) throws SQLException {
if (name == null) {
throw new SQLException("Column name cannot be null");
}
String lowerName = name.toLowerCase(Locale.ROOT);
// The specs in JDBC 4.0 specify that ResultSet.findColumn and
// ResultSet.getXXX(String name) should use column alias (AS in the query). If label is not found, we use
// original table name.
if (aliasMap == null) {
aliasMap = new HashMap<>();
int counter = 0;
for (ColumnInformation ci : columnInfo) {
String columnAlias = ci.getName();
if (columnAlias != null) {
columnAlias = columnAlias.toLowerCase(Locale.ROOT);
if (!aliasMap.containsKey(columnAlias)) {
aliasMap.put(columnAlias, counter);
}
String tableName = ci.getTable();
if (tableName != null) {
tableName = tableName.toLowerCase(Locale.ROOT);
if (!aliasMap.containsKey(tableName + "." + columnAlias)) {
aliasMap.put(tableName + "." + columnAlias, counter);
}
}
}
counter++;
}
}

Integer res = aliasMap.get(lowerName);
if (res != null) {
return res;
}

if (originalMap == null) {
originalMap = new HashMap<String, Integer>();
int counter = 0;
for (ColumnInformation ci : columnInfo) {
String columnRealName = ci.getOriginalName();
if (columnRealName != null && !columnRealName.isEmpty()) {
columnRealName = columnRealName.toLowerCase(Locale.ROOT);
if (!originalMap.containsKey(columnRealName)) {
originalMap.put(columnRealName, counter);
}
Integer res = aliasMap.get(lowerName);
if (res != null) {
return res;
}

String tableName = ci.getOriginalTable();
if (tableName != null && !tableName.isEmpty()) {
tableName = tableName.toLowerCase(Locale.ROOT);
if (!originalMap.containsKey(tableName + "." + columnRealName)) {
originalMap.put(tableName + "." + columnRealName, counter);
}
}
}
counter++;
if (originalMap == null) {
originalMap = new HashMap<>();
int counter = 0;
for (ColumnInformation ci : columnInfo) {
String columnRealName = ci.getOriginalName();
if (columnRealName != null) {
columnRealName = columnRealName.toLowerCase(Locale.ROOT);
if (!originalMap.containsKey(columnRealName)) {
originalMap.put(columnRealName, counter);
}
String tableName = ci.getOriginalTable();
if (tableName != null) {
tableName = tableName.toLowerCase(Locale.ROOT);
if (!originalMap.containsKey(tableName + "." + columnRealName)) {
originalMap.put(tableName + "." + columnRealName, counter);
}
}
}
counter++;
}
}

res = originalMap.get(lowerName);

if (res == null) {
throw ExceptionMapper.get("No such column: " + name, "42S22", 1054, null, false);
}
return res;
res = originalMap.get(lowerName);

if (res == null) {
throw ExceptionMapper.get("No such column: " + name, "42S22", 1054, null, false);
}
return res;

}
}

0 comments on commit d4a7bd8

Please sign in to comment.
You can’t perform that action at this time.