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