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 ff22caf..a122fac 100644 --- a/test/src/main/java/com/moparisthebest/jdbc/codegen/QmDao.java +++ b/test/src/main/java/com/moparisthebest/jdbc/codegen/QmDao.java @@ -246,4 +246,7 @@ public interface QmDao extends JdbcMapper { ZoneOffset getZoneOffsetString(long valNo) throws SQLException; //IFJAVA8_END + + @SQL("SELECT person_no, first_name, last_name, birth_date from person WHERE {person_no IN personNos} ORDER BY person_no") + List getFieldPeople(List personNos) 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 2fa7745..0697cb3 100644 --- a/test/src/main/java/com/moparisthebest/jdbc/codegen/QueryMapperQmDao.java +++ b/test/src/main/java/com/moparisthebest/jdbc/codegen/QueryMapperQmDao.java @@ -1,15 +1,13 @@ package com.moparisthebest.jdbc.codegen; -import com.moparisthebest.jdbc.QueryMapper; -import com.moparisthebest.jdbc.ResultSetMapper; +import com.moparisthebest.jdbc.*; import com.moparisthebest.jdbc.dto.*; import com.moparisthebest.jdbc.util.CaseInsensitiveHashMap; import com.moparisthebest.jdbc.util.ResultSetIterable; import java.sql.Connection; import java.sql.SQLException; -import java.util.List; -import java.util.Map; +import java.util.*; //IFJAVA8_START import java.util.stream.Stream; @@ -55,10 +53,48 @@ public class QueryMapperQmDao implements QmDao { public static final String selectNumVal = "SELECT num_val FROM val WHERE val_no = ?"; public static final String selectStrVal = "SELECT str_val FROM val WHERE val_no = ?"; + private static final Collection> noArrayInListSupport; + + static { + Collection> no = new ArrayList>(); + for(final String connectionClassName : new String[]{ + "org.hsqldb.jdbc.JDBCConnection", "org.apache.derby.impl.jdbc.EmbedConnection" + // h2 doesn't support this with java6 either... + /*IFJAVA6_START + , "org.h2.jdbc.JdbcConnection" + IFJAVA6_END*/ + }) + try { + no.add(Class.forName(connectionClassName)); + } catch(Exception e) { + // ignore + } + noArrayInListSupport = Collections.unmodifiableCollection(no); + } + + public static boolean supportsArrayInList(final Connection conn) { + for(final Class connectionClass : noArrayInListSupport) { + try { + if(conn.isWrapperFor(connectionClass)) + return false; + } catch (SQLException e) { + // ignore... how could this happen? + } + } + // assume Connections DO support this unless we KNOW otherwise + return true; + } + + public static InList getBestInList(final Connection conn) { + return supportsArrayInList(conn) ? ArrayInList.instance() : BindInList.instance(); + } + protected final QueryMapper qm; + protected final ListQueryMapper lqm; public QueryMapperQmDao(final Connection conn, final ResultSetMapper rsm) { this.qm = new QueryMapper(conn, rsm); + this.lqm = new ListQueryMapper(qm, getBestInList(qm.getConnection())); } @Override @@ -70,8 +106,13 @@ public class QueryMapperQmDao implements QmDao { return qm; } + public ListQueryMapper getLqm() { + return lqm; + } + @Override public void close() { + tryClose(lqm); tryClose(qm); } @@ -378,4 +419,9 @@ public class QueryMapperQmDao implements QmDao { } //IFJAVA8_END + + @Override + public List getFieldPeople(final List personNos) throws SQLException { + return lqm.toList("SELECT * from person WHERE " + ListQueryMapper.inListReplace + " ORDER BY person_no", FieldPerson.class, lqm.inList("person_no", personNos)); + } } diff --git a/test/src/main/java/com/moparisthebest/jdbc/codegen/QueryMapperTypeQmDao.java b/test/src/main/java/com/moparisthebest/jdbc/codegen/QueryMapperTypeQmDao.java index 424064e..0255b92 100644 --- a/test/src/main/java/com/moparisthebest/jdbc/codegen/QueryMapperTypeQmDao.java +++ b/test/src/main/java/com/moparisthebest/jdbc/codegen/QueryMapperTypeQmDao.java @@ -1,5 +1,6 @@ package com.moparisthebest.jdbc.codegen; +import com.moparisthebest.jdbc.ListQueryMapper; import com.moparisthebest.jdbc.ResultSetMapper; import com.moparisthebest.jdbc.TypeReference; import com.moparisthebest.jdbc.dto.*; @@ -330,4 +331,9 @@ public class QueryMapperTypeQmDao extends QueryMapperQmDao { } //IFJAVA8_END + + @Override + public List getFieldPeople(final List personNos) throws SQLException { + return lqm.toType("SELECT * from person WHERE " + ListQueryMapper.inListReplace + " ORDER BY person_no", new TypeReference>() {}, lqm.inList("person_no", personNos)); + } } diff --git a/test/src/test/java/com/moparisthebest/jdbc/QueryMapperTest.java b/test/src/test/java/com/moparisthebest/jdbc/QueryMapperTest.java index 5804a7d..fa33f28 100644 --- a/test/src/test/java/com/moparisthebest/jdbc/QueryMapperTest.java +++ b/test/src/test/java/com/moparisthebest/jdbc/QueryMapperTest.java @@ -21,6 +21,7 @@ import java.util.stream.Stream; //IFJAVA8_END import static com.moparisthebest.jdbc.TryClose.tryClose; +import static com.moparisthebest.jdbc.codegen.QueryMapperQmDao.supportsArrayInList; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @@ -493,13 +494,10 @@ public class QueryMapperTest { @Test public void testListQueryMapperList() throws SQLException { - if(!(qm instanceof QueryMapperQmDao)) - return; // todo: port this when JdbcMapper supports in-lists on generic SQL backends - final ListQueryMapper lqm = new ListQueryMapper(((QueryMapperQmDao)qm).getQm()); - final List fromDb = lqm.toList("SELECT * from person WHERE " + ListQueryMapper.inListReplace + " ORDER BY person_no", - FieldPerson.class, lqm.inList("person_no", Arrays.asList(people[0].getPersonNo(), people[1].getPersonNo(), people[2].getPersonNo()))); + if(!(qm instanceof QueryMapperQmDao) && !supportsArrayInList(conn)) + return; + final List fromDb = qm.getFieldPeople(Arrays.asList(people[0].getPersonNo(), people[1].getPersonNo(), people[2].getPersonNo())); assertArrayEquals(people, fromDb.toArray()); - lqm.close(); } @Test