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    *      https://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              this.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          this.sql = sql;
62          try {
63              this.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          this.sql = sql;
72          this.autoGeneratedKeys = autoGeneratedKeys;
73          try {
74              this.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          this.sql = sql;
83          try {
84              this.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          this.sql = sql;
94          try {
95              this.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         this.sql = sql;
104         this.columnIndexes = columnIndexes;
105         try {
106             this.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         this.sql = sql;
115         this.columnNames = columnNames;
116         try {
117             this.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         checkQueryTimeout();
193         return new TesterResultSet(this, resultSetType, resultSetConcurrency);
194     }
195 
196     @Override
197     public ResultSet executeQuery(final String sql) throws SQLException {
198         checkOpen();
199         if ("null".equals(sql)) {
200             return null;
201         }
202         checkQueryTimeout();
203         return new TesterResultSet(this, resultSetType, resultSetConcurrency);
204     }
205 
206     @Override
207     public int executeUpdate() throws SQLException {
208         checkOpen();
209         return (int) rowsUpdated;
210     }
211 
212     @Override
213     public int executeUpdate(final String sql) throws SQLException {
214         checkOpen();
215         return (int) rowsUpdated;
216     }
217 
218     @Override
219     public int executeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException {
220         checkOpen();
221         return 0;
222     }
223 
224     @Override
225     public int executeUpdate(final String sql, final int[] columnIndexes) throws SQLException {
226         checkOpen();
227         return 0;
228     }
229 
230     @Override
231     public int executeUpdate(final String sql, final String[] columnNames) throws SQLException {
232         checkOpen();
233         return 0;
234     }
235 
236     public int getAutoGeneratedKeys() {
237         return autoGeneratedKeys;
238     }
239 
240     public String getCatalog() {
241         return catalog;
242     }
243 
244     public int[] getColumnIndexes() {
245         return columnIndexes;
246     }
247 
248     public String[] getColumnNames() {
249         return columnNames;
250     }
251 
252     @Override
253     public ResultSet getGeneratedKeys() throws SQLException {
254         return new TesterResultSet(this, resultSetType, resultSetConcurrency);
255     }
256 
257     @Override
258     public ResultSetMetaData getMetaData() throws SQLException {
259         checkOpen();
260         return resultSetMetaData;
261     }
262 
263     @Override
264     public boolean getMoreResults(final int current) throws SQLException {
265         throw new SQLException("Not implemented.");
266     }
267 
268     @Override
269     public java.sql.ParameterMetaData getParameterMetaData() throws SQLException {
270         throw new SQLException("Not implemented.");
271     }
272 
273     /** For junit test only */
274     public String getSql() {
275         return sql;
276     }
277 
278     @Override
279     public void setArray (final int i, final Array x) throws SQLException {
280         checkOpen();
281     }
282 
283     @Override
284     public void setAsciiStream(final int parameterIndex, final InputStream inputStream) throws SQLException {
285         throw new SQLException("Not implemented.");
286     }
287 
288     @Override
289     public void setAsciiStream(final int parameterIndex, final InputStream inputStream, final long length) throws SQLException {
290         throw new SQLException("Not implemented.");
291     }
292 
293     @Override
294     public void setAsciiStream(final int parameterIndex, final java.io.InputStream x, final int length) throws SQLException {
295         checkOpen();
296     }
297 
298     @Override
299     public void setBigDecimal(final int parameterIndex, final BigDecimal x) throws SQLException {
300         checkOpen();
301     }
302 
303     @Override
304     public void setBinaryStream(final int parameterIndex, final InputStream inputStream) throws SQLException {
305         throw new SQLException("Not implemented.");
306     }
307 
308     @Override
309     public void setBinaryStream(final int parameterIndex, final InputStream inputStream, final long length) throws SQLException {
310         throw new SQLException("Not implemented.");
311     }
312 
313     @Override
314     public void setBinaryStream(final int parameterIndex, final java.io.InputStream x, final int length) throws SQLException {
315         checkOpen();
316     }
317 
318     @Override
319     public void setBlob (final int i, final Blob x) throws SQLException {
320         checkOpen();
321     }
322 
323     @Override
324     public void setBlob(final int parameterIndex, final InputStream inputStream) throws SQLException {
325         throw new SQLException("Not implemented.");
326     }
327 
328     @Override
329     public void setBlob(final int parameterIndex, final InputStream inputStream, final long length) throws SQLException {
330         throw new SQLException("Not implemented.");
331     }
332 
333     @Override
334     public void setBoolean(final int parameterIndex, final boolean x) throws SQLException {
335         checkOpen();
336     }
337 
338     @Override
339     public void setByte(final int parameterIndex, final byte x) throws SQLException {
340         checkOpen();
341     }
342 
343     @Override
344     public void setBytes(final int parameterIndex, final byte[] x) throws SQLException {
345         checkOpen();
346     }
347 
348     @Override
349     public void setCharacterStream(final int parameterIndex, final java.io.Reader reader, final int length) throws SQLException {
350         checkOpen();
351     }
352 
353     @Override
354     public void setCharacterStream(final int parameterIndex, final Reader reader) throws SQLException {
355         throw new SQLException("Not implemented.");
356     }
357 
358     @Override
359     public void setCharacterStream(final int parameterIndex, final Reader reader, final long length) throws SQLException {
360         throw new SQLException("Not implemented.");
361     }
362 
363     @Override
364     public void setClob (final int i, final Clob x) throws SQLException {
365         checkOpen();
366     }
367 
368     @Override
369     public void setClob(final int parameterIndex, final Reader reader) throws SQLException {
370         throw new SQLException("Not implemented.");
371     }
372 
373     @Override
374     public void setClob(final int parameterIndex, final Reader reader, final long length) throws SQLException {
375         throw new SQLException("Not implemented.");
376     }
377 
378     @Override
379     public void setDate(final int parameterIndex, final java.sql.Date x) throws SQLException {
380         checkOpen();
381     }
382 
383     @Override
384     public void setDate(final int parameterIndex, final java.sql.Date x, final Calendar cal) throws SQLException {
385         checkOpen();
386     }
387 
388     @Override
389     public void setDouble(final int parameterIndex, final double x) throws SQLException {
390         checkOpen();
391     }
392 
393     @Override
394     public void setFloat(final int parameterIndex, final float x) throws SQLException {
395         checkOpen();
396     }
397 
398     @Override
399     public void setInt(final int parameterIndex, final int x) throws SQLException {
400         checkOpen();
401     }
402 
403     @Override
404     public void setLong(final int parameterIndex, final long x) throws SQLException {
405         checkOpen();
406     }
407 
408     @Override
409     public void setNCharacterStream(final int parameterIndex, final Reader reader) throws SQLException {
410         throw new SQLException("Not implemented.");
411     }
412 
413     @Override
414     public void setNCharacterStream(final int parameterIndex, final Reader value, final long length) throws SQLException {
415         throw new SQLException("Not implemented.");
416     }
417 
418     @Override
419     public void setNClob(final int parameterIndex, final NClob value) throws SQLException {
420         throw new SQLException("Not implemented.");
421     }
422 
423     @Override
424     public void setNClob(final int parameterIndex, final Reader reader) throws SQLException {
425         throw new SQLException("Not implemented.");
426     }
427 
428     @Override
429     public void setNClob(final int parameterIndex, final Reader reader, final long length) throws SQLException {
430         throw new SQLException("Not implemented.");
431     }
432 
433     @Override
434     public void setNString(final int parameterIndex, final String value) throws SQLException {
435         throw new SQLException("Not implemented.");
436     }
437 
438     @Override
439     public void setNull(final int parameterIndex, final int sqlType) throws SQLException {
440         checkOpen();
441     }
442 
443     @Override
444     public void setNull (final int paramIndex, final int sqlType, final String typeName) throws SQLException {
445         checkOpen();
446     }
447 
448     @Override
449     public void setObject(final int parameterIndex, final Object x) throws SQLException {
450         checkOpen();
451     }
452 
453     @Override
454     public void setObject(final int parameterIndex, final Object x, final int targetSqlType) throws SQLException {
455         checkOpen();
456     }
457 
458     @Override
459     public void setObject(final int parameterIndex, final Object x, final int targetSqlType, final int scale) throws SQLException {
460         checkOpen();
461     }
462 
463     @Override
464     public void setObject(final int parameterIndex, final Object x, final SQLType targetSqlType) throws SQLException {
465         checkOpen();
466     }
467 
468     @Override
469     public void setObject(final int parameterIndex, final Object x, final SQLType targetSqlType, final int scaleOrLength) throws SQLException {
470         checkOpen();
471     }
472 
473     @Override
474     public void setRef (final int i, final Ref x) throws SQLException {
475         checkOpen();
476     }
477 
478     @Override
479     public void setRowId(final int parameterIndex, final RowId value) throws SQLException {
480         throw new SQLException("Not implemented.");
481     }
482 
483     @Override
484     public void setShort(final int parameterIndex, final short x) throws SQLException {
485         checkOpen();
486     }
487 
488     @Override
489     public void setSQLXML(final int parameterIndex, final SQLXML value) throws SQLException {
490         throw new SQLException("Not implemented.");
491     }
492 
493     @Override
494     public void setString(final int parameterIndex, final String x) throws SQLException {
495         checkOpen();
496     }
497 
498     @Override
499     public void setTime(final int parameterIndex, final java.sql.Time x) throws SQLException {
500         checkOpen();
501     }
502 
503     @Override
504     public void setTime(final int parameterIndex, final java.sql.Time x, final Calendar cal) throws SQLException {
505         checkOpen();
506     }
507 
508     @Override
509     public void setTimestamp(final int parameterIndex, final java.sql.Timestamp x) throws SQLException {
510         checkOpen();
511     }
512 
513     @Override
514     public void setTimestamp(final int parameterIndex, final java.sql.Timestamp x, final Calendar cal) throws SQLException {
515         checkOpen();
516     }
517 
518     /** @deprecated */
519     @Deprecated
520     @Override
521     public void setUnicodeStream(final int parameterIndex, final java.io.InputStream x, final int length) throws SQLException {
522         checkOpen();
523     }
524 
525     @Override
526     public void setURL(final int parameterIndex, final java.net.URL x) throws SQLException {
527         throw new SQLException("Not implemented.");
528     }
529 
530     @Override
531     public String toString() {
532         return sql;
533     }
534 }