KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > teamkonzept > db > TKExtendedPrepQuery


1 /*
2  * $Header: /cvsroot/webman-cms/source/webman/com/teamkonzept/db/TKExtendedPrepQuery.java,v 1.8 2002/02/16 15:37:07 alex Exp $
3  *
4  */

5 package com.teamkonzept.db;
6
7 import java.sql.*;
8 import java.util.Vector JavaDoc;
9 import java.util.Stack JavaDoc;
10 import java.util.Enumeration JavaDoc;
11 import java.util.NoSuchElementException JavaDoc;
12
13 import com.teamkonzept.lib.*;
14 import com.teamkonzept.webman.db.TKWebmanDBManager;
15 import com.teamkonzept.webman.mainint.WebmanExceptionHandler;
16
17 import org.apache.log4j.Category;
18
19 /**
20  * This class represents queries that allow parameters to be set.
21  * It is primary used to construct queries that will be executed
22  * seperately, that is, it is up to the implementor of query classes
23  * to provided sort of a workflow for all queries. <br>
24  * All subclasses must implement method <tt>execute()</tt> where the
25  * <code>TKQuery</code> should be used to set and retrieve
26  * values of all queries (and subclasses of it).
27  * @see com.teamkonzept.db.TKPrepQuery
28  * @see java.sql.PreparedStatement
29  *
30  * @author $Author: alex $
31  * @version $Revision: 1.8 $
32 */

33 public abstract class TKExtendedPrepQuery extends TKPrepQuery {
34
35     /** Logging Category */
36     static Category cat = Category.getInstance(TKExtendedPrepQuery.class.getName());
37
38     /**
39      * Contains all result set produced by this query class.
40      */

41     protected Vector JavaDoc results = null;
42
43     /**
44      * Holds all atomic queries.
45      */

46     protected TKPrepQuery[] queries = null;
47
48     /**
49      * Holds classes passed as argument to method <tt>init(Class[])</tt>.
50      * Used for caching.
51      */

52     private Class JavaDoc[] prevClasses = null;
53
54     /**
55      * Adds a result set in order to reuse it (e.g. <tt>fetchResultSet()</tt>).
56      * @param rs das ResultSet was dazu soll
57      */

58     protected void addResult(ResultSet rs) {
59       if (rs != null) {
60         if (results == null) {
61           results = new Vector JavaDoc();
62         }
63         //results.push(rs);
64
results.addElement(rs);
65       }
66     }
67
68     /**
69      * Adds one or more result set(s) in order to reuse it (e.g. <tt>fetchResultSet()</tt>).
70      */

71     protected void addResults(ResultSet[] rs) {
72       if (rs != null) {
73         if (results == null) {
74           results = new Vector JavaDoc();
75         }
76           for (int i = 0; i < rs.length; i++) {
77             //results.push(rs);
78
results.addElement(rs);
79           }
80       }
81     }
82
83     /**
84      * This method is intended to be used for execution of multiple queries that
85      * may depend on each other. Since this method has been declared abstract
86      * all subclasses must provide an implementation and should control the
87      * logic by means of handling all dependencies of subsequent queries.
88      */

89     public abstract boolean execute() throws SQLException;
90
91     /**
92      * Initializes query objects as given in an array of classes. Note
93      * that the classes objects should be an instance of TKPrepQuery.
94      */

95     protected void init(Class JavaDoc[] classes) throws SQLException, ClassCastException JavaDoc {
96       if (classes == null) {
97         return;
98       }
99       if (prevClasses == null || !classes.equals(prevClasses)) {
100         prevClasses = classes;
101           queries = new TKPrepQuery[classes.length];
102           for (int i = 0; i < classes.length; i++) {
103             queries[i] = (TKPrepQuery) TKWebmanDBManager.newQuery(classes[i]);
104           }
105       }
106     }
107
108     /**
109      * Returns the current result set. Multiple calls of this
110      * method will return subsequent result sets.
111      * @return the current result set or <tt>null</tt> if no more
112      * result sets are available.
113      */

114      public ResultSet fetchResultSet() {
115        if (results != null && results.size() > 0) { // if we have at least one element
116
ResultSet tmp = (ResultSet) results.elementAt(0);
117           results.removeElementAt(0);
118           return tmp;
119        }
120        return null;
121      }
122
123     /**
124      * Returns <code>true</code> if this (complex) query expects one
125      * or more results, <code>false</code> otherwise. That is, if at least
126      * one ResultSet has been added (with methods <code>addResult(ResultSet)</code>
127      * or <code>addResults(ResultSet[])</code>, respectively.
128      */

129     protected boolean hasResults() {
130         return results != null && results.size() > 0;
131     }
132
133     /**
134      * Prints all parameters and type information stored in <code>queryParams</code>
135      * for each query to a <code>TKLog</code> stream only if
136      * <code>TKQuery.DEBUG</code> is set to true.
137      */

138     protected void debugParams()
139     {
140         if (cat.isDebugEnabled())
141         {
142         if (paramOrder == null) {
143           cat.debug("paramOrder is null!");
144           return;
145         }
146         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("Parameters of class ");
147         buf.append(getClass().getName());
148         String JavaDoc currentParamName = null;
149         Object JavaDoc currentParam = null;
150         buf.append(": {");
151         for (int i = 0; i < paramOrder.length; i++) {
152           currentParamName = paramOrder[i];
153           currentParam = queryParams.get(currentParamName);
154           buf.append(currentParamName);
155           buf.append("=");
156           buf.append(currentParam);
157           buf.append(" [type: ");
158           buf.append(currentParam.getClass().getName());
159           buf.append("]");
160           if (i < (paramOrder.length - 1)) {
161             buf.append(", ");
162           }
163         }
164         buf.append("}");
165         cat.debug(buf.toString());
166       }
167     }
168
169     /**
170      * Returns all <code>TKPrepQuery</code> objects.
171      */

172     protected Enumeration JavaDoc queries() {
173             return new Enumeration JavaDoc() {
174                 int count = 0;
175           int elementCount = queries.length;
176
177                 public boolean hasMoreElements() {
178                    return count < elementCount;
179                 }
180
181                 public Object JavaDoc nextElement() {
182                     if (count < elementCount) {
183                          return queries[count++].sqlString;
184             }
185                     throw new NoSuchElementException JavaDoc("Statement Enumeration");
186                 }
187             };
188     }
189
190     /**
191      * Closes all queries associated with this extended query.
192      * @see com.teamkonzept.db.TKPrepQuery.close()
193      */

194     public void close() throws SQLException {
195     if (queries == null) {
196         return;
197     }
198     cat.debug("queries to close: " + queries.length);
199     for (int i = 0; i < queries.length; i++) {
200         queries[i].close();
201     }
202     cat.debug("queries closed");
203     }
204 }
205
206
Popular Tags