001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.dbcp;
019:
020: import java.math.BigDecimal;
021: import java.sql.Array;
022: import java.sql.Blob;
023: import java.sql.Clob;
024: import java.sql.PreparedStatement;
025: import java.sql.Ref;
026: import java.sql.ResultSet;
027: import java.sql.ResultSetMetaData;
028: import java.sql.SQLException;
029: import java.util.Calendar;
030:
031: /**
032: * A base delegating implementation of {@link PreparedStatement}.
033: * <p>
034: * All of the methods from the {@link PreparedStatement} interface
035: * simply check to see that the {@link PreparedStatement} is active,
036: * and call the corresponding method on the "delegate"
037: * provided in my constructor.
038: * <p>
039: * Extends AbandonedTrace to implement Statement tracking and
040: * logging of code which created the Statement. Tracking the
041: * Statement ensures that the Connection which created it can
042: * close any open Statement's on Connection close.
043: *
044: * @author Rodney Waldhoff
045: * @author Glenn L. Nielsen
046: * @author James House
047: * @author Dirk Verbeeck
048: * @version $Revision: 500687 $ $Date: 2007-01-27 16:33:47 -0700 (Sat, 27 Jan 2007) $
049: */
050: public class DelegatingPreparedStatement extends DelegatingStatement
051: implements PreparedStatement {
052:
053: /** My delegate. */
054: protected PreparedStatement _stmt = null;
055:
056: /**
057: * Create a wrapper for the Statement which traces this
058: * Statement to the Connection which created it and the
059: * code which created it.
060: *
061: * @param s the {@link PreparedStatement} to delegate all calls to.
062: * @param c the {@link DelegatingConnection} that created this statement.
063: */
064: public DelegatingPreparedStatement(DelegatingConnection c,
065: PreparedStatement s) {
066: super (c, s);
067: _stmt = s;
068: }
069:
070: public boolean equals(Object obj) {
071: PreparedStatement delegate = (PreparedStatement) getInnermostDelegate();
072: if (delegate == null) {
073: return false;
074: }
075: if (obj instanceof DelegatingPreparedStatement) {
076: DelegatingPreparedStatement s = (DelegatingPreparedStatement) obj;
077: return delegate.equals(s.getInnermostDelegate());
078: } else {
079: return delegate.equals(obj);
080: }
081: }
082:
083: /** Sets my delegate. */
084: public void setDelegate(PreparedStatement s) {
085: super .setDelegate(s);
086: _stmt = s;
087: }
088:
089: public ResultSet executeQuery() throws SQLException {
090: checkOpen();
091: try {
092: return DelegatingResultSet.wrapResultSet(this , _stmt
093: .executeQuery());
094: } catch (SQLException e) {
095: handleException(e);
096: return null;
097: }
098: }
099:
100: public int executeUpdate() throws SQLException {
101: checkOpen();
102: try {
103: return _stmt.executeUpdate();
104: } catch (SQLException e) {
105: handleException(e);
106: return 0;
107: }
108: }
109:
110: public void setNull(int parameterIndex, int sqlType)
111: throws SQLException {
112: checkOpen();
113: try {
114: _stmt.setNull(parameterIndex, sqlType);
115: } catch (SQLException e) {
116: handleException(e);
117: }
118: }
119:
120: public void setBoolean(int parameterIndex, boolean x)
121: throws SQLException {
122: checkOpen();
123: try {
124: _stmt.setBoolean(parameterIndex, x);
125: } catch (SQLException e) {
126: handleException(e);
127: }
128: }
129:
130: public void setByte(int parameterIndex, byte x) throws SQLException {
131: checkOpen();
132: try {
133: _stmt.setByte(parameterIndex, x);
134: } catch (SQLException e) {
135: handleException(e);
136: }
137: }
138:
139: public void setShort(int parameterIndex, short x)
140: throws SQLException {
141: checkOpen();
142: try {
143: _stmt.setShort(parameterIndex, x);
144: } catch (SQLException e) {
145: handleException(e);
146: }
147: }
148:
149: public void setInt(int parameterIndex, int x) throws SQLException {
150: checkOpen();
151: try {
152: _stmt.setInt(parameterIndex, x);
153: } catch (SQLException e) {
154: handleException(e);
155: }
156: }
157:
158: public void setLong(int parameterIndex, long x) throws SQLException {
159: checkOpen();
160: try {
161: _stmt.setLong(parameterIndex, x);
162: } catch (SQLException e) {
163: handleException(e);
164: }
165: }
166:
167: public void setFloat(int parameterIndex, float x)
168: throws SQLException {
169: checkOpen();
170: try {
171: _stmt.setFloat(parameterIndex, x);
172: } catch (SQLException e) {
173: handleException(e);
174: }
175: }
176:
177: public void setDouble(int parameterIndex, double x)
178: throws SQLException {
179: checkOpen();
180: try {
181: _stmt.setDouble(parameterIndex, x);
182: } catch (SQLException e) {
183: handleException(e);
184: }
185: }
186:
187: public void setBigDecimal(int parameterIndex, BigDecimal x)
188: throws SQLException {
189: checkOpen();
190: try {
191: _stmt.setBigDecimal(parameterIndex, x);
192: } catch (SQLException e) {
193: handleException(e);
194: }
195: }
196:
197: public void setString(int parameterIndex, String x)
198: throws SQLException {
199: checkOpen();
200: try {
201: _stmt.setString(parameterIndex, x);
202: } catch (SQLException e) {
203: handleException(e);
204: }
205: }
206:
207: public void setBytes(int parameterIndex, byte[] x)
208: throws SQLException {
209: checkOpen();
210: try {
211: _stmt.setBytes(parameterIndex, x);
212: } catch (SQLException e) {
213: handleException(e);
214: }
215: }
216:
217: public void setDate(int parameterIndex, java.sql.Date x)
218: throws SQLException {
219: checkOpen();
220: try {
221: _stmt.setDate(parameterIndex, x);
222: } catch (SQLException e) {
223: handleException(e);
224: }
225: }
226:
227: public void setTime(int parameterIndex, java.sql.Time x)
228: throws SQLException {
229: checkOpen();
230: try {
231: _stmt.setTime(parameterIndex, x);
232: } catch (SQLException e) {
233: handleException(e);
234: }
235: }
236:
237: public void setTimestamp(int parameterIndex, java.sql.Timestamp x)
238: throws SQLException {
239: checkOpen();
240: try {
241: _stmt.setTimestamp(parameterIndex, x);
242: } catch (SQLException e) {
243: handleException(e);
244: }
245: }
246:
247: public void setAsciiStream(int parameterIndex,
248: java.io.InputStream x, int length) throws SQLException {
249: checkOpen();
250: try {
251: _stmt.setAsciiStream(parameterIndex, x, length);
252: } catch (SQLException e) {
253: handleException(e);
254: }
255: }
256:
257: /** @deprecated */
258: public void setUnicodeStream(int parameterIndex,
259: java.io.InputStream x, int length) throws SQLException {
260: checkOpen();
261: try {
262: _stmt.setUnicodeStream(parameterIndex, x, length);
263: } catch (SQLException e) {
264: handleException(e);
265: }
266: }
267:
268: public void setBinaryStream(int parameterIndex,
269: java.io.InputStream x, int length) throws SQLException {
270: checkOpen();
271: try {
272: _stmt.setBinaryStream(parameterIndex, x, length);
273: } catch (SQLException e) {
274: handleException(e);
275: }
276: }
277:
278: public void clearParameters() throws SQLException {
279: checkOpen();
280: try {
281: _stmt.clearParameters();
282: } catch (SQLException e) {
283: handleException(e);
284: }
285: }
286:
287: public void setObject(int parameterIndex, Object x,
288: int targetSqlType, int scale) throws SQLException {
289: checkOpen();
290: try {
291: _stmt.setObject(parameterIndex, x, targetSqlType, scale);
292: } catch (SQLException e) {
293: handleException(e);
294: }
295: }
296:
297: public void setObject(int parameterIndex, Object x,
298: int targetSqlType) throws SQLException {
299: checkOpen();
300: try {
301: _stmt.setObject(parameterIndex, x, targetSqlType);
302: } catch (SQLException e) {
303: handleException(e);
304: }
305: }
306:
307: public void setObject(int parameterIndex, Object x)
308: throws SQLException {
309: checkOpen();
310: try {
311: _stmt.setObject(parameterIndex, x);
312: } catch (SQLException e) {
313: handleException(e);
314: }
315: }
316:
317: public boolean execute() throws SQLException {
318: checkOpen();
319: try {
320: return _stmt.execute();
321: } catch (SQLException e) {
322: handleException(e);
323: return false;
324: }
325: }
326:
327: public void addBatch() throws SQLException {
328: checkOpen();
329: try {
330: _stmt.addBatch();
331: } catch (SQLException e) {
332: handleException(e);
333: }
334: }
335:
336: public void setCharacterStream(int parameterIndex,
337: java.io.Reader reader, int length) throws SQLException {
338: checkOpen();
339: try {
340: _stmt.setCharacterStream(parameterIndex, reader, length);
341: } catch (SQLException e) {
342: handleException(e);
343: }
344: }
345:
346: public void setRef(int i, Ref x) throws SQLException {
347: checkOpen();
348: try {
349: _stmt.setRef(i, x);
350: } catch (SQLException e) {
351: handleException(e);
352: }
353: }
354:
355: public void setBlob(int i, Blob x) throws SQLException {
356: checkOpen();
357: try {
358: _stmt.setBlob(i, x);
359: } catch (SQLException e) {
360: handleException(e);
361: }
362: }
363:
364: public void setClob(int i, Clob x) throws SQLException {
365: checkOpen();
366: try {
367: _stmt.setClob(i, x);
368: } catch (SQLException e) {
369: handleException(e);
370: }
371: }
372:
373: public void setArray(int i, Array x) throws SQLException {
374: checkOpen();
375: try {
376: _stmt.setArray(i, x);
377: } catch (SQLException e) {
378: handleException(e);
379: }
380: }
381:
382: public ResultSetMetaData getMetaData() throws SQLException {
383: checkOpen();
384: try {
385: return _stmt.getMetaData();
386: } catch (SQLException e) {
387: handleException(e);
388: return null;
389: }
390: }
391:
392: public void setDate(int parameterIndex, java.sql.Date x,
393: Calendar cal) throws SQLException {
394: checkOpen();
395: try {
396: _stmt.setDate(parameterIndex, x, cal);
397: } catch (SQLException e) {
398: handleException(e);
399: }
400: }
401:
402: public void setTime(int parameterIndex, java.sql.Time x,
403: Calendar cal) throws SQLException {
404: checkOpen();
405: try {
406: _stmt.setTime(parameterIndex, x, cal);
407: } catch (SQLException e) {
408: handleException(e);
409: }
410: }
411:
412: public void setTimestamp(int parameterIndex, java.sql.Timestamp x,
413: Calendar cal) throws SQLException {
414: checkOpen();
415: try {
416: _stmt.setTimestamp(parameterIndex, x, cal);
417: } catch (SQLException e) {
418: handleException(e);
419: }
420: }
421:
422: public void setNull(int paramIndex, int sqlType, String typeName)
423: throws SQLException {
424: checkOpen();
425: try {
426: _stmt.setNull(paramIndex, sqlType, typeName);
427: } catch (SQLException e) {
428: handleException(e);
429: }
430: }
431:
432: /**
433: * Returns a String representation of this object.
434: *
435: * @return String
436: * @since 1.2.2
437: */
438: public String toString() {
439: return _stmt.toString();
440: }
441:
442: // ------------------- JDBC 3.0 -----------------------------------------
443: // Will be commented by the build process on a JDBC 2.0 system
444:
445: /* JDBC_3_ANT_KEY_BEGIN */
446:
447: public void setURL(int parameterIndex, java.net.URL x)
448: throws SQLException {
449: checkOpen();
450: try {
451: _stmt.setURL(parameterIndex, x);
452: } catch (SQLException e) {
453: handleException(e);
454: }
455: }
456:
457: public java.sql.ParameterMetaData getParameterMetaData()
458: throws SQLException {
459: checkOpen();
460: try {
461: return _stmt.getParameterMetaData();
462: } catch (SQLException e) {
463: handleException(e);
464: return null;
465: }
466: }
467:
468: /* JDBC_3_ANT_KEY_END */
469: }
|