1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.openjpa.lib.jdbc;
20
21 import java.io.InputStream;
22 import java.io.Reader;
23 import java.math.BigDecimal;
24 import java.net.URL;
25 import java.sql.Array;
26 import java.sql.Blob;
27 import java.sql.CallableStatement;
28 import java.sql.Clob;
29 import java.sql.Connection;
30 import java.sql.Date;
31 import java.sql.ParameterMetaData;
32 import java.sql.Ref;
33 import java.sql.ResultSet;
34 import java.sql.ResultSetMetaData;
35 import java.sql.SQLException;
36 import java.sql.SQLWarning;
37 import java.sql.Time;
38 import java.sql.Timestamp;
39 import java.util.Calendar;
40 import java.util.Map;
41
42 import org.apache.openjpa.lib.util.Closeable;
43
44 /**
45 * {@link CallableStatement} that delegates to an internal statement.
46 *
47 * @author Abe White
48 */
49 public class DelegatingCallableStatement
50 implements CallableStatement, Closeable {
51
52 private final CallableStatement _stmnt;
53 private final DelegatingCallableStatement _del;
54 private final Connection _conn;
55
56 public DelegatingCallableStatement(CallableStatement stmnt,
57 Connection conn) {
58 _conn = conn;
59 _stmnt = stmnt;
60 if (_stmnt instanceof DelegatingCallableStatement)
61 _del = (DelegatingCallableStatement) _stmnt;
62 else
63 _del = null;
64 }
65
66 private ResultSet wrapResult(boolean wrap, ResultSet rs) {
67 if (!wrap)
68 return rs;
69
70 // never wrap null
71 if (rs == null)
72 return null;
73
74 return new DelegatingResultSet(rs, this);
75 }
76
77 /**
78 * Return the wrapped statement.
79 */
80 public CallableStatement getDelegate() {
81 return _stmnt;
82 }
83
84 /**
85 * Return the base underlying data store statement.
86 */
87 public CallableStatement getInnermostDelegate() {
88 return (_del == null) ? _stmnt : _del.getInnermostDelegate();
89 }
90
91 public int hashCode() {
92 return getInnermostDelegate().hashCode();
93 }
94
95 public boolean equals(Object other) {
96 if (other == this)
97 return true;
98 if (other instanceof DelegatingCallableStatement)
99 other = ((DelegatingCallableStatement) other).
100 getInnermostDelegate();
101 return getInnermostDelegate().equals(other);
102 }
103
104 public String toString() {
105 StringBuffer buf = new StringBuffer("prepstmnt ").append(hashCode());
106 appendInfo(buf);
107 return buf.toString();
108 }
109
110 protected void appendInfo(StringBuffer buf) {
111 if (_del != null)
112 _del.appendInfo(buf);
113 }
114
115 public ResultSet executeQuery(String str) throws SQLException {
116 return executeQuery(true);
117 }
118
119 /**
120 * Execute the query, with the option of not wrapping it in a
121 * {@link DelegatingResultSet}, which is the default.
122 */
123 protected ResultSet executeQuery(String sql, boolean wrap)
124 throws SQLException {
125 ResultSet rs;
126 if (_del != null)
127 rs = _del.executeQuery(sql, false);
128 else
129 rs = _stmnt.executeQuery(sql);
130
131 return wrapResult(wrap, rs);
132 }
133
134 public int executeUpdate(String str) throws SQLException {
135 return _stmnt.executeUpdate(str);
136 }
137
138 public boolean execute(String str) throws SQLException {
139 return _stmnt.execute(str);
140 }
141
142 public void close() throws SQLException {
143 _stmnt.close();
144 }
145
146 public int getMaxFieldSize() throws SQLException {
147 return _stmnt.getMaxFieldSize();
148 }
149
150 public void setMaxFieldSize(int i) throws SQLException {
151 _stmnt.setMaxFieldSize(i);
152 }
153
154 public int getMaxRows() throws SQLException {
155 return _stmnt.getMaxRows();
156 }
157
158 public void setMaxRows(int i) throws SQLException {
159 _stmnt.setMaxRows(i);
160 }
161
162 public void setEscapeProcessing(boolean bool) throws SQLException {
163 _stmnt.setEscapeProcessing(bool);
164 }
165
166 public int getQueryTimeout() throws SQLException {
167 return _stmnt.getQueryTimeout();
168 }
169
170 public void setQueryTimeout(int i) throws SQLException {
171 _stmnt.setQueryTimeout(i);
172 }
173
174 public void cancel() throws SQLException {
175 _stmnt.cancel();
176 }
177
178 public SQLWarning getWarnings() throws SQLException {
179 return _stmnt.getWarnings();
180 }
181
182 public void clearWarnings() throws SQLException {
183 _stmnt.clearWarnings();
184 }
185
186 public void setCursorName(String str) throws SQLException {
187 _stmnt.setCursorName(str);
188 }
189
190 public ResultSet getResultSet() throws SQLException {
191 return getResultSet(true);
192 }
193
194 /**
195 * Get the last result set, with the option of not wrapping it in a
196 * {@link DelegatingResultSet}, which is the default.
197 */
198 protected ResultSet getResultSet(boolean wrap) throws SQLException {
199 ResultSet rs;
200 if (_del != null)
201 rs = _del.getResultSet(false);
202 else
203 rs = _stmnt.getResultSet();
204
205 return wrapResult(wrap, rs);
206 }
207
208 public int getUpdateCount() throws SQLException {
209 return _stmnt.getUpdateCount();
210 }
211
212 public boolean getMoreResults() throws SQLException {
213 return _stmnt.getMoreResults();
214 }
215
216 public void setFetchDirection(int i) throws SQLException {
217 _stmnt.setFetchDirection(i);
218 }
219
220 public int getFetchDirection() throws SQLException {
221 return _stmnt.getFetchDirection();
222 }
223
224 public void setFetchSize(int i) throws SQLException {
225 _stmnt.setFetchSize(i);
226 }
227
228 public int getFetchSize() throws SQLException {
229 return _stmnt.getFetchSize();
230 }
231
232 public int getResultSetConcurrency() throws SQLException {
233 return _stmnt.getResultSetConcurrency();
234 }
235
236 public int getResultSetType() throws SQLException {
237 return _stmnt.getResultSetType();
238 }
239
240 public void addBatch(String str) throws SQLException {
241 _stmnt.addBatch(str);
242 }
243
244 public void clearBatch() throws SQLException {
245 _stmnt.clearBatch();
246 }
247
248 public int[] executeBatch() throws SQLException {
249 return _stmnt.executeBatch();
250 }
251
252 public Connection getConnection() throws SQLException {
253 return _conn;
254 }
255
256 public ResultSet executeQuery() throws SQLException {
257 return executeQuery(true);
258 }
259
260 /**
261 * Execute the query, with the option of not wrapping it in a
262 * {@link DelegatingResultSet}, which is the default.
263 */
264 protected ResultSet executeQuery(boolean wrap) throws SQLException {
265 ResultSet rs;
266 if (_del != null)
267 rs = _del.executeQuery(false);
268 else
269 rs = _stmnt.executeQuery();
270
271 return wrapResult(wrap, rs);
272 }
273
274 public int executeUpdate() throws SQLException {
275 return _stmnt.executeUpdate();
276 }
277
278 public void setNull(int i1, int i2) throws SQLException {
279 _stmnt.setNull(i1, i2);
280 }
281
282 public void setBoolean(int i, boolean b) throws SQLException {
283 _stmnt.setBoolean(i, b);
284 }
285
286 public void setByte(int i, byte b) throws SQLException {
287 _stmnt.setByte(i, b);
288 }
289
290 public void setShort(int i, short s) throws SQLException {
291 _stmnt.setShort(i, s);
292 }
293
294 public void setInt(int i1, int i2) throws SQLException {
295 _stmnt.setInt(i1, i2);
296 }
297
298 public void setLong(int i, long l) throws SQLException {
299 _stmnt.setLong(i, l);
300 }
301
302 public void setFloat(int i, float f) throws SQLException {
303 _stmnt.setFloat(i, f);
304 }
305
306 public void setDouble(int i, double d) throws SQLException {
307 _stmnt.setDouble(i, d);
308 }
309
310 public void setBigDecimal(int i, BigDecimal bd) throws SQLException {
311 _stmnt.setBigDecimal(i, bd);
312 }
313
314 public void setString(int i, String s) throws SQLException {
315 _stmnt.setString(i, s);
316 }
317
318 public void setBytes(int i, byte[] b) throws SQLException {
319 _stmnt.setBytes(i, b);
320 }
321
322 public void setDate(int i, Date d) throws SQLException {
323 _stmnt.setDate(i, d);
324 }
325
326 public void setTime(int i, Time t) throws SQLException {
327 _stmnt.setTime(i, t);
328 }
329
330 public void setTimestamp(int i, Timestamp t) throws SQLException {
331 _stmnt.setTimestamp(i, t);
332 }
333
334 public void setAsciiStream(int i1, InputStream is, int i2)
335 throws SQLException {
336 _stmnt.setAsciiStream(i1, is, i2);
337 }
338
339 public void setUnicodeStream(int i1, InputStream is, int i2)
340 throws SQLException {
341 _stmnt.setUnicodeStream(i1, is, i2);
342 }
343
344 public void setBinaryStream(int i1, InputStream is, int i2)
345 throws SQLException {
346 _stmnt.setBinaryStream(i1, is, i2);
347 }
348
349 public void clearParameters() throws SQLException {
350 _stmnt.clearParameters();
351 }
352
353 public void setObject(int i1, Object o, int i2, int i3)
354 throws SQLException {
355 _stmnt.setObject(i1, o, i2, i3);
356 }
357
358 public void setObject(int i1, Object o, int i2) throws SQLException {
359 _stmnt.setObject(i1, o, i2);
360 }
361
362 public void setObject(int i, Object o) throws SQLException {
363 _stmnt.setObject(i, o);
364 }
365
366 public boolean execute() throws SQLException {
367 return _stmnt.execute();
368 }
369
370 public void addBatch() throws SQLException {
371 _stmnt.addBatch();
372 }
373
374 public void setCharacterStream(int i1, Reader r, int i2)
375 throws SQLException {
376 _stmnt.setCharacterStream(i1, r, i2);
377 }
378
379 public void setRef(int i, Ref r) throws SQLException {
380 _stmnt.setRef(i, r);
381 }
382
383 public void setBlob(int i, Blob b) throws SQLException {
384 _stmnt.setBlob(i, b);
385 }
386
387 public void setClob(int i, Clob c) throws SQLException {
388 _stmnt.setClob(i, c);
389 }
390
391 public void setArray(int i, Array a) throws SQLException {
392 _stmnt.setArray(i, a);
393 }
394
395 public ResultSetMetaData getMetaData() throws SQLException {
396 return _stmnt.getMetaData();
397 }
398
399 public void setDate(int i, Date d, Calendar c) throws SQLException {
400 _stmnt.setDate(i, d, c);
401 }
402
403 public void setTime(int i, Time t, Calendar c) throws SQLException {
404 _stmnt.setTime(i, t, c);
405 }
406
407 public void setTimestamp(int i, Timestamp t, Calendar c)
408 throws SQLException {
409 _stmnt.setTimestamp(i, t, c);
410 }
411
412 public void setNull(int i1, int i2, String s) throws SQLException {
413 _stmnt.setNull(i1, i2, s);
414 }
415
416 // JDBC 3.0 (unsupported) methods follow; these are required to be able
417 // to compile against JDK 1.4
418
419 public boolean getMoreResults(int i) throws SQLException {
420 throw new UnsupportedOperationException();
421 }
422
423 public ResultSet getGeneratedKeys() throws SQLException {
424 throw new UnsupportedOperationException();
425 }
426
427 public int executeUpdate(String s, int i) throws SQLException {
428 throw new UnsupportedOperationException();
429 }
430
431 public int executeUpdate(String s, int[] ia) throws SQLException {
432 throw new UnsupportedOperationException();
433 }
434
435 public int executeUpdate(String s, String[] sa) throws SQLException {
436 throw new UnsupportedOperationException();
437 }
438
439 public boolean execute(String s, int i) throws SQLException {
440 throw new UnsupportedOperationException();
441 }
442
443 public boolean execute(String s, int[] ia) throws SQLException {
444 throw new UnsupportedOperationException();
445 }
446
447 public boolean execute(String s, String[] sa) throws SQLException {
448 throw new UnsupportedOperationException();
449 }
450
451 public int getResultSetHoldability() throws SQLException {
452 throw new UnsupportedOperationException();
453 }
454
455 public void setURL(int i, URL url) throws SQLException {
456 throw new UnsupportedOperationException();
457 }
458
459 public ParameterMetaData getParameterMetaData() throws SQLException {
460 throw new UnsupportedOperationException();
461 }
462
463 /////////////////////////////
464 // CallableStatement methods
465 /////////////////////////////
466
467 public void registerOutParameter(int i1, int i2) throws SQLException {
468 _stmnt.registerOutParameter(i1, i2);
469 }
470
471 public void registerOutParameter(int i1, int i2, int i3)
472 throws SQLException {
473 _stmnt.registerOutParameter(i1, i2, i3);
474 }
475
476 public boolean wasNull() throws SQLException {
477 return _stmnt.wasNull();
478 }
479
480 public String getString(int i) throws SQLException {
481 return _stmnt.getString(i);
482 }
483
484 public boolean getBoolean(int i) throws SQLException {
485 return _stmnt.getBoolean(i);
486 }
487
488 public byte getByte(int i) throws SQLException {
489 return _stmnt.getByte(i);
490 }
491
492 public short getShort(int i) throws SQLException {
493 return _stmnt.getShort(i);
494 }
495
496 public int getInt(int i) throws SQLException {
497 return _stmnt.getInt(i);
498 }
499
500 public long getLong(int i) throws SQLException {
501 return _stmnt.getLong(i);
502 }
503
504 public float getFloat(int i) throws SQLException {
505 return _stmnt.getFloat(i);
506 }
507
508 public double getDouble(int i) throws SQLException {
509 return _stmnt.getDouble(i);
510 }
511
512 public BigDecimal getBigDecimal(int a, int b) throws SQLException {
513 return _stmnt.getBigDecimal(a, b);
514 }
515
516 public byte[] getBytes(int i) throws SQLException {
517 return _stmnt.getBytes(i);
518 }
519
520 public Date getDate(int i) throws SQLException {
521 return _stmnt.getDate(i);
522 }
523
524 public Time getTime(int i) throws SQLException {
525 return _stmnt.getTime(i);
526 }
527
528 public Timestamp getTimestamp(int i) throws SQLException {
529 return _stmnt.getTimestamp(i);
530 }
531
532 public Object getObject(int i) throws SQLException {
533 return _stmnt.getObject(i);
534 }
535
536 public BigDecimal getBigDecimal(int i) throws SQLException {
537 return _stmnt.getBigDecimal(i);
538 }
539
540 public Object getObject(int i, Map m) throws SQLException {
541 return _stmnt.getObject(i, m);
542 }
543
544 public Ref getRef(int i) throws SQLException {
545 return _stmnt.getRef(i);
546 }
547
548 public Blob getBlob(int i) throws SQLException {
549 return _stmnt.getBlob(i);
550 }
551
552 public Clob getClob(int i) throws SQLException {
553 return _stmnt.getClob(i);
554 }
555
556 public Array getArray(int i) throws SQLException {
557 return _stmnt.getArray(i);
558 }
559
560 public Date getDate(int i, Calendar c) throws SQLException {
561 return _stmnt.getDate(i, c);
562 }
563
564 public Time getTime(int i, Calendar c) throws SQLException {
565 return _stmnt.getTime(i, c);
566 }
567
568 public Timestamp getTimestamp(int i, Calendar c) throws SQLException {
569 return _stmnt.getTimestamp(i, c);
570 }
571
572 public void registerOutParameter(int i1, int i2, String s)
573 throws SQLException {
574 _stmnt.registerOutParameter(i1, i2, s);
575 }
576
577 // JDBC 3.0 (unsupported) methods follow; these are required to be able
578 // to compile against JDK 1.4
579
580 public void registerOutParameter(String s, int i) throws SQLException {
581 throw new UnsupportedOperationException();
582 }
583
584 public void registerOutParameter(String s, int i1, int i2)
585 throws SQLException {
586 throw new UnsupportedOperationException();
587 }
588
589 public void registerOutParameter(String s1, int i, String s2)
590 throws SQLException {
591 throw new UnsupportedOperationException();
592 }
593
594 public URL getURL(int i) throws SQLException {
595 throw new UnsupportedOperationException();
596 }
597
598 public void setURL(String a, URL b) throws SQLException {
599 throw new UnsupportedOperationException();
600 }
601
602 public URL getURL(String a) throws SQLException {
603 throw new UnsupportedOperationException();
604 }
605
606 public void setNull(String a, int b) throws SQLException {
607 throw new UnsupportedOperationException();
608 }
609
610 public void setBoolean(String a, boolean b) throws SQLException {
611 throw new UnsupportedOperationException();
612 }
613
614 public void setByte(String a, byte b) throws SQLException {
615 throw new UnsupportedOperationException();
616 }
617
618 public void setShort(String a, short b) throws SQLException {
619 throw new UnsupportedOperationException();
620 }
621
622 public void setInt(String a, int b) throws SQLException {
623 throw new UnsupportedOperationException();
624 }
625
626 public void setLong(String a, long b) throws SQLException {
627 throw new UnsupportedOperationException();
628 }
629
630 public void setFloat(String a, float b) throws SQLException {
631 throw new UnsupportedOperationException();
632 }
633
634 public void setDouble(String a, double b) throws SQLException {
635 throw new UnsupportedOperationException();
636 }
637
638 public void setBigDecimal(String a, BigDecimal b) throws SQLException {
639 throw new UnsupportedOperationException();
640 }
641
642 public void setString(String a, String b) throws SQLException {
643 throw new UnsupportedOperationException();
644 }
645
646 public void setBytes(String a, byte[] b) throws SQLException {
647 throw new UnsupportedOperationException();
648 }
649
650 public void setDate(String a, Date b) throws SQLException {
651 throw new UnsupportedOperationException();
652 }
653
654 public void setTime(String a, Time b) throws SQLException {
655 throw new UnsupportedOperationException();
656 }
657
658 public void setTimestamp(String a, Timestamp b) throws SQLException {
659 throw new UnsupportedOperationException();
660 }
661
662 public void setAsciiStream(String a, InputStream b, int c)
663 throws SQLException {
664 throw new UnsupportedOperationException();
665 }
666
667 public void setBinaryStream(String a, InputStream b, int c)
668 throws SQLException {
669 throw new UnsupportedOperationException();
670 }
671
672 public void setObject(String a, Object b, int c, int d)
673 throws SQLException {
674 throw new UnsupportedOperationException();
675 }
676
677 public void setObject(String a, Object b, int c) throws SQLException {
678 throw new UnsupportedOperationException();
679 }
680
681 public void setObject(String a, Object b) throws SQLException {
682 throw new UnsupportedOperationException();
683 }
684
685 public void setCharacterStream(String a, Reader b, int c)
686 throws SQLException {
687 throw new UnsupportedOperationException();
688 }
689
690 public void setDate(String a, Date b, Calendar c) throws SQLException {
691 throw new UnsupportedOperationException();
692 }
693
694 public void setTime(String a, Time b, Calendar c) throws SQLException {
695 throw new UnsupportedOperationException();
696 }
697
698 public void setTimestamp(String a, Timestamp b, Calendar c)
699 throws SQLException {
700 throw new UnsupportedOperationException();
701 }
702
703 public void setNull(String a, int b, String c) throws SQLException {
704 throw new UnsupportedOperationException();
705 }
706
707 public String getString(String a) throws SQLException {
708 throw new UnsupportedOperationException();
709 }
710
711 public boolean getBoolean(String a) throws SQLException {
712 throw new UnsupportedOperationException();
713 }
714
715 public byte getByte(String a) throws SQLException {
716 throw new UnsupportedOperationException();
717 }
718
719 public short getShort(String a) throws SQLException {
720 throw new UnsupportedOperationException();
721 }
722
723 public int getInt(String a) throws SQLException {
724 throw new UnsupportedOperationException();
725 }
726
727 public long getLong(String a) throws SQLException {
728 throw new UnsupportedOperationException();
729 }
730
731 public float getFloat(String a) throws SQLException {
732 throw new UnsupportedOperationException();
733 }
734
735 public double getDouble(String a) throws SQLException {
736 throw new UnsupportedOperationException();
737 }
738
739 public byte[] getBytes(String a) throws SQLException {
740 throw new UnsupportedOperationException();
741 }
742
743 public Date getDate(String a) throws SQLException {
744 throw new UnsupportedOperationException();
745 }
746
747 public Time getTime(String a) throws SQLException {
748 throw new UnsupportedOperationException();
749 }
750
751 public Timestamp getTimestamp(String a) throws SQLException {
752 throw new UnsupportedOperationException();
753 }
754
755 public Object getObject(String a) throws SQLException {
756 throw new UnsupportedOperationException();
757 }
758
759 public BigDecimal getBigDecimal(String a) throws SQLException {
760 throw new UnsupportedOperationException();
761 }
762
763 public Object getObject(String a, Map b) throws SQLException {
764 throw new UnsupportedOperationException();
765 }
766
767 public Ref getRef(String a) throws SQLException {
768 throw new UnsupportedOperationException();
769 }
770
771 public Blob getBlob(String a) throws SQLException {
772 throw new UnsupportedOperationException();
773 }
774
775 public Clob getClob(String a) throws SQLException {
776 throw new UnsupportedOperationException();
777 }
778
779 public Array getArray(String a) throws SQLException {
780 throw new UnsupportedOperationException();
781 }
782
783 public Date getDate(String a, Calendar b) throws SQLException {
784 throw new UnsupportedOperationException();
785 }
786
787 public Time getTime(String a, Calendar b) throws SQLException {
788 throw new UnsupportedOperationException();
789 }
790
791 public Timestamp getTimestamp(String a, Calendar b) throws SQLException {
792 throw new UnsupportedOperationException();
793 }
794 }