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