View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.dbcp;
19  
20  import java.math.BigDecimal;
21  import java.sql.Array;
22  import java.sql.Blob;
23  import java.sql.Clob;
24  import java.sql.PreparedStatement;
25  import java.sql.Ref;
26  import java.sql.ResultSet;
27  import java.sql.ResultSetMetaData;
28  import java.sql.SQLException;
29  import java.util.Calendar;
30  /* JDBC_4_ANT_KEY_BEGIN */
31  import java.io.InputStream;
32  import java.io.Reader;
33  import java.sql.NClob;
34  import java.sql.RowId;
35  import java.sql.SQLXML;
36  /* JDBC_4_ANT_KEY_END */
37  
38  /**
39   * A base delegating implementation of {@link PreparedStatement}.
40   * <p>
41   * All of the methods from the {@link PreparedStatement} interface
42   * simply check to see that the {@link PreparedStatement} is active,
43   * and call the corresponding method on the "delegate"
44   * provided in my constructor.
45   * <p>
46   * Extends AbandonedTrace to implement Statement tracking and
47   * logging of code which created the Statement. Tracking the 
48   * Statement ensures that the Connection which created it can
49   * close any open Statement's on Connection close.
50   *
51   * @author Rodney Waldhoff
52   * @author Glenn L. Nielsen
53   * @author James House
54   * @author Dirk Verbeeck
55   * @version $Revision: 1023401 $ $Date: 2010-10-16 21:54:24 -0400 (Sat, 16 Oct 2010) $
56   */
57  public class DelegatingPreparedStatement extends DelegatingStatement
58          implements PreparedStatement {
59  
60      /**
61       * Create a wrapper for the Statement which traces this
62       * Statement to the Connection which created it and the
63       * code which created it.
64       *
65       * @param s the {@link PreparedStatement} to delegate all calls to.
66       * @param c the {@link DelegatingConnection} that created this statement.
67       */
68      public DelegatingPreparedStatement(DelegatingConnection c,
69                                         PreparedStatement s) {
70          super(c, s);
71      }
72  
73      public boolean equals(Object obj) {
74          PreparedStatement delegate = (PreparedStatement) getInnermostDelegate();
75          if (delegate == null) {
76              return false;
77          }
78          if (obj instanceof DelegatingPreparedStatement) {
79              DelegatingPreparedStatement s = (DelegatingPreparedStatement) obj;
80              return delegate.equals(s.getInnermostDelegate());
81          }
82          else {
83              return delegate.equals(obj);
84          }
85      }
86  
87      /** Sets my delegate. */
88      public void setDelegate(PreparedStatement s) {
89          super.setDelegate(s);
90          _stmt = s;
91      }
92  
93      public ResultSet executeQuery() throws SQLException {
94          checkOpen();
95          try {
96              return DelegatingResultSet.wrapResultSet(this,((PreparedStatement)_stmt).executeQuery());
97          }
98          catch (SQLException e) {
99              handleException(e);
100             throw new AssertionError();
101         }
102     }
103 
104     public int executeUpdate() throws SQLException
105     { checkOpen(); try { return ((PreparedStatement)_stmt).executeUpdate(); } catch (SQLException e) { handleException(e); return 0; } }
106 
107     public void setNull(int parameterIndex, int sqlType) throws SQLException
108     { checkOpen(); try { ((PreparedStatement)_stmt).setNull(parameterIndex,sqlType); } catch (SQLException e) { handleException(e); } }
109 
110     public void setBoolean(int parameterIndex, boolean x) throws SQLException
111     { checkOpen(); try { ((PreparedStatement)_stmt).setBoolean(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
112 
113     public void setByte(int parameterIndex, byte x) throws SQLException
114     { checkOpen(); try { ((PreparedStatement)_stmt).setByte(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
115 
116     public void setShort(int parameterIndex, short x) throws SQLException
117     { checkOpen(); try { ((PreparedStatement)_stmt).setShort(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
118 
119     public void setInt(int parameterIndex, int x) throws SQLException
120     { checkOpen(); try { ((PreparedStatement)_stmt).setInt(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
121 
122     public void setLong(int parameterIndex, long x) throws SQLException
123     { checkOpen(); try { ((PreparedStatement)_stmt).setLong(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
124 
125     public void setFloat(int parameterIndex, float x) throws SQLException
126     { checkOpen(); try { ((PreparedStatement)_stmt).setFloat(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
127 
128     public void setDouble(int parameterIndex, double x) throws SQLException
129     { checkOpen(); try { ((PreparedStatement)_stmt).setDouble(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
130 
131     public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException
132     { checkOpen(); try { ((PreparedStatement)_stmt).setBigDecimal(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
133 
134     public void setString(int parameterIndex, String x) throws SQLException
135     { checkOpen(); try { ((PreparedStatement)_stmt).setString(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
136 
137     public void setBytes(int parameterIndex, byte[] x) throws SQLException
138     { checkOpen(); try { ((PreparedStatement)_stmt).setBytes(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
139 
140     public void setDate(int parameterIndex, java.sql.Date x) throws SQLException
141     { checkOpen(); try { ((PreparedStatement)_stmt).setDate(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
142 
143     public void setTime(int parameterIndex, java.sql.Time x) throws SQLException
144     { checkOpen(); try { ((PreparedStatement)_stmt).setTime(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
145 
146     public void setTimestamp(int parameterIndex, java.sql.Timestamp x) throws SQLException
147     { checkOpen(); try { ((PreparedStatement)_stmt).setTimestamp(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
148 
149     public void setAsciiStream(int parameterIndex, java.io.InputStream x, int length) throws SQLException
150     { checkOpen(); try { ((PreparedStatement)_stmt).setAsciiStream(parameterIndex,x,length); } catch (SQLException e) { handleException(e); } }
151 
152     /** @deprecated */
153     public void setUnicodeStream(int parameterIndex, java.io.InputStream x, int length) throws SQLException
154     { checkOpen(); try { ((PreparedStatement)_stmt).setUnicodeStream(parameterIndex,x,length); } catch (SQLException e) { handleException(e); } }
155     
156     public void setBinaryStream(int parameterIndex, java.io.InputStream x, int length) throws SQLException
157     { checkOpen(); try { ((PreparedStatement)_stmt).setBinaryStream(parameterIndex,x,length); } catch (SQLException e) { handleException(e); } }
158 
159     public void clearParameters() throws SQLException
160     { checkOpen(); try { ((PreparedStatement)_stmt).clearParameters(); } catch (SQLException e) { handleException(e); } }
161 
162     public void setObject(int parameterIndex, Object x, int targetSqlType, int scale) throws SQLException
163     { checkOpen(); try { ((PreparedStatement)_stmt).setObject(parameterIndex, x, targetSqlType, scale); } catch (SQLException e) { handleException(e); } }
164 
165     public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException
166     { checkOpen(); try { ((PreparedStatement)_stmt).setObject(parameterIndex, x, targetSqlType); } catch (SQLException e) { handleException(e); } }
167 
168     public void setObject(int parameterIndex, Object x) throws SQLException
169     { checkOpen(); try { ((PreparedStatement)_stmt).setObject(parameterIndex, x); } catch (SQLException e) { handleException(e); } }
170 
171     public boolean execute() throws SQLException
172     { checkOpen(); try { return ((PreparedStatement)_stmt).execute(); } catch (SQLException e) { handleException(e); return false; } }
173 
174     public void addBatch() throws SQLException
175     { checkOpen(); try { ((PreparedStatement)_stmt).addBatch(); } catch (SQLException e) { handleException(e); } }
176 
177     public void setCharacterStream(int parameterIndex, java.io.Reader reader, int length) throws SQLException
178     { checkOpen(); try { ((PreparedStatement)_stmt).setCharacterStream(parameterIndex,reader,length); } catch (SQLException e) { handleException(e); } }
179 
180     public void setRef(int i, Ref x) throws SQLException
181     { checkOpen(); try { ((PreparedStatement)_stmt).setRef(i,x); } catch (SQLException e) { handleException(e); } }
182 
183     public void setBlob(int i, Blob x) throws SQLException
184     { checkOpen(); try { ((PreparedStatement)_stmt).setBlob(i,x); } catch (SQLException e) { handleException(e); } }
185 
186     public void setClob(int i, Clob x) throws SQLException
187     { checkOpen(); try { ((PreparedStatement)_stmt).setClob(i,x); } catch (SQLException e) { handleException(e); } }
188 
189     public void setArray(int i, Array x) throws SQLException
190     { checkOpen(); try { ((PreparedStatement)_stmt).setArray(i,x); } catch (SQLException e) { handleException(e); } }
191 
192     public ResultSetMetaData getMetaData() throws SQLException
193     { checkOpen(); try { return ((PreparedStatement)_stmt).getMetaData(); } catch (SQLException e) { handleException(e); throw new AssertionError(); } }
194 
195     public void setDate(int parameterIndex, java.sql.Date x, Calendar cal) throws SQLException
196     { checkOpen(); try { ((PreparedStatement)_stmt).setDate(parameterIndex,x,cal); } catch (SQLException e) { handleException(e); } }
197 
198     public void setTime(int parameterIndex, java.sql.Time x, Calendar cal) throws SQLException
199     { checkOpen(); try { ((PreparedStatement)_stmt).setTime(parameterIndex,x,cal); } catch (SQLException e) { handleException(e); } }
200 
201     public void setTimestamp(int parameterIndex, java.sql.Timestamp x, Calendar cal) throws SQLException
202     { checkOpen(); try { ((PreparedStatement)_stmt).setTimestamp(parameterIndex,x,cal); } catch (SQLException e) { handleException(e); } }
203 
204     public void setNull(int paramIndex, int sqlType, String typeName) throws SQLException
205     { checkOpen(); try { ((PreparedStatement)_stmt).setNull(paramIndex,sqlType,typeName); } catch (SQLException e) { handleException(e); } }
206 
207     /**
208      * Returns a String representation of this object.
209      *
210      * @return String 
211      * @since 1.2.2
212      */
213     public String toString() {
214     return _stmt.toString();
215     }
216 
217     public void setURL(int parameterIndex, java.net.URL x) throws SQLException
218     { checkOpen(); try { ((PreparedStatement)_stmt).setURL(parameterIndex, x); } catch (SQLException e) { handleException(e); } }
219 
220     public java.sql.ParameterMetaData getParameterMetaData() throws SQLException
221     { checkOpen(); try { return ((PreparedStatement)_stmt).getParameterMetaData(); } catch (SQLException e) { handleException(e); throw new AssertionError(); } }
222 
223 /* JDBC_4_ANT_KEY_BEGIN */
224 
225     public void setRowId(int parameterIndex, RowId value) throws SQLException {
226         checkOpen();
227         try {
228             ((PreparedStatement)_stmt).setRowId(parameterIndex, value);
229         }
230         catch (SQLException e) {
231             handleException(e);
232         }
233     }
234 
235     public void setNString(int parameterIndex, String value) throws SQLException {
236         checkOpen();
237         try {
238             ((PreparedStatement)_stmt).setNString(parameterIndex, value);
239         }
240         catch (SQLException e) {
241             handleException(e);
242         }
243     }
244 
245     public void setNCharacterStream(int parameterIndex, Reader value, long length) throws SQLException {
246         checkOpen();
247         try {
248             ((PreparedStatement)_stmt).setNCharacterStream(parameterIndex, value, length);
249         }
250         catch (SQLException e) {
251             handleException(e);
252         }
253     }
254 
255     public void setNClob(int parameterIndex, NClob value) throws SQLException {
256         checkOpen();
257         try {
258             ((PreparedStatement)_stmt).setNClob(parameterIndex, value);
259         }
260         catch (SQLException e) {
261             handleException(e);
262         }
263     }
264 
265     public void setClob(int parameterIndex, Reader reader, long length) throws SQLException {
266         checkOpen();
267         try {
268             ((PreparedStatement)_stmt).setClob(parameterIndex, reader, length);
269         }
270         catch (SQLException e) {
271             handleException(e);
272         }
273     }
274 
275     public void setBlob(int parameterIndex, InputStream inputStream, long length) throws SQLException {
276         checkOpen();
277         try {
278             ((PreparedStatement)_stmt).setBlob(parameterIndex, inputStream, length);
279         }
280         catch (SQLException e) {
281             handleException(e);
282         }
283     }
284 
285     public void setNClob(int parameterIndex, Reader reader, long length) throws SQLException {
286         checkOpen();
287         try {
288             ((PreparedStatement)_stmt).setNClob(parameterIndex, reader, length);
289         }
290         catch (SQLException e) {
291             handleException(e);
292         }
293     }
294 
295     public void setSQLXML(int parameterIndex, SQLXML value) throws SQLException {
296         checkOpen();
297         try {
298             ((PreparedStatement)_stmt).setSQLXML(parameterIndex, value);
299         }
300         catch (SQLException e) {
301             handleException(e);
302         }
303     }
304 
305     public void setAsciiStream(int parameterIndex, InputStream inputStream, long length) throws SQLException {
306         checkOpen();
307         try {
308             ((PreparedStatement)_stmt).setAsciiStream(parameterIndex, inputStream, length);
309         }
310         catch (SQLException e) {
311             handleException(e);
312         }
313     }
314 
315     public void setBinaryStream(int parameterIndex, InputStream inputStream, long length) throws SQLException {
316         checkOpen();
317         try {
318             ((PreparedStatement)_stmt).setBinaryStream(parameterIndex, inputStream, length);
319         }
320         catch (SQLException e) {
321             handleException(e);
322         }
323     }
324 
325     public void setCharacterStream(int parameterIndex, Reader reader, long length) throws SQLException {
326         checkOpen();
327         try {
328             ((PreparedStatement)_stmt).setCharacterStream(parameterIndex, reader, length);
329         }
330         catch (SQLException e) {
331             handleException(e);
332         }
333     }
334 
335     public void setAsciiStream(int parameterIndex, InputStream inputStream) throws SQLException {
336         checkOpen();
337         try {
338             ((PreparedStatement)_stmt).setAsciiStream(parameterIndex, inputStream);
339         }
340         catch (SQLException e) {
341             handleException(e);
342         }
343     }
344 
345     public void setBinaryStream(int parameterIndex, InputStream inputStream) throws SQLException {
346         checkOpen();
347         try {
348             ((PreparedStatement)_stmt).setBinaryStream(parameterIndex, inputStream);
349         }
350         catch (SQLException e) {
351             handleException(e);
352         }
353     }
354 
355     public void setCharacterStream(int parameterIndex, Reader reader) throws SQLException {
356         checkOpen();
357         try {
358             ((PreparedStatement)_stmt).setCharacterStream(parameterIndex, reader);
359         }
360         catch (SQLException e) {
361             handleException(e);
362         }
363     }
364 
365     public void setNCharacterStream(int parameterIndex, Reader reader) throws SQLException {
366         checkOpen();
367         try {
368             ((PreparedStatement)_stmt).setNCharacterStream(parameterIndex, reader);
369         }
370         catch (SQLException e) {
371             handleException(e);
372         }
373     }
374 
375     public void setClob(int parameterIndex, Reader reader) throws SQLException {
376         checkOpen();
377         try {
378             ((PreparedStatement)_stmt).setClob(parameterIndex, reader);
379         }
380         catch (SQLException e) {
381             handleException(e);
382         }
383     }
384 
385     public void setBlob(int parameterIndex, InputStream inputStream) throws SQLException {
386         checkOpen();
387         try {
388             ((PreparedStatement)_stmt).setBlob(parameterIndex, inputStream);
389         }
390         catch (SQLException e) {
391             handleException(e);
392         }
393     }
394 
395     public void setNClob(int parameterIndex, Reader reader) throws SQLException {
396         checkOpen();
397         try {
398             ((PreparedStatement)_stmt).setNClob(parameterIndex, reader);
399         }
400         catch (SQLException e) {
401             handleException(e);
402         }
403     }
404 /* JDBC_4_ANT_KEY_END */
405 }