mirror of
https://github.com/moparisthebest/JdbcMapper
synced 2024-12-21 06:48:51 -05:00
Fix regression in QueryMapper.toResultSet() when ListQueryMapper was moved up, add tests for fix
This commit is contained in:
parent
94d3ba89e0
commit
39402c8613
@ -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> T insertGetGeneratedKeyType(final String sql, final PreparedStatementFactory psf, final TypeReference<T> 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);
|
||||
|
@ -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<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;
|
||||
}
|
||||
|
@ -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<Long> sql) throws SQLException {
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
@ -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<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);
|
||||
}
|
||||
|
||||
@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));
|
||||
}
|
||||
}
|
||||
|
@ -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<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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user