Test ArrayInList support in JdbcMapper and QueryMapper where supported

This commit is contained in:
Travis Burtrum 2018-05-15 08:10:52 -04:00
parent 12f13121ee
commit 2db37683d6
4 changed files with 63 additions and 10 deletions

View File

@ -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<FieldPerson> getFieldPeople(List<Long> personNos) throws SQLException;
}

View File

@ -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<Class<?>> noArrayInListSupport;
static {
Collection<Class<?>> no = new ArrayList<Class<?>>();
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<FieldPerson> getFieldPeople(final List<Long> personNos) throws SQLException {
return lqm.toList("SELECT * from person WHERE " + ListQueryMapper.inListReplace + " ORDER BY person_no", FieldPerson.class, lqm.inList("person_no", personNos));
}
}

View File

@ -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<FieldPerson> getFieldPeople(final List<Long> personNos) throws SQLException {
return lqm.toType("SELECT * from person WHERE " + ListQueryMapper.inListReplace + " ORDER BY person_no", new TypeReference<List<FieldPerson>>() {}, lqm.inList("person_no", personNos));
}
}

View File

@ -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<FieldPerson> 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<FieldPerson> fromDb = qm.getFieldPeople(Arrays.asList(people[0].getPersonNo(), people[1].getPersonNo(), people[2].getPersonNo()));
assertArrayEquals(people, fromDb.toArray());
lqm.close();
}
@Test