Skip to content

Commit c39b529

Browse files
committed
added vararg variants of query methods to JdbcTemplate (as known from SimpleJdbcTemplate; SPR-6858)
1 parent 54acebd commit c39b529

File tree

2 files changed

+127
-2
lines changed

2 files changed

+127
-2
lines changed

org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/JdbcOperations.java

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -380,6 +380,21 @@ <T> T query(String sql, Object[] args, int[] argTypes, ResultSetExtractor<T> rse
380380
*/
381381
<T> T query(String sql, Object[] args, ResultSetExtractor<T> rse) throws DataAccessException;
382382

383+
/**
384+
* Query given SQL to create a prepared statement from SQL and a list
385+
* of arguments to bind to the query, reading the ResultSet with a
386+
* ResultSetExtractor.
387+
* @param sql SQL query to execute
388+
* @param rse object that will extract results
389+
* @param args arguments to bind to the query
390+
* (leaving it to the PreparedStatement to guess the corresponding SQL type);
391+
* may also contain {@link SqlParameterValue} objects which indicate not
392+
* only the argument value but also the SQL type and optionally the scale
393+
* @return an arbitrary result object, as returned by the ResultSetExtractor
394+
* @throws DataAccessException if the query fails
395+
*/
396+
<T> T query(String sql, ResultSetExtractor<T> rse, Object... args) throws DataAccessException;
397+
383398
/**
384399
* Query using a prepared statement, reading the ResultSet on a per-row
385400
* basis with a RowCallbackHandler.
@@ -437,6 +452,20 @@ void query(String sql, Object[] args, int[] argTypes, RowCallbackHandler rch)
437452
*/
438453
void query(String sql, Object[] args, RowCallbackHandler rch) throws DataAccessException;
439454

455+
/**
456+
* Query given SQL to create a prepared statement from SQL and a list of
457+
* arguments to bind to the query, reading the ResultSet on a per-row basis
458+
* with a RowCallbackHandler.
459+
* @param sql SQL query to execute
460+
* @param rch object that will extract results, one row at a time
461+
* @param args arguments to bind to the query
462+
* (leaving it to the PreparedStatement to guess the corresponding SQL type);
463+
* may also contain {@link SqlParameterValue} objects which indicate not
464+
* only the argument value but also the SQL type and optionally the scale
465+
* @throws DataAccessException if the query fails
466+
*/
467+
void query(String sql, RowCallbackHandler rch, Object... args) throws DataAccessException;
468+
440469
/**
441470
* Query using a prepared statement, mapping each row to a Java object
442471
* via a RowMapper.
@@ -497,6 +526,21 @@ <T> List<T> query(String sql, Object[] args, int[] argTypes, RowMapper<T> rowMap
497526
*/
498527
<T> List<T> query(String sql, Object[] args, RowMapper<T> rowMapper) throws DataAccessException;
499528

529+
/**
530+
* Query given SQL to create a prepared statement from SQL and a list
531+
* of arguments to bind to the query, mapping each row to a Java object
532+
* via a RowMapper.
533+
* @param sql SQL query to execute
534+
* @param rowMapper object that will map one object per row
535+
* @param args arguments to bind to the query
536+
* (leaving it to the PreparedStatement to guess the corresponding SQL type);
537+
* may also contain {@link SqlParameterValue} objects which indicate not
538+
* only the argument value but also the SQL type and optionally the scale
539+
* @return the result List, containing mapped objects
540+
* @throws DataAccessException if the query fails
541+
*/
542+
<T> List<T> query(String sql, RowMapper<T> rowMapper, Object... args) throws DataAccessException;
543+
500544
/**
501545
* Query given SQL to create a prepared statement from SQL and a list
502546
* of arguments to bind to the query, mapping a single result row to a
@@ -533,6 +577,24 @@ <T> T queryForObject(String sql, Object[] args, int[] argTypes, RowMapper<T> row
533577
<T> T queryForObject(String sql, Object[] args, RowMapper<T> rowMapper)
534578
throws DataAccessException;
535579

580+
/**
581+
* Query given SQL to create a prepared statement from SQL and a list
582+
* of arguments to bind to the query, mapping a single result row to a
583+
* Java object via a RowMapper.
584+
* @param sql SQL query to execute
585+
* @param rowMapper object that will map one object per row
586+
* @param args arguments to bind to the query
587+
* (leaving it to the PreparedStatement to guess the corresponding SQL type);
588+
* may also contain {@link SqlParameterValue} objects which indicate not
589+
* only the argument value but also the SQL type and optionally the scale
590+
* @return the single mapped object
591+
* @throws IncorrectResultSizeDataAccessException if the query does not
592+
* return exactly one row
593+
* @throws DataAccessException if the query fails
594+
*/
595+
<T> T queryForObject(String sql, RowMapper<T> rowMapper, Object... args)
596+
throws DataAccessException;
597+
536598
/**
537599
* Query given SQL to create a prepared statement from SQL and a
538600
* list of arguments to bind to the query, expecting a result object.
@@ -572,6 +634,25 @@ <T> T queryForObject(String sql, Object[] args, int[] argTypes, Class<T> require
572634
*/
573635
<T> T queryForObject(String sql, Object[] args, Class<T> requiredType) throws DataAccessException;
574636

637+
/**
638+
* Query given SQL to create a prepared statement from SQL and a
639+
* list of arguments to bind to the query, expecting a result object.
640+
* <p>The query is expected to be a single row/single column query; the returned
641+
* result will be directly mapped to the corresponding object type.
642+
* @param sql SQL query to execute
643+
* @param requiredType the type that the result object is expected to match
644+
* @param args arguments to bind to the query
645+
* (leaving it to the PreparedStatement to guess the corresponding SQL type);
646+
* may also contain {@link SqlParameterValue} objects which indicate not
647+
* only the argument value but also the SQL type and optionally the scale
648+
* @return the result object of the required type, or <code>null</code> in case of SQL NULL
649+
* @throws IncorrectResultSizeDataAccessException if the query does not return
650+
* exactly one row, or does not return exactly one column in that row
651+
* @throws DataAccessException if the query fails
652+
* @see #queryForObject(String, Class)
653+
*/
654+
<T> T queryForObject(String sql, Class<T> requiredType, Object... args) throws DataAccessException;
655+
575656
/**
576657
* Query given SQL to create a prepared statement from SQL and a
577658
* list of arguments to bind to the query, expecting a result Map.
@@ -725,6 +806,25 @@ <T>List<T> queryForList(String sql, Object[] args, int[] argTypes, Class<T> elem
725806
*/
726807
<T> List<T> queryForList(String sql, Object[] args, Class<T> elementType) throws DataAccessException;
727808

809+
/**
810+
* Query given SQL to create a prepared statement from SQL and a
811+
* list of arguments to bind to the query, expecting a result list.
812+
* <p>The results will be mapped to a List (one entry for each row) of
813+
* result objects, each of them matching the specified element type.
814+
* @param sql SQL query to execute
815+
* @param elementType the required type of element in the result list
816+
* (for example, <code>Integer.class</code>)
817+
* @param args arguments to bind to the query
818+
* (leaving it to the PreparedStatement to guess the corresponding SQL type);
819+
* may also contain {@link SqlParameterValue} objects which indicate not
820+
* only the argument value but also the SQL type and optionally the scale
821+
* @return a List of objects that match the specified element type
822+
* @throws DataAccessException if the query fails
823+
* @see #queryForList(String, Class)
824+
* @see SingleColumnRowMapper
825+
*/
826+
<T> List<T> queryForList(String sql, Class<T> elementType, Object... args) throws DataAccessException;
827+
728828
/**
729829
* Query given SQL to create a prepared statement from SQL and a
730830
* list of arguments to bind to the query, expecting a result list.

org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -673,6 +673,10 @@ public <T> T query(String sql, Object[] args, ResultSetExtractor<T> rse) throws
673673
return query(sql, newArgPreparedStatementSetter(args), rse);
674674
}
675675

676+
public <T> T query(String sql, ResultSetExtractor<T> rse, Object... args) throws DataAccessException {
677+
return query(sql, newArgPreparedStatementSetter(args), rse);
678+
}
679+
676680
public void query(PreparedStatementCreator psc, RowCallbackHandler rch) throws DataAccessException {
677681
query(psc, new RowCallbackHandlerResultSetExtractor(rch));
678682
}
@@ -689,6 +693,10 @@ public void query(String sql, Object[] args, RowCallbackHandler rch) throws Data
689693
query(sql, newArgPreparedStatementSetter(args), rch);
690694
}
691695

696+
public void query(String sql, RowCallbackHandler rch, Object... args) throws DataAccessException {
697+
query(sql, newArgPreparedStatementSetter(args), rch);
698+
}
699+
692700
public <T> List<T> query(PreparedStatementCreator psc, RowMapper<T> rowMapper) throws DataAccessException {
693701
return query(psc, new RowMapperResultSetExtractor<T>(rowMapper));
694702
}
@@ -705,6 +713,10 @@ public <T> List<T> query(String sql, Object[] args, RowMapper<T> rowMapper) thro
705713
return query(sql, args, new RowMapperResultSetExtractor<T>(rowMapper));
706714
}
707715

716+
public <T> List<T> query(String sql, RowMapper<T> rowMapper, Object... args) throws DataAccessException {
717+
return query(sql, args, new RowMapperResultSetExtractor<T>(rowMapper));
718+
}
719+
708720
public <T> T queryForObject(String sql, Object[] args, int[] argTypes, RowMapper<T> rowMapper)
709721
throws DataAccessException {
710722

@@ -717,6 +729,11 @@ public <T> T queryForObject(String sql, Object[] args, RowMapper<T> rowMapper) t
717729
return DataAccessUtils.requiredSingleResult(results);
718730
}
719731

732+
public <T> T queryForObject(String sql, RowMapper<T> rowMapper, Object... args) throws DataAccessException {
733+
List<T> results = query(sql, args, new RowMapperResultSetExtractor<T>(rowMapper, 1));
734+
return DataAccessUtils.requiredSingleResult(results);
735+
}
736+
720737
public <T> T queryForObject(String sql, Object[] args, int[] argTypes, Class<T> requiredType)
721738
throws DataAccessException {
722739

@@ -727,6 +744,10 @@ public <T> T queryForObject(String sql, Object[] args, Class<T> requiredType) th
727744
return queryForObject(sql, args, getSingleColumnRowMapper(requiredType));
728745
}
729746

747+
public <T> T queryForObject(String sql, Class<T> requiredType, Object... args) throws DataAccessException {
748+
return queryForObject(sql, args, getSingleColumnRowMapper(requiredType));
749+
}
750+
730751
public Map<String, Object> queryForMap(String sql, Object[] args, int[] argTypes) throws DataAccessException {
731752
return queryForObject(sql, args, argTypes, getColumnMapRowMapper());
732753
}
@@ -763,6 +784,10 @@ public <T> List<T> queryForList(String sql, Object[] args, Class<T> elementType)
763784
return query(sql, args, getSingleColumnRowMapper(elementType));
764785
}
765786

787+
public <T> List<T> queryForList(String sql, Class<T> elementType, Object... args) throws DataAccessException {
788+
return query(sql, args, getSingleColumnRowMapper(elementType));
789+
}
790+
766791
public List<Map<String, Object>> queryForList(String sql, Object[] args, int[] argTypes) throws DataAccessException {
767792
return query(sql, args, argTypes, getColumnMapRowMapper());
768793
}

0 commit comments

Comments
 (0)