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.tomcat.dbcp.dbcp2;
19
20 import java.io.InputStream;
21 import java.io.Reader;
22 import java.math.BigDecimal;
23 import java.sql.Array;
24 import java.sql.Blob;
25 import java.sql.Clob;
26 import java.sql.Date;
27 import java.sql.NClob;
28 import java.sql.PreparedStatement;
29 import java.sql.Ref;
30 import java.sql.ResultSet;
31 import java.sql.ResultSetMetaData;
32 import java.sql.RowId;
33 import java.sql.SQLException;
34 import java.sql.SQLType;
35 import java.sql.SQLXML;
36 import java.sql.Statement;
37 import java.sql.Time;
38 import java.sql.Timestamp;
39 import java.util.Calendar;
40
41 /**
42  * A base delegating implementation of {@link PreparedStatement}.
43  * <p>
44  * All of the methods from the {@link PreparedStatement} interface simply check to see that the
45  * {@link PreparedStatement} is active, and call the corresponding method on the "delegate" provided in my constructor.
46  * <p>
47  * Extends AbandonedTrace to implement Statement tracking and logging of code which created the Statement. Tracking the
48  * Statement ensures that the Connection which created it can close any open Statement's on Connection close.
49  *
50  * @since 2.0
51  */

52 public class DelegatingPreparedStatement extends DelegatingStatement implements PreparedStatement {
53
54     /**
55      * Create a wrapper for the Statement which traces this Statement to the Connection which created it and the code
56      * which created it.
57      *
58      * @param statement
59      *            the {@link PreparedStatement} to delegate all calls to.
60      * @param connection
61      *            the {@link DelegatingConnection} that created this statement.
62      */

63     public DelegatingPreparedStatement(final DelegatingConnection<?> connection, final PreparedStatement statement) {
64         super(connection, statement);
65     }
66
67     @Override
68     public void addBatch() throws SQLException {
69         checkOpen();
70         try {
71             getDelegatePreparedStatement().addBatch();
72         } catch (final SQLException e) {
73             handleException(e);
74         }
75     }
76
77     @Override
78     public void clearParameters() throws SQLException {
79         checkOpen();
80         try {
81             getDelegatePreparedStatement().clearParameters();
82         } catch (final SQLException e) {
83             handleException(e);
84         }
85     }
86
87     @Override
88     public boolean execute() throws SQLException {
89         checkOpen();
90         if (getConnectionInternal() != null) {
91             getConnectionInternal().setLastUsed();
92         }
93         try {
94             return getDelegatePreparedStatement().execute();
95         } catch (final SQLException e) {
96             handleException(e);
97             return false;
98         }
99     }
100
101     /**
102      * @since 2.5.0
103      */

104     @Override
105     public long executeLargeUpdate() throws SQLException {
106         checkOpen();
107         try {
108             return getDelegatePreparedStatement().executeLargeUpdate();
109         } catch (final SQLException e) {
110             handleException(e);
111             return 0;
112         }
113     }
114
115     @Override
116     public ResultSet executeQuery() throws SQLException {
117         checkOpen();
118         if (getConnectionInternal() != null) {
119             getConnectionInternal().setLastUsed();
120         }
121         try {
122             return DelegatingResultSet.wrapResultSet(this, getDelegatePreparedStatement().executeQuery());
123         } catch (final SQLException e) {
124             handleException(e);
125             throw new AssertionError();
126         }
127     }
128
129     @Override
130     public int executeUpdate() throws SQLException {
131         checkOpen();
132         if (getConnectionInternal() != null) {
133             getConnectionInternal().setLastUsed();
134         }
135         try {
136             return getDelegatePreparedStatement().executeUpdate();
137         } catch (final SQLException e) {
138             handleException(e);
139             return 0;
140         }
141     }
142
143     private PreparedStatement getDelegatePreparedStatement() {
144         return (PreparedStatement) getDelegate();
145     }
146
147     @Override
148     public ResultSetMetaData getMetaData() throws SQLException {
149         checkOpen();
150         try {
151             return getDelegatePreparedStatement().getMetaData();
152         } catch (final SQLException e) {
153             handleException(e);
154             throw new AssertionError();
155         }
156     }
157
158     @Override
159     public java.sql.ParameterMetaData getParameterMetaData() throws SQLException {
160         checkOpen();
161         try {
162             return getDelegatePreparedStatement().getParameterMetaData();
163         } catch (final SQLException e) {
164             handleException(e);
165             throw new AssertionError();
166         }
167     }
168
169     @Override
170     public void setArray(final int i, final Array x) throws SQLException {
171         checkOpen();
172         try {
173             getDelegatePreparedStatement().setArray(i, x);
174         } catch (final SQLException e) {
175             handleException(e);
176         }
177     }
178
179     @Override
180     public void setAsciiStream(final int parameterIndex, final InputStream inputStream) throws SQLException {
181         checkOpen();
182         try {
183             getDelegatePreparedStatement().setAsciiStream(parameterIndex, inputStream);
184         } catch (final SQLException e) {
185             handleException(e);
186         }
187     }
188
189     @Override
190     public void setAsciiStream(final int parameterIndex, final InputStream x, final int length) throws SQLException {
191         checkOpen();
192         try {
193             getDelegatePreparedStatement().setAsciiStream(parameterIndex, x, length);
194         } catch (final SQLException e) {
195             handleException(e);
196         }
197     }
198
199     @Override
200     public void setAsciiStream(final int parameterIndex, final InputStream inputStream, final long length)
201             throws SQLException {
202         checkOpen();
203         try {
204             getDelegatePreparedStatement().setAsciiStream(parameterIndex, inputStream, length);
205         } catch (final SQLException e) {
206             handleException(e);
207         }
208     }
209
210     @Override
211     public void setBigDecimal(final int parameterIndex, final BigDecimal x) throws SQLException {
212         checkOpen();
213         try {
214             getDelegatePreparedStatement().setBigDecimal(parameterIndex, x);
215         } catch (final SQLException e) {
216             handleException(e);
217         }
218     }
219
220     @Override
221     public void setBinaryStream(final int parameterIndex, final InputStream inputStream) throws SQLException {
222         checkOpen();
223         try {
224             getDelegatePreparedStatement().setBinaryStream(parameterIndex, inputStream);
225         } catch (final SQLException e) {
226             handleException(e);
227         }
228     }
229
230     @Override
231     public void setBinaryStream(final int parameterIndex, final InputStream x, final int length) throws SQLException {
232         checkOpen();
233         try {
234             getDelegatePreparedStatement().setBinaryStream(parameterIndex, x, length);
235         } catch (final SQLException e) {
236             handleException(e);
237         }
238     }
239
240     @Override
241     public void setBinaryStream(final int parameterIndex, final InputStream inputStream, final long length)
242             throws SQLException {
243         checkOpen();
244         try {
245             getDelegatePreparedStatement().setBinaryStream(parameterIndex, inputStream, length);
246         } catch (final SQLException e) {
247             handleException(e);
248         }
249     }
250
251     @Override
252     public void setBlob(final int i, final Blob x) throws SQLException {
253         checkOpen();
254         try {
255             getDelegatePreparedStatement().setBlob(i, x);
256         } catch (final SQLException e) {
257             handleException(e);
258         }
259     }
260
261     @Override
262     public void setBlob(final int parameterIndex, final InputStream inputStream) throws SQLException {
263         checkOpen();
264         try {
265             getDelegatePreparedStatement().setBlob(parameterIndex, inputStream);
266         } catch (final SQLException e) {
267             handleException(e);
268         }
269     }
270
271     @Override
272     public void setBlob(final int parameterIndex, final InputStream inputStream, final long length)
273             throws SQLException {
274         checkOpen();
275         try {
276             getDelegatePreparedStatement().setBlob(parameterIndex, inputStream, length);
277         } catch (final SQLException e) {
278             handleException(e);
279         }
280     }
281
282     @Override
283     public void setBoolean(final int parameterIndex, final boolean x) throws SQLException {
284         checkOpen();
285         try {
286             getDelegatePreparedStatement().setBoolean(parameterIndex, x);
287         } catch (final SQLException e) {
288             handleException(e);
289         }
290     }
291
292     @Override
293     public void setByte(final int parameterIndex, final byte x) throws SQLException {
294         checkOpen();
295         try {
296             getDelegatePreparedStatement().setByte(parameterIndex, x);
297         } catch (final SQLException e) {
298             handleException(e);
299         }
300     }
301
302     @Override
303     public void setBytes(final int parameterIndex, final byte[] x) throws SQLException {
304         checkOpen();
305         try {
306             getDelegatePreparedStatement().setBytes(parameterIndex, x);
307         } catch (final SQLException e) {
308             handleException(e);
309         }
310     }
311
312     @Override
313     public void setCharacterStream(final int parameterIndex, final Reader reader) throws SQLException {
314         checkOpen();
315         try {
316             getDelegatePreparedStatement().setCharacterStream(parameterIndex, reader);
317         } catch (final SQLException e) {
318             handleException(e);
319         }
320     }
321
322     @Override
323     public void setCharacterStream(final int parameterIndex, final Reader reader, final int length)
324             throws SQLException {
325         checkOpen();
326         try {
327             getDelegatePreparedStatement().setCharacterStream(parameterIndex, reader, length);
328         } catch (final SQLException e) {
329             handleException(e);
330         }
331     }
332
333     @Override
334     public void setCharacterStream(final int parameterIndex, final Reader reader, final long length)
335             throws SQLException {
336         checkOpen();
337         try {
338             getDelegatePreparedStatement().setCharacterStream(parameterIndex, reader, length);
339         } catch (final SQLException e) {
340             handleException(e);
341         }
342     }
343
344     @Override
345     public void setClob(final int i, final Clob x) throws SQLException {
346         checkOpen();
347         try {
348             getDelegatePreparedStatement().setClob(i, x);
349         } catch (final SQLException e) {
350             handleException(e);
351         }
352     }
353
354     @Override
355     public void setClob(final int parameterIndex, final Reader reader) throws SQLException {
356         checkOpen();
357         try {
358             getDelegatePreparedStatement().setClob(parameterIndex, reader);
359         } catch (final SQLException e) {
360             handleException(e);
361         }
362     }
363
364     @Override
365     public void setClob(final int parameterIndex, final Reader reader, final long length) throws SQLException {
366         checkOpen();
367         try {
368             getDelegatePreparedStatement().setClob(parameterIndex, reader, length);
369         } catch (final SQLException e) {
370             handleException(e);
371         }
372     }
373
374     @Override
375     public void setDate(final int parameterIndex, final Date x) throws SQLException {
376         checkOpen();
377         try {
378             getDelegatePreparedStatement().setDate(parameterIndex, x);
379         } catch (final SQLException e) {
380             handleException(e);
381         }
382     }
383
384     @Override
385     public void setDate(final int parameterIndex, final Date x, final Calendar cal) throws SQLException {
386         checkOpen();
387         try {
388             getDelegatePreparedStatement().setDate(parameterIndex, x, cal);
389         } catch (final SQLException e) {
390             handleException(e);
391         }
392     }
393
394     @Override
395     public void setDouble(final int parameterIndex, final double x) throws SQLException {
396         checkOpen();
397         try {
398             getDelegatePreparedStatement().setDouble(parameterIndex, x);
399         } catch (final SQLException e) {
400             handleException(e);
401         }
402     }
403
404     @Override
405     public void setFloat(final int parameterIndex, final float x) throws SQLException {
406         checkOpen();
407         try {
408             getDelegatePreparedStatement().setFloat(parameterIndex, x);
409         } catch (final SQLException e) {
410             handleException(e);
411         }
412     }
413
414     @Override
415     public void setInt(final int parameterIndex, final int x) throws SQLException {
416         checkOpen();
417         try {
418             getDelegatePreparedStatement().setInt(parameterIndex, x);
419         } catch (final SQLException e) {
420             handleException(e);
421         }
422     }
423
424     @Override
425     public void setLong(final int parameterIndex, final long x) throws SQLException {
426         checkOpen();
427         try {
428             getDelegatePreparedStatement().setLong(parameterIndex, x);
429         } catch (final SQLException e) {
430             handleException(e);
431         }
432     }
433
434     @Override
435     public void setNCharacterStream(final int parameterIndex, final Reader reader) throws SQLException {
436         checkOpen();
437         try {
438             getDelegatePreparedStatement().setNCharacterStream(parameterIndex, reader);
439         } catch (final SQLException e) {
440             handleException(e);
441         }
442     }
443
444     @Override
445     public void setNCharacterStream(final int parameterIndex, final Reader value, final long length)
446             throws SQLException {
447         checkOpen();
448         try {
449             getDelegatePreparedStatement().setNCharacterStream(parameterIndex, value, length);
450         } catch (final SQLException e) {
451             handleException(e);
452         }
453     }
454
455     @Override
456     public void setNClob(final int parameterIndex, final NClob value) throws SQLException {
457         checkOpen();
458         try {
459             getDelegatePreparedStatement().setNClob(parameterIndex, value);
460         } catch (final SQLException e) {
461             handleException(e);
462         }
463     }
464
465     @Override
466     public void setNClob(final int parameterIndex, final Reader reader) throws SQLException {
467         checkOpen();
468         try {
469             getDelegatePreparedStatement().setNClob(parameterIndex, reader);
470         } catch (final SQLException e) {
471             handleException(e);
472         }
473     }
474
475     @Override
476     public void setNClob(final int parameterIndex, final Reader reader, final long length) throws SQLException {
477         checkOpen();
478         try {
479             getDelegatePreparedStatement().setNClob(parameterIndex, reader, length);
480         } catch (final SQLException e) {
481             handleException(e);
482         }
483     }
484
485     @Override
486     public void setNString(final int parameterIndex, final String value) throws SQLException {
487         checkOpen();
488         try {
489             getDelegatePreparedStatement().setNString(parameterIndex, value);
490         } catch (final SQLException e) {
491             handleException(e);
492         }
493     }
494
495     @Override
496     public void setNull(final int parameterIndex, final int sqlType) throws SQLException {
497         checkOpen();
498         try {
499             getDelegatePreparedStatement().setNull(parameterIndex, sqlType);
500         } catch (final SQLException e) {
501             handleException(e);
502         }
503     }
504
505     @Override
506     public void setNull(final int paramIndex, final int sqlType, final String typeName) throws SQLException {
507         checkOpen();
508         try {
509             getDelegatePreparedStatement().setNull(paramIndex, sqlType, typeName);
510         } catch (final SQLException e) {
511             handleException(e);
512         }
513     }
514
515     @Override
516     public void setObject(final int parameterIndex, final Object x) throws SQLException {
517         checkOpen();
518         try {
519             getDelegatePreparedStatement().setObject(parameterIndex, x);
520         } catch (final SQLException e) {
521             handleException(e);
522         }
523     }
524
525     @Override
526     public void setObject(final int parameterIndex, final Object x, final int targetSqlType) throws SQLException {
527         checkOpen();
528         try {
529             getDelegatePreparedStatement().setObject(parameterIndex, x, targetSqlType);
530         } catch (final SQLException e) {
531             handleException(e);
532         }
533     }
534
535     @Override
536     public void setObject(final int parameterIndex, final Object x, final int targetSqlType, final int scale)
537             throws SQLException {
538         checkOpen();
539         try {
540             getDelegatePreparedStatement().setObject(parameterIndex, x, targetSqlType, scale);
541         } catch (final SQLException e) {
542             handleException(e);
543         }
544     }
545
546     /**
547      * @since 2.5.0
548      */

549     @Override
550     public void setObject(final int parameterIndex, final Object x, final SQLType targetSqlType) throws SQLException {
551         checkOpen();
552         try {
553             getDelegatePreparedStatement().setObject(parameterIndex, x, targetSqlType);
554         } catch (final SQLException e) {
555             handleException(e);
556         }
557     }
558
559     /**
560      * @since 2.5.0
561      */

562     @Override
563     public void setObject(final int parameterIndex, final Object x, final SQLType targetSqlType, final int scaleOrLength) throws SQLException {
564         checkOpen();
565         try {
566             getDelegatePreparedStatement().setObject(parameterIndex, x, targetSqlType, scaleOrLength);
567         } catch (final SQLException e) {
568             handleException(e);
569         }
570     }
571
572     @Override
573     public void setRef(final int i, final Ref x) throws SQLException {
574         checkOpen();
575         try {
576             getDelegatePreparedStatement().setRef(i, x);
577         } catch (final SQLException e) {
578             handleException(e);
579         }
580     }
581
582     @Override
583     public void setRowId(final int parameterIndex, final RowId value) throws SQLException {
584         checkOpen();
585         try {
586             getDelegatePreparedStatement().setRowId(parameterIndex, value);
587         } catch (final SQLException e) {
588             handleException(e);
589         }
590     }
591
592     @Override
593     public void setShort(final int parameterIndex, final short x) throws SQLException {
594         checkOpen();
595         try {
596             getDelegatePreparedStatement().setShort(parameterIndex, x);
597         } catch (final SQLException e) {
598             handleException(e);
599         }
600     }
601
602     @Override
603     public void setSQLXML(final int parameterIndex, final SQLXML value) throws SQLException {
604         checkOpen();
605         try {
606             getDelegatePreparedStatement().setSQLXML(parameterIndex, value);
607         } catch (final SQLException e) {
608             handleException(e);
609         }
610     }
611
612     @Override
613     public void setString(final int parameterIndex, final String x) throws SQLException {
614         checkOpen();
615         try {
616             getDelegatePreparedStatement().setString(parameterIndex, x);
617         } catch (final SQLException e) {
618             handleException(e);
619         }
620     }
621
622     @Override
623     public void setTime(final int parameterIndex, final Time x) throws SQLException {
624         checkOpen();
625         try {
626             getDelegatePreparedStatement().setTime(parameterIndex, x);
627         } catch (final SQLException e) {
628             handleException(e);
629         }
630     }
631
632     @Override
633     public void setTime(final int parameterIndex, final Time x, final Calendar cal) throws SQLException {
634         checkOpen();
635         try {
636             getDelegatePreparedStatement().setTime(parameterIndex, x, cal);
637         } catch (final SQLException e) {
638             handleException(e);
639         }
640     }
641
642     @Override
643     public void setTimestamp(final int parameterIndex, final Timestamp x) throws SQLException {
644         checkOpen();
645         try {
646             getDelegatePreparedStatement().setTimestamp(parameterIndex, x);
647         } catch (final SQLException e) {
648             handleException(e);
649         }
650     }
651
652     @Override
653     public void setTimestamp(final int parameterIndex, final Timestamp x, final Calendar cal) throws SQLException {
654         checkOpen();
655         try {
656             getDelegatePreparedStatement().setTimestamp(parameterIndex, x, cal);
657         } catch (final SQLException e) {
658             handleException(e);
659         }
660     }
661
662     /** @deprecated Use setAsciiStream(), setCharacterStream() or setNCharacterStream() */
663     @Deprecated
664     @Override
665     public void setUnicodeStream(final int parameterIndex, final InputStream x, final int length) throws SQLException {
666         checkOpen();
667         try {
668             getDelegatePreparedStatement().setUnicodeStream(parameterIndex, x, length);
669         } catch (final SQLException e) {
670             handleException(e);
671         }
672     }
673
674     @Override
675     public void setURL(final int parameterIndex, final java.net.URL x) throws SQLException {
676         checkOpen();
677         try {
678             getDelegatePreparedStatement().setURL(parameterIndex, x);
679         } catch (final SQLException e) {
680             handleException(e);
681         }
682     }
683
684     /**
685      * Returns a String representation of this object.
686      *
687      * @return String
688      */

689     @SuppressWarnings("resource")
690     @Override
691     public synchronized String toString() {
692         final Statement statement = getDelegate();
693         return statement == null ? "NULL" : statement.toString();
694     }
695 }
696