View Javadoc

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      // TYPE_SCROLL_INSENSITIVE indicates the type for a ResultSet object
93      // that is scrollable but generally not sensitive to changes made by others.
94      //
95      // CONCUR_READ_ONLY indicates  the concurrency mode for a ResultSet object
96      // that may NOT be updated
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       // something goes wrong...
107       // finally clause must close the allocated resources;
108       closeResources = true;
109       throw e;
110     }
111     finally
112     {
113       // if any error occurs, close the allocated resources
114       // and rollback any open transaction;
115       if (closeResources)
116          closeResources(rs, st, con, false);
117     }
118 
119     // remember to use rsDataList.close() to free the allocated resources;
120     return rsDataList;
121   }
122 }