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