KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > core > namedparam > NamedParameterJdbcTemplate


1 /*
2  * Copyright 2002-2006 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.core.namedparam;
18
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.sql.DataSource JavaDoc;
23
24 import org.springframework.dao.DataAccessException;
25 import org.springframework.dao.support.DataAccessUtils;
26 import org.springframework.jdbc.core.ColumnMapRowMapper;
27 import org.springframework.jdbc.core.JdbcOperations;
28 import org.springframework.jdbc.core.JdbcTemplate;
29 import org.springframework.jdbc.core.PreparedStatementCreatorFactory;
30 import org.springframework.jdbc.core.RowCallbackHandler;
31 import org.springframework.jdbc.core.RowMapper;
32 import org.springframework.jdbc.core.RowMapperResultSetExtractor;
33 import org.springframework.jdbc.core.SingleColumnRowMapper;
34 import org.springframework.jdbc.core.SqlRowSetResultSetExtractor;
35 import org.springframework.jdbc.support.KeyHolder;
36 import org.springframework.jdbc.support.rowset.SqlRowSet;
37 import org.springframework.util.Assert;
38
39 /**
40  * This class provides basic set of JDBC operations allowing the use of
41  * named parameters rather than the traditional '?' placeholders.
42  *
43  * <p>It delegates to a wrapped {@link #getJdbcOperations() JdbcTemplate} once
44  * the substitution from named parameters to JDBC style '?' placeholders is done
45  * at execution time. It also allows for expanding a {@link java.util.List} of
46  * values to the appropriate number of placeholders.
47  *
48  * <p>The underlying {@link org.springframework.jdbc.core.JdbcTemplate} is
49  * exposed to allow for convenient access to the traditional
50  * {@link org.springframework.jdbc.core.JdbcTemplate} methods.
51  *
52  * @author Thomas Risberg
53  * @author Juergen Hoeller
54  * @since 2.0
55  * @see NamedParameterJdbcOperations
56  * @see org.springframework.jdbc.core.JdbcTemplate
57  */

58 public class NamedParameterJdbcTemplate implements NamedParameterJdbcOperations {
59
60     /**
61      * The JdbcTemplate we are wrapping
62      */

63     private final JdbcOperations classicJdbcTemplate;
64
65
66     /**
67      * Create a new NamedParameterJdbcTemplate for the given {@link DataSource}.
68      * <p>Creates a classic Spring {@link org.springframework.jdbc.core.JdbcTemplate} and wraps it.
69      * @param dataSource the JDBC DataSource to access
70      */

71     public NamedParameterJdbcTemplate(DataSource JavaDoc dataSource) {
72         Assert.notNull(dataSource, "The [dataSource] argument cannot be null.");
73         this.classicJdbcTemplate = new JdbcTemplate(dataSource);
74     }
75
76     /**
77      * Create a new NamedParameterJdbcTemplate for the given classic
78      * Spring {@link org.springframework.jdbc.core.JdbcTemplate}.
79      * @param classicJdbcTemplate the classic Spring JdbcTemplate to wrap
80      */

81     public NamedParameterJdbcTemplate(JdbcOperations classicJdbcTemplate) {
82         Assert.notNull(classicJdbcTemplate, "The [classicJdbcTemplate] argument cannot be null.");
83         this.classicJdbcTemplate = classicJdbcTemplate;
84     }
85
86     /**
87      * Expose the classic Spring JdbcTemplate to allow invocation of
88      * less commonly used methods.
89      */

90     public JdbcOperations getJdbcOperations() {
91         return this.classicJdbcTemplate;
92     }
93
94
95     //-------------------------------------------------------------------------
96
// Query operations
97
//-------------------------------------------------------------------------
98

99     public void query(String JavaDoc sql, SqlParameterSource paramSource, RowCallbackHandler rch)
100             throws DataAccessException {
101
102         ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
103         Object JavaDoc[] args = NamedParameterUtils.buildValueArray(parsedSql, paramSource);
104         int[] argTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, paramSource);
105         String JavaDoc sqlToUse = NamedParameterUtils.substituteNamedParameters(sql, paramSource);
106         getJdbcOperations().query(sqlToUse, args, argTypes, rch);
107     }
108
109     public void query(String JavaDoc sql, Map JavaDoc paramMap, RowCallbackHandler rch) throws DataAccessException {
110         query(sql, new MapSqlParameterSource(paramMap), rch);
111     }
112
113     public List JavaDoc query(String JavaDoc sql, SqlParameterSource paramSource, RowMapper rowMapper)
114             throws DataAccessException {
115
116         ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
117         Object JavaDoc[] args = NamedParameterUtils.buildValueArray(parsedSql, paramSource);
118         int[] argTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, paramSource);
119         String JavaDoc sqlToUse = NamedParameterUtils.substituteNamedParameters(sql, paramSource);
120         return (List JavaDoc) getJdbcOperations().query(
121                 sqlToUse, args, argTypes, new RowMapperResultSetExtractor(rowMapper));
122     }
123
124     public List JavaDoc query(String JavaDoc sql, Map JavaDoc paramMap, RowMapper rowMapper) throws DataAccessException {
125         return query(sql, new MapSqlParameterSource(paramMap), rowMapper);
126     }
127
128     public Object JavaDoc queryForObject(String JavaDoc sql, SqlParameterSource paramSource, RowMapper rowMapper)
129             throws DataAccessException {
130
131         ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
132         Object JavaDoc[] args = NamedParameterUtils.buildValueArray(parsedSql, paramSource);
133         int[] argTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, paramSource);
134         String JavaDoc sqlToUse = NamedParameterUtils.substituteNamedParameters(sql, paramSource);
135         List JavaDoc results = (List JavaDoc) getJdbcOperations().query(
136                 sqlToUse, args, argTypes, new RowMapperResultSetExtractor(rowMapper, 1));
137         return DataAccessUtils.requiredSingleResult(results);
138     }
139
140     public Object JavaDoc queryForObject(String JavaDoc sql, Map JavaDoc paramMap, RowMapper rowMapper) throws DataAccessException {
141         return queryForObject(sql, new MapSqlParameterSource(paramMap), rowMapper);
142     }
143
144     public Object JavaDoc queryForObject(String JavaDoc sql, SqlParameterSource paramSource, Class JavaDoc requiredType)
145             throws DataAccessException {
146         return queryForObject(sql, paramSource, new SingleColumnRowMapper(requiredType));
147     }
148
149     public Object JavaDoc queryForObject(String JavaDoc sql, Map JavaDoc paramMap, Class JavaDoc requiredType) throws DataAccessException {
150         return queryForObject(sql, paramMap, new SingleColumnRowMapper(requiredType));
151     }
152
153     public Map JavaDoc queryForMap(String JavaDoc sql, SqlParameterSource paramSource) throws DataAccessException {
154         return (Map JavaDoc) queryForObject(sql, paramSource, new ColumnMapRowMapper());
155     }
156
157     public Map JavaDoc queryForMap(String JavaDoc sql, Map JavaDoc paramMap) throws DataAccessException {
158         return (Map JavaDoc) queryForObject(sql, paramMap, new ColumnMapRowMapper());
159     }
160
161     public long queryForLong(String JavaDoc sql, SqlParameterSource paramSource) throws DataAccessException {
162         Number JavaDoc number = (Number JavaDoc) queryForObject(sql, paramSource, Number JavaDoc.class);
163         return (number != null ? number.longValue() : 0);
164     }
165
166     public long queryForLong(String JavaDoc sql, Map JavaDoc paramMap) throws DataAccessException {
167         return queryForLong(sql, new MapSqlParameterSource(paramMap));
168     }
169
170     public int queryForInt(String JavaDoc sql, SqlParameterSource paramSource) throws DataAccessException {
171         Number JavaDoc number = (Number JavaDoc) queryForObject(sql, paramSource, Number JavaDoc.class);
172         return (number != null ? number.intValue() : 0);
173     }
174
175     public int queryForInt(String JavaDoc sql, Map JavaDoc paramMap) throws DataAccessException {
176         return queryForInt(sql, new MapSqlParameterSource(paramMap));
177     }
178
179     public List JavaDoc queryForList(String JavaDoc sql, SqlParameterSource paramSource, Class JavaDoc elementType)
180             throws DataAccessException {
181         return query(sql, paramSource, new SingleColumnRowMapper(elementType));
182     }
183
184     public List JavaDoc queryForList(String JavaDoc sql, Map JavaDoc paramMap, Class JavaDoc elementType) throws DataAccessException {
185         return queryForList(sql, new MapSqlParameterSource(paramMap), elementType);
186     }
187
188     public List JavaDoc queryForList(String JavaDoc sql, SqlParameterSource paramSource) throws DataAccessException {
189         return query(sql, paramSource, new ColumnMapRowMapper());
190     }
191
192     public List JavaDoc queryForList(String JavaDoc sql, Map JavaDoc paramMap) throws DataAccessException {
193         return queryForList(sql, new MapSqlParameterSource(paramMap));
194     }
195
196     public SqlRowSet queryForRowSet(String JavaDoc sql, SqlParameterSource paramSource) throws DataAccessException {
197         ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
198         Object JavaDoc[] args = NamedParameterUtils.buildValueArray(parsedSql, paramSource);
199         int[] argTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, paramSource);
200         String JavaDoc sqlToUse = NamedParameterUtils.substituteNamedParameters(sql, paramSource);
201         return (SqlRowSet) getJdbcOperations().query(sqlToUse, args, argTypes, new SqlRowSetResultSetExtractor());
202     }
203
204     public SqlRowSet queryForRowSet(String JavaDoc sql, Map JavaDoc paramMap) throws DataAccessException {
205         return queryForRowSet(sql, new MapSqlParameterSource(paramMap));
206     }
207
208
209     //-------------------------------------------------------------------------
210
// Update operations
211
//-------------------------------------------------------------------------
212

213     public int update(String JavaDoc sql, SqlParameterSource paramSource) throws DataAccessException {
214         ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
215         Object JavaDoc[] args = NamedParameterUtils.buildValueArray(parsedSql, paramSource);
216         int[] argTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, paramSource);
217         String JavaDoc sqlToUse = NamedParameterUtils.substituteNamedParameters(sql, paramSource);
218         return getJdbcOperations().update(sqlToUse, args, argTypes);
219     }
220
221     public int update(String JavaDoc sql, Map JavaDoc paramMap) throws DataAccessException {
222         return update(sql, new MapSqlParameterSource(paramMap));
223     }
224
225     public int update(String JavaDoc sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder)
226             throws DataAccessException {
227
228         return update(sql, paramSource, generatedKeyHolder, null);
229     }
230
231     public int update(
232             String JavaDoc sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder, String JavaDoc[] keyColumnNames)
233             throws DataAccessException {
234
235         ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
236         Object JavaDoc[] args = NamedParameterUtils.buildValueArray(parsedSql, paramSource);
237         int[] argTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, paramSource);
238         String JavaDoc sqlToUse = NamedParameterUtils.substituteNamedParameters(sql, paramSource);
239         PreparedStatementCreatorFactory pscf = new PreparedStatementCreatorFactory(sqlToUse, argTypes);
240         if (keyColumnNames != null) {
241             pscf.setGeneratedKeysColumnNames(keyColumnNames);
242         }
243         else {
244             pscf.setReturnGeneratedKeys(true);
245         }
246         return getJdbcOperations().update(pscf.newPreparedStatementCreator(args), generatedKeyHolder);
247     }
248
249 }
250
Popular Tags