01: /*
02: * Copyright 2002-2005 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.jdbc.object;
18:
19: import java.sql.ResultSet;
20: import java.sql.SQLException;
21: import java.util.Map;
22:
23: import javax.sql.DataSource;
24:
25: /**
26: * Reusable query in which concrete subclasses must implement the abstract
27: * mapRow(ResultSet, int) method to convert each row of the JDBC ResultSet
28: * into an object.
29: *
30: * <p>Simplifies MappingSqlQueryWithParameters API by dropping parameters and
31: * context. Most subclasses won't care about parameters. If you don't use
32: * contextual information, subclass this instead of MappingSqlQueryWithParameters.
33: *
34: * @author Rod Johnson
35: * @author Thomas Risberg
36: * @author Jean-Pierre Pawlak
37: * @see MappingSqlQueryWithParameters
38: */
39: public abstract class MappingSqlQuery extends
40: MappingSqlQueryWithParameters {
41:
42: /**
43: * Constructor that allows use as a JavaBean.
44: */
45: public MappingSqlQuery() {
46: }
47:
48: /**
49: * Convenient constructor with DataSource and SQL string.
50: * @param ds DataSource to use to obtain connections
51: * @param sql SQL to run
52: */
53: public MappingSqlQuery(DataSource ds, String sql) {
54: super (ds, sql);
55: }
56:
57: /**
58: * This method is implemented to invoke the simpler mapRow
59: * template method, ignoring parameters.
60: * @see #mapRow(ResultSet, int)
61: */
62: protected final Object mapRow(ResultSet rs, int rowNum,
63: Object[] parameters, Map context) throws SQLException {
64:
65: return mapRow(rs, rowNum);
66: }
67:
68: /**
69: * Subclasses must implement this method to convert each row of the
70: * ResultSet into an object of the result type.
71: * <p>Subclasses of this class, as opposed to direct subclasses of
72: * MappingSqlQueryWithParameters, don't need to concern themselves
73: * with the parameters to the execute method of the query object.
74: * @param rs ResultSet we're working through
75: * @param rowNum row number (from 0) we're up to
76: * @return an object of the result type
77: * @throws SQLException if there's an error extracting data.
78: * Subclasses can simply not catch SQLExceptions, relying on the
79: * framework to clean up.
80: */
81: protected abstract Object mapRow(ResultSet rs, int rowNum)
82: throws SQLException;
83:
84: }
|