From 67ae0d257c77940036a1d3b3084a6cbacd9e3360 Mon Sep 17 00:00:00 2001 From: moparisthebest Date: Fri, 26 May 2017 11:43:33 -0400 Subject: [PATCH] Change getColumnName to getColumnLabel, should work the same on oracle and fix mysql... --- .../jdbc/AbstractRowMapper.java | 21 +++++++++++++++++-- .../jdbc/CompilingRowToObjectMapper.java | 15 ++----------- .../moparisthebest/jdbc/ResultSetMapper.java | 6 +++--- .../jdbc/RowToObjectMapper.java | 10 ++++----- 4 files changed, 29 insertions(+), 23 deletions(-) diff --git a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/AbstractRowMapper.java b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/AbstractRowMapper.java index 3a46bb5..68bbe42 100644 --- a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/AbstractRowMapper.java +++ b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/AbstractRowMapper.java @@ -50,6 +50,8 @@ public abstract class AbstractRowMapper implements RowMapper { 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 implements RowMapper { this(resultSet, returnTypeClass, cal, null); } + protected AbstractRowMapper(String[] keys, Class returnTypeClass, Calendar cal, Class 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 implements RowMapper { */ 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; } } diff --git a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/CompilingRowToObjectMapper.java b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/CompilingRowToObjectMapper.java index 8e5b663..402c5e8 100644 --- a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/CompilingRowToObjectMapper.java +++ b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/CompilingRowToObjectMapper.java @@ -30,8 +30,6 @@ public class CompilingRowToObjectMapper extends RowToObjectMapper { protected final Compiler compiler; protected final ResultSetToObject resultSetToObject; - protected String[] keys = null; // for caching if we must generate class - public CompilingRowToObjectMapper(final Compiler compiler, final Map> cache, ResultSet resultSet, Class returnTypeClass, Calendar cal, Class mapValType, Class mapKeyType) { this(compiler, cache, resultSet, returnTypeClass, cal, mapValType, mapKeyType, false); } @@ -47,7 +45,6 @@ public class CompilingRowToObjectMapper extends RowToObjectMapper { 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 extends RowToObjectMapper { 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 extractColumnValue(final int index, final Class classType) throws SQLException { - return super.extractColumnValue(index, classType); - } - */ @Override protected String[] getKeysFromResultSet() throws SQLException { @@ -197,18 +187,17 @@ public class CompilingRowToObjectMapper extends RowToObjectMapper { try { // todo: does not call getMapImplementation, I think that's fine java.append("final ").append(tType).append(" ret = new ").append(tType).append("();\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) { diff --git a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java index afe3099..e0ce3f0 100644 --- a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java +++ b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java @@ -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) { diff --git a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/RowToObjectMapper.java b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/RowToObjectMapper.java index 251daf0..abf233c 100644 --- a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/RowToObjectMapper.java +++ b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/RowToObjectMapper.java @@ -189,15 +189,15 @@ public class RowToObjectMapper extends AbstractRowMapper { if(returnMap) // we want a map try { final Map 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 extends AbstractRowMapper { 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()); }