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