KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > dao > client > template > SqlMapDaoTemplate


1 /*
2  * Copyright 2004 Clinton Begin
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 package com.ibatis.dao.client.template;
17
18 import com.ibatis.common.util.PaginatedList;
19 import com.ibatis.dao.client.DaoException;
20 import com.ibatis.dao.client.DaoManager;
21 import com.ibatis.dao.engine.transaction.sqlmap.SqlMapDaoTransaction;
22 import com.ibatis.sqlmap.client.SqlMapExecutor;
23 import com.ibatis.sqlmap.client.SqlMapTransactionManager;
24 import com.ibatis.sqlmap.client.event.RowHandler;
25
26 import java.sql.SQLException JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29
30 /**
31  * A DaoTemplate for SQL Map implementations that provides a
32  * convenient method to access the SqlMapExecutor. This class
33  * also provides SqlMapExecutor method wrappers that conveniently
34  * wrap SQLExceptions with DAO Exceptions.
35  *
36  * @author Zach Scott
37  */

38 public abstract class SqlMapDaoTemplate extends DaoTemplate implements SqlMapExecutor {
39
40   /**
41    * The DaoManager that manages this Dao instance will be passed
42    * in as the parameter to this constructor automatically upon
43    * instantiation.
44    *
45    * @param daoManager
46    */

47   public SqlMapDaoTemplate(DaoManager daoManager) {
48     super(daoManager);
49   }
50
51   /**
52    * Gets the SQL Map Executor associated with the current
53    * DaoTransaction that this Dao is working under. The SqlMapExecutor
54    * interface declares a number of methods for executing statements
55    * via an SqlMapClient instance.
56    *
57    * @return A SqlMapExecutor instance.
58    */

59   protected SqlMapExecutor getSqlMapExecutor() {
60     SqlMapDaoTransaction trans = (SqlMapDaoTransaction) daoManager.getTransaction(this);
61     return trans.getSqlMap();
62   }
63
64   /**
65    * Gets the SQL Map Transaction Manager associated with the current
66    * DaoTransaction that this Dao is working under. The SqlMapExecutor
67    * interface declares a number of methods for executing statements
68    * via an SqlMapClient instance.
69    * <p/>
70    * NOTE: It is rare to require this in a DAO. Only very special
71    * cases of DAO implementations will require access to the
72    * SqlMapTransactionManager. Messing with transactions at this
73    * level might be dangerous to your data integrity (e.g. committing
74    * too early).
75    *
76    * @return A SqlMapTransactionManager instance.
77    */

78   protected SqlMapTransactionManager getSqlMapTransactionManager() {
79     SqlMapDaoTransaction trans = (SqlMapDaoTransaction) daoManager.getTransaction(this);
80     return trans.getSqlMap();
81   }
82
83   /**
84    * Executes a mapped SQL INSERT statement.
85    * Insert is a bit different from other update methods, as it
86    * provides facilities for returning the primary key of the
87    * newly inserted row (rather than the effected rows). This
88    * functionality is of course optional.
89    * <p/>
90    * The parameter object is generally used to supply the input
91    * data for the INSERT values.
92    *
93    * @param id The name of the statement to execute.
94    * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.).
95    * @return The primary key of the newly inserted row. This might be automatically
96    * generated by the RDBMS, or selected from a sequence table or other source.
97    */

98   public Object JavaDoc insert(String JavaDoc id, Object JavaDoc parameterObject) {
99     try {
100       return getSqlMapExecutor().insert(id, parameterObject);
101     } catch (SQLException JavaDoc e) {
102       throw new DaoException("Failed to insert - id ["
103           + id + "], parameterObject [" + parameterObject + "]. Cause: " + e, e);
104     }
105   }
106
107   /**
108    * Executes a mapped SQL UPDATE statement.
109    * Update can also be used for any other update statement type,
110    * such as inserts and deletes. Update returns the number of
111    * rows effected.
112    * <p/>
113    * The parameter object is generally used to supply the input
114    * data for the UPDATE values as well as the WHERE clause parameter(s).
115    *
116    * @param id The name of the statement to execute.
117    * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.).
118    * @return The number of rows effected.
119    */

120   public int update(String JavaDoc id, Object JavaDoc parameterObject) {
121     try {
122       return getSqlMapExecutor().update(id, parameterObject);
123     } catch (SQLException JavaDoc e) {
124       throw new DaoException("Failed to update - id ["
125           + id + "] - parameterObject [" + parameterObject + "]. Cause: " + e, e);
126     }
127   }
128
129   /**
130    * Executes a mapped SQL DELETE statement.
131    * Delete returns the number of rows effected.
132    * <p/>
133    * The parameter object is generally used to supply the input
134    * data for the WHERE clause parameter(s) of the DELETE statement.
135    *
136    * @param id The name of the statement to execute.
137    * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.).
138    * @return The number of rows effected.
139    */

140   public int delete(String JavaDoc id, Object JavaDoc parameterObject) {
141     try {
142       return getSqlMapExecutor().delete(id, parameterObject);
143     } catch (SQLException JavaDoc e) {
144       throw new DaoException("Failed to delete - id ["
145           + id + "] - parameterObject [" + parameterObject + "]. Cause: " + e, e);
146     }
147   }
148
149   /**
150    * Executes a mapped SQL SELECT statement that returns data to populate
151    * a single object instance.
152    * <p/>
153    * The parameter object is generally used to supply the input
154    * data for the WHERE clause parameter(s) of the SELECT statement.
155    *
156    * @param id The name of the statement to execute.
157    * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.).
158    * @return The single result object populated with the result set data.
159    */

160   public Object JavaDoc queryForObject(String JavaDoc id, Object JavaDoc parameterObject) {
161     try {
162       return getSqlMapExecutor().queryForObject(id, parameterObject);
163     } catch (SQLException JavaDoc e) {
164       throw new DaoException("Failed to execute queryForObject - id ["
165           + id + "], parameterObject [" + parameterObject + "]. Cause: " + e, e);
166     }
167   }
168
169   /**
170    * Executes a mapped SQL SELECT statement that returns data to populate
171    * the supplied result object.
172    * <p/>
173    * The parameter object is generally used to supply the input
174    * data for the WHERE clause parameter(s) of the SELECT statement.
175    *
176    * @param id The name of the statement to execute.
177    * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.).
178    * @param resultObject The result object instance that should be populated with result data.
179    * @return The single result object as supplied by the resultObject parameter, populated with the result set data.
180    */

181   public Object JavaDoc queryForObject(String JavaDoc id, Object JavaDoc parameterObject, Object JavaDoc resultObject) {
182     try {
183       return getSqlMapExecutor().queryForObject(id, parameterObject, resultObject);
184     } catch (SQLException JavaDoc e) {
185       throw new DaoException("Failed to queryForObject - id ["
186           + id + "], parameterObject [" + parameterObject + "]. Cause: " + e, e);
187     }
188   }
189
190   /**
191    * Executes a mapped SQL SELECT statement that returns data to populate
192    * a number of result objects.
193    * <p/>
194    * The parameter object is generally used to supply the input
195    * data for the WHERE clause parameter(s) of the SELECT statement.
196    *
197    * @param id The name of the statement to execute.
198    * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.).
199    * @return A List of result objects.
200    */

201   public List queryForList(String JavaDoc id, Object JavaDoc parameterObject) {
202     try {
203       return getSqlMapExecutor().queryForList(id, parameterObject);
204     } catch (SQLException JavaDoc e) {
205       throw new DaoException("Failed to queryForList - id ["
206           + id + "], parameterObject [" + parameterObject + "]. Cause: " + e, e);
207     }
208   }
209
210   /**
211    * Executes a mapped SQL SELECT statement that returns data to populate
212    * a number of result objects within a certain range.
213    * <p/>
214    * The parameter object is generally used to supply the input
215    * data for the WHERE clause parameter(s) of the SELECT statement.
216    *
217    * @param id The name of the statement to execute.
218    * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.).
219    * @param skip The number of results to ignore.
220    * @param max The maximum number of results to return.
221    * @return A List of result objects.
222    */

223   public List queryForList(String JavaDoc id, Object JavaDoc parameterObject, int skip, int max) {
224     try {
225       return getSqlMapExecutor().queryForList(id, parameterObject, skip, max);
226     } catch (SQLException JavaDoc e) {
227       throw new DaoException("Failed to queryForList - id ["
228           + id + "], parameterObject [" + parameterObject + "], skip ["
229           + skip + "], max [" + max + "]. Cause: " + e, e);
230     }
231   }
232
233   /**
234    * Executes a mapped SQL SELECT statement that returns data to populate
235    * a number of result objects that will be handled one at a time by a
236    * RowHandler.
237    * <p/>
238    * This is generally a good approach to take when dealing with large sets
239    * of records (i.e. hundreds, thousands...) that need to be processed without
240    * eating up all of the system resources.
241    * <p/>
242    * The parameter object is generally used to supply the input
243    * data for the WHERE clause parameter(s) of the SELECT statement.
244    *
245    * @param id The name of the statement to execute.
246    * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.).
247    * @param rowHandler A RowHandler instance
248    */

249   public void queryWithRowHandler(String JavaDoc id, Object JavaDoc parameterObject, RowHandler rowHandler) {
250     try {
251       getSqlMapExecutor().queryWithRowHandler(id, parameterObject, rowHandler);
252     } catch (SQLException JavaDoc e) {
253       throw new DaoException("Failed to queryForList - id [" + id + "], parameterObject ["
254           + parameterObject + "], rowHandler [ " + rowHandler + "]. Cause: " + e, e);
255     }
256   }
257
258   /**
259    * Executes a mapped SQL SELECT statement that returns data to populate
260    * a number of result objects a page at a time.
261    * <p/>
262    * The parameter object is generally used to supply the input
263    * data for the WHERE clause parameter(s) of the SELECT statement.
264    *
265    * @param id The name of the statement to execute.
266    * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.).
267    * @param pageSize The maximum number of result objects each page can hold.
268    * @return A PaginatedList of result objects.
269    */

270   public PaginatedList queryForPaginatedList(String JavaDoc id, Object JavaDoc parameterObject, int pageSize) {
271     try {
272       return getSqlMapExecutor().queryForPaginatedList(id, parameterObject, pageSize);
273     } catch (SQLException JavaDoc e) {
274       throw new DaoException("Failed to queryForPaginatedList - id [" + id + "], parameterObject ["
275           + parameterObject + "], pageSize [" + pageSize + "]. Cause: " + e, e);
276     }
277   }
278
279   /**
280    * Executes a mapped SQL SELECT statement that returns data to populate
281    * a number of result objects that will be keyed into a Map.
282    * <p/>
283    * The parameter object is generally used to supply the input
284    * data for the WHERE clause parameter(s) of the SELECT statement.
285    *
286    * @param id The name of the statement to execute.
287    * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.).
288    * @param keyProp The property to be used as the key in the Map.
289    * @return A Map keyed by keyProp with values being the result object instance.
290    */

291   public Map JavaDoc queryForMap(String JavaDoc id, Object JavaDoc parameterObject, String JavaDoc keyProp) {
292     try {
293       return getSqlMapExecutor().queryForMap(id, parameterObject, keyProp);
294     } catch (SQLException JavaDoc e) {
295       throw new DaoException("Failed to queryForMap - id [" + id + "], parameterObject ["
296           + parameterObject + "], keyProp [" + keyProp + "]. Cause: " + e, e);
297     }
298   }
299
300   /**
301    * Executes a mapped SQL SELECT statement that returns data to populate
302    * a number of result objects from which one property will be keyed into a Map.
303    * <p/>
304    * The parameter object is generally used to supply the input
305    * data for the WHERE clause parameter(s) of the SELECT statement.
306    *
307    * @param id The name of the statement to execute.
308    * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.).
309    * @param keyProp The property to be used as the key in the Map.
310    * @param valueProp The property to be used as the value in the Map.
311    * @return A Map keyed by keyProp with values of valueProp.
312    */

313   public Map JavaDoc queryForMap(String JavaDoc id, Object JavaDoc parameterObject, String JavaDoc keyProp, String JavaDoc valueProp) {
314     try {
315       return getSqlMapExecutor().queryForMap(id, parameterObject, keyProp, valueProp);
316     } catch (SQLException JavaDoc e) {
317       throw new DaoException("Failed to queryForMap - id [" + id + "], parameterObject ["
318           + parameterObject + "], keyProp [" + keyProp + "], valueProp ["
319           + valueProp + "]. Cause: " + e, e);
320     }
321   }
322
323   /**
324    * Starts a batch in which update statements will be cached before being sent to
325    * the database all at once. This can improve overall performance of updates update
326    * when dealing with numerous updates (e.g. inserting 1:M related data).
327    */

328   public void startBatch() {
329     try {
330       getSqlMapExecutor().startBatch();
331     } catch (SQLException JavaDoc e) {
332       throw new DaoException("Failed to startBatch. Cause: " + e, e);
333     }
334   }
335
336   /**
337    * Executes (flushes) all statements currently batched.
338    */

339   public int executeBatch() {
340     try {
341       return getSqlMapExecutor().executeBatch();
342     } catch (SQLException JavaDoc e) {
343       throw new DaoException("Failed to executeBatch. Cause: " + e, e);
344     }
345   }
346
347 }
348
Popular Tags