From d3c4f89316a10788c1d2da7a93f5ffb2ac03c560 Mon Sep 17 00:00:00 2001 From: moparisthebest Date: Wed, 9 May 2018 23:42:44 -0400 Subject: [PATCH] Add Collection> support to QueryMapper.toType and test for it --- .../moparisthebest/jdbc/ResultSetMapper.java | 17 ++++++++++++++++- .../com/moparisthebest/jdbc/codegen/QmDao.java | 3 +++ .../jdbc/codegen/QueryMapperQmDao.java | 7 +++++-- .../moparisthebest/jdbc/QueryMapperTest.java | 15 +++++++++++++++ 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/querymapper/src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java b/querymapper/src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java index e0de913..f0ea218 100644 --- a/querymapper/src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java +++ b/querymapper/src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java @@ -470,7 +470,22 @@ public class ResultSetMapper implements RowMapperProvider { if (returnType.isArray()) { return toArray(rs, returnType.getComponentType(), arrayMaxLength, cal); } 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)) { Type[] types = type.getActualTypeArguments(); if (types[1] instanceof ParameterizedType) { // for collectionMaps diff --git a/test/src/main/java/com/moparisthebest/jdbc/codegen/QmDao.java b/test/src/main/java/com/moparisthebest/jdbc/codegen/QmDao.java index 5d8fc8b..7347349 100644 --- a/test/src/main/java/com/moparisthebest/jdbc/codegen/QmDao.java +++ b/test/src/main/java/com/moparisthebest/jdbc/codegen/QmDao.java @@ -156,6 +156,9 @@ public interface QmDao extends JdbcMapper { @SQL(bobTomMap) List> getBobTomMapCaseInsensitive() throws SQLException; + @SQL(bobTomMap) + List> getBobTomMapCaseInsensitiveType() throws SQLException; + @SQL(selectThreePeople) List getThreePeople(long personNo1, long personNo2, long personNo3) throws SQLException; diff --git a/test/src/main/java/com/moparisthebest/jdbc/codegen/QueryMapperQmDao.java b/test/src/main/java/com/moparisthebest/jdbc/codegen/QueryMapperQmDao.java index b92d8ea..afa84ca 100644 --- a/test/src/main/java/com/moparisthebest/jdbc/codegen/QueryMapperQmDao.java +++ b/test/src/main/java/com/moparisthebest/jdbc/codegen/QueryMapperQmDao.java @@ -233,11 +233,14 @@ public class QueryMapperQmDao implements QmDao { @Override @SuppressWarnings("unchecked") public List> getBobTomMapCaseInsensitive() throws SQLException { - // todo: ParameterizedTypeImpl cannot be cast to java.lang.Class - // return qm.toType(bobTomMap, new TypeReference>>() {}); return (List>)(Object)qm.toListMap(bobTomMap, CaseInsensitiveHashMap.class, String.class); } + @Override + public List> getBobTomMapCaseInsensitiveType() throws SQLException { + return qm.toType(bobTomMap, new TypeReference>>() {}); + } + @Override public List getThreePeople(final long personNo1, final long personNo2, final long personNo3) throws SQLException { return qm.toList(selectThreePeople, diff --git a/test/src/test/java/com/moparisthebest/jdbc/QueryMapperTest.java b/test/src/test/java/com/moparisthebest/jdbc/QueryMapperTest.java index 8cfca6d..b562672 100644 --- a/test/src/test/java/com/moparisthebest/jdbc/QueryMapperTest.java +++ b/test/src/test/java/com/moparisthebest/jdbc/QueryMapperTest.java @@ -417,6 +417,21 @@ public class QueryMapperTest { assertEquals("tom", map.get("TOM")); } + @Test + public void testCaseInsensitiveMapJdbcMapperType() throws Throwable { + final Map 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 public void testList() throws SQLException { final List fromDb = qm.getThreePeople(people[0].getPersonNo(), people[1].getPersonNo(), people[2].getPersonNo());