From 39402c861332a46f6705df7cc0d553555a3b472e Mon Sep 17 00:00:00 2001 From: moparisthebest Date: Fri, 8 Mar 2019 10:56:10 -0500 Subject: [PATCH] Fix regression in QueryMapper.toResultSet() when ListQueryMapper was moved up, add tests for fix --- .../com/moparisthebest/jdbc/QueryMapper.java | 6 ++--- .../moparisthebest/jdbc/codegen/QmDao.java | 4 ++++ .../jdbc/codegen/QueryMapperQmDao.java | 6 +++++ .../jdbc/codegen/QueryMapperTypeQmDao.java | 6 +++++ .../moparisthebest/jdbc/QueryMapperTest.java | 22 +++++++++++++++---- 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/querymapper/src/main/java/com/moparisthebest/jdbc/QueryMapper.java b/querymapper/src/main/java/com/moparisthebest/jdbc/QueryMapper.java index 340f96b..e1c2424 100644 --- a/querymapper/src/main/java/com/moparisthebest/jdbc/QueryMapper.java +++ b/querymapper/src/main/java/com/moparisthebest/jdbc/QueryMapper.java @@ -288,7 +288,7 @@ public class QueryMapper implements JdbcMapper { public Long insertGetGeneratedKey(final String sql, final Object... bindObjects) throws SQLException { PreparedStatement ps = null; try { - ps = getSingleColumnPreparedStatementFactory().prepareStatement(conn, sql); + ps = getPreparedStatement(sql, getSingleColumnPreparedStatementFactory(), bindObjects); return this.insertGetGeneratedKey(ps, bindObjects); } finally { tryClose(ps); @@ -302,7 +302,7 @@ public class QueryMapper implements JdbcMapper { public T insertGetGeneratedKeyType(final String sql, final PreparedStatementFactory psf, final TypeReference typeReference, final Object... bindObjects) throws SQLException { PreparedStatement ps = null; try { - ps = psf.prepareStatement(conn, sql); + ps = getPreparedStatement(sql, psf, bindObjects); return this.insertGetGeneratedKeyType(ps, typeReference, bindObjects); } finally { tryClose(ps); @@ -359,7 +359,7 @@ public class QueryMapper implements JdbcMapper { PreparedStatement ps = null; ResultSet rs = null; try { - ps = psf.prepareStatement(conn, sql); + ps = getPreparedStatement(sql, psf, bindObjects); rs = this.toResultSet(ps, bindObjects); error = false; return new StatementClosingResultSet(rs, ps); 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 ea61ae5..8f52ec4 100644 --- a/test/src/main/java/com/moparisthebest/jdbc/codegen/QmDao.java +++ b/test/src/main/java/com/moparisthebest/jdbc/codegen/QmDao.java @@ -3,6 +3,7 @@ package com.moparisthebest.jdbc.codegen; import com.moparisthebest.jdbc.dto.*; import com.moparisthebest.jdbc.util.*; +import java.sql.ResultSet; import java.sql.SQLException; import java.util.Collection; import java.util.List; @@ -283,4 +284,7 @@ public interface QmDao extends JdbcMapper { @SQL("INSERT {sql:sql}") void insertRandomSqlIterable(Iterable 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 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 653761e..899deeb 100644 --- a/test/src/main/java/com/moparisthebest/jdbc/codegen/QueryMapperQmDao.java +++ b/test/src/main/java/com/moparisthebest/jdbc/codegen/QueryMapperQmDao.java @@ -5,6 +5,7 @@ import com.moparisthebest.jdbc.dto.*; import com.moparisthebest.jdbc.util.*; import java.sql.Connection; +import java.sql.ResultSet; import java.sql.SQLException; import java.util.*; @@ -447,4 +448,9 @@ public class QueryMapperQmDao implements QmDao { public void insertRandomSqlIterable(final Iterable sql) throws SQLException { qm.executeUpdate("INSERT " + sql, sql); } + + @Override + public ResultSet getFieldPeopleResultSet(final List 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)); + } } 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 e9045d2..05d303b 100644 --- a/test/src/main/java/com/moparisthebest/jdbc/codegen/QueryMapperTypeQmDao.java +++ b/test/src/main/java/com/moparisthebest/jdbc/codegen/QueryMapperTypeQmDao.java @@ -6,6 +6,7 @@ import com.moparisthebest.jdbc.dto.*; import com.moparisthebest.jdbc.util.*; import java.sql.Connection; +import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import java.util.Map; @@ -377,4 +378,9 @@ public class QueryMapperTypeQmDao extends QueryMapperQmDao { public List 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>() {}, personNo1, sql, firstName); } + + @Override + public ResultSet getFieldPeopleResultSet(final List 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() {}, qm.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 f72a092..218ac80 100644 --- a/test/src/test/java/com/moparisthebest/jdbc/QueryMapperTest.java +++ b/test/src/test/java/com/moparisthebest/jdbc/QueryMapperTest.java @@ -8,7 +8,6 @@ import com.moparisthebest.jdbc.codegen.QueryMapperTypeQmDao; import com.moparisthebest.jdbc.dto.*; import com.moparisthebest.jdbc.util.Bindable; import com.moparisthebest.jdbc.util.ResultSetIterable; -import com.moparisthebest.jdbc.util.SqlBuilder; import org.junit.*; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; @@ -16,6 +15,7 @@ import org.junit.runners.Parameterized; import javax.sql.DataSource; import java.sql.Connection; import java.sql.DriverManager; +import java.sql.ResultSet; import java.sql.SQLException; import java.util.*; //IFJAVA8_START @@ -26,9 +26,7 @@ import java.util.stream.Stream; import static com.moparisthebest.jdbc.OptimalInList.*; import static com.moparisthebest.jdbc.TryClose.tryClose; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; +import static org.junit.Assert.*; /** * 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(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 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); + } + } }