KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > object > MappingSqlQueryWithParameters


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
20 import java.sql.SQLException JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.sql.DataSource JavaDoc;
24
25 import org.springframework.jdbc.core.RowMapper;
26
27 /**
28  * Reusable RDBMS query in which concrete subclasses must implement
29  * the abstract mapRow(ResultSet, int) method to map each row of
30  * the JDBC ResultSet into an object.
31  *
32  * <p>Such manual mapping is usually preferable to "automatic"
33  * mapping using reflection, which can become complex in non-trivial
34  * cases. For example, the present class allows different objects
35  * to be used for different rows (for example, if a subclass is indicated).
36  * It allows computed fields to be set. And there's no need for
37  * ResultSet columns to have the same names as bean properties.
38  * The Pareto Principle in action: going the extra mile to automate
39  * the extraction process makes the framework much more complex
40  * and delivers little real benefit.
41  *
42  * <p>Subclasses can be constructed providing SQL, parameter types
43  * and a DataSource. SQL will often vary between subclasses.
44  *
45  * @author Rod Johnson
46  * @author Thomas Risberg
47  * @author Jean-Pierre Pawlak
48  * @see org.springframework.jdbc.object.MappingSqlQuery
49  * @see org.springframework.jdbc.object.SqlQuery
50  */

51 public abstract class MappingSqlQueryWithParameters extends SqlQuery {
52
53     /**
54      * Constructor to allow use as a JavaBean
55      */

56     public MappingSqlQueryWithParameters() {
57     }
58
59     /**
60      * Convenient constructor with DataSource and SQL string.
61      * @param ds DataSource to use to get connections
62      * @param sql SQL to run
63      */

64     public MappingSqlQueryWithParameters(DataSource JavaDoc ds, String JavaDoc sql) {
65         super(ds, sql);
66     }
67
68
69     /**
70      * Implementation of protected abstract method. This invokes the subclass's
71      * implementation of the mapRow() method.
72      */

73     protected RowMapper newRowMapper(Object JavaDoc[] parameters, Map JavaDoc context) {
74         return new RowMapperImpl(parameters, context);
75     }
76
77     /**
78      * Subclasses must implement this method to convert each row
79      * of the ResultSet into an object of the result type.
80      * @param rs ResultSet we're working through
81      * @param rowNum row number (from 0) we're up to
82      * @param parameters to the query (passed to the execute() method).
83      * Subclasses are rarely interested in these.
84      * It can be <code>null</code> if there are no parameters.
85      * @param context passed to the execute() method.
86      * It can be <code>null</code> if no contextual information is need.
87      * @return an object of the result type
88      * @throws SQLException if there's an error extracting data.
89      * Subclasses can simply not catch SQLExceptions, relying on the
90      * framework to clean up.
91      */

92     protected abstract Object JavaDoc mapRow(ResultSet JavaDoc rs, int rowNum, Object JavaDoc[] parameters, Map JavaDoc context)
93             throws SQLException JavaDoc;
94
95
96     /**
97      * Implementation of RowMapper that calls the enclosing
98      * class's <code>mapRow</code> method for each row.
99      */

100     protected class RowMapperImpl implements RowMapper {
101
102         private final Object JavaDoc[] params;
103
104         private final Map JavaDoc context;
105
106         /**
107          * Use an array results. More efficient if we know how many results to expect.
108          */

109         public RowMapperImpl(Object JavaDoc[] parameters, Map JavaDoc context) {
110             this.params = parameters;
111             this.context = context;
112         }
113
114         public Object JavaDoc mapRow(ResultSet JavaDoc rs, int rowNum) throws SQLException JavaDoc {
115             return MappingSqlQueryWithParameters.this.mapRow(rs, rowNum, this.params, this.context);
116         }
117     }
118
119 }
120
Popular Tags