Add annother inlist test where an array is re-used, fix hsqldb string type

This commit is contained in:
Travis Burtrum 2018-05-20 12:51:37 -04:00
parent 6e56622ce0
commit 4874968824
8 changed files with 47 additions and 5 deletions

View File

@ -129,7 +129,7 @@ public interface JdbcMapper extends Closeable {
public enum DatabaseType { public enum DatabaseType {
DEFAULT(null, null), DEFAULT(null, null),
STANDARD("numeric", "text"), STANDARD("numeric", "text"),
UNNEST("numeric", "text"), UNNEST("NUMERIC", "VARCHAR"),
ORACLE("ARRAY_NUM_TYPE", "ARRAY_STR_TYPE"); ORACLE("ARRAY_NUM_TYPE", "ARRAY_STR_TYPE");
public final String arrayNumberTypeName, arrayStringTypeName; public final String arrayNumberTypeName, arrayStringTypeName;

View File

@ -1,5 +1,7 @@
package com.moparisthebest.jdbc; package com.moparisthebest.jdbc;
import com.moparisthebest.jdbc.codegen.JdbcMapper;
import java.sql.Array; import java.sql.Array;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
@ -24,7 +26,7 @@ public class ArrayInList implements InList {
} }
public ArrayInList() { public ArrayInList() {
this("numeric", "text"); this(JdbcMapper.DatabaseType.STANDARD.arrayNumberTypeName, JdbcMapper.DatabaseType.STANDARD.arrayStringTypeName);
} }
protected String columnAppend(final String columnName) { protected String columnAppend(final String columnName) {

View File

@ -1,5 +1,7 @@
package com.moparisthebest.jdbc; package com.moparisthebest.jdbc;
import com.moparisthebest.jdbc.codegen.JdbcMapper;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.sql.Array; import java.sql.Array;
@ -39,7 +41,7 @@ public class OracleArrayInList extends ArrayInList {
} }
public OracleArrayInList() { public OracleArrayInList() {
this("ARRAY_NUM_TYPE", "ARRAY_STR_TYPE"); this(JdbcMapper.DatabaseType.ORACLE.arrayNumberTypeName, JdbcMapper.DatabaseType.ORACLE.arrayStringTypeName);
} }
protected String columnAppend(final String columnName) { protected String columnAppend(final String columnName) {

View File

@ -1,5 +1,7 @@
package com.moparisthebest.jdbc; package com.moparisthebest.jdbc;
import com.moparisthebest.jdbc.codegen.JdbcMapper;
/** /**
* HSQLDB requires array in lists to be implemented this way: * HSQLDB requires array in lists to be implemented this way:
* https://stackoverflow.com/questions/35939489/createarrayof-string-in-hsqldb-jdbc-returns-abstractmethoderror/35964424#35964424 * https://stackoverflow.com/questions/35939489/createarrayof-string-in-hsqldb-jdbc-returns-abstractmethoderror/35964424#35964424
@ -17,7 +19,7 @@ public class UnNestArrayInList extends ArrayInList {
} }
public UnNestArrayInList() { public UnNestArrayInList() {
super(); this(JdbcMapper.DatabaseType.UNNEST.arrayNumberTypeName, JdbcMapper.DatabaseType.UNNEST.arrayStringTypeName);
} }
protected String columnAppend(final String columnName) { protected String columnAppend(final String columnName) {

View File

@ -249,4 +249,7 @@ public interface QmDao extends JdbcMapper {
@SQL("SELECT person_no, first_name, last_name, birth_date from person WHERE {person_no IN personNos} ORDER BY person_no") @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; List<FieldPerson> getFieldPeople(List<Long> personNos) throws SQLException;
@SQL("SELECT person_no, first_name, last_name, birth_date from person WHERE {person_no IN personNos} AND ({first_name IN names} OR {last_name IN names}) ORDER BY person_no")
List<FieldPerson> getFieldPeopleByName(List<Long> personNos, List<String> names) throws SQLException;
} }

View File

@ -14,6 +14,7 @@ import java.util.stream.Stream;
import java.time.*; import java.time.*;
//IFJAVA8_END //IFJAVA8_END
import static com.moparisthebest.jdbc.ListQueryMapper.inListReplace;
import static com.moparisthebest.jdbc.TryClose.tryClose; import static com.moparisthebest.jdbc.TryClose.tryClose;
public class QueryMapperQmDao implements QmDao { public class QueryMapperQmDao implements QmDao {
@ -456,6 +457,16 @@ public class QueryMapperQmDao implements QmDao {
@Override @Override
public List<FieldPerson> getFieldPeople(final List<Long> personNos) throws SQLException { 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)); return lqm.toList("SELECT * from person WHERE " + inListReplace + " ORDER BY person_no", FieldPerson.class, lqm.inList("person_no", personNos));
}
@Override
public List<FieldPerson> getFieldPeopleByName(final List<Long> personNos, final List<String> names) throws SQLException {
return lqm.toList("SELECT * from person WHERE " + inListReplace + " AND (" + inListReplace + " OR " + inListReplace + ") ORDER BY person_no",
FieldPerson.class,
lqm.inList("person_no", personNos),
lqm.inList("first_name", names),
lqm.inList("last_name", names)
);
} }
} }

View File

@ -15,6 +15,8 @@ import java.util.Map;
//IFJAVA8_START //IFJAVA8_START
import java.util.stream.Stream; import java.util.stream.Stream;
import java.time.*; import java.time.*;
import static com.moparisthebest.jdbc.ListQueryMapper.inListReplace;
//IFJAVA8_END //IFJAVA8_END
public class QueryMapperTypeQmDao extends QueryMapperQmDao { public class QueryMapperTypeQmDao extends QueryMapperQmDao {
@ -336,4 +338,14 @@ public class QueryMapperTypeQmDao extends QueryMapperQmDao {
public List<FieldPerson> getFieldPeople(final List<Long> personNos) throws SQLException { 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)); return lqm.toType("SELECT * from person WHERE " + ListQueryMapper.inListReplace + " ORDER BY person_no", new TypeReference<List<FieldPerson>>() {}, lqm.inList("person_no", personNos));
} }
@Override
public List<FieldPerson> getFieldPeopleByName(final List<Long> personNos, final List<String> names) throws SQLException {
return lqm.toType("SELECT * from person WHERE " + inListReplace + " AND (" + inListReplace + " OR " + inListReplace + ") ORDER BY person_no",
new TypeReference<List<FieldPerson>>() {},
lqm.inList("person_no", personNos),
lqm.inList("first_name", names),
lqm.inList("last_name", names)
);
}
} }

View File

@ -533,6 +533,16 @@ public class QueryMapperTest {
assertArrayEquals(people, fromDb.toArray()); assertArrayEquals(people, fromDb.toArray());
} }
@Test
public void testListQueryMapperListMultiple() throws SQLException {
if(!supportsInList(qm))
return;
final List<FieldPerson> fromDb = qm.getFieldPeopleByName(
Arrays.asList(people[0].getPersonNo(), people[1].getPersonNo(), people[2].getPersonNo()),
Arrays.asList(people[0].getFirstName(), people[1].getFirstName(), people[2].getFirstName()));
assertArrayEquals(people, fromDb.toArray());
}
@Test @Test
public void testResultSetIterable() throws SQLException { public void testResultSetIterable() throws SQLException {
final ResultSetIterable<FieldPerson> rsi = qm.getThreePeopleResultSetIterable(people[0].getPersonNo(), people[1].getPersonNo(), people[2].getPersonNo()); final ResultSetIterable<FieldPerson> rsi = qm.getThreePeopleResultSetIterable(people[0].getPersonNo(), people[1].getPersonNo(), people[2].getPersonNo());