|
19 | 19 | import java.util.Collection;
|
20 | 20 | import java.util.List;
|
21 | 21 | import java.util.Map;
|
| 22 | +import java.util.Optional; |
22 | 23 | import java.util.stream.Stream;
|
23 | 24 |
|
24 | 25 | import org.springframework.dao.DataAccessException;
|
@@ -717,8 +718,160 @@ <T> T queryForObject(String sql, Object[] args, int[] argTypes, Class<T> require
|
717 | 718 | <T> T queryForObject(String sql, Class<T> requiredType, @Nullable Object... args) throws DataAccessException;
|
718 | 719 |
|
719 | 720 | /**
|
720 |
| - * Query given SQL to create a prepared statement from SQL and a list of |
721 |
| - * arguments to bind to the query, expecting a result map. |
| 721 | + * Execute a query given static SQL, mapping a single result row to a Java |
| 722 | + * object via a RowMapper. |
| 723 | + * <p>Uses a JDBC Statement, not a PreparedStatement. If you want to |
| 724 | + * execute a static query with a PreparedStatement, use the overloaded |
| 725 | + * {@link #queryForObject(String, RowMapper, Object...)} method with |
| 726 | + * {@code null} as argument array. |
| 727 | + * @param sql the SQL query to execute |
| 728 | + * @param rowMapper object that will map one object per row |
| 729 | + * @return optional with the single mapped object, empty optional if |
| 730 | + * no row or {@code null} is returned |
| 731 | + * @throws IncorrectResultSizeDataAccessException if the query does |
| 732 | + * return more than one row |
| 733 | + * @throws DataAccessException if there is any problem executing the query |
| 734 | + * @see #queryForOptional(String, Object[], RowMapper) |
| 735 | + */ |
| 736 | + <T> Optional<T> queryForOptional(String sql, RowMapper<T> rowMapper) throws DataAccessException; |
| 737 | + |
| 738 | + /** |
| 739 | + * Execute a query for a result object, given static SQL. |
| 740 | + * <p>Uses a JDBC Statement, not a PreparedStatement. If you want to |
| 741 | + * execute a static query with a PreparedStatement, use the overloaded |
| 742 | + * {@link #queryForOptional(String, Class, Object...)} method with |
| 743 | + * {@code null} as argument array. |
| 744 | + * <p>This method is useful for running static SQL with a known outcome. |
| 745 | + * The query is expected to be a single row/single column query; the returned |
| 746 | + * result will be directly mapped to the corresponding object type. |
| 747 | + * @param sql the SQL query to execute |
| 748 | + * @param requiredType the type that the result object is expected to match |
| 749 | + * @return optional with the result object of the required type, empty |
| 750 | + * optional if no row is returned or in case of SQL NULL |
| 751 | + * @throws IncorrectResultSizeDataAccessException if the query does return |
| 752 | + * more than one row, or does not return exactly one column in that row |
| 753 | + * @throws DataAccessException if there is any problem executing the query |
| 754 | + * @see #queryForOptional(String, Object[], Class) |
| 755 | + */ |
| 756 | + <T> Optional<T> queryForOptional(String sql, Class<T> requiredType) throws DataAccessException; |
| 757 | + |
| 758 | + /** |
| 759 | + * Query given SQL to create a prepared statement from SQL and a list |
| 760 | + * of arguments to bind to the query, mapping a single result row to a |
| 761 | + * Java object via a RowMapper. |
| 762 | + * @param sql the SQL query to execute |
| 763 | + * @param args arguments to bind to the query |
| 764 | + * (leaving it to the PreparedStatement to guess the corresponding SQL type) |
| 765 | + * @param argTypes the SQL types of the arguments |
| 766 | + * (constants from {@code java.sql.Types}) |
| 767 | + * @param rowMapper object that will map one object per row |
| 768 | + * @return optional with the single mapped object, empty optional if |
| 769 | + * no row or {@code null} is returned |
| 770 | + * @throws IncorrectResultSizeDataAccessException if the query does |
| 771 | + * return more than one row |
| 772 | + * @throws DataAccessException if the query fails |
| 773 | + */ |
| 774 | + <T> Optional<T> queryForOptional(String sql, Object[] args, int[] argTypes, RowMapper<T> rowMapper) throws DataAccessException; |
| 775 | + |
| 776 | + /** |
| 777 | + * Query given SQL to create a prepared statement from SQL and a list |
| 778 | + * of arguments to bind to the query, mapping a single result row to a |
| 779 | + * Java object via a RowMapper. |
| 780 | + * @param sql the SQL query to execute |
| 781 | + * @param args arguments to bind to the query |
| 782 | + * (leaving it to the PreparedStatement to guess the corresponding SQL type); |
| 783 | + * may also contain {@link SqlParameterValue} objects which indicate not |
| 784 | + * only the argument value but also the SQL type and optionally the scale |
| 785 | + * @param rowMapper object that will map one object per row |
| 786 | + * @return optional with the single mapped object, empty optional if |
| 787 | + * no row or {@code null} is returned |
| 788 | + * @throws IncorrectResultSizeDataAccessException if the query does |
| 789 | + * return more than one row |
| 790 | + * @throws DataAccessException if the query fails |
| 791 | + */ |
| 792 | + <T> Optional<T> queryForOptional(String sql, Object[] args, RowMapper<T> rowMapper) throws DataAccessException; |
| 793 | + |
| 794 | + /** |
| 795 | + * Query given SQL to create a prepared statement from SQL and a list |
| 796 | + * of arguments to bind to the query, mapping a single result row to a |
| 797 | + * Java object via a RowMapper. |
| 798 | + * @param sql the SQL query to execute |
| 799 | + * @param rowMapper object that will map one object per row |
| 800 | + * @param args arguments to bind to the query |
| 801 | + * (leaving it to the PreparedStatement to guess the corresponding SQL type); |
| 802 | + * may also contain {@link SqlParameterValue} objects which indicate not |
| 803 | + * only the argument value but also the SQL type and optionally the scale |
| 804 | + * @return optional with the single mapped object, empty optional if |
| 805 | + * no row or {@code null} is returned |
| 806 | + * @throws IncorrectResultSizeDataAccessException if the query does |
| 807 | + * return more than one row |
| 808 | + * @throws DataAccessException if the query fails |
| 809 | + */ |
| 810 | + <T> Optional<T> queryForOptional(String sql, RowMapper<T> rowMapper, Object... args) throws DataAccessException; |
| 811 | + |
| 812 | + /** |
| 813 | + * Query given SQL to create a prepared statement from SQL and a |
| 814 | + * list of arguments to bind to the query, expecting a result object. |
| 815 | + * <p>The query is expected to be a single row/single column query; the returned |
| 816 | + * result will be directly mapped to the corresponding object type. |
| 817 | + * @param sql the SQL query to execute |
| 818 | + * @param args arguments to bind to the query |
| 819 | + * @param argTypes the SQL types of the arguments |
| 820 | + * (constants from {@code java.sql.Types}) |
| 821 | + * @param requiredType the type that the result object is expected to match |
| 822 | + * @return optional with the result object of the required type, empty optional if |
| 823 | + * no row is returned or in case of SQL NULL |
| 824 | + * @throws IncorrectResultSizeDataAccessException if the query does return |
| 825 | + * more than one row, or does not return exactly one column in that row |
| 826 | + * @throws DataAccessException if the query fails |
| 827 | + * @see #queryForObject(String, Class) |
| 828 | + * @see java.sql.Types |
| 829 | + */ |
| 830 | + <T> Optional<T> queryForOptional(String sql, Object[] args, int[] argTypes, Class<T> requiredType) throws DataAccessException; |
| 831 | + |
| 832 | + /** |
| 833 | + * Query given SQL to create a prepared statement from SQL and a |
| 834 | + * list of arguments to bind to the query, expecting a result object. |
| 835 | + * <p>The query is expected to be a single row/single column query; the returned |
| 836 | + * result will be directly mapped to the corresponding object type. |
| 837 | + * @param sql the SQL query to execute |
| 838 | + * @param args arguments to bind to the query |
| 839 | + * (leaving it to the PreparedStatement to guess the corresponding SQL type); |
| 840 | + * may also contain {@link SqlParameterValue} objects which indicate not |
| 841 | + * only the argument value but also the SQL type and optionally the scale |
| 842 | + * @param requiredType the type that the result object is expected to match |
| 843 | + * @return optional with the result object of the required type, empty optional if |
| 844 | + * no row is returned or in case of SQL NULL |
| 845 | + * @throws IncorrectResultSizeDataAccessException if the query does return |
| 846 | + * more than one row, or does not return exactly one column in that row |
| 847 | + * @throws DataAccessException if the query fails |
| 848 | + * @see #queryForObject(String, Class) |
| 849 | + */ |
| 850 | + <T> Optional<T> queryForOptional(String sql, Object[] args, Class<T> requiredType) throws DataAccessException; |
| 851 | + |
| 852 | + /** |
| 853 | + * Query given SQL to create a prepared statement from SQL and a |
| 854 | + * list of arguments to bind to the query, expecting a result object. |
| 855 | + * <p>The query is expected to be a single row/single column query; the returned |
| 856 | + * result will be directly mapped to the corresponding object type. |
| 857 | + * @param sql the SQL query to execute |
| 858 | + * @param requiredType the type that the result object is expected to match |
| 859 | + * @param args arguments to bind to the query |
| 860 | + * (leaving it to the PreparedStatement to guess the corresponding SQL type); |
| 861 | + * may also contain {@link SqlParameterValue} objects which indicate not |
| 862 | + * only the argument value but also the SQL type and optionally the scale |
| 863 | + * @return optional with the result object of the required type, empty optional if |
| 864 | + * no row is returned or in case of SQL NULL |
| 865 | + * @throws IncorrectResultSizeDataAccessException if the query does return |
| 866 | + * more than one row, or does not return exactly one column in that row |
| 867 | + * @throws DataAccessException if the query fails |
| 868 | + * @see #queryForOptional(String, Class) |
| 869 | + */ |
| 870 | + <T> Optional<T> queryForOptional(String sql, Class<T> requiredType, Object... args) throws DataAccessException; |
| 871 | + |
| 872 | + /** |
| 873 | + * Query given SQL to create a prepared statement from SQL and a |
| 874 | + * list of arguments to bind to the query, expecting a result Map. |
722 | 875 | * <p>The query is expected to be a single row query; the result row will be
|
723 | 876 | * mapped to a Map (one entry for each column, using the column name as the key).
|
724 | 877 | * @param sql the SQL query to execute
|
|
0 commit comments