From c02c111826945ac4cc6781cde96be097a9f3b47d Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 17 Feb 2010 22:19:49 +0000 Subject: [PATCH] added vararg variants of query methods to JdbcTemplate (as known from SimpleJdbcTemplate; SPR-6858) git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@2998 50f2f4bb-b051-0410-bef5-90022cba6387 --- .../jdbc/core/JdbcOperations.java | 102 +++++++++++++++++- .../jdbc/core/JdbcTemplate.java | 27 ++++- 2 files changed, 127 insertions(+), 2 deletions(-) diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/JdbcOperations.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/JdbcOperations.java index 27642b4e200..62280503e50 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/JdbcOperations.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/JdbcOperations.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -380,6 +380,21 @@ public interface JdbcOperations { */ T query(String sql, Object[] args, ResultSetExtractor rse) throws DataAccessException; + /** + * Query given SQL to create a prepared statement from SQL and a list + * of arguments to bind to the query, reading the ResultSet with a + * ResultSetExtractor. + * @param sql SQL query to execute + * @param rse object that will extract results + * @param args arguments to bind to the query + * (leaving it to the PreparedStatement to guess the corresponding SQL type); + * may also contain {@link SqlParameterValue} objects which indicate not + * only the argument value but also the SQL type and optionally the scale + * @return an arbitrary result object, as returned by the ResultSetExtractor + * @throws DataAccessException if the query fails + */ + T query(String sql, ResultSetExtractor rse, Object... args) throws DataAccessException; + /** * Query using a prepared statement, reading the ResultSet on a per-row * basis with a RowCallbackHandler. @@ -437,6 +452,20 @@ public interface JdbcOperations { */ void query(String sql, Object[] args, RowCallbackHandler rch) throws DataAccessException; + /** + * Query given SQL to create a prepared statement from SQL and a list of + * arguments to bind to the query, reading the ResultSet on a per-row basis + * with a RowCallbackHandler. + * @param sql SQL query to execute + * @param rch object that will extract results, one row at a time + * @param args arguments to bind to the query + * (leaving it to the PreparedStatement to guess the corresponding SQL type); + * may also contain {@link SqlParameterValue} objects which indicate not + * only the argument value but also the SQL type and optionally the scale + * @throws DataAccessException if the query fails + */ + void query(String sql, RowCallbackHandler rch, Object... args) throws DataAccessException; + /** * Query using a prepared statement, mapping each row to a Java object * via a RowMapper. @@ -497,6 +526,21 @@ public interface JdbcOperations { */ List query(String sql, Object[] args, RowMapper rowMapper) throws DataAccessException; + /** + * Query given SQL to create a prepared statement from SQL and a list + * of arguments to bind to the query, mapping each row to a Java object + * via a RowMapper. + * @param sql SQL query to execute + * @param rowMapper object that will map one object per row + * @param args arguments to bind to the query + * (leaving it to the PreparedStatement to guess the corresponding SQL type); + * may also contain {@link SqlParameterValue} objects which indicate not + * only the argument value but also the SQL type and optionally the scale + * @return the result List, containing mapped objects + * @throws DataAccessException if the query fails + */ + List query(String sql, RowMapper rowMapper, Object... args) throws DataAccessException; + /** * Query given SQL to create a prepared statement from SQL and a list * of arguments to bind to the query, mapping a single result row to a @@ -533,6 +577,24 @@ public interface JdbcOperations { T queryForObject(String sql, Object[] args, RowMapper rowMapper) throws DataAccessException; + /** + * Query given SQL to create a prepared statement from SQL and a list + * of arguments to bind to the query, mapping a single result row to a + * Java object via a RowMapper. + * @param sql SQL query to execute + * @param rowMapper object that will map one object per row + * @param args arguments to bind to the query + * (leaving it to the PreparedStatement to guess the corresponding SQL type); + * may also contain {@link SqlParameterValue} objects which indicate not + * only the argument value but also the SQL type and optionally the scale + * @return the single mapped object + * @throws IncorrectResultSizeDataAccessException if the query does not + * return exactly one row + * @throws DataAccessException if the query fails + */ + T queryForObject(String sql, RowMapper rowMapper, Object... args) + throws DataAccessException; + /** * Query given SQL to create a prepared statement from SQL and a * list of arguments to bind to the query, expecting a result object. @@ -572,6 +634,25 @@ public interface JdbcOperations { */ T queryForObject(String sql, Object[] args, Class requiredType) throws DataAccessException; + /** + * Query given SQL to create a prepared statement from SQL and a + * list of arguments to bind to the query, expecting a result object. + *

The query is expected to be a single row/single column query; the returned + * result will be directly mapped to the corresponding object type. + * @param sql SQL query to execute + * @param requiredType the type that the result object is expected to match + * @param args arguments to bind to the query + * (leaving it to the PreparedStatement to guess the corresponding SQL type); + * may also contain {@link SqlParameterValue} objects which indicate not + * only the argument value but also the SQL type and optionally the scale + * @return the result object of the required type, or null in case of SQL NULL + * @throws IncorrectResultSizeDataAccessException if the query does not return + * exactly one row, or does not return exactly one column in that row + * @throws DataAccessException if the query fails + * @see #queryForObject(String, Class) + */ + T queryForObject(String sql, Class requiredType, Object... args) throws DataAccessException; + /** * Query given SQL to create a prepared statement from SQL and a * list of arguments to bind to the query, expecting a result Map. @@ -725,6 +806,25 @@ public interface JdbcOperations { */ List queryForList(String sql, Object[] args, Class elementType) throws DataAccessException; + /** + * Query given SQL to create a prepared statement from SQL and a + * list of arguments to bind to the query, expecting a result list. + *

The results will be mapped to a List (one entry for each row) of + * result objects, each of them matching the specified element type. + * @param sql SQL query to execute + * @param elementType the required type of element in the result list + * (for example, Integer.class) + * @param args arguments to bind to the query + * (leaving it to the PreparedStatement to guess the corresponding SQL type); + * may also contain {@link SqlParameterValue} objects which indicate not + * only the argument value but also the SQL type and optionally the scale + * @return a List of objects that match the specified element type + * @throws DataAccessException if the query fails + * @see #queryForList(String, Class) + * @see SingleColumnRowMapper + */ + List queryForList(String sql, Class elementType, Object... args) throws DataAccessException; + /** * Query given SQL to create a prepared statement from SQL and a * list of arguments to bind to the query, expecting a result list. diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java index df14692e357..5f786605f0f 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -673,6 +673,10 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations { return query(sql, newArgPreparedStatementSetter(args), rse); } + public T query(String sql, ResultSetExtractor rse, Object... args) throws DataAccessException { + return query(sql, newArgPreparedStatementSetter(args), rse); + } + public void query(PreparedStatementCreator psc, RowCallbackHandler rch) throws DataAccessException { query(psc, new RowCallbackHandlerResultSetExtractor(rch)); } @@ -689,6 +693,10 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations { query(sql, newArgPreparedStatementSetter(args), rch); } + public void query(String sql, RowCallbackHandler rch, Object... args) throws DataAccessException { + query(sql, newArgPreparedStatementSetter(args), rch); + } + public List query(PreparedStatementCreator psc, RowMapper rowMapper) throws DataAccessException { return query(psc, new RowMapperResultSetExtractor(rowMapper)); } @@ -705,6 +713,10 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations { return query(sql, args, new RowMapperResultSetExtractor(rowMapper)); } + public List query(String sql, RowMapper rowMapper, Object... args) throws DataAccessException { + return query(sql, args, new RowMapperResultSetExtractor(rowMapper)); + } + public T queryForObject(String sql, Object[] args, int[] argTypes, RowMapper rowMapper) throws DataAccessException { @@ -717,6 +729,11 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations { return DataAccessUtils.requiredSingleResult(results); } + public T queryForObject(String sql, RowMapper rowMapper, Object... args) throws DataAccessException { + List results = query(sql, args, new RowMapperResultSetExtractor(rowMapper, 1)); + return DataAccessUtils.requiredSingleResult(results); + } + public T queryForObject(String sql, Object[] args, int[] argTypes, Class requiredType) throws DataAccessException { @@ -727,6 +744,10 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations { return queryForObject(sql, args, getSingleColumnRowMapper(requiredType)); } + public T queryForObject(String sql, Class requiredType, Object... args) throws DataAccessException { + return queryForObject(sql, args, getSingleColumnRowMapper(requiredType)); + } + public Map queryForMap(String sql, Object[] args, int[] argTypes) throws DataAccessException { return queryForObject(sql, args, argTypes, getColumnMapRowMapper()); } @@ -763,6 +784,10 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations { return query(sql, args, getSingleColumnRowMapper(elementType)); } + public List queryForList(String sql, Class elementType, Object... args) throws DataAccessException { + return query(sql, args, getSingleColumnRowMapper(elementType)); + } + public List> queryForList(String sql, Object[] args, int[] argTypes) throws DataAccessException { return query(sql, args, argTypes, getColumnMapRowMapper()); }