Browse Source

Consistent Object result declarations for ResultQuerySpec

Closes gh-31403
pull/31407/head
Juergen Hoeller 1 year ago
parent
commit
c0d98fcc7a
  1. 10
      spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/DefaultJdbcClient.java
  2. 9
      spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/JdbcClient.java
  3. 22
      spring-jdbc/src/test/java/org/springframework/jdbc/core/simple/JdbcClientIndexedParameterTests.java
  4. 216
      spring-jdbc/src/test/java/org/springframework/jdbc/core/simple/JdbcClientQueryTests.java

10
spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/DefaultJdbcClient.java

@ -285,10 +285,9 @@ final class DefaultJdbcClient implements JdbcClient { @@ -285,10 +285,9 @@ final class DefaultJdbcClient implements JdbcClient {
return classicOps.queryForMap(sql, indexedParams.toArray());
}
@SuppressWarnings("unchecked")
@Override
public <T> List<T> singleColumn() {
return (List<T>) classicOps.queryForList(sql, Object.class, indexedParams.toArray());
public List<Object> singleColumn() {
return classicOps.queryForList(sql, Object.class, indexedParams.toArray());
}
}
@ -310,10 +309,9 @@ final class DefaultJdbcClient implements JdbcClient { @@ -310,10 +309,9 @@ final class DefaultJdbcClient implements JdbcClient {
return namedParamOps.queryForMap(sql, namedParamSource);
}
@SuppressWarnings("unchecked")
@Override
public <T> List<T> singleColumn() {
return (List<T>) namedParamOps.queryForList(sql, namedParamSource, Object.class);
public List<Object> singleColumn() {
return namedParamOps.queryForList(sql, namedParamSource, Object.class);
}
}

9
spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/JdbcClient.java

@ -323,17 +323,16 @@ public interface JdbcClient { @@ -323,17 +323,16 @@ public interface JdbcClient {
* Retrieve a single column result,
* retaining the order from the original database result.
* @return a (potentially empty) list of rows, with each
* row represented as a column value of the given type
* row represented as its single column value
*/
<T> List<T> singleColumn();
List<Object> singleColumn();
/**
* Retrieve a single value result.
* @return the single row represented as its single
* column value of the given type
* @return the single row represented as its single column value
* @see DataAccessUtils#requiredSingleResult(Collection)
*/
default <T> T singleValue() {
default Object singleValue() {
return DataAccessUtils.requiredSingleResult(singleColumn());
}
}

22
spring-jdbc/src/test/java/org/springframework/jdbc/core/simple/JdbcClientIndexedParameterTests.java

@ -95,7 +95,7 @@ public class JdbcClientIndexedParameterTests { @@ -95,7 +95,7 @@ public class JdbcClientIndexedParameterTests {
@Test
public void testQueryWithResultSetExtractor() throws SQLException {
public void queryWithResultSetExtractor() throws SQLException {
given(resultSet.next()).willReturn(true);
given(resultSet.getInt("id")).willReturn(1);
given(resultSet.getString("forename")).willReturn("rod");
@ -122,7 +122,7 @@ public class JdbcClientIndexedParameterTests { @@ -122,7 +122,7 @@ public class JdbcClientIndexedParameterTests {
}
@Test
public void testQueryWithResultSetExtractorNoParameters() throws SQLException {
public void queryWithResultSetExtractorNoParameters() throws SQLException {
given(resultSet.next()).willReturn(true);
given(resultSet.getInt("id")).willReturn(1);
given(resultSet.getString("forename")).willReturn("rod");
@ -145,7 +145,7 @@ public class JdbcClientIndexedParameterTests { @@ -145,7 +145,7 @@ public class JdbcClientIndexedParameterTests {
}
@Test
public void testQueryWithRowCallbackHandler() throws SQLException {
public void queryWithRowCallbackHandler() throws SQLException {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt("id")).willReturn(1);
given(resultSet.getString("forename")).willReturn("rod");
@ -172,7 +172,7 @@ public class JdbcClientIndexedParameterTests { @@ -172,7 +172,7 @@ public class JdbcClientIndexedParameterTests {
}
@Test
public void testQueryWithRowCallbackHandlerNoParameters() throws SQLException {
public void queryWithRowCallbackHandlerNoParameters() throws SQLException {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt("id")).willReturn(1);
given(resultSet.getString("forename")).willReturn("rod");
@ -195,7 +195,7 @@ public class JdbcClientIndexedParameterTests { @@ -195,7 +195,7 @@ public class JdbcClientIndexedParameterTests {
}
@Test
public void testQueryWithRowMapper() throws SQLException {
public void queryWithRowMapper() throws SQLException {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt("id")).willReturn(1);
given(resultSet.getString("forename")).willReturn("rod");
@ -223,7 +223,7 @@ public class JdbcClientIndexedParameterTests { @@ -223,7 +223,7 @@ public class JdbcClientIndexedParameterTests {
}
@Test
public void testQueryWithRowMapperNoParameters() throws SQLException {
public void queryWithRowMapperNoParameters() throws SQLException {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt("id")).willReturn(1);
given(resultSet.getString("forename")).willReturn("rod");
@ -247,7 +247,7 @@ public class JdbcClientIndexedParameterTests { @@ -247,7 +247,7 @@ public class JdbcClientIndexedParameterTests {
}
@Test
public void testQueryForObjectWithRowMapper() throws SQLException {
public void queryForObjectWithRowMapper() throws SQLException {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt("id")).willReturn(1);
given(resultSet.getString("forename")).willReturn("rod");
@ -274,7 +274,7 @@ public class JdbcClientIndexedParameterTests { @@ -274,7 +274,7 @@ public class JdbcClientIndexedParameterTests {
}
@Test
public void testQueryForStreamWithRowMapper() throws SQLException {
public void queryForStreamWithRowMapper() throws SQLException {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt("id")).willReturn(1);
given(resultSet.getString("forename")).willReturn("rod");
@ -307,7 +307,7 @@ public class JdbcClientIndexedParameterTests { @@ -307,7 +307,7 @@ public class JdbcClientIndexedParameterTests {
}
@Test
public void testUpdate() throws SQLException {
public void update() throws SQLException {
given(preparedStatement.executeUpdate()).willReturn(1);
params.add(1);
@ -323,7 +323,7 @@ public class JdbcClientIndexedParameterTests { @@ -323,7 +323,7 @@ public class JdbcClientIndexedParameterTests {
}
@Test
public void testUpdateWithTypedParameters() throws SQLException {
public void updateWithTypedParameters() throws SQLException {
given(preparedStatement.executeUpdate()).willReturn(1);
params.add(new SqlParameterValue(Types.DECIMAL, 1));
@ -339,7 +339,7 @@ public class JdbcClientIndexedParameterTests { @@ -339,7 +339,7 @@ public class JdbcClientIndexedParameterTests {
}
@Test
public void testUpdateAndGeneratedKeys() throws SQLException {
public void updateAndGeneratedKeys() throws SQLException {
given(resultSetMetaData.getColumnCount()).willReturn(1);
given(resultSetMetaData.getColumnLabel(1)).willReturn("1");
given(resultSet.getMetaData()).willReturn(resultSetMetaData);

216
spring-jdbc/src/test/java/org/springframework/jdbc/core/simple/JdbcClientQueryTests.java

@ -64,6 +64,7 @@ public class JdbcClientQueryTests { @@ -64,6 +64,7 @@ public class JdbcClientQueryTests {
given(dataSource.getConnection()).willReturn(connection);
given(connection.prepareStatement(anyString())).willReturn(preparedStatement);
given(preparedStatement.executeQuery()).willReturn(resultSet);
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
given(resultSetMetaData.getColumnCount()).willReturn(1);
given(resultSetMetaData.getColumnLabel(1)).willReturn("age");
}
@ -72,8 +73,7 @@ public class JdbcClientQueryTests { @@ -72,8 +73,7 @@ public class JdbcClientQueryTests {
// Indexed parameters
@Test
public void testQueryForListWithIndexedParam() throws Exception {
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
public void queryForListWithIndexedParam() throws Exception {
given(resultSet.next()).willReturn(true, true, false);
given(resultSet.getObject(1)).willReturn(11, 12);
@ -92,7 +92,7 @@ public class JdbcClientQueryTests { @@ -92,7 +92,7 @@ public class JdbcClientQueryTests {
}
@Test
public void testQueryForListWithIndexedParamAndEmptyResult() throws Exception {
public void queryForListWithIndexedParamAndEmptyResult() throws Exception {
given(resultSet.next()).willReturn(false);
List<Map<String, Object>> li = client.sql("SELECT AGE FROM CUSTMR WHERE ID < ?")
@ -107,8 +107,7 @@ public class JdbcClientQueryTests { @@ -107,8 +107,7 @@ public class JdbcClientQueryTests {
}
@Test
public void testQueryForListWithIndexedParamAndSingleRow() throws Exception {
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
public void queryForListWithIndexedParamAndSingleRow() throws Exception {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getObject(1)).willReturn(11);
@ -125,12 +124,28 @@ public class JdbcClientQueryTests { @@ -125,12 +124,28 @@ public class JdbcClientQueryTests {
}
@Test
public void testQueryForListWithIndexedParamAndSingleColumn() throws Exception {
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
public void queryForMapWithIndexedParamAndSingleRow() throws Exception {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getObject(1)).willReturn(11);
List<Integer> li = client.sql("SELECT AGE FROM CUSTMR WHERE ID < ?")
Map<String, Object> map = client.sql("SELECT AGE FROM CUSTMR WHERE ID < ?")
.param(1, 3)
.query().singleRow();
assertThat(map.get("age")).as("Row is Integer").isEqualTo(11);
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID < ?");
verify(preparedStatement).setObject(1, 3);
verify(resultSet).close();
verify(preparedStatement).close();
verify(connection).close();
}
@Test
public void queryForListWithIndexedParamAndSingleColumn() throws Exception {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getObject(1)).willReturn(11);
List<Object> li = client.sql("SELECT AGE FROM CUSTMR WHERE ID < ?")
.param(1, 3)
.query().singleColumn();
@ -144,17 +159,16 @@ public class JdbcClientQueryTests { @@ -144,17 +159,16 @@ public class JdbcClientQueryTests {
}
@Test
public void testQueryForMapWithIndexedParamAndSingleRow() throws Exception {
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
public void queryForIntegerWithIndexedParamAndSingleValue() throws Exception {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getObject(1)).willReturn(11);
given(resultSet.getObject(1)).willReturn(22);
Map<String, Object> map = client.sql("SELECT AGE FROM CUSTMR WHERE ID < ?")
Integer value = (Integer) client.sql("SELECT AGE FROM CUSTMR WHERE ID = ?")
.param(1, 3)
.query().singleRow();
.query().singleValue();
assertThat(map.get("age")).as("Row is Integer").isEqualTo(11);
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID < ?");
assertThat(value).isEqualTo(22);
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?");
verify(preparedStatement).setObject(1, 3);
verify(resultSet).close();
verify(preparedStatement).close();
@ -162,13 +176,14 @@ public class JdbcClientQueryTests { @@ -162,13 +176,14 @@ public class JdbcClientQueryTests {
}
@Test
public void testQueryForIntegerWithIndexedParamAndRowMapper() throws Exception {
public void queryForIntegerWithIndexedParamAndRowMapper() throws Exception {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt(1)).willReturn(22);
Integer value = client.sql("SELECT AGE FROM CUSTMR WHERE ID = ?")
.param(1, 3)
.query((rs, rowNum) -> rs.getInt(1)).single();
.query((rs, rowNum) -> rs.getInt(1))
.single();
assertThat(value).isEqualTo(22);
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?");
@ -179,13 +194,14 @@ public class JdbcClientQueryTests { @@ -179,13 +194,14 @@ public class JdbcClientQueryTests {
}
@Test
public void testQueryForOptionalWithIndexedParamAndRowMapper() throws Exception {
public void queryForOptionalWithIndexedParamAndRowMapper() throws Exception {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt(1)).willReturn(22);
Optional<Integer> value = client.sql("SELECT AGE FROM CUSTMR WHERE ID = ?")
.param(1, 3)
.query((rs, rowNum) -> rs.getInt(1)).optional();
.query((rs, rowNum) -> rs.getInt(1))
.optional();
assertThat(value.get()).isEqualTo(22);
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?");
@ -196,14 +212,13 @@ public class JdbcClientQueryTests { @@ -196,14 +212,13 @@ public class JdbcClientQueryTests {
}
@Test
public void testQueryForIntegerWithIndexedParam() throws Exception {
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
public void queryForIntegerWithIndexedParam() throws Exception {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getObject(1)).willReturn(22);
given(resultSet.getInt(1)).willReturn(22);
Integer value = client.sql("SELECT AGE FROM CUSTMR WHERE ID = ?")
.param(1, 3)
.query().singleValue();
.query(Integer.class).single();
assertThat(value).isEqualTo(22);
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?");
@ -214,14 +229,13 @@ public class JdbcClientQueryTests { @@ -214,14 +229,13 @@ public class JdbcClientQueryTests {
}
@Test
public void testQueryForIntWithIndexedParam() throws Exception {
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
public void queryForIntWithIndexedParam() throws Exception {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getObject(1)).willReturn(22);
given(resultSet.getInt(1)).willReturn(22);
int i = client.sql("SELECT AGE FROM CUSTMR WHERE ID = ?")
.param(1, 3)
.query().singleValue();
.query(Integer.class).single();
assertThat(i).as("Return of an int").isEqualTo(22);
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?");
@ -232,7 +246,7 @@ public class JdbcClientQueryTests { @@ -232,7 +246,7 @@ public class JdbcClientQueryTests {
}
@Test
public void testQueryForObjectWithIndexedParamAndList() {
public void queryForObjectWithIndexedParamAndList() {
assertThatIllegalArgumentException().isThrownBy(() ->
client.sql("SELECT AGE FROM CUSTMR WHERE ID IN (?)").param(Arrays.asList(3, 4)).query().singleValue());
}
@ -241,8 +255,7 @@ public class JdbcClientQueryTests { @@ -241,8 +255,7 @@ public class JdbcClientQueryTests {
// Named parameters
@Test
public void testQueryForListWithNamedParam() throws Exception {
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
public void queryForListWithNamedParam() throws Exception {
given(resultSet.next()).willReturn(true, true, false);
given(resultSet.getObject(1)).willReturn(11, 12);
@ -262,7 +275,7 @@ public class JdbcClientQueryTests { @@ -262,7 +275,7 @@ public class JdbcClientQueryTests {
}
@Test
public void testQueryForListWithNamedParamAndEmptyResult() throws Exception {
public void queryForListWithNamedParamAndEmptyResult() throws Exception {
given(resultSet.next()).willReturn(false);
List<Map<String, Object>> li = client.sql("SELECT AGE FROM CUSTMR WHERE ID < :id")
@ -278,8 +291,7 @@ public class JdbcClientQueryTests { @@ -278,8 +291,7 @@ public class JdbcClientQueryTests {
}
@Test
public void testQueryForListWithNamedParamAndSingleRow() throws Exception {
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
public void queryForListWithNamedParamAndSingleRow() throws Exception {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getObject(1)).willReturn(11);
@ -297,17 +309,15 @@ public class JdbcClientQueryTests { @@ -297,17 +309,15 @@ public class JdbcClientQueryTests {
}
@Test
public void testQueryForListWithNamedParamAndSingleColumn() throws Exception {
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
public void queryForMapWithNamedParamAndSingleRow() throws Exception {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getObject(1)).willReturn(11);
List<Integer> li = client.sql("SELECT AGE FROM CUSTMR WHERE ID < :id")
Map<String, Object> map = client.sql("SELECT AGE FROM CUSTMR WHERE ID < :id")
.param("id", 3)
.query().singleColumn();
.query().singleRow();
assertThat(li.size()).as("All rows returned").isEqualTo(1);
assertThat(li.get(0)).as("First row is Integer").isEqualTo(11);
assertThat(map.get("age")).as("Row is Integer").isEqualTo(11);
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID < ?");
verify(preparedStatement).setObject(1, 3);
verify(resultSet).close();
@ -316,16 +326,16 @@ public class JdbcClientQueryTests { @@ -316,16 +326,16 @@ public class JdbcClientQueryTests {
}
@Test
public void testQueryForMapWithNamedParamAndSingleRow() throws Exception {
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
public void queryForListWithNamedParamAndSingleColumn() throws Exception {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getObject(1)).willReturn(11);
Map<String, Object> map = client.sql("SELECT AGE FROM CUSTMR WHERE ID < :id")
List<Object> li = client.sql("SELECT AGE FROM CUSTMR WHERE ID < :id")
.param("id", 3)
.query().singleRow();
.query().singleColumn();
assertThat(map.get("age")).as("Row is Integer").isEqualTo(11);
assertThat(li.size()).as("All rows returned").isEqualTo(1);
assertThat(li.get(0)).as("First row is Integer").isEqualTo(11);
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID < ?");
verify(preparedStatement).setObject(1, 3);
verify(resultSet).close();
@ -334,14 +344,13 @@ public class JdbcClientQueryTests { @@ -334,14 +344,13 @@ public class JdbcClientQueryTests {
}
@Test
public void testQueryForIntegerWithNamedParamAndRowMapper() throws Exception {
public void queryForIntegerWithNamedParamAndSingleValue() throws Exception {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt(1)).willReturn(22);
given(resultSet.getObject(1)).willReturn(22);
Integer value = client.sql("SELECT AGE FROM CUSTMR WHERE ID = :id")
Integer value = (Integer) client.sql("SELECT AGE FROM CUSTMR WHERE ID = :id")
.param("id", 3)
.query((rs, rowNum) -> rs.getInt(1))
.single();
.query().singleValue();
assertThat(value).isEqualTo(22);
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?");
@ -352,14 +361,13 @@ public class JdbcClientQueryTests { @@ -352,14 +361,13 @@ public class JdbcClientQueryTests {
}
@Test
public void testQueryForIntegerWithNamedParamAndMappedSimpleValue() throws Exception {
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
public void queryForIntegerWithNamedParamAndRowMapper() throws Exception {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt(1)).willReturn(22);
Integer value = client.sql("SELECT AGE FROM CUSTMR WHERE ID = :id")
.param("id", 3)
.query(Integer.class)
.query((rs, rowNum) -> rs.getInt(1))
.single();
assertThat(value).isEqualTo(22);
@ -371,18 +379,16 @@ public class JdbcClientQueryTests { @@ -371,18 +379,16 @@ public class JdbcClientQueryTests {
}
@Test
public void testQueryForMappedRecordWithNamedParam() throws Exception {
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
given(resultSet.findColumn("age")).willReturn(1);
public void queryForOptionalWithNamedParamAndRowMapper() throws Exception {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt(1)).willReturn(22);
AgeRecord value = client.sql("SELECT AGE FROM CUSTMR WHERE ID = :id")
Optional<Integer> value = client.sql("SELECT AGE FROM CUSTMR WHERE ID = :id")
.param("id", 3)
.query(AgeRecord.class)
.single();
.query((rs, rowNum) -> rs.getInt(1))
.optional();
assertThat(value.age()).isEqualTo(22);
assertThat(value.get()).isEqualTo(22);
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?");
verify(preparedStatement).setObject(1, 3);
verify(resultSet).close();
@ -391,33 +397,13 @@ public class JdbcClientQueryTests { @@ -391,33 +397,13 @@ public class JdbcClientQueryTests {
}
@Test
public void testQueryForMappedFieldHolderWithNamedParam() throws Exception {
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
public void queryForIntegerWithNamedParam() throws Exception {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt(1)).willReturn(22);
AgeFieldHolder value = client.sql("SELECT AGE FROM CUSTMR WHERE ID = :id")
.param("id", 3)
.query(AgeFieldHolder.class)
.single();
assertThat(value.age).isEqualTo(22);
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?");
verify(preparedStatement).setObject(1, 3);
verify(resultSet).close();
verify(preparedStatement).close();
verify(connection).close();
}
@Test
public void testQueryForIntegerWithNamedParam() throws Exception {
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
given(resultSet.next()).willReturn(true, false);
given(resultSet.getObject(1)).willReturn(22);
Integer value = client.sql("SELECT AGE FROM CUSTMR WHERE ID = :id")
.param("id", 3)
.query().singleValue();
.query(Integer.class).single();
assertThat(value).isEqualTo(22);
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?");
@ -428,14 +414,13 @@ public class JdbcClientQueryTests { @@ -428,14 +414,13 @@ public class JdbcClientQueryTests {
}
@Test
public void testQueryForIntegerWithNamedParamAndList() throws Exception {
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
public void queryForIntegerWithNamedParamAndList() throws Exception {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getObject(1)).willReturn(22);
given(resultSet.getInt(1)).willReturn(22);
Integer value = client.sql("SELECT AGE FROM CUSTMR WHERE ID IN (:ids)")
.param("ids", Arrays.asList(3, 4))
.query().singleValue();
.query(Integer.class).single();
assertThat(value).isEqualTo(22);
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID IN (?, ?)");
@ -447,17 +432,16 @@ public class JdbcClientQueryTests { @@ -447,17 +432,16 @@ public class JdbcClientQueryTests {
}
@Test
public void testQueryForIntegerWithNamedParamAndListOfExpressionLists() throws Exception {
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
public void queryForIntegerWithNamedParamAndListOfExpressionLists() throws Exception {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getObject(1)).willReturn(22);
given(resultSet.getInt(1)).willReturn(22);
List<Object[]> l1 = new ArrayList<>();
l1.add(new Object[] {3, "Rod"});
l1.add(new Object[] {4, "Juergen"});
Integer value = client.sql("SELECT AGE FROM CUSTMR WHERE (ID, NAME) IN (:multiExpressionList)")
.param("multiExpressionList", l1)
.query().singleValue();
.query(Integer.class).single();
assertThat(value).isEqualTo(22);
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE (ID, NAME) IN ((?, ?), (?, ?))");
@ -471,14 +455,13 @@ public class JdbcClientQueryTests { @@ -471,14 +455,13 @@ public class JdbcClientQueryTests {
}
@Test
public void testQueryForIntWithNamedParam() throws Exception {
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
public void queryForIntWithNamedParam() throws Exception {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getObject(1)).willReturn(22);
given(resultSet.getInt(1)).willReturn(22);
int i = client.sql("SELECT AGE FROM CUSTMR WHERE ID = :id")
.param("id", 3)
.query().singleValue();
.query(Integer.class).single();
assertThat(i).as("Return of an int").isEqualTo(22);
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?");
@ -489,8 +472,7 @@ public class JdbcClientQueryTests { @@ -489,8 +472,7 @@ public class JdbcClientQueryTests {
}
@Test
public void testQueryForLongWithParamBean() throws Exception {
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
public void queryForLongWithParamBean() throws Exception {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getLong(1)).willReturn(87L);
@ -507,8 +489,7 @@ public class JdbcClientQueryTests { @@ -507,8 +489,7 @@ public class JdbcClientQueryTests {
}
@Test
public void testQueryForLongWithParamBeanWithCollection() throws Exception {
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
public void queryForLongWithParamBeanWithCollection() throws Exception {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getLong(1)).willReturn(87L);
@ -526,8 +507,7 @@ public class JdbcClientQueryTests { @@ -526,8 +507,7 @@ public class JdbcClientQueryTests {
}
@Test
public void testQueryForLongWithParamRecord() throws Exception {
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
public void queryForLongWithParamRecord() throws Exception {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getLong(1)).willReturn(87L);
@ -544,8 +524,7 @@ public class JdbcClientQueryTests { @@ -544,8 +524,7 @@ public class JdbcClientQueryTests {
}
@Test
public void testQueryForLongWithParamFieldHolder() throws Exception {
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
public void queryForLongWithParamFieldHolder() throws Exception {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getLong(1)).willReturn(87L);
@ -561,6 +540,41 @@ public class JdbcClientQueryTests { @@ -561,6 +540,41 @@ public class JdbcClientQueryTests {
verify(connection).close();
}
@Test
public void queryForMappedRecordWithNamedParam() throws Exception {
given(resultSet.findColumn("age")).willReturn(1);
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt(1)).willReturn(22);
AgeRecord value = client.sql("SELECT AGE FROM CUSTMR WHERE ID = :id")
.param("id", 3)
.query(AgeRecord.class).single();
assertThat(value.age()).isEqualTo(22);
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?");
verify(preparedStatement).setObject(1, 3);
verify(resultSet).close();
verify(preparedStatement).close();
verify(connection).close();
}
@Test
public void queryForMappedFieldHolderWithNamedParam() throws Exception {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt(1)).willReturn(22);
AgeFieldHolder value = client.sql("SELECT AGE FROM CUSTMR WHERE ID = :id")
.param("id", 3)
.query(AgeFieldHolder.class).single();
assertThat(value.age).isEqualTo(22);
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?");
verify(preparedStatement).setObject(1, 3);
verify(resultSet).close();
verify(preparedStatement).close();
verify(connection).close();
}
static class ParameterBean {

Loading…
Cancel
Save