Skip to content

Commit

Permalink
Added useful query variants without parameters to NamedParameterJdbcT…
Browse files Browse the repository at this point in the history
…emplate, for convenience in DAOs

Also deprecated NamedParameterJdbcTemplate's queryForInt/Long operations in favor of queryForObject.

Issue: SPR-10256
Issue: SPR-10257
  • Loading branch information
jhoeller committed Feb 9, 2013
1 parent 9881517 commit 3fa6723
Show file tree
Hide file tree
Showing 4 changed files with 273 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.jdbc.core.namedparam;

/**
* A simple empty implementation of the {@link SqlParameterSource} interface.
*
* @author Juergen Hoeller
* @since 3.2.2
*/
public class EmptySqlParameterSource implements SqlParameterSource {

/**
* A shared instance of {@link EmptySqlParameterSource}.
*/
public static final EmptySqlParameterSource INSTANCE = new EmptySqlParameterSource();


public boolean hasValue(String paramName) {
return false;
}

public Object getValue(String paramName) throws IllegalArgumentException {
throw new IllegalArgumentException("This SqlParameterSource is empty");
}

public int getSqlType(String paramName) {
return TYPE_UNKNOWN;
}

public String getTypeName(String paramName) {
return null;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
Expand Down Expand Up @@ -88,6 +88,21 @@ <T> T execute(String sql, SqlParameterSource paramSource, PreparedStatementCallb
<T> T execute(String sql, Map<String, ?> paramMap, PreparedStatementCallback<T> action)
throws DataAccessException;

/**
* Execute a JDBC data access operation, implemented as callback action
* working on a JDBC PreparedStatement. This allows for implementing arbitrary
* data access operations on a single Statement, within Spring's managed
* JDBC environment: that is, participating in Spring-managed transactions
* and converting JDBC SQLExceptions into Spring's DataAccessException hierarchy.
* <p>The callback action can return a result object, for example a
* domain object or a collection of domain objects.
* @param sql SQL to execute
* @param action callback object that specifies the action
* @return a result object returned by the action, or {@code null}
* @throws DataAccessException if there is any problem
*/
<T> T execute(String sql, PreparedStatementCallback<T> action) 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
Expand Down Expand Up @@ -115,6 +130,19 @@ <T> T query(String sql, SqlParameterSource paramSource, ResultSetExtractor<T> rs
<T> T query(String sql, Map<String, ?> paramMap, ResultSetExtractor<T> rse)
throws DataAccessException;

/**
* Query given SQL to create a prepared statement from SQL,
* reading the ResultSet with a ResultSetExtractor.
* <p>Note: In contrast to the JdbcOperations method with the same signature,
* this query variant always uses a PreparedStatement. It is effectively
* equivalent to a query call with an empty parameter Map.
* @param sql SQL query to execute
* @param rse object that will extract results
* @return an arbitrary result object, as returned by the ResultSetExtractor
* @throws org.springframework.dao.DataAccessException if the query fails
*/
<T> T query(String sql, ResultSetExtractor<T> 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 on a per-row basis
Expand All @@ -139,6 +167,18 @@ void query(String sql, SqlParameterSource paramSource, RowCallbackHandler rch)
*/
void query(String sql, Map<String, ?> paramMap, RowCallbackHandler rch) throws DataAccessException;

/**
* Query given SQL to create a prepared statement from SQL,
* reading the ResultSet on a per-row basis with a RowCallbackHandler.
* <p>Note: In contrast to the JdbcOperations method with the same signature,
* this query variant always uses a PreparedStatement. It is effectively
* equivalent to a query call with an empty parameter Map.
* @param sql SQL query to execute
* @param rch object that will extract results, one row at a time
* @throws org.springframework.dao.DataAccessException if the query fails
*/
void query(String sql, RowCallbackHandler rch) 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
Expand Down Expand Up @@ -166,6 +206,19 @@ <T> List<T> query(String sql, SqlParameterSource paramSource, RowMapper<T> rowMa
<T> List<T> query(String sql, Map<String, ?> paramMap, RowMapper<T> rowMapper)
throws DataAccessException;

/**
* Query given SQL to create a prepared statement from SQL,
* mapping each row to a Java object via a RowMapper.
* <p>Note: In contrast to the JdbcOperations method with the same signature,
* this query variant always uses a PreparedStatement. It is effectively
* equivalent to a query call with an empty parameter Map.
* @param sql SQL query to execute
* @param rowMapper object that will map one object per row
* @return the result List, containing mapped objects
* @throws org.springframework.dao.DataAccessException if the query fails
*/
<T> List<T> query(String sql, RowMapper<T> 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
Expand Down Expand Up @@ -245,8 +298,7 @@ <T> T queryForObject(String sql, Map<String, ?> paramMap, Class<T> requiredType)
* @param paramSource container of arguments to bind to the query
* @return the result Map (one entry for each column, using the column name as the key)
* @throws org.springframework.dao.IncorrectResultSizeDataAccessException
* if the query does not return exactly one row, or does not return exactly
* one column in that row
* if the query does not return exactly one row
* @throws org.springframework.dao.DataAccessException if the query fails
* @see org.springframework.jdbc.core.JdbcTemplate#queryForMap(String)
* @see org.springframework.jdbc.core.ColumnMapRowMapper
Expand All @@ -266,8 +318,7 @@ <T> T queryForObject(String sql, Map<String, ?> paramMap, Class<T> requiredType)
* (leaving it to the PreparedStatement to guess the corresponding SQL type)
* @return the result Map (one entry for each column, using the column name as the key)
* @throws org.springframework.dao.IncorrectResultSizeDataAccessException
* if the query does not return exactly one row, or does not return exactly
* one column in that row
* if the query does not return exactly one row
* @throws org.springframework.dao.DataAccessException if the query fails
* @see org.springframework.jdbc.core.JdbcTemplate#queryForMap(String)
* @see org.springframework.jdbc.core.ColumnMapRowMapper
Expand All @@ -287,7 +338,9 @@ <T> T queryForObject(String sql, Map<String, ?> paramMap, Class<T> requiredType)
* one column in that row
* @throws org.springframework.dao.DataAccessException if the query fails
* @see org.springframework.jdbc.core.JdbcTemplate#queryForLong(String)
* @deprecated in favor of {@link #queryForObject(String, SqlParameterSource, Class)}
*/
@Deprecated
long queryForLong(String sql, SqlParameterSource paramSource) throws DataAccessException;

/**
Expand All @@ -304,7 +357,9 @@ <T> T queryForObject(String sql, Map<String, ?> paramMap, Class<T> requiredType)
* one column in that row
* @throws org.springframework.dao.DataAccessException if the query fails
* @see org.springframework.jdbc.core.JdbcTemplate#queryForLong(String)
* @deprecated in favor of {@link #queryForObject(String, Map, Class)}
*/
@Deprecated
long queryForLong(String sql, Map<String, ?> paramMap) throws DataAccessException;

/**
Expand All @@ -319,7 +374,9 @@ <T> T queryForObject(String sql, Map<String, ?> paramMap, Class<T> requiredType)
* exactly one row, or does not return exactly one column in that row
* @throws org.springframework.dao.DataAccessException if the query fails
* @see org.springframework.jdbc.core.JdbcTemplate#queryForInt(String)
* @deprecated in favor of {@link #queryForObject(String, SqlParameterSource, Class)}
*/
@Deprecated
int queryForInt(String sql, SqlParameterSource paramSource) throws DataAccessException;

/**
Expand All @@ -335,7 +392,9 @@ <T> T queryForObject(String sql, Map<String, ?> paramMap, Class<T> requiredType)
* exactly one row, or does not return exactly one column in that row
* @throws org.springframework.dao.DataAccessException if the query fails
* @see org.springframework.jdbc.core.JdbcTemplate#queryForInt(String)
* @deprecated in favor of {@link #queryForObject(String, Map, Class)}
*/
@Deprecated
int queryForInt(String sql, Map<String, ?> paramMap) throws DataAccessException;

/**
Expand Down Expand Up @@ -378,8 +437,8 @@ <T> List<T> queryForList(String sql, Map<String, ?> paramMap, Class<T> elementTy
* list of arguments to bind to the query, expecting a result list.
* <p>The results will be mapped to a List (one entry for each row) of
* Maps (one entry for each column, using the column name as the key).
* Thus Each element in the list will be of the form returned by this interface's
* queryForMap() methods.
* Each element in the list will be of the form returned by this interface's
* {@code queryForMap} methods.
* @param sql SQL query to execute
* @param paramSource container of arguments to bind to the query
* @return a List that contains a Map per row
Expand All @@ -394,7 +453,7 @@ <T> List<T> queryForList(String sql, Map<String, ?> paramMap, Class<T> elementTy
* <p>The results will be mapped to a List (one entry for each row) of
* Maps (one entry for each column, using the column name as the key).
* Each element in the list will be of the form returned by this interface's
* queryForMap() methods.
* {@code queryForMap} methods.
* @param sql SQL query to execute
* @param paramMap map of parameters to bind to the query
* (leaving it to the PreparedStatement to guess the corresponding SQL type)
Expand Down Expand Up @@ -499,14 +558,14 @@ int update(String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHol
* @param batchValues the array of Maps containing the batch of arguments for the query
* @return an array containing the numbers of rows affected by each update in the batch
*/
public int[] batchUpdate(String sql, Map<String, ?>[] batchValues);
int[] batchUpdate(String sql, Map<String, ?>[] batchValues);

/**
* Execute a batch using the supplied SQL statement with the batch of supplied arguments.
* @param sql the SQL statement to execute
* @param batchArgs the array of {@link SqlParameterSource} containing the batch of arguments for the query
* @return an array containing the numbers of rows affected by each update in the batch
*/
public int[] batchUpdate(String sql, SqlParameterSource[] batchArgs);
int[] batchUpdate(String sql, SqlParameterSource[] batchArgs);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
Expand Down Expand Up @@ -137,6 +137,10 @@ public <T> T execute(String sql, Map<String, ?> paramMap, PreparedStatementCallb
return execute(sql, new MapSqlParameterSource(paramMap), action);
}

public <T> T execute(String sql, PreparedStatementCallback<T> action) throws DataAccessException {
return execute(sql, EmptySqlParameterSource.INSTANCE, action);
}

public <T> T query(String sql, SqlParameterSource paramSource, ResultSetExtractor<T> rse)
throws DataAccessException {

Expand All @@ -149,6 +153,10 @@ public <T> T query(String sql, Map<String, ?> paramMap, ResultSetExtractor<T> rs
return query(sql, new MapSqlParameterSource(paramMap), rse);
}

public <T> T query(String sql, ResultSetExtractor<T> rse) throws DataAccessException {
return query(sql, EmptySqlParameterSource.INSTANCE, rse);
}

public void query(String sql, SqlParameterSource paramSource, RowCallbackHandler rch)
throws DataAccessException {

Expand All @@ -161,6 +169,10 @@ public void query(String sql, Map<String, ?> paramMap, RowCallbackHandler rch)
query(sql, new MapSqlParameterSource(paramMap), rch);
}

public void query(String sql, RowCallbackHandler rch) throws DataAccessException {
query(sql, EmptySqlParameterSource.INSTANCE, rch);
}

public <T> List<T> query(String sql, SqlParameterSource paramSource, RowMapper<T> rowMapper)
throws DataAccessException {

Expand All @@ -173,6 +185,10 @@ public <T> List<T> query(String sql, Map<String, ?> paramMap, RowMapper<T> rowMa
return query(sql, new MapSqlParameterSource(paramMap), rowMapper);
}

public <T> List<T> query(String sql, RowMapper<T> rowMapper) throws DataAccessException {
return query(sql, EmptySqlParameterSource.INSTANCE, rowMapper);
}

public <T> T queryForObject(String sql, SqlParameterSource paramSource, RowMapper<T> rowMapper)
throws DataAccessException {

Expand Down Expand Up @@ -206,20 +222,24 @@ public Map<String, Object> queryForMap(String sql, Map<String, ?> paramMap) thro
return queryForObject(sql, paramMap, new ColumnMapRowMapper());
}

@Deprecated
public long queryForLong(String sql, SqlParameterSource paramSource) throws DataAccessException {
Number number = queryForObject(sql, paramSource, Long.class);
return (number != null ? number.longValue() : 0);
}

@Deprecated
public long queryForLong(String sql, Map<String, ?> paramMap) throws DataAccessException {
return queryForLong(sql, new MapSqlParameterSource(paramMap));
}

@Deprecated
public int queryForInt(String sql, SqlParameterSource paramSource) throws DataAccessException {
Number number = queryForObject(sql, paramSource, Integer.class);
return (number != null ? number.intValue() : 0);
}

@Deprecated
public int queryForInt(String sql, Map<String, ?> paramMap) throws DataAccessException {
return queryForInt(sql, new MapSqlParameterSource(paramMap));
}
Expand Down
Loading

0 comments on commit 3fa6723

Please sign in to comment.