Change getColumnName to getColumnLabel, should work the same on oracle and fix mysql...

This commit is contained in:
Travis Burtrum 2017-05-26 11:43:33 -04:00
parent 5d859c49d7
commit 67ae0d257c
4 changed files with 29 additions and 23 deletions

View File

@ -50,6 +50,8 @@ public abstract class AbstractRowMapper<K, T> implements RowMapper<K,T> {
protected final boolean mapOnlySecondColumn;
protected String[] keys = null; // for caching if we must generate class
/**
* Create a new RowMapper for the specified ResultSet and return type Class.
* @param resultSet ResultSet to map
@ -75,6 +77,17 @@ public abstract class AbstractRowMapper<K, T> implements RowMapper<K,T> {
this(resultSet, returnTypeClass, cal, null);
}
protected AbstractRowMapper(String[] keys, Class<T> returnTypeClass, Calendar cal, Class<K> mapKeyType) {
_resultSet = null;
_returnTypeClass = returnTypeClass;
_cal = cal;
_mapKeyType = mapKeyType;
this.keys = keys;
_columnCount = keys.length - 1;
mapOnlySecondColumn = _mapKeyType != null && _columnCount == 2;
}
/**
* Build a String array of column names from the ResultSet.
* @return A String array containing the column names contained within the ResultSet.
@ -82,13 +95,17 @@ public abstract class AbstractRowMapper<K, T> implements RowMapper<K,T> {
*/
protected String[] getKeysFromResultSet() throws SQLException {
if(this.keys != null)
return this.keys;
String[] keys;
final ResultSetMetaData md = _resultSet.getMetaData();
keys = new String[_columnCount + 1];
for (int i = 1; i <= _columnCount; i++) {
keys[i] = md.getColumnName(i).toUpperCase();
//keys[i] = md.getColumnName(i).toUpperCase();
keys[i] = md.getColumnLabel(i).toUpperCase();
}
return keys;
return this.keys = keys;
}
}

View File

@ -30,8 +30,6 @@ public class CompilingRowToObjectMapper<K, T> extends RowToObjectMapper<K, T> {
protected final Compiler compiler;
protected final ResultSetToObject<K, T> resultSetToObject;
protected String[] keys = null; // for caching if we must generate class
public CompilingRowToObjectMapper(final Compiler compiler, final Map<CachingRowToObjectMapper.ResultSetKey, ResultSetToObject<?,?>> cache, ResultSet resultSet, Class<T> returnTypeClass, Calendar cal, Class<?> mapValType, Class<K> mapKeyType) {
this(compiler, cache, resultSet, returnTypeClass, cal, mapValType, mapKeyType, false);
}
@ -47,7 +45,6 @@ public class CompilingRowToObjectMapper<K, T> extends RowToObjectMapper<K, T> {
if (resultSetToObject == null) {
//System.out.printf("cache miss, keys: %s\n", keys);
// generate and put into cache
this.keys = keys.keys;
cache.put(keys, this.resultSetToObject = genClass());
this.keys = null;
this._fields = null;
@ -72,13 +69,6 @@ public class CompilingRowToObjectMapper<K, T> extends RowToObjectMapper<K, T> {
public K getMapKey() throws SQLException {
return resultSetToObject.getFirstColumn(_resultSet, _cal);
}
// todo: generate/cache these to make it faster for Map and MapCollection? maybe just getKey and getVal methods instead
/*
@Override
public <E> E extractColumnValue(final int index, final Class<E> classType) throws SQLException {
return super.extractColumnValue(index, classType);
}
*/
@Override
protected String[] getKeysFromResultSet() throws SQLException {
@ -197,18 +187,17 @@ public class CompilingRowToObjectMapper<K, T> extends RowToObjectMapper<K, T> {
try {
// todo: does not call getMapImplementation, I think that's fine
java.append("final ").append(tType).append("<String, Object> ret = new ").append(tType).append("<String, Object>();\n");
final ResultSetMetaData md = _resultSet.getMetaData();
final int columnLength = _columnCount + 1;
if (componentType != null && componentType != Object.class) { // we want a specific value type
int typeId = _tmf.getTypeId(componentType);
for (int x = 1; x < columnLength; ++x) {
java.append("ret.put(").append(escapeMapKeyString(md.getColumnName(x).toLowerCase())).append(", ");
java.append("ret.put(").append(escapeMapKeyString(keys[x]).toLowerCase()).append(", ");
extractColumnValueString(java, x, typeId);
java.append(");\n");
}
} else // we want a generic object type
for (int x = 1; x < columnLength; ++x)
java.append("ret.put(").append(escapeMapKeyString(md.getColumnName(x).toLowerCase())).append(", rs.getObject(").append(x).append("));\n");
java.append("ret.put(").append(escapeMapKeyString(keys[x].toLowerCase())).append(", rs.getObject(").append(x).append("));\n");
java.append("return ret;\n");
return;
} catch (Throwable e) {

View File

@ -189,7 +189,7 @@ public class ResultSetMapper implements RowMapperProvider {
try {
ResultSetMetaData md = rs.getMetaData();
badColumn = e.getCause() instanceof SQLExceptionColumnNum ? ((SQLExceptionColumnNum)e.getCause()).getColumnNum() : 1;
columnName = md.getColumnName(badColumn);
columnName = md.getColumnLabel(badColumn);
columnType = md.getColumnTypeName(badColumn);
returnedType = ((list != null && !list.isEmpty()) ? list.iterator().next() : rs.getObject(badColumn)).getClass().getName();
} catch (Throwable t) {
@ -269,7 +269,7 @@ public class ResultSetMapper implements RowMapperProvider {
try {
ResultSetMetaData md = rs.getMetaData();
badColumn = e.getCause() instanceof SQLExceptionColumnNum ? ((SQLExceptionColumnNum)e.getCause()).getColumnNum() : 1;
columnName = md.getColumnName(badColumn);
columnName = md.getColumnLabel(badColumn);
columnType = md.getColumnTypeName(badColumn);
returnedType = ((map != null && !map.isEmpty()) ? map.values().iterator().next() : rs.getObject(badColumn)).getClass().getName();
} catch (Throwable t) {
@ -355,7 +355,7 @@ public class ResultSetMapper implements RowMapperProvider {
try {
ResultSetMetaData md = rs.getMetaData();
badColumn = e.getCause() instanceof SQLExceptionColumnNum ? ((SQLExceptionColumnNum)e.getCause()).getColumnNum() : 1;
columnName = md.getColumnName(badColumn);
columnName = md.getColumnLabel(badColumn);
columnType = md.getColumnTypeName(badColumn);
returnedType = ((map != null && !map.isEmpty()) ? map.values().iterator().next() : rs.getObject(badColumn)).getClass().getName();
} catch (Throwable t) {

View File

@ -189,15 +189,15 @@ public class RowToObjectMapper<K, T> extends AbstractRowMapper<K, T> {
if(returnMap) // we want a map
try {
final Map<String, Object> ret = getMapImplementation();
final ResultSetMetaData md = _resultSet.getMetaData();
final String[] keys = getKeysFromResultSet();
final int columnLength = _columnCount+1;
if(componentType != null && componentType != Object.class){ // we want a specific value type
int typeId = _tmf.getTypeId(componentType);
for(int x = 1; x < columnLength; ++x)
ret.put(md.getColumnName(x).toLowerCase(), extractColumnValue(x, typeId));
ret.put(keys[x].toLowerCase(), extractColumnValue(x, typeId));
} else // we want a generic object type
for(int x = 1; x < columnLength; ++x)
ret.put(md.getColumnName(x).toLowerCase(), _resultSet.getObject(x));
ret.put(keys[x].toLowerCase(), _resultSet.getObject(x));
return _returnTypeClass.cast(ret);
} catch (Throwable e) {
throw new MapperException(e.getClass().getName() + " when trying to create a Map<String, "
@ -285,13 +285,13 @@ public class RowToObjectMapper<K, T> extends AbstractRowMapper<K, T> {
if (f instanceof Field) {
throw new MapperException("The declared Java type for field " + ((Field) f).getName()
+ ((Field) f).getType().toString()
+ " is incompatible with the SQL format of column " + i + " '" + md.getColumnName(i)
+ " is incompatible with the SQL format of column " + i + " '" + md.getColumnLabel(i)
+ "' (" + md.getColumnTypeName(i)
+ ") which returns objects of type " + _args[0].getClass().getName());
} else {
throw new MapperException("The declared Java type for method " + ((Method) f).getName()
+ ((Method) f).getParameterTypes()[0].toString()
+ " is incompatible with the SQL format of column " + i + " '" + md.getColumnName(i)
+ " is incompatible with the SQL format of column " + i + " '" + md.getColumnLabel(i)
+ "' (" + md.getColumnTypeName(i)
+ ") which returns objects of type " + _args[0].getClass().getName());
}