Add Collection<Map<String,T>> support to QueryMapper.toType and test for it

This commit is contained in:
Travis Burtrum 2018-05-09 23:42:44 -04:00
parent c151f3ba0e
commit d3c4f89316
4 changed files with 39 additions and 3 deletions

View File

@ -470,7 +470,22 @@ public class ResultSetMapper implements RowMapperProvider {
if (returnType.isArray()) { if (returnType.isArray()) {
return toArray(rs, returnType.getComponentType(), arrayMaxLength, cal); return toArray(rs, returnType.getComponentType(), arrayMaxLength, cal);
} else if (Collection.class.isAssignableFrom(returnType)) { } else if (Collection.class.isAssignableFrom(returnType)) {
return toCollection(rs, returnType, (Class) type.getActualTypeArguments()[0], arrayMaxLength, cal); final Type componentType = type.getActualTypeArguments()[0];
if(componentType instanceof ParameterizedType) {
final ParameterizedType parameterizedType = ((ParameterizedType) componentType);
final Type rawType = parameterizedType.getRawType();
if(rawType instanceof Class && Map.class.isAssignableFrom((Class)rawType)) {
final Type[] mapTypes = parameterizedType.getActualTypeArguments();
if (mapTypes.length == 2 && mapTypes[0].equals(String.class) && mapTypes[1] instanceof Class)
return toCollectionMap(rs, returnType, (Class) rawType, (Class) mapTypes[1]);
}
}
// if we didn't match above signature, try just a regular collection
if(componentType instanceof Class) {
return toCollection(rs, returnType, (Class) componentType, arrayMaxLength, cal);
}
// or give up...
throw new MapperException("unknown Collection type to map...");
} else if (Map.class.isAssignableFrom(returnType)) { } else if (Map.class.isAssignableFrom(returnType)) {
Type[] types = type.getActualTypeArguments(); Type[] types = type.getActualTypeArguments();
if (types[1] instanceof ParameterizedType) { // for collectionMaps if (types[1] instanceof ParameterizedType) { // for collectionMaps

View File

@ -156,6 +156,9 @@ public interface QmDao extends JdbcMapper {
@SQL(bobTomMap) @SQL(bobTomMap)
List<CaseInsensitiveHashMap<String, String>> getBobTomMapCaseInsensitive() throws SQLException; List<CaseInsensitiveHashMap<String, String>> getBobTomMapCaseInsensitive() throws SQLException;
@SQL(bobTomMap)
List<CaseInsensitiveHashMap<String, String>> getBobTomMapCaseInsensitiveType() throws SQLException;
@SQL(selectThreePeople) @SQL(selectThreePeople)
List<FieldPerson> getThreePeople(long personNo1, long personNo2, long personNo3) throws SQLException; List<FieldPerson> getThreePeople(long personNo1, long personNo2, long personNo3) throws SQLException;

View File

@ -233,11 +233,14 @@ public class QueryMapperQmDao implements QmDao {
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public List<CaseInsensitiveHashMap<String, String>> getBobTomMapCaseInsensitive() throws SQLException { public List<CaseInsensitiveHashMap<String, String>> getBobTomMapCaseInsensitive() throws SQLException {
// todo: ParameterizedTypeImpl cannot be cast to java.lang.Class
// return qm.toType(bobTomMap, new TypeReference<List<CaseInsensitiveHashMap<String, String>>>() {});
return (List<CaseInsensitiveHashMap<String, String>>)(Object)qm.toListMap(bobTomMap, CaseInsensitiveHashMap.class, String.class); return (List<CaseInsensitiveHashMap<String, String>>)(Object)qm.toListMap(bobTomMap, CaseInsensitiveHashMap.class, String.class);
} }
@Override
public List<CaseInsensitiveHashMap<String, String>> getBobTomMapCaseInsensitiveType() throws SQLException {
return qm.toType(bobTomMap, new TypeReference<List<CaseInsensitiveHashMap<String, String>>>() {});
}
@Override @Override
public List<FieldPerson> getThreePeople(final long personNo1, final long personNo2, final long personNo3) throws SQLException { public List<FieldPerson> getThreePeople(final long personNo1, final long personNo2, final long personNo3) throws SQLException {
return qm.toList(selectThreePeople, return qm.toList(selectThreePeople,

View File

@ -417,6 +417,21 @@ public class QueryMapperTest {
assertEquals("tom", map.get("TOM")); assertEquals("tom", map.get("TOM"));
} }
@Test
public void testCaseInsensitiveMapJdbcMapperType() throws Throwable {
final Map<String, String> map = qm.getBobTomMapCaseInsensitiveType().get(0);
assertEquals("bob", map.get("bob"));
assertEquals("bob", map.get("Bob"));
assertEquals("bob", map.get("BoB"));
assertEquals("bob", map.get("BOb"));
assertEquals("bob", map.get("BOB"));
assertEquals("tom", map.get("tom"));
assertEquals("tom", map.get("Tom"));
assertEquals("tom", map.get("ToM"));
assertEquals("tom", map.get("TOm"));
assertEquals("tom", map.get("TOM"));
}
@Test @Test
public void testList() throws SQLException { public void testList() throws SQLException {
final List<FieldPerson> fromDb = qm.getThreePeople(people[0].getPersonNo(), people[1].getPersonNo(), people[2].getPersonNo()); final List<FieldPerson> fromDb = qm.getThreePeople(people[0].getPersonNo(), people[1].getPersonNo(), people[2].getPersonNo());