@Override @Nullable public <T> T query(PreparedStatementCreator psc, ResultSetExtractor<T> rse) throws DataAccessException { return query(psc, null, rse); }
@Override @Nullable public <T> T query(String sql, @Nullable PreparedStatementSetter pss, ResultSetExtractor<T> rse) throws DataAccessException { return query(new SimplePreparedStatementCreator(sql), pss, rse); }
@Override @Nullable public <T> T queryForObject(String sql, RowMapper<T> rowMapper) throws DataAccessException { List<T> results = query(sql, rowMapper); return DataAccessUtils.nullableSingleResult(results); }
@Override @Nullable public <T> T query(String sql, Object[] args, int[] argTypes, ResultSetExtractor<T> rse) throws DataAccessException { return query(sql, newArgTypePreparedStatementSetter(args, argTypes), rse); }
@Override @Nullable public <T> T queryForObject(String sql, @Nullable Object[] args, RowMapper<T> rowMapper) throws DataAccessException { List<T> results = query(sql, args, new RowMapperResultSetExtractor<>(rowMapper, 1)); return DataAccessUtils.nullableSingleResult(results); }
@Override @Nullable public <T> T queryForObject(String sql, RowMapper<T> rowMapper, @Nullable Object... args) throws DataAccessException { List<T> results = query(sql, args, new RowMapperResultSetExtractor<>(rowMapper, 1)); return DataAccessUtils.nullableSingleResult(results); }
@Override @Nullable public <T> T queryForObject(String sql, Object[] args, int[] argTypes, RowMapper<T> rowMapper) throws DataAccessException { List<T> results = query(sql, args, argTypes, new RowMapperResultSetExtractor<>(rowMapper, 1)); return DataAccessUtils.nullableSingleResult(results); }
@Test public void preparedStatementCreatorWithRowMapper() throws SQLException { result = template.query(con -> preparedStatement, testRowMapper); verify(preparedStatement).close(); }
@Test public void staticQueryWithRowMapper() throws SQLException { result = template.query("some SQL", testRowMapper); verify(statement).close(); }
@Test public void preparedStatementSetterWithRowMapper() throws SQLException { result = template.query("some SQL", ps -> ps.setString(1, "test"), testRowMapper); verify(preparedStatement).setString(1, "test"); verify(preparedStatement).close(); }
@Test public void testStringsWithEmptyPreparedStatementArgs() throws Exception { doTestStrings(null, null, null, null, (template, sql, rch) -> template.query(sql, (Object[]) null, rch)); }
@Test public void testStringsWithEmptyPreparedStatementSetter() throws Exception { doTestStrings(null, null, null, null, (template, sql, rch) -> template.query(sql, (PreparedStatementSetter) null, rch)); }
@Test public void testStringsWithPreparedStatementArgs() throws Exception { final Integer argument = 99; doTestStrings(null, null, null, argument, (template, sql, rch) -> template.query(sql, new Object[] {argument}, rch)); }
@Test public void queryWithArgsAndTypesAndRowMapper() throws SQLException { result = template.query("some SQL", new Object[] { "test1", "test2" }, new int[] { Types.VARCHAR, Types.VARCHAR }, testRowMapper); verify(preparedStatement).setString(1, "test1"); verify(preparedStatement).setString(2, "test2"); verify(preparedStatement).close(); }
@Test public void testCloseConnectionOnRequest() throws Exception { String sql = "SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3"; given(this.resultSet.next()).willReturn(false); given(this.connection.createStatement()).willReturn(this.preparedStatement); RowCountCallbackHandler rcch = new RowCountCallbackHandler(); this.template.query(sql, rcch); verify(this.resultSet).close(); verify(this.preparedStatement).close(); verify(this.connection).close(); }
@Test public void testQueryWithSpaceInColumnNameAndLocalDateTime() throws Exception { Mock mock = new Mock(MockType.THREE); List<SpacePerson> result = mock.getJdbcTemplate().query( "select last_name as \"Last Name\", age, birth_date, balance from people", new BeanPropertyRowMapper<>(SpacePerson.class)); assertEquals(1, result.size()); verifyPerson(result.get(0)); mock.verifyClosed(); }
@Test public void testStaticQueryWithRowMapper() throws Exception { Mock mock = new Mock(); List<Person> result = mock.getJdbcTemplate().query( "select name, age, birth_date, balance from people", new BeanPropertyRowMapper<>(Person.class)); assertEquals(1, result.size()); verifyPerson(result.get(0)); mock.verifyClosed(); }
@Test public void testQueryWithSpaceInColumnNameAndLocalDate() throws Exception { Mock mock = new Mock(MockType.THREE); List<DatePerson> result = mock.getJdbcTemplate().query( "select last_name as \"Last Name\", age, birth_date, balance from people", new BeanPropertyRowMapper<>(DatePerson.class)); assertEquals(1, result.size()); verifyPerson(result.get(0)); mock.verifyClosed(); }
@Test public void testMappingWithNoUnpopulatedFieldsFound() throws Exception { Mock mock = new Mock(); List<ConcretePerson> result = mock.getJdbcTemplate().query( "select name, age, birth_date, balance from people", new BeanPropertyRowMapper<>(ConcretePerson.class, true)); assertEquals(1, result.size()); verifyPerson(result.get(0)); mock.verifyClosed(); }
@Test public void testMappingNullValue() throws Exception { BeanPropertyRowMapper<Person> mapper = new BeanPropertyRowMapper<>(Person.class); Mock mock = new Mock(MockType.TWO); thrown.expect(TypeMismatchException.class); mock.getJdbcTemplate().query( "select name, null as age, birth_date, balance from people", mapper); }