KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > dbcp > DelegatingStatement


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
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.apache.commons.dbcp;
18
19 import java.sql.Connection JavaDoc;
20 import java.sql.ResultSet JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.sql.SQLWarning JavaDoc;
23 import java.sql.Statement JavaDoc;
24 import java.util.List JavaDoc;
25
26 /**
27  * A base delegating implementation of {@link Statement}.
28  * <p>
29  * All of the methods from the {@link Statement} interface
30  * simply check to see that the {@link Statement} is active,
31  * and call the corresponding method on the "delegate"
32  * provided in my constructor.
33  * <p>
34  * Extends AbandonedTrace to implement Statement tracking and
35  * logging of code which created the Statement. Tracking the
36  * Statement ensures that the Connection which created it can
37  * close any open Statement's on Connection close.
38  *
39  * @author Rodney Waldhoff
40  * @author Glenn L. Nielsen
41  * @author James House
42  * @author Dirk Verbeeck
43  * @version $Revision: 1.17 $ $Date: 2004/03/06 13:35:31 $
44  */

45 public class DelegatingStatement extends AbandonedTrace implements Statement JavaDoc {
46     /** My delegate. */
47     protected Statement JavaDoc _stmt = null;
48     /** The connection that created me. **/
49     protected DelegatingConnection _conn = null;
50
51     /**
52      * Create a wrapper for the Statement which traces this
53      * Statement to the Connection which created it and the
54      * code which created it.
55      *
56      * @param s the {@link Statement} to delegate all calls to.
57      * @param c the {@link DelegatingConnection} that created this statement.
58      */

59     public DelegatingStatement(DelegatingConnection c, Statement JavaDoc s) {
60         super(c);
61         _stmt = s;
62         _conn = c;
63     }
64
65     /**
66      * Returns my underlying {@link Statement}.
67      * @return my underlying {@link Statement}.
68      * @see #getInnermostDelegate
69      */

70     public Statement JavaDoc getDelegate() {
71         return _stmt;
72     }
73
74     public boolean equals(Object JavaDoc obj) {
75         Statement JavaDoc delegate = getInnermostDelegate();
76         if (delegate == null) {
77             return false;
78         }
79         if (obj instanceof DelegatingStatement) {
80             DelegatingStatement s = (DelegatingStatement) obj;
81             return delegate.equals(s.getInnermostDelegate());
82         }
83         else {
84             return delegate.equals(obj);
85         }
86     }
87
88     public int hashCode() {
89         Object JavaDoc obj = getInnermostDelegate();
90         if (obj == null) {
91             return 0;
92         }
93         return obj.hashCode();
94     }
95     
96     /**
97      * If my underlying {@link Statement} is not a
98      * <tt>DelegatingStatement</tt>, returns it,
99      * otherwise recursively invokes this method on
100      * my delegate.
101      * <p>
102      * Hence this method will return the first
103      * delegate that is not a <tt>DelegatingStatement</tt>
104      * or <tt>null</tt> when no non-<tt>DelegatingStatement</tt>
105      * delegate can be found by transversing this chain.
106      * <p>
107      * This method is useful when you may have nested
108      * <tt>DelegatingStatement</tt>s, and you want to make
109      * sure to obtain a "genuine" {@link Statement}.
110      * @see #getDelegate
111      */

112     public Statement JavaDoc getInnermostDelegate() {
113         Statement JavaDoc s = _stmt;
114         while(s != null && s instanceof DelegatingStatement) {
115             s = ((DelegatingStatement)s).getDelegate();
116             if(this == s) {
117                 return null;
118             }
119         }
120         return s;
121     }
122
123     /** Sets my delegate. */
124     public void setDelegate(Statement JavaDoc s) {
125         _stmt = s;
126     }
127
128     protected boolean _closed = false;
129
130     protected boolean isClosed() {
131         return _closed;
132     }
133
134     protected void checkOpen() throws SQLException JavaDoc {
135         if(isClosed()) {
136             throw new SQLException JavaDoc(this.getClass().getName() + " is closed.");
137         }
138     }
139
140     /**
141      * Close this DelegatingStatement, and close
142      * any ResultSets that were not explicitly closed.
143      */

144     public void close() throws SQLException JavaDoc {
145         try {
146             try {
147                 if (_conn != null) {
148                     _conn.removeTrace(this);
149                     _conn = null;
150                 }
151         
152                 // The JDBC spec requires that a statment close any open
153
// ResultSet's when it is closed.
154
// FIXME The PreparedStatement we're wrapping should handle this for us.
155
// See bug 17301 for what could happen when ResultSets are closed twice.
156
List JavaDoc resultSets = getTrace();
157                 if( resultSets != null) {
158                     ResultSet JavaDoc[] set = (ResultSet JavaDoc[]) resultSets.toArray(new ResultSet JavaDoc[resultSets.size()]);
159                     for (int i = 0; i < set.length; i++) {
160                         set[i].close();
161                     }
162                     clearTrace();
163                 }
164         
165                 _stmt.close();
166             }
167             catch (SQLException JavaDoc e) {
168                 handleException(e);
169             }
170         }
171         finally {
172             _closed = true;
173         }
174     }
175
176     protected void handleException(SQLException JavaDoc e) throws SQLException JavaDoc {
177         if (_conn != null) {
178             _conn.handleException(e);
179         }
180         else {
181             throw e;
182         }
183     }
184
185     protected void activate() throws SQLException JavaDoc {
186         if(_stmt instanceof DelegatingStatement) {
187             ((DelegatingStatement)_stmt).activate();
188         }
189     }
190
191     protected void passivate() throws SQLException JavaDoc {
192         if(_stmt instanceof DelegatingStatement) {
193             ((DelegatingStatement)_stmt).passivate();
194         }
195     }
196
197     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
198         checkOpen();
199         return _conn; // return the delegating connection that created this
200
}
201
202     public ResultSet JavaDoc executeQuery(String JavaDoc sql) throws SQLException JavaDoc {
203         checkOpen();
204         try {
205             return DelegatingResultSet.wrapResultSet(this,_stmt.executeQuery(sql));
206         }
207         catch (SQLException JavaDoc e) {
208             handleException(e);
209             return null;
210         }
211     }
212
213     public ResultSet JavaDoc getResultSet() throws SQLException JavaDoc {
214         checkOpen();
215         try {
216             return DelegatingResultSet.wrapResultSet(this,_stmt.getResultSet());
217         }
218         catch (SQLException JavaDoc e) {
219             handleException(e);
220             return null;
221         }
222     }
223
224     public int executeUpdate(String JavaDoc sql) throws SQLException JavaDoc
225     { checkOpen(); try { return _stmt.executeUpdate(sql); } catch (SQLException JavaDoc e) { handleException(e); return 0; } }
226
227     public int getMaxFieldSize() throws SQLException JavaDoc
228     { checkOpen(); try { return _stmt.getMaxFieldSize(); } catch (SQLException JavaDoc e) { handleException(e); return 0; } }
229
230     public void setMaxFieldSize(int max) throws SQLException JavaDoc
231     { checkOpen(); try { _stmt.setMaxFieldSize(max); } catch (SQLException JavaDoc e) { handleException(e); } }
232
233     public int getMaxRows() throws SQLException JavaDoc
234     { checkOpen(); try { return _stmt.getMaxRows(); } catch (SQLException JavaDoc e) { handleException(e); return 0; } }
235
236     public void setMaxRows(int max) throws SQLException JavaDoc
237     { checkOpen(); try { _stmt.setMaxRows(max); } catch (SQLException JavaDoc e) { handleException(e); } }
238
239     public void setEscapeProcessing(boolean enable) throws SQLException JavaDoc
240     { checkOpen(); try { _stmt.setEscapeProcessing(enable); } catch (SQLException JavaDoc e) { handleException(e); } }
241
242     public int getQueryTimeout() throws SQLException JavaDoc
243     { checkOpen(); try { return _stmt.getQueryTimeout(); } catch (SQLException JavaDoc e) { handleException(e); return 0; } }
244
245     public void setQueryTimeout(int seconds) throws SQLException JavaDoc
246     { checkOpen(); try { _stmt.setQueryTimeout(seconds); } catch (SQLException JavaDoc e) { handleException(e); } }
247
248     public void cancel() throws SQLException JavaDoc
249     { checkOpen(); try { _stmt.cancel(); } catch (SQLException JavaDoc e) { handleException(e); } }
250
251     public SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc
252     { checkOpen(); try { return _stmt.getWarnings(); } catch (SQLException JavaDoc e) { handleException(e); return null; } }
253
254     public void clearWarnings() throws SQLException JavaDoc
255     { checkOpen(); try { _stmt.clearWarnings(); } catch (SQLException JavaDoc e) { handleException(e); } }
256
257     public void setCursorName(String JavaDoc name) throws SQLException JavaDoc
258     { checkOpen(); try { _stmt.setCursorName(name); } catch (SQLException JavaDoc e) { handleException(e); } }
259
260     public boolean execute(String JavaDoc sql) throws SQLException JavaDoc
261     { checkOpen(); try { return _stmt.execute(sql); } catch (SQLException JavaDoc e) { handleException(e); return false; } }
262
263     public int getUpdateCount() throws SQLException JavaDoc
264     { checkOpen(); try { return _stmt.getUpdateCount(); } catch (SQLException JavaDoc e) { handleException(e); return 0; } }
265
266     public boolean getMoreResults() throws SQLException JavaDoc
267     { checkOpen(); try { return _stmt.getMoreResults(); } catch (SQLException JavaDoc e) { handleException(e); return false; } }
268
269     public void setFetchDirection(int direction) throws SQLException JavaDoc
270     { checkOpen(); try { _stmt.setFetchDirection(direction); } catch (SQLException JavaDoc e) { handleException(e); } }
271
272     public int getFetchDirection() throws SQLException JavaDoc
273     { checkOpen(); try { return _stmt.getFetchDirection(); } catch (SQLException JavaDoc e) { handleException(e); return 0; } }
274
275     public void setFetchSize(int rows) throws SQLException JavaDoc
276     { checkOpen(); try { _stmt.setFetchSize(rows); } catch (SQLException JavaDoc e) { handleException(e); } }
277
278     public int getFetchSize() throws SQLException JavaDoc
279     { checkOpen(); try { return _stmt.getFetchSize(); } catch (SQLException JavaDoc e) { handleException(e); return 0; } }
280
281     public int getResultSetConcurrency() throws SQLException JavaDoc
282     { checkOpen(); try { return _stmt.getResultSetConcurrency(); } catch (SQLException JavaDoc e) { handleException(e); return 0; } }
283
284     public int getResultSetType() throws SQLException JavaDoc
285     { checkOpen(); try { return _stmt.getResultSetType(); } catch (SQLException JavaDoc e) { handleException(e); return 0; } }
286
287     public void addBatch(String JavaDoc sql) throws SQLException JavaDoc
288     { checkOpen(); try { _stmt.addBatch(sql); } catch (SQLException JavaDoc e) { handleException(e); } }
289
290     public void clearBatch() throws SQLException JavaDoc
291     { checkOpen(); try { _stmt.clearBatch(); } catch (SQLException JavaDoc e) { handleException(e); } }
292
293     public int[] executeBatch() throws SQLException JavaDoc
294     { checkOpen(); try { return _stmt.executeBatch(); } catch (SQLException JavaDoc e) { handleException(e); return null; } }
295
296     // ------------------- JDBC 3.0 -----------------------------------------
297
// Will be commented by the build process on a JDBC 2.0 system
298

299 /* JDBC_3_ANT_KEY_BEGIN */
300
301     public boolean getMoreResults(int current) throws SQLException JavaDoc
302     { checkOpen(); try { return _stmt.getMoreResults(current); } catch (SQLException JavaDoc e) { handleException(e); return false; } }
303
304     public ResultSet JavaDoc getGeneratedKeys() throws SQLException JavaDoc
305     { checkOpen(); try { return _stmt.getGeneratedKeys(); } catch (SQLException JavaDoc e) { handleException(e); return null; } }
306
307     public int executeUpdate(String JavaDoc sql, int autoGeneratedKeys) throws SQLException JavaDoc
308     { checkOpen(); try { return _stmt.executeUpdate(sql, autoGeneratedKeys); } catch (SQLException JavaDoc e) { handleException(e); return 0; } }
309
310     public int executeUpdate(String JavaDoc sql, int columnIndexes[]) throws SQLException JavaDoc
311     { checkOpen(); try { return _stmt.executeUpdate(sql, columnIndexes); } catch (SQLException JavaDoc e) { handleException(e); return 0; } }
312
313     public int executeUpdate(String JavaDoc sql, String JavaDoc columnNames[]) throws SQLException JavaDoc
314     { checkOpen(); try { return _stmt.executeUpdate(sql, columnNames); } catch (SQLException JavaDoc e) { handleException(e); return 0; } }
315
316     public boolean execute(String JavaDoc sql, int autoGeneratedKeys) throws SQLException JavaDoc
317     { checkOpen(); try { return _stmt.execute(sql, autoGeneratedKeys); } catch (SQLException JavaDoc e) { handleException(e); return false; } }
318
319     public boolean execute(String JavaDoc sql, int columnIndexes[]) throws SQLException JavaDoc
320     { checkOpen(); try { return _stmt.execute(sql, columnIndexes); } catch (SQLException JavaDoc e) { handleException(e); return false; } }
321
322     public boolean execute(String JavaDoc sql, String JavaDoc columnNames[]) throws SQLException JavaDoc
323     { checkOpen(); try { return _stmt.execute(sql, columnNames); } catch (SQLException JavaDoc e) { handleException(e); return false; } }
324
325     public int getResultSetHoldability() throws SQLException JavaDoc
326     { checkOpen(); try { return _stmt.getResultSetHoldability(); } catch (SQLException JavaDoc e) { handleException(e); return 0; } }
327
328 /* JDBC_3_ANT_KEY_END */
329 }
330
Popular Tags