Fix regression in QueryMapper.toResultSet() when ListQueryMapper was moved up, add tests for fix

This commit is contained in:
Travis Burtrum 2019-03-08 10:56:10 -05:00
parent 94d3ba89e0
commit 39402c8613
5 changed files with 37 additions and 7 deletions

View File

@ -288,7 +288,7 @@ public class QueryMapper implements JdbcMapper {
public Long insertGetGeneratedKey(final String sql, final Object... bindObjects) throws SQLException { public Long insertGetGeneratedKey(final String sql, final Object... bindObjects) throws SQLException {
PreparedStatement ps = null; PreparedStatement ps = null;
try { try {
ps = getSingleColumnPreparedStatementFactory().prepareStatement(conn, sql); ps = getPreparedStatement(sql, getSingleColumnPreparedStatementFactory(), bindObjects);
return this.insertGetGeneratedKey(ps, bindObjects); return this.insertGetGeneratedKey(ps, bindObjects);
} finally { } finally {
tryClose(ps); tryClose(ps);
@ -302,7 +302,7 @@ public class QueryMapper implements JdbcMapper {
public <T> T insertGetGeneratedKeyType(final String sql, final PreparedStatementFactory psf, final TypeReference<T> typeReference, final Object... bindObjects) throws SQLException { public <T> T insertGetGeneratedKeyType(final String sql, final PreparedStatementFactory psf, final TypeReference<T> typeReference, final Object... bindObjects) throws SQLException {
PreparedStatement ps = null; PreparedStatement ps = null;
try { try {
ps = psf.prepareStatement(conn, sql); ps = getPreparedStatement(sql, psf, bindObjects);
return this.insertGetGeneratedKeyType(ps, typeReference, bindObjects); return this.insertGetGeneratedKeyType(ps, typeReference, bindObjects);
} finally { } finally {
tryClose(ps); tryClose(ps);
@ -359,7 +359,7 @@ public class QueryMapper implements JdbcMapper {
PreparedStatement ps = null; PreparedStatement ps = null;
ResultSet rs = null; ResultSet rs = null;
try { try {
ps = psf.prepareStatement(conn, sql); ps = getPreparedStatement(sql, psf, bindObjects);
rs = this.toResultSet(ps, bindObjects); rs = this.toResultSet(ps, bindObjects);
error = false; error = false;
return new StatementClosingResultSet(rs, ps); return new StatementClosingResultSet(rs, ps);

View File

@ -3,6 +3,7 @@ package com.moparisthebest.jdbc.codegen;
import com.moparisthebest.jdbc.dto.*; import com.moparisthebest.jdbc.dto.*;
import com.moparisthebest.jdbc.util.*; import com.moparisthebest.jdbc.util.*;
import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -283,4 +284,7 @@ public interface QmDao extends JdbcMapper {
@SQL("INSERT {sql:sql}") @SQL("INSERT {sql:sql}")
void insertRandomSqlIterable(Iterable<Long> sql) throws SQLException; void insertRandomSqlIterable(Iterable<Long> sql) throws SQLException;
@SQL("SELECT person_no, first_name, last_name, birth_date from person WHERE {person_no IN personNos} ORDER BY person_no")
ResultSet getFieldPeopleResultSet(List<Long> personNos) throws SQLException;
} }

View File

@ -5,6 +5,7 @@ import com.moparisthebest.jdbc.dto.*;
import com.moparisthebest.jdbc.util.*; import com.moparisthebest.jdbc.util.*;
import java.sql.Connection; import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.*; import java.util.*;
@ -447,4 +448,9 @@ public class QueryMapperQmDao implements QmDao {
public void insertRandomSqlIterable(final Iterable<Long> sql) throws SQLException { public void insertRandomSqlIterable(final Iterable<Long> sql) throws SQLException {
qm.executeUpdate("INSERT " + sql, sql); qm.executeUpdate("INSERT " + sql, sql);
} }
@Override
public ResultSet getFieldPeopleResultSet(final List<Long> personNos) throws SQLException {
return qm.toResultSet("SELECT person_no, first_name, last_name, birth_date from person WHERE " + inListReplace + " ORDER BY person_no", qm.inList("person_no", personNos));
}
} }

View File

@ -6,6 +6,7 @@ import com.moparisthebest.jdbc.dto.*;
import com.moparisthebest.jdbc.util.*; import com.moparisthebest.jdbc.util.*;
import java.sql.Connection; import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -377,4 +378,9 @@ public class QueryMapperTypeQmDao extends QueryMapperQmDao {
public List<Long> selectRandomSqlBuilder(final long personNo1, final Bindable sql, final String firstName) throws SQLException { public List<Long> selectRandomSqlBuilder(final long personNo1, final Bindable sql, final String firstName) throws SQLException {
return qm.toType("SELECT person_no FROM person WHERE person_no = ? " + sql + " OR first_name = ?", new TypeReference<List<Long>>() {}, personNo1, sql, firstName); return qm.toType("SELECT person_no FROM person WHERE person_no = ? " + sql + " OR first_name = ?", new TypeReference<List<Long>>() {}, personNo1, sql, firstName);
} }
@Override
public ResultSet getFieldPeopleResultSet(final List<Long> personNos) throws SQLException {
return qm.toType("SELECT person_no, first_name, last_name, birth_date from person WHERE " + inListReplace + " ORDER BY person_no", new TypeReference<ResultSet>() {}, qm.inList("person_no", personNos));
}
} }

View File

@ -8,7 +8,6 @@ import com.moparisthebest.jdbc.codegen.QueryMapperTypeQmDao;
import com.moparisthebest.jdbc.dto.*; import com.moparisthebest.jdbc.dto.*;
import com.moparisthebest.jdbc.util.Bindable; import com.moparisthebest.jdbc.util.Bindable;
import com.moparisthebest.jdbc.util.ResultSetIterable; import com.moparisthebest.jdbc.util.ResultSetIterable;
import com.moparisthebest.jdbc.util.SqlBuilder;
import org.junit.*; import org.junit.*;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.Parameterized; import org.junit.runners.Parameterized;
@ -16,6 +15,7 @@ import org.junit.runners.Parameterized;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.*; import java.util.*;
//IFJAVA8_START //IFJAVA8_START
@ -26,9 +26,7 @@ import java.util.stream.Stream;
import static com.moparisthebest.jdbc.OptimalInList.*; import static com.moparisthebest.jdbc.OptimalInList.*;
import static com.moparisthebest.jdbc.TryClose.tryClose; import static com.moparisthebest.jdbc.TryClose.tryClose;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
/** /**
* Created by mopar on 6/10/14. * Created by mopar on 6/10/14.
@ -778,4 +776,20 @@ public class QueryMapperTest {
assertEquals(Collections.singletonList(3L), qm.selectRandomSqlBuilder(3L, Bindable.empty, "NoNameMatch")); assertEquals(Collections.singletonList(3L), qm.selectRandomSqlBuilder(3L, Bindable.empty, "NoNameMatch"));
assertEquals(arr, qm.selectRandomSqlBuilder(2L, qm.sqlBuilder().append("OR person_no = ? OR ", 1L).appendInList("person_no", Collections.singletonList(3L)), "NoNameMatch")); assertEquals(arr, qm.selectRandomSqlBuilder(2L, qm.sqlBuilder().append("OR person_no = ? OR ", 1L).appendInList("person_no", Collections.singletonList(3L)), "NoNameMatch"));
} }
@Test
public void testSelectResultSet() throws Throwable {
final List<Long> arr = Arrays.asList(1L, 2L, 3L);
ResultSet rs = null;
try {
rs = qm.getFieldPeopleResultSet(arr);
// looking for 3 rows in order here
for (final long personNo : arr) {
assertTrue(rs.next());
assertEquals(rs.getLong("person_no"), personNo);
}
} finally {
tryClose(rs);
}
}
} }