1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.dbcp2;
19
20 import java.sql.Connection;
21 import java.sql.ResultSet;
22 import java.sql.SQLException;
23 import java.sql.SQLWarning;
24 import java.sql.Statement;
25
26
27
28
29 public class TesterStatement extends AbandonedTrace implements Statement {
30
31 protected final Connection _connection;
32 protected boolean _open = true;
33 protected final long _rowsUpdated = 1;
34 protected final boolean _executeResponse = true;
35 protected int _maxFieldSize = 1024;
36 protected long _maxRows = 1024;
37 protected boolean _escapeProcessing;
38 protected int _queryTimeout = 1000;
39 protected String _cursorName;
40 protected int _fetchDirection = 1;
41 protected int _fetchSize = 1;
42 protected int _resultSetConcurrency = 1;
43 protected int _resultSetType = 1;
44 private int _resultSetHoldability = 1;
45 protected ResultSet _resultSet;
46 protected boolean _sqlExceptionOnClose;
47
48 public TesterStatement(final Connection conn) {
49 _connection = conn;
50 }
51
52 public TesterStatement(final Connection conn, final int resultSetType, final int resultSetConcurrency) {
53 _connection = conn;
54 _resultSetType = resultSetType;
55 _resultSetConcurrency = resultSetConcurrency;
56 }
57
58 public TesterStatement(final Connection conn, final int resultSetType, final int resultSetConcurrency,
59 final int resultSetHoldability) {
60 _connection = conn;
61 _resultSetType = resultSetType;
62 _resultSetConcurrency = resultSetConcurrency;
63 _resultSetHoldability = resultSetHoldability;
64 }
65
66 @Override
67 public void addBatch(final String sql) throws SQLException {
68 checkOpen();
69 }
70
71 @Override
72 public void cancel() throws SQLException {
73 checkOpen();
74 }
75
76 protected void checkOpen() throws SQLException {
77 if (!_open) {
78 throw new SQLException("Connection is closed.");
79 }
80 }
81
82 @Override
83 public void clearBatch() throws SQLException {
84 checkOpen();
85 }
86
87 @Override
88 public void clearWarnings() throws SQLException {
89 checkOpen();
90 }
91
92 @Override
93 public void close() throws SQLException {
94 if (_sqlExceptionOnClose) {
95 throw new SQLException("TestSQLExceptionOnClose");
96 }
97
98
99 if (!_open) {
100 return;
101 }
102
103 _open = false;
104 if (_resultSet != null) {
105 _resultSet.close();
106 _resultSet = null;
107 }
108 }
109
110 @Override
111 public void closeOnCompletion() throws SQLException {
112 throw new SQLException("Not implemented.");
113 }
114
115 @Override
116 public boolean execute(final String sql) throws SQLException {
117 checkOpen();
118 if ("invalid".equals(sql)) {
119 throw new SQLException("invalid query");
120 }
121 return _executeResponse;
122 }
123
124 @Override
125 public boolean execute(final String sql, final int autoGeneratedKeys)
126 throws SQLException {
127 throw new SQLException("Not implemented.");
128 }
129
130 @Override
131 public boolean execute(final String sql, final int[] columnIndexes)
132 throws SQLException {
133 throw new SQLException("Not implemented.");
134 }
135
136 @Override
137 public boolean execute(final String sql, final String[] columnNames)
138 throws SQLException {
139 throw new SQLException("Not implemented.");
140 }
141
142 @Override
143 public int[] executeBatch() throws SQLException {
144 checkOpen();
145 return new int[0];
146 }
147
148 @Override
149 public long[] executeLargeBatch() throws SQLException {
150 checkOpen();
151 return new long[0];
152 }
153
154 @Override
155 public long executeLargeUpdate(final String sql) throws SQLException {
156 checkOpen();
157 return _rowsUpdated;
158 }
159
160 @Override
161 public long executeLargeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException {
162 throw new SQLException("Not implemented.");
163 }
164
165 @Override
166 public long executeLargeUpdate(final String sql, final int[] columnIndexes) throws SQLException {
167 throw new SQLException("Not implemented.");
168 }
169
170 @Override
171 public long executeLargeUpdate(final String sql, final String[] columnNames) throws SQLException {
172 throw new SQLException("Not implemented.");
173 }
174
175 @Override
176 public ResultSet executeQuery(final String sql) throws SQLException {
177 checkOpen();
178 if ("null".equals(sql)) {
179 return null;
180 }
181 if ("invalid".equals(sql)) {
182 throw new SQLException("invalid query");
183 }
184 if ("broken".equals(sql)) {
185 throw new SQLException("broken connection");
186 }
187 if ("select username".equals(sql)) {
188 final String userName = ((TesterConnection) _connection).getUserName();
189 final Object[][] data = { { userName } };
190 return new TesterResultSet(this, data);
191 }
192
193 if (_queryTimeout > 0 && _queryTimeout < 5) {
194 throw new SQLException("query timeout");
195 }
196 return new TesterResultSet(this);
197 }
198
199 @Override
200 public int executeUpdate(final String sql) throws SQLException {
201 checkOpen();
202 return (int) _rowsUpdated;
203 }
204
205 @Override
206 public int executeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException {
207 throw new SQLException("Not implemented.");
208 }
209
210 @Override
211 public int executeUpdate(final String sql, final int[] columnIndexes) throws SQLException {
212 throw new SQLException("Not implemented.");
213 }
214
215 @Override
216 public int executeUpdate(final String sql, final String[] columnNames) throws SQLException {
217 throw new SQLException("Not implemented.");
218 }
219
220 @Override
221 public Connection getConnection() throws SQLException {
222 checkOpen();
223 return _connection;
224 }
225
226 @Override
227 public int getFetchDirection() throws SQLException {
228 checkOpen();
229 return _fetchDirection;
230 }
231
232 @Override
233 public int getFetchSize() throws SQLException {
234 checkOpen();
235 return _fetchSize;
236 }
237
238 @Override
239 public ResultSet getGeneratedKeys() throws SQLException {
240 return new TesterResultSet(this);
241 }
242
243 @Override
244 public long getLargeMaxRows() throws SQLException {
245 checkOpen();
246 return _maxRows;
247 }
248
249 @Override
250 public long getLargeUpdateCount() throws SQLException {
251 checkOpen();
252 return _rowsUpdated;
253 }
254
255 @Override
256 public int getMaxFieldSize() throws SQLException {
257 checkOpen();
258 return _maxFieldSize;
259 }
260
261 @Override
262 public int getMaxRows() throws SQLException {
263 checkOpen();
264 return (int) _maxRows;
265 }
266
267 @Override
268 public boolean getMoreResults() throws SQLException {
269 checkOpen();
270 return false;
271 }
272
273 @Override
274 public boolean getMoreResults(final int current) throws SQLException {
275 throw new SQLException("Not implemented.");
276 }
277
278 @Override
279 public int getQueryTimeout() throws SQLException {
280 checkOpen();
281 return _queryTimeout;
282 }
283
284 @Override
285 public ResultSet getResultSet() throws SQLException {
286 checkOpen();
287 if (_resultSet == null) {
288 _resultSet = new TesterResultSet(this);
289 }
290 return _resultSet;
291 }
292
293 @Override
294 public int getResultSetConcurrency() throws SQLException {
295 checkOpen();
296 return _resultSetConcurrency;
297 }
298
299 @Override
300 public int getResultSetHoldability() throws SQLException {
301 checkOpen();
302 return _resultSetHoldability;
303 }
304
305 @Override
306 public int getResultSetType() throws SQLException {
307 checkOpen();
308 return _resultSetType;
309 }
310
311 @Override
312 public int getUpdateCount() throws SQLException {
313 checkOpen();
314 return (int) _rowsUpdated;
315 }
316
317 @Override
318 public SQLWarning getWarnings() throws SQLException {
319 checkOpen();
320 return null;
321 }
322
323 @Override
324 public boolean isClosed() throws SQLException {
325 return !_open;
326 }
327
328 @Override
329 public boolean isCloseOnCompletion() throws SQLException {
330 throw new SQLException("Not implemented.");
331 }
332
333 @Override
334 public boolean isPoolable() throws SQLException {
335 throw new SQLException("Not implemented.");
336 }
337
338 public boolean isSqlExceptionOnClose() {
339 return _sqlExceptionOnClose;
340 }
341
342 @Override
343 public boolean isWrapperFor(final Class<?> iface) throws SQLException {
344 throw new SQLException("Not implemented.");
345 }
346
347 @Override
348 public void setCursorName(final String name) throws SQLException {
349 checkOpen();
350 _cursorName = name;
351 }
352
353 @Override
354 public void setEscapeProcessing(final boolean enable) throws SQLException {
355 checkOpen();
356 _escapeProcessing = enable;
357 }
358
359 @Override
360 public void setFetchDirection(final int direction) throws SQLException {
361 checkOpen();
362 _fetchDirection = direction;
363 }
364
365 @Override
366 public void setFetchSize(final int rows) throws SQLException {
367 checkOpen();
368 _fetchSize = rows;
369 }
370
371 @Override
372 public void setLargeMaxRows(final long max) throws SQLException {
373 checkOpen();
374 _maxRows = max;
375 }
376
377 @Override
378 public void setMaxFieldSize(final int max) throws SQLException {
379 checkOpen();
380 _maxFieldSize = max;
381 }
382
383 @Override
384 public void setMaxRows(final int max) throws SQLException {
385 checkOpen();
386 _maxRows = max;
387 }
388
389 @Override
390 public void setPoolable(final boolean poolable) throws SQLException {
391 throw new SQLException("Not implemented.");
392 }
393
394 @Override
395 public void setQueryTimeout(final int seconds) throws SQLException {
396 checkOpen();
397 _queryTimeout = seconds;
398 }
399
400 public void setSqlExceptionOnClose(final boolean _sqlExceptionOnClose) {
401 this._sqlExceptionOnClose = _sqlExceptionOnClose;
402 }
403
404 @Override
405 public <T> T unwrap(final Class<T> iface) throws SQLException {
406 throw new SQLException("Not implemented.");
407 }
408 }