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    
018    package org.apache.commons.dbcp;
019    
020    import java.math.BigDecimal;
021    import java.sql.Array;
022    import java.sql.Blob;
023    import java.sql.Clob;
024    import java.sql.PreparedStatement;
025    import java.sql.Ref;
026    import java.sql.ResultSet;
027    import java.sql.ResultSetMetaData;
028    import java.sql.SQLException;
029    import java.util.Calendar;
030    /*
031    import java.io.InputStream;
032    import java.io.Reader;
033    import java.sql.NClob;
034    import java.sql.RowId;
035    import java.sql.SQLXML;
036    */
037    
038    /**
039     * A base delegating implementation of {@link PreparedStatement}.
040     * <p>
041     * All of the methods from the {@link PreparedStatement} interface
042     * simply check to see that the {@link PreparedStatement} is active,
043     * and call the corresponding method on the "delegate"
044     * provided in my constructor.
045     * <p>
046     * Extends AbandonedTrace to implement Statement tracking and
047     * logging of code which created the Statement. Tracking the 
048     * Statement ensures that the Connection which created it can
049     * close any open Statement's on Connection close.
050     *
051     * @author Rodney Waldhoff
052     * @author Glenn L. Nielsen
053     * @author James House
054     * @author Dirk Verbeeck
055     * @version $Revision: 892307 $ $Date: 2013-12-31 23:27:28 +0000 (Tue, 31 Dec 2013) $
056     */
057    public class DelegatingPreparedStatement extends DelegatingStatement
058            implements PreparedStatement {
059    
060        /**
061         * Create a wrapper for the Statement which traces this
062         * Statement to the Connection which created it and the
063         * code which created it.
064         *
065         * @param s the {@link PreparedStatement} to delegate all calls to.
066         * @param c the {@link DelegatingConnection} that created this statement.
067         */
068        public DelegatingPreparedStatement(DelegatingConnection c,
069                                           PreparedStatement s) {
070            super(c, s);
071        }
072    
073        public boolean equals(Object obj) {
074            PreparedStatement delegate = (PreparedStatement) getInnermostDelegate();
075            if (delegate == null) {
076                return false;
077            }
078            if (obj instanceof DelegatingPreparedStatement) {
079                DelegatingPreparedStatement s = (DelegatingPreparedStatement) obj;
080                return delegate.equals(s.getInnermostDelegate());
081            }
082            else {
083                return delegate.equals(obj);
084            }
085        }
086    
087        /** Sets my delegate. */
088        public void setDelegate(PreparedStatement s) {
089            super.setDelegate(s);
090            _stmt = s;
091        }
092    
093        public ResultSet executeQuery() throws SQLException {
094            checkOpen();
095            try {
096                return DelegatingResultSet.wrapResultSet(this,((PreparedStatement)_stmt).executeQuery());
097            }
098            catch (SQLException e) {
099                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    /*
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    */
405    }