1 /***
2 * The contents of this file are subject to the Mozilla Public
3 * License Version 1.1 (the "License"); you may not use this file
4 * except in compliance with the License. You may obtain a copy of
5 * the License at http://www.mozilla.org/MPL/
6 *
7 * Software distributed under the License is distributed on an "AS
8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 * implied. See the License for the specific language governing
10 * rights and limitations under the License.
11 *
12 * The Original Code is pow2toolkit library.
13 *
14 * The Initial Owner of the Original Code is
15 * Power Of Two S.R.L. (www.pow2.com)
16 *
17 * Portions created by Power Of Two S.R.L. are
18 * Copyright (C) Power Of Two S.R.L.
19 * All Rights Reserved.
20 *
21 * Contributor(s):
22 */
23
24 package com.pow2.dao.datalist;
25
26 import java.sql.*;
27
28 import org.apache.log4j.*;
29
30 import com.pow2.dao.AbstractDAO;
31
32
33
34 /***
35 * Query data access object class.
36 *
37 * @author Luca Fossato
38 * @created 7 dicembre 2002
39 */
40 public class QueryDAO extends AbstractDAO
41 {
42 /*** Log4J category. */
43 protected static Category cat = Category.getInstance(QueryDAO.class);
44
45 /*** an handle to the unique UserDAO instance. */
46 private static QueryDAO instance = null;
47
48 /***
49 * Default constructor.
50 */
51 protected QueryDAO()
52 {
53 super();
54 }
55
56
57 /***
58 * Get the unique instance of QueryDAO class.
59 *
60 * @return the instance of QueryDAO class
61 */
62 public static synchronized QueryDAO instance()
63 {
64 if (instance == null)
65 instance = new QueryDAO();
66
67 return instance;
68 }
69
70
71 /***
72 * Execute the input query and return a ResultSetDataList object
73 * that incapsulates the resultSet.
74 *
75 * @param query the Query object
76 * @param rowMapper the ResultSetRowMapper object
77 * @return a ResultSetDataList object
78 * @exception Exception if any error occurs
79 */
80 public ResultSetDataList executeQuery(Query query,
81 ResultSetRowMapper rowMapper)
82 throws Exception
83 {
84 boolean closeResources = false;
85 Statement st = null;
86 ResultSet rs = null;
87 Connection con = null;
88 ResultSetDataList rsDataList = null;
89
90 String q = query.toString();
91
92
93
94
95
96
97 try
98 {
99 con = getConnection();
100 st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
101 rs = st.executeQuery(q);
102 rsDataList = new ResultSetDataListImpl(rowMapper, con, st, rs);
103 }
104 catch(SQLException e)
105 {
106
107
108 closeResources = true;
109 throw e;
110 }
111 finally
112 {
113
114
115 if (closeResources)
116 closeResources(rs, st, con, false);
117 }
118
119
120 return rsDataList;
121 }
122 }