1 | /* |
2 | |
3 | Derby - Class org.apache.derby.iapi.jdbc.BrokeredStatement |
4 | |
5 | Copyright 2003, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.iapi.jdbc; |
22 | |
23 | import org.apache.derby.iapi.reference.JDBC30Translation; |
24 | import org.apache.derby.iapi.reference.SQLState; |
25 | |
26 | import org.apache.derby.iapi.error.StandardException; |
27 | import org.apache.derby.iapi.error.PublicAPI; |
28 | import org.apache.derby.iapi.services.info.JVMInfo; |
29 | import org.apache.derby.impl.jdbc.Util; |
30 | |
31 | import java.sql.Connection; |
32 | import java.sql.ResultSet; |
33 | import java.sql.SQLException; |
34 | import java.sql.SQLWarning; |
35 | import java.sql.Statement; |
36 | |
37 | import java.lang.reflect.*; |
38 | |
39 | /** |
40 | A Statement implementation that forwards all of its requests to an underlying Statement. |
41 | */ |
42 | public class BrokeredStatement implements EngineStatement |
43 | { |
44 | |
45 | /** |
46 | My control. Use the controlCheck() method to obtain the control |
47 | when calling a check method. This will result in the correct exception |
48 | being thrown if the statement is already closed. |
49 | */ |
50 | final BrokeredStatementControl control; |
51 | |
52 | final int jdbcLevel; |
53 | final int resultSetType; |
54 | final int resultSetConcurrency; |
55 | final int resultSetHoldability; |
56 | |
57 | /** |
58 | My state |
59 | */ |
60 | private String cursorName; |
61 | private Boolean escapeProcessing; |
62 | |
63 | BrokeredStatement(BrokeredStatementControl control, int jdbcLevel) throws SQLException |
64 | { |
65 | this.control = control; |
66 | this.jdbcLevel = jdbcLevel; |
67 | |
68 | // save the state of the Statement while we are pretty much guaranteed the |
69 | // underlying statement is open. |
70 | resultSetType = getResultSetType(); |
71 | resultSetConcurrency = getResultSetConcurrency(); |
72 | |
73 | resultSetHoldability = getResultSetHoldability(); |
74 | } |
75 | |
76 | |
77 | public final void addBatch(String sql) |
78 | throws SQLException |
79 | { |
80 | getStatement().addBatch( sql); |
81 | } |
82 | |
83 | public final void clearBatch() |
84 | throws SQLException |
85 | { |
86 | getStatement().clearBatch(); |
87 | } |
88 | |
89 | public final int[] executeBatch() |
90 | throws SQLException |
91 | { |
92 | return getStatement().executeBatch(); |
93 | } |
94 | |
95 | |
96 | public final void cancel() |
97 | throws SQLException |
98 | { |
99 | getStatement().cancel(); |
100 | } |
101 | |
102 | public final boolean execute(String sql) throws SQLException |
103 | { |
104 | return getStatement().execute(sql); |
105 | } |
106 | |
107 | public final ResultSet executeQuery(String sql) throws SQLException |
108 | { |
109 | return wrapResultSet(getStatement().executeQuery(sql)); |
110 | } |
111 | |
112 | public final int executeUpdate(String sql) throws SQLException |
113 | { |
114 | return getStatement().executeUpdate(sql); |
115 | } |
116 | |
117 | |
118 | /** |
119 | * In many cases, it is desirable to immediately release a |
120 | * Statements's database and JDBC resources instead of waiting for |
121 | * this to happen when it is automatically closed; the close |
122 | * method provides this immediate release. |
123 | * |
124 | * <P><B>Note:</B> A Statement is automatically closed when it is |
125 | * garbage collected. When a Statement is closed its current |
126 | * ResultSet, if one exists, is also closed. |
127 | * @exception SQLException thrown on failure. |
128 | */ |
129 | public final void close() throws SQLException |
130 | { |
131 | getStatement().close(); |
132 | } |
133 | |
134 | public final Connection getConnection() |
135 | throws SQLException |
136 | { |
137 | return getStatement().getConnection(); |
138 | } |
139 | |
140 | public final int getFetchDirection() |
141 | throws SQLException |
142 | { |
143 | return getStatement().getFetchDirection(); |
144 | } |
145 | |
146 | public final int getFetchSize() |
147 | throws SQLException |
148 | { |
149 | return getStatement().getFetchSize(); |
150 | } |
151 | |
152 | public final int getMaxFieldSize() |
153 | throws SQLException |
154 | { |
155 | return getStatement().getMaxFieldSize(); |
156 | } |
157 | |
158 | public final int getMaxRows() |
159 | throws SQLException |
160 | { |
161 | return getStatement().getMaxRows(); |
162 | } |
163 | |
164 | public final int getResultSetConcurrency() |
165 | throws SQLException |
166 | { |
167 | return getStatement().getResultSetConcurrency(); |
168 | } |
169 | |
170 | /** |
171 | * The maxFieldSize limit (in bytes) is set to limit the size of |
172 | * data that can be returned for any column value; it only applies |
173 | * to BINARY, VARBINARY, LONGVARBINARY, CHAR, VARCHAR, and |
174 | * LONGVARCHAR fields. If the limit is exceeded, the excess data |
175 | * is silently discarded. |
176 | * |
177 | * @param max the new max column size limit; zero means unlimited |
178 | * @exception SQLException thrown on failure. |
179 | */ |
180 | public final void setMaxFieldSize(int max) throws SQLException |
181 | { |
182 | getStatement().setMaxFieldSize(max); |
183 | } |
184 | |
185 | /** |
186 | * The maxRows limit is set to limit the number of rows that any |
187 | * ResultSet can contain. If the limit is exceeded, the excess |
188 | * rows are silently dropped. |
189 | * |
190 | * @param max the new max rows limit; zero means unlimited |
191 | * @exception SQLException thrown on failure. |
192 | */ |
193 | public final void setMaxRows(int max) throws SQLException |
194 | { |
195 | getStatement().setMaxRows( max); |
196 | } |
197 | |
198 | /** |
199 | * If escape scanning is on (the default) the driver will do |
200 | * escape substitution before sending the SQL to the database. |
201 | * |
202 | * @param enable true to enable; false to disable |
203 | * @exception SQLException thrown on failure. |
204 | */ |
205 | public final void setEscapeProcessing(boolean enable) throws SQLException |
206 | { |
207 | getStatement().setEscapeProcessing( enable); |
208 | escapeProcessing = enable ? Boolean.TRUE : Boolean.FALSE; |
209 | } |
210 | |
211 | /** |
212 | * The first warning reported by calls on this Statement is |
213 | * returned. A Statment's execute methods clear its SQLWarning |
214 | * chain. Subsequent Statement warnings will be chained to this |
215 | * SQLWarning. |
216 | * |
217 | * <p>The warning chain is automatically cleared each time |
218 | * a statement is (re)executed. |
219 | * |
220 | * <P><B>Note:</B> If you are processing a ResultSet then any |
221 | * warnings associated with ResultSet reads will be chained on the |
222 | * ResultSet object. |
223 | * |
224 | * @return the first SQLWarning or null |
225 | * @exception SQLException thrown on failure. |
226 | */ |
227 | public final SQLWarning getWarnings() throws SQLException |
228 | { |
229 | return getStatement().getWarnings(); |
230 | } |
231 | |
232 | /** |
233 | * After this call getWarnings returns null until a new warning is |
234 | * reported for this Statement. |
235 | * @exception SQLException thrown on failure. |
236 | */ |
237 | public final void clearWarnings() throws SQLException |
238 | { |
239 | getStatement().clearWarnings(); |
240 | } |
241 | |
242 | /** |
243 | * setCursorName defines the SQL cursor name that will be used by |
244 | * subsequent Statement execute methods. This name can then be |
245 | * used in SQL positioned update/delete statements to identify the |
246 | * current row in the ResultSet generated by this getStatement(). If |
247 | * the database doesn't support positioned update/delete, this |
248 | * method is a noop. |
249 | * |
250 | * <P><B>Note:</B> By definition, positioned update/delete |
251 | * execution must be done by a different Statement than the one |
252 | * which generated the ResultSet being used for positioning. Also, |
253 | * cursor names must be unique within a Connection. |
254 | * |
255 | * @param name the new cursor name. |
256 | */ |
257 | public final void setCursorName(String name) throws SQLException |
258 | { |
259 | getStatement().setCursorName( name); |
260 | cursorName = name; |
261 | } |
262 | |
263 | |
264 | /** |
265 | * getResultSet returns the current result as a ResultSet. It |
266 | * should only be called once per result. |
267 | * |
268 | * @return the current result as a ResultSet; null if the result |
269 | * is an update count or there are no more results or the statement |
270 | * was closed. |
271 | * @see #execute |
272 | */ |
273 | public final ResultSet getResultSet() throws SQLException |
274 | { |
275 | return wrapResultSet(getStatement().getResultSet()); |
276 | } |
277 | |
278 | /** |
279 | * getUpdateCount returns the current result as an update count; |
280 | * if the result is a ResultSet or there are no more results -1 |
281 | * is returned. It should only be called once per result. |
282 | * |
283 | * <P>The only way to tell for sure that the result is an update |
284 | * count is to first test to see if it is a ResultSet. If it is |
285 | * not a ResultSet it is either an update count or there are no |
286 | * more results. |
287 | * |
288 | * @return the current result as an update count; -1 if it is a |
289 | * ResultSet or there are no more results |
290 | * @see #execute |
291 | */ |
292 | public final int getUpdateCount() throws SQLException |
293 | { |
294 | return getStatement().getUpdateCount(); |
295 | } |
296 | |
297 | /** |
298 | * getMoreResults moves to a Statement's next result. It returns true if |
299 | * this result is a ResultSet. getMoreResults also implicitly |
300 | * closes any current ResultSet obtained with getResultSet. |
301 | * |
302 | * There are no more results when (!getMoreResults() && |
303 | * (getUpdateCount() == -1) |
304 | * |
305 | * @return true if the next result is a ResultSet; false if it is |
306 | * an update count or there are no more results |
307 | * @see #execute |
308 | * @exception SQLException thrown on failure. |
309 | */ |
310 | public final boolean getMoreResults() throws SQLException |
311 | { |
312 | return getStatement().getMoreResults(); |
313 | } |
314 | |
315 | /** |
316 | * JDBC 2.0 |
317 | * |
318 | * Determine the result set type. |
319 | * |
320 | * @exception SQLException Feature not implemented for now. |
321 | */ |
322 | public final int getResultSetType() |
323 | throws SQLException |
324 | { |
325 | return getStatement().getResultSetType(); |
326 | } |
327 | |
328 | /** |
329 | * JDBC 2.0 |
330 | * |
331 | * Give a hint as to the direction in which the rows in a result set |
332 | * will be processed. The hint applies only to result sets created |
333 | * using this Statement object. The default value is |
334 | * ResultSet.FETCH_FORWARD. |
335 | * |
336 | * @param direction the initial direction for processing rows |
337 | * @exception SQLException if a database-access error occurs or direction |
338 | * is not one of ResultSet.FETCH_FORWARD, ResultSet.FETCH_REVERSE, or |
339 | * ResultSet.FETCH_UNKNOWN |
340 | */ |
341 | public final void setFetchDirection(int direction) throws SQLException |
342 | { |
343 | getStatement().setFetchDirection( direction); |
344 | } |
345 | |
346 | /** |
347 | * JDBC 2.0 |
348 | * |
349 | * Give the JDBC driver a hint as to the number of rows that should |
350 | * be fetched from the database when more rows are needed. The number |
351 | * of rows specified only affects result sets created using this |
352 | * getStatement(). If the value specified is zero, then the hint is ignored. |
353 | * The default value is zero. |
354 | * |
355 | * @param rows the number of rows to fetch |
356 | * @exception SQLException if a database-access error occurs, or the |
357 | * condition 0 <= rows <= this.getMaxRows() is not satisfied. |
358 | */ |
359 | public final void setFetchSize(int rows) throws SQLException |
360 | { |
361 | getStatement().setFetchSize( rows); |
362 | } |
363 | |
364 | public final int getQueryTimeout() |
365 | throws SQLException |
366 | { |
367 | return getStatement().getQueryTimeout(); |
368 | } |
369 | |
370 | public final void setQueryTimeout(int seconds) |
371 | throws SQLException |
372 | { |
373 | getStatement().setQueryTimeout( seconds); |
374 | } |
375 | |
376 | |
377 | /* |
378 | ** JDBC 3.0 methods |
379 | */ |
380 | public final boolean execute(String sql, |
381 | int autoGeneratedKeys) |
382 | throws SQLException |
383 | { |
384 | |
385 | return getStatement().execute( sql, autoGeneratedKeys); |
386 | } |
387 | |
388 | |
389 | public final boolean execute(String sql, |
390 | int[] columnIndexes) |
391 | throws SQLException |
392 | { |
393 | return getStatement().execute( sql, columnIndexes); |
394 | } |
395 | |
396 | public final boolean execute(String sql, |
397 | String[] columnNames) |
398 | throws SQLException |
399 | { |
400 | return getStatement().execute( sql, columnNames); |
401 | } |
402 | |
403 | public final int executeUpdate(String sql, |
404 | int autoGeneratedKeys) |
405 | throws SQLException |
406 | { |
407 | int retVal = getStatement().executeUpdate( sql, autoGeneratedKeys); |
408 | return retVal; |
409 | } |
410 | |
411 | public final int executeUpdate(String sql, |
412 | int[] columnIndexes) |
413 | throws SQLException |
414 | { |
415 | return getStatement().executeUpdate( sql, columnIndexes); |
416 | } |
417 | |
418 | public final int executeUpdate(String sql, |
419 | String[] columnNames) |
420 | throws SQLException |
421 | { |
422 | |
423 | return getStatement().executeUpdate( sql, columnNames); |
424 | } |
425 | |
426 | |
427 | |
428 | /** |
429 | * JDBC 3.0 |
430 | * |
431 | * Moves to this Statement obect's next result, deals with any current ResultSet |
432 | * object(s) according to the instructions specified by the given flag, and |
433 | * returns true if the next result is a ResultSet object |
434 | * |
435 | * @param current - one of the following Statement constants indicating what |
436 | * should happen to current ResultSet objects obtained using the method |
437 | * getResultSetCLOSE_CURRENT_RESULT, KEEP_CURRENT_RESULT, or CLOSE_ALL_RESULTS |
438 | * @return true if the next result is a ResultSet; false if it is |
439 | * an update count or there are no more results |
440 | * @see #execute |
441 | * @exception SQLException thrown on failure. |
442 | */ |
443 | public final boolean getMoreResults(int current) throws SQLException |
444 | { |
445 | return ((EngineStatement) getStatement()).getMoreResults( current); |
446 | } |
447 | |
448 | /** |
449 | * JDBC 3.0 |
450 | * |
451 | * Retrieves any auto-generated keys created as a result of executing this |
452 | * Statement object. If this Statement object did not generate any keys, an empty |
453 | * ResultSet object is returned. If this Statement is a non-insert statement, |
454 | * an exception will be thrown. |
455 | * |
456 | * @return a ResultSet object containing the auto-generated key(s) generated by |
457 | * the execution of this Statement object |
458 | * @exception SQLException if a database access error occurs |
459 | */ |
460 | public final ResultSet getGeneratedKeys() throws SQLException |
461 | { |
462 | return wrapResultSet(getStatement().getGeneratedKeys()); |
463 | } |
464 | |
465 | /** |
466 | * Return the holdability of ResultSets created by this Statement. |
467 | * If this Statement is active in a global transaction the |
468 | * CLOSE_CURSORS_ON_COMMIT will be returned regardless of |
469 | * the holdability it was created with. In a local transaction |
470 | * the original create holdabilty will be returned. |
471 | */ |
472 | public final int getResultSetHoldability() |
473 | throws SQLException |
474 | { |
475 | int holdability = |
476 | ((EngineStatement) getStatement()).getResultSetHoldability(); |
477 | |
478 | // Holdability might be downgraded. |
479 | return controlCheck().checkHoldCursors(holdability); |
480 | } |
481 | |
482 | /* |
483 | ** Control methods |
484 | */ |
485 | |
486 | public Statement createDuplicateStatement(Connection conn, Statement oldStatement) throws SQLException { |
487 | |
488 | Statement newStatement; |
489 | |
490 | if (jdbcLevel == 2) |
491 | newStatement = conn.createStatement(resultSetType, resultSetConcurrency); |
492 | else |
493 | newStatement = conn.createStatement(resultSetType, resultSetConcurrency, |
494 | resultSetHoldability); |
495 | |
496 | setStatementState(oldStatement, newStatement); |
497 | |
498 | return newStatement; |
499 | } |
500 | |
501 | void setStatementState(Statement oldStatement, Statement newStatement) throws SQLException { |
502 | if (cursorName != null) |
503 | newStatement.setCursorName(cursorName); |
504 | if (escapeProcessing != null) |
505 | newStatement.setEscapeProcessing(escapeProcessing.booleanValue()); |
506 | |
507 | newStatement.setFetchDirection(oldStatement.getFetchDirection()); |
508 | newStatement.setFetchSize(oldStatement.getFetchSize()); |
509 | newStatement.setMaxFieldSize(oldStatement.getMaxFieldSize()); |
510 | newStatement.setMaxRows(oldStatement.getMaxRows()); |
511 | newStatement.setQueryTimeout(oldStatement.getQueryTimeout()); |
512 | } |
513 | |
514 | public Statement getStatement() throws SQLException { |
515 | return control.getRealStatement(); |
516 | } |
517 | |
518 | /** |
519 | * Provide the control access to every ResultSet we return. |
520 | * If required the control can wrap the ResultSet, but |
521 | * it (the control) must ensure a underlying ResultSet is |
522 | * only wrapped once, if say java.sql.Statement.getResultSet |
523 | * is returned twice. |
524 | * |
525 | * @param rs ResultSet being returned, can be null. |
526 | */ |
527 | final ResultSet wrapResultSet(ResultSet rs) { |
528 | return control.wrapResultSet(this, rs); |
529 | } |
530 | |
531 | /** |
532 | Get the BrokeredStatementControl in order to perform a check. |
533 | Obtained indirectly to ensure that the correct exception is |
534 | thrown if the Statement has been closed. |
535 | */ |
536 | final BrokeredStatementControl controlCheck() throws SQLException |
537 | { |
538 | // simplest method that will throw an exception if the Statement is closed |
539 | getStatement().getConnection(); |
540 | return control; |
541 | } |
542 | |
543 | /** |
544 | * Returns false unless <code>iface</code> is implemented |
545 | * |
546 | * @param iface a Class defining an interface. |
547 | * @return true if this implements the interface or |
548 | * directly or indirectly wraps an object |
549 | * that does. |
550 | * @throws java.sql.SQLException if an error occurs while determining |
551 | * whether this is a wrapper for an object |
552 | * with the given interface. |
553 | */ |
554 | public boolean isWrapperFor(Class iface) throws SQLException { |
555 | checkIfClosed(); |
556 | return iface.isInstance(this); |
557 | } |
558 | |
559 | /** |
560 | * Checks if the statement is closed. Not implemented for this |
561 | * class since <code>isClosed()</code> is a new method in JDBC |
562 | * 4.0. The JDBC 4.0 sub-classes should override this method. |
563 | * |
564 | * @return <code>true</code> if the statement is closed, |
565 | * <code>false</code> otherwise |
566 | * @exception SQLException not-implemented exception |
567 | */ |
568 | protected boolean isClosed() throws SQLException { |
569 | // Not implemented since we cannot forward the call to a JDBC |
570 | // 4.0 method from this class. This dummy implementation is |
571 | // provided here so that checkIfClosed() can be implemented |
572 | // once in this class instead of once in each of the |
573 | // Brokered*Statement40 classes. |
574 | throw Util.notImplemented(); |
575 | } |
576 | |
577 | /** |
578 | * Checks if the statement is closed and throws an exception if it |
579 | * is. This method relies on the <code>isClosed()</code> method |
580 | * and therefore only works with JDBC 4.0. |
581 | * |
582 | * @exception SQLException if the statement is closed |
583 | */ |
584 | protected final void checkIfClosed() |
585 | throws SQLException |
586 | { |
587 | if (isClosed()) { |
588 | throw Util.generateCsSQLException(SQLState.ALREADY_CLOSED, |
589 | "Statement"); |
590 | } |
591 | } |
592 | } |