001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.jdbc.object;
018
019import java.sql.ResultSet;
020import java.sql.SQLException;
021import java.util.Map;
022
023import javax.sql.DataSource;
024
025import org.springframework.lang.Nullable;
026
027/**
028 * Reusable query in which concrete subclasses must implement the abstract
029 * mapRow(ResultSet, int) method to convert each row of the JDBC ResultSet
030 * into an object.
031 *
032 * <p>Simplifies MappingSqlQueryWithParameters API by dropping parameters and
033 * context. Most subclasses won't care about parameters. If you don't use
034 * contextual information, subclass this instead of MappingSqlQueryWithParameters.
035 *
036 * @author Rod Johnson
037 * @author Thomas Risberg
038 * @author Jean-Pierre Pawlak
039 * @param <T> the result type
040 * @see MappingSqlQueryWithParameters
041 */
042public abstract class MappingSqlQuery<T> extends MappingSqlQueryWithParameters<T> {
043
044        /**
045         * Constructor that allows use as a JavaBean.
046         */
047        public MappingSqlQuery() {
048        }
049
050        /**
051         * Convenient constructor with DataSource and SQL string.
052         * @param ds the DataSource to use to obtain connections
053         * @param sql the SQL to run
054         */
055        public MappingSqlQuery(DataSource ds, String sql) {
056                super(ds, sql);
057        }
058
059
060        /**
061         * This method is implemented to invoke the simpler mapRow
062         * template method, ignoring parameters.
063         * @see #mapRow(ResultSet, int)
064         */
065        @Override
066        @Nullable
067        protected final T mapRow(ResultSet rs, int rowNum, @Nullable Object[] parameters, @Nullable Map<?, ?> context)
068                        throws SQLException {
069
070                return mapRow(rs, rowNum);
071        }
072
073        /**
074         * Subclasses must implement this method to convert each row of the
075         * ResultSet into an object of the result type.
076         * <p>Subclasses of this class, as opposed to direct subclasses of
077         * MappingSqlQueryWithParameters, don't need to concern themselves
078         * with the parameters to the execute method of the query object.
079         * @param rs the ResultSet we're working through
080         * @param rowNum row number (from 0) we're up to
081         * @return an object of the result type
082         * @throws SQLException if there's an error extracting data.
083         * Subclasses can simply not catch SQLExceptions, relying on the
084         * framework to clean up.
085         */
086        @Nullable
087        protected abstract T mapRow(ResultSet rs, int rowNum) throws SQLException;
088
089}