View Javadoc
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.commons.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.Connection;
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.util.Calendar;
37  
38  /**
39   * A dummy {@link PreparedStatement}, for testing purposes.
40   */
41  public class TesterPreparedStatement extends TesterStatement implements PreparedStatement {
42  
43      private final ResultSetMetaData _resultSetMetaData = null;
44      private String _sql;
45      private String _catalog;
46      private int _autoGeneratedKeys = 1;
47      private int[] _columnIndexes;
48      private String[] _columnNames;
49  
50      public TesterPreparedStatement(final Connection conn) {
51          super(conn);
52          try {
53              _catalog = conn.getCatalog();
54          } catch (final SQLException e) {
55              // Ignored
56          }
57      }
58  
59      public TesterPreparedStatement(final Connection conn, final String sql) {
60          super(conn);
61          _sql = sql;
62          try {
63              _catalog = conn.getCatalog();
64          } catch (final SQLException e) {
65              // Ignored
66          }
67      }
68  
69      public TesterPreparedStatement(final Connection conn, final String sql, final int autoGeneratedKeys) {
70          super(conn);
71          _sql = sql;
72          _autoGeneratedKeys = autoGeneratedKeys;
73          try {
74              _catalog = conn.getCatalog();
75          } catch (final SQLException e) {
76              // Ignored
77          }
78      }
79  
80      public TesterPreparedStatement(final Connection conn, final String sql, final int resultSetType, final int resultSetConcurrency) {
81          super(conn, resultSetType, resultSetConcurrency);
82          _sql = sql;
83          try {
84              _catalog = conn.getCatalog();
85          } catch (final SQLException e) {
86              // Ignored
87          }
88      }
89  
90      public TesterPreparedStatement(final Connection conn, final String sql, final int resultSetType, final int resultSetConcurrency,
91              final int resultSetHoldability) {
92          super(conn, resultSetType, resultSetConcurrency, resultSetHoldability);
93          _sql = sql;
94          try {
95              _catalog = conn.getCatalog();
96          } catch (final SQLException e) {
97              // Ignored
98          }
99      }
100 
101     public TesterPreparedStatement(final Connection conn, final String sql, final int[] columnIndexes) {
102         super(conn);
103         _sql = sql;
104         _columnIndexes = columnIndexes;
105         try {
106             _catalog = conn.getCatalog();
107         } catch (final SQLException e) {
108             // Ignored
109         }
110     }
111 
112     public TesterPreparedStatement(final Connection conn, final String sql, final String[] columnNames) {
113         super(conn);
114         _sql = sql;
115         _columnNames = columnNames;
116         try {
117             _catalog = conn.getCatalog();
118         } catch (final SQLException e) {
119             // Ignored
120         }
121     }
122 
123     @Override
124     public void addBatch() throws SQLException {
125         checkOpen();
126     }
127 
128     @Override
129     public void clearParameters() throws SQLException {
130         checkOpen();
131     }
132 
133     @Override
134     public boolean execute() throws SQLException {
135         checkOpen(); return true;
136     }
137 
138     @Override
139     public boolean execute(final String sql, final int autoGeneratedKeys) throws SQLException {
140         checkOpen();
141         return true;
142     }
143 
144     @Override
145     public boolean execute(final String sl, final int[] columnIndexes) throws SQLException {
146         checkOpen();
147         return true;
148     }
149 
150     @Override
151     public boolean execute(final String sql, final String[] columnNames) throws SQLException {
152         checkOpen();
153         return true;
154     }
155 
156     @Override
157     public long executeLargeUpdate() throws SQLException {
158         checkOpen();
159         return _rowsUpdated;
160     }
161 
162     @Override
163     public long executeLargeUpdate(final String sql) throws SQLException {
164         checkOpen();
165         return _rowsUpdated;
166     }
167 
168     @Override
169     public long executeLargeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException {
170         checkOpen();
171         return 0;
172     }
173 
174     @Override
175     public long executeLargeUpdate(final String sql, final int[] columnIndexes) throws SQLException {
176         checkOpen();
177         return 0;
178     }
179 
180     @Override
181     public long executeLargeUpdate(final String sql, final String[] columnNames) throws SQLException {
182         checkOpen();
183         return 0;
184     }
185 
186     @Override
187     public ResultSet executeQuery() throws SQLException {
188         checkOpen();
189         if ("null".equals(_sql)) {
190             return null;
191         }
192         if (_queryTimeout > 0 && _queryTimeout < 5) {
193             // Simulate timeout if queryTimout is set to less than 5 seconds
194             throw new SQLException("query timeout");
195         }
196         return new TesterResultSet(this, _resultSetType, _resultSetConcurrency);
197     }
198 
199     @Override
200     public ResultSet executeQuery(final String sql) throws SQLException {
201         checkOpen();
202         if ("null".equals(sql)) {
203             return null;
204         }
205         return new TesterResultSet(this, _resultSetType, _resultSetConcurrency);
206     }
207 
208     @Override
209     public int executeUpdate() throws SQLException {
210         checkOpen();
211         return (int) _rowsUpdated;
212     }
213 
214     @Override
215     public int executeUpdate(final String sql) throws SQLException {
216         checkOpen();
217         return (int) _rowsUpdated;
218     }
219 
220     @Override
221     public int executeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException {
222         checkOpen();
223         return 0;
224     }
225 
226     @Override
227     public int executeUpdate(final String sql, final int[] columnIndexes) throws SQLException {
228         checkOpen();
229         return 0;
230     }
231 
232     @Override
233     public int executeUpdate(final String sql, final String[] columnNames) throws SQLException {
234         checkOpen();
235         return 0;
236     }
237 
238     public int getAutoGeneratedKeys() {
239         return _autoGeneratedKeys;
240     }
241 
242     public String getCatalog() {
243         return _catalog;
244     }
245 
246     public int[] getColumnIndexes() {
247         return _columnIndexes;
248     }
249 
250     public String[] getColumnNames() {
251         return _columnNames;
252     }
253 
254     @Override
255     public ResultSet getGeneratedKeys() throws SQLException {
256         return new TesterResultSet(this, _resultSetType, _resultSetConcurrency);
257     }
258 
259     @Override
260     public ResultSetMetaData getMetaData() throws SQLException {
261         checkOpen();
262         return _resultSetMetaData;
263     }
264 
265     @Override
266     public boolean getMoreResults(final int current) throws SQLException {
267         throw new SQLException("Not implemented.");
268     }
269 
270     @Override
271     public java.sql.ParameterMetaData getParameterMetaData() throws SQLException {
272         throw new SQLException("Not implemented.");
273     }
274 
275     /** For junit test only */
276     public String getSql() {
277         return _sql;
278     }
279 
280     @Override
281     public void setArray (final int i, final Array x) throws SQLException {
282         checkOpen();
283     }
284 
285     @Override
286     public void setAsciiStream(final int parameterIndex, final InputStream inputStream) throws SQLException {
287         throw new SQLException("Not implemented.");
288     }
289 
290     @Override
291     public void setAsciiStream(final int parameterIndex, final InputStream inputStream, final long length) throws SQLException {
292         throw new SQLException("Not implemented.");
293     }
294 
295     @Override
296     public void setAsciiStream(final int parameterIndex, final java.io.InputStream x, final int length) throws SQLException {
297         checkOpen();
298     }
299 
300     @Override
301     public void setBigDecimal(final int parameterIndex, final BigDecimal x) throws SQLException {
302         checkOpen();
303     }
304 
305     @Override
306     public void setBinaryStream(final int parameterIndex, final InputStream inputStream) throws SQLException {
307         throw new SQLException("Not implemented.");
308     }
309 
310     @Override
311     public void setBinaryStream(final int parameterIndex, final InputStream inputStream, final long length) throws SQLException {
312         throw new SQLException("Not implemented.");
313     }
314 
315     @Override
316     public void setBinaryStream(final int parameterIndex, final java.io.InputStream x, final int length) throws SQLException {
317         checkOpen();
318     }
319 
320     @Override
321     public void setBlob (final int i, final Blob x) throws SQLException {
322         checkOpen();
323     }
324 
325     @Override
326     public void setBlob(final int parameterIndex, final InputStream inputStream) throws SQLException {
327         throw new SQLException("Not implemented.");
328     }
329 
330     @Override
331     public void setBlob(final int parameterIndex, final InputStream inputStream, final long length) throws SQLException {
332         throw new SQLException("Not implemented.");
333     }
334 
335     @Override
336     public void setBoolean(final int parameterIndex, final boolean x) throws SQLException {
337         checkOpen();
338     }
339 
340     @Override
341     public void setByte(final int parameterIndex, final byte x) throws SQLException {
342         checkOpen();
343     }
344 
345     @Override
346     public void setBytes(final int parameterIndex, final byte[] x) throws SQLException {
347         checkOpen();
348     }
349 
350     @Override
351     public void setCharacterStream(final int parameterIndex, final java.io.Reader reader, final int length) throws SQLException {
352         checkOpen();
353     }
354 
355     @Override
356     public void setCharacterStream(final int parameterIndex, final Reader reader) throws SQLException {
357         throw new SQLException("Not implemented.");
358     }
359 
360     @Override
361     public void setCharacterStream(final int parameterIndex, final Reader reader, final long length) throws SQLException {
362         throw new SQLException("Not implemented.");
363     }
364 
365     @Override
366     public void setClob (final int i, final Clob x) throws SQLException {
367         checkOpen();
368     }
369 
370     @Override
371     public void setClob(final int parameterIndex, final Reader reader) throws SQLException {
372         throw new SQLException("Not implemented.");
373     }
374 
375     @Override
376     public void setClob(final int parameterIndex, final Reader reader, final long length) throws SQLException {
377         throw new SQLException("Not implemented.");
378     }
379 
380     @Override
381     public void setDate(final int parameterIndex, final java.sql.Date x) throws SQLException {
382         checkOpen();
383     }
384 
385     @Override
386     public void setDate(final int parameterIndex, final java.sql.Date x, final Calendar cal) throws SQLException {
387         checkOpen();
388     }
389 
390     @Override
391     public void setDouble(final int parameterIndex, final double x) throws SQLException {
392         checkOpen();
393     }
394 
395     @Override
396     public void setFloat(final int parameterIndex, final float x) throws SQLException {
397         checkOpen();
398     }
399 
400     @Override
401     public void setInt(final int parameterIndex, final int x) throws SQLException {
402         checkOpen();
403     }
404 
405     @Override
406     public void setLong(final int parameterIndex, final long x) throws SQLException {
407         checkOpen();
408     }
409 
410     @Override
411     public void setNCharacterStream(final int parameterIndex, final Reader reader) throws SQLException {
412         throw new SQLException("Not implemented.");
413     }
414 
415     @Override
416     public void setNCharacterStream(final int parameterIndex, final Reader value, final long length) throws SQLException {
417         throw new SQLException("Not implemented.");
418     }
419 
420     @Override
421     public void setNClob(final int parameterIndex, final NClob value) throws SQLException {
422         throw new SQLException("Not implemented.");
423     }
424 
425     @Override
426     public void setNClob(final int parameterIndex, final Reader reader) throws SQLException {
427         throw new SQLException("Not implemented.");
428     }
429 
430     @Override
431     public void setNClob(final int parameterIndex, final Reader reader, final long length) throws SQLException {
432         throw new SQLException("Not implemented.");
433     }
434 
435     @Override
436     public void setNString(final int parameterIndex, final String value) throws SQLException {
437         throw new SQLException("Not implemented.");
438     }
439 
440     @Override
441     public void setNull(final int parameterIndex, final int sqlType) throws SQLException {
442         checkOpen();
443     }
444 
445     @Override
446     public void setNull (final int paramIndex, final int sqlType, final String typeName) throws SQLException {
447         checkOpen();
448     }
449 
450     @Override
451     public void setObject(final int parameterIndex, final Object x) throws SQLException {
452         checkOpen();
453     }
454 
455     @Override
456     public void setObject(final int parameterIndex, final Object x, final int targetSqlType) throws SQLException {
457         checkOpen();
458     }
459 
460     @Override
461     public void setObject(final int parameterIndex, final Object x, final int targetSqlType, final int scale) throws SQLException {
462         checkOpen();
463     }
464 
465     @Override
466     public void setObject(final int parameterIndex, final Object x, final SQLType targetSqlType) throws SQLException {
467         checkOpen();
468     }
469 
470     @Override
471     public void setObject(final int parameterIndex, final Object x, final SQLType targetSqlType, final int scaleOrLength) throws SQLException {
472         checkOpen();
473     }
474 
475     @Override
476     public void setRef (final int i, final Ref x) throws SQLException {
477         checkOpen();
478     }
479 
480     @Override
481     public void setRowId(final int parameterIndex, final RowId value) throws SQLException {
482         throw new SQLException("Not implemented.");
483     }
484 
485     @Override
486     public void setShort(final int parameterIndex, final short x) throws SQLException {
487         checkOpen();
488     }
489 
490     @Override
491     public void setSQLXML(final int parameterIndex, final SQLXML value) throws SQLException {
492         throw new SQLException("Not implemented.");
493     }
494 
495     @Override
496     public void setString(final int parameterIndex, final String x) throws SQLException {
497         checkOpen();
498     }
499 
500     @Override
501     public void setTime(final int parameterIndex, final java.sql.Time x) throws SQLException {
502         checkOpen();
503     }
504 
505     @Override
506     public void setTime(final int parameterIndex, final java.sql.Time x, final Calendar cal) throws SQLException {
507         checkOpen();
508     }
509 
510     @Override
511     public void setTimestamp(final int parameterIndex, final java.sql.Timestamp x) throws SQLException {
512         checkOpen();
513     }
514 
515     @Override
516     public void setTimestamp(final int parameterIndex, final java.sql.Timestamp x, final Calendar cal) throws SQLException {
517         checkOpen();
518     }
519 
520     /** @deprecated */
521     @Deprecated
522     @Override
523     public void setUnicodeStream(final int parameterIndex, final java.io.InputStream x, final int length) throws SQLException {
524         checkOpen();
525     }
526 
527     @Override
528     public void setURL(final int parameterIndex, final java.net.URL x) throws SQLException {
529         throw new SQLException("Not implemented.");
530     }
531 
532     @Override
533     public String toString() {
534         return _sql;
535     }
536 }