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.Clob;
28 import java.sql.Connection;
29 import java.sql.Date;
30 import java.sql.ParameterMetaData;
31 import java.sql.PreparedStatement;
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
41 import org.apache.openjpa.lib.util.Closeable;
42
43 /**
44 * Wrapper around an existing statement. Subclasses can override the
45 * methods whose behavior they mean to change. The <code>equals</code> and
46 * <code>hashCode</code> methods pass through to the base underlying data
47 * store statement.
48 *
49 * @author Abe White
50 */
51 public class DelegatingPreparedStatement
52 implements PreparedStatement, Closeable {
53
54 private final PreparedStatement _stmnt;
55 private final DelegatingPreparedStatement _del;
56 private final Connection _conn;
57
58 public DelegatingPreparedStatement(PreparedStatement stmnt,
59 Connection conn) {
60 _conn = conn;
61 _stmnt = stmnt;
62 if (_stmnt instanceof DelegatingPreparedStatement)
63 _del = (DelegatingPreparedStatement) _stmnt;
64 else
65 _del = null;
66 }
67
68 protected ResultSet wrapResult(ResultSet rs, boolean wrap) {
69 if (!wrap || rs == null)
70 return rs;
71 return new DelegatingResultSet(rs, this);
72 }
73
74 /**
75 * Return the wrapped statement.
76 */
77 public PreparedStatement getDelegate() {
78 return _stmnt;
79 }
80
81 /**
82 * Return the base underlying data store statement.
83 */
84 public PreparedStatement getInnermostDelegate() {
85 return (_del == null) ? _stmnt : _del.getInnermostDelegate();
86 }
87
88 public int hashCode() {
89 return getInnermostDelegate().hashCode();
90 }
91
92 public boolean equals(Object other) {
93 if (other == this)
94 return true;
95 if (other instanceof DelegatingPreparedStatement)
96 other = ((DelegatingPreparedStatement) other).
97 getInnermostDelegate();
98 return getInnermostDelegate().equals(other);
99 }
100
101 public String toString() {
102 StringBuffer buf = new StringBuffer("prepstmnt ").append(hashCode());
103 appendInfo(buf);
104 return buf.toString();
105 }
106
107 protected void appendInfo(StringBuffer buf) {
108 if (_del != null)
109 _del.appendInfo(buf);
110 }
111
112 public ResultSet executeQuery(String str) throws SQLException {
113 return executeQuery(str, true);
114 }
115
116 /**
117 * Execute the query, with the option of not wrapping it in a
118 * {@link DelegatingResultSet}, which is the default.
119 */
120 protected ResultSet executeQuery(String sql, boolean wrap)
121 throws SQLException {
122 ResultSet rs;
123 if (_del != null)
124 rs = _del.executeQuery(sql, false);
125 else
126 rs = _stmnt.executeQuery(sql);
127 return wrapResult(rs, wrap);
128 }
129
130 public int executeUpdate(String str) throws SQLException {
131 return _stmnt.executeUpdate(str);
132 }
133
134 public boolean execute(String str) throws SQLException {
135 return _stmnt.execute(str);
136 }
137
138 public void close() throws SQLException {
139 _stmnt.close();
140 }
141
142 public int getMaxFieldSize() throws SQLException {
143 return _stmnt.getMaxFieldSize();
144 }
145
146 public void setMaxFieldSize(int i) throws SQLException {
147 _stmnt.setMaxFieldSize(i);
148 }
149
150 public int getMaxRows() throws SQLException {
151 return _stmnt.getMaxRows();
152 }
153
154 public void setMaxRows(int i) throws SQLException {
155 _stmnt.setMaxRows(i);
156 }
157
158 public void setEscapeProcessing(boolean bool) throws SQLException {
159 _stmnt.setEscapeProcessing(bool);
160 }
161
162 public int getQueryTimeout() throws SQLException {
163 return _stmnt.getQueryTimeout();
164 }
165
166 public void setQueryTimeout(int i) throws SQLException {
167 _stmnt.setQueryTimeout(i);
168 }
169
170 public void cancel() throws SQLException {
171 _stmnt.cancel();
172 }
173
174 public SQLWarning getWarnings() throws SQLException {
175 return _stmnt.getWarnings();
176 }
177
178 public void clearWarnings() throws SQLException {
179 _stmnt.clearWarnings();
180 }
181
182 public void setCursorName(String str) throws SQLException {
183 _stmnt.setCursorName(str);
184 }
185
186 public ResultSet getResultSet() throws SQLException {
187 return getResultSet(true);
188 }
189
190 /**
191 * Get the last result set, with the option of not wrapping it in a
192 * {@link DelegatingResultSet}, which is the default.
193 */
194 protected ResultSet getResultSet(boolean wrap) throws SQLException {
195 ResultSet rs;
196 if (_del != null)
197 rs = _del.getResultSet(false);
198 else
199 rs = _stmnt.getResultSet();
200 return wrapResult(rs, wrap);
201 }
202
203 public int getUpdateCount() throws SQLException {
204 return _stmnt.getUpdateCount();
205 }
206
207 public boolean getMoreResults() throws SQLException {
208 return _stmnt.getMoreResults();
209 }
210
211 public void setFetchDirection(int i) throws SQLException {
212 _stmnt.setFetchDirection(i);
213 }
214
215 public int getFetchDirection() throws SQLException {
216 return _stmnt.getFetchDirection();
217 }
218
219 public void setFetchSize(int i) throws SQLException {
220 _stmnt.setFetchSize(i);
221 }
222
223 public int getFetchSize() throws SQLException {
224 return _stmnt.getFetchSize();
225 }
226
227 public int getResultSetConcurrency() throws SQLException {
228 return _stmnt.getResultSetConcurrency();
229 }
230
231 public int getResultSetType() throws SQLException {
232 return _stmnt.getResultSetType();
233 }
234
235 public void addBatch(String str) throws SQLException {
236 _stmnt.addBatch(str);
237 }
238
239 public void clearBatch() throws SQLException {
240 _stmnt.clearBatch();
241 }
242
243 public int[] executeBatch() throws SQLException {
244 return _stmnt.executeBatch();
245 }
246
247 public Connection getConnection() throws SQLException {
248 return _conn;
249 }
250
251 public ResultSet executeQuery() throws SQLException {
252 return executeQuery(true);
253 }
254
255 /**
256 * Execute the query, with the option of not wrapping it in a
257 * {@link DelegatingResultSet}, which is the default.
258 */
259 protected ResultSet executeQuery(boolean wrap) throws SQLException {
260 ResultSet rs;
261 if (_del != null)
262 rs = _del.executeQuery(false);
263 else
264 rs = _stmnt.executeQuery();
265 return wrapResult(rs, wrap);
266 }
267
268 public int executeUpdate() throws SQLException {
269 return _stmnt.executeUpdate();
270 }
271
272 public void setNull(int i1, int i2) throws SQLException {
273 _stmnt.setNull(i1, i2);
274 }
275
276 public void setBoolean(int i, boolean b) throws SQLException {
277 _stmnt.setBoolean(i, b);
278 }
279
280 public void setByte(int i, byte b) throws SQLException {
281 _stmnt.setByte(i, b);
282 }
283
284 public void setShort(int i, short s) throws SQLException {
285 _stmnt.setShort(i, s);
286 }
287
288 public void setInt(int i1, int i2) throws SQLException {
289 _stmnt.setInt(i1, i2);
290 }
291
292 public void setLong(int i, long l) throws SQLException {
293 _stmnt.setLong(i, l);
294 }
295
296 public void setFloat(int i, float f) throws SQLException {
297 _stmnt.setFloat(i, f);
298 }
299
300 public void setDouble(int i, double d) throws SQLException {
301 _stmnt.setDouble(i, d);
302 }
303
304 public void setBigDecimal(int i, BigDecimal bd) throws SQLException {
305 _stmnt.setBigDecimal(i, bd);
306 }
307
308 public void setString(int i, String s) throws SQLException {
309 _stmnt.setString(i, s);
310 }
311
312 public void setBytes(int i, byte[] b) throws SQLException {
313 _stmnt.setBytes(i, b);
314 }
315
316 public void setDate(int i, Date d) throws SQLException {
317 _stmnt.setDate(i, d);
318 }
319
320 public void setTime(int i, Time t) throws SQLException {
321 _stmnt.setTime(i, t);
322 }
323
324 public void setTimestamp(int i, Timestamp t) throws SQLException {
325 _stmnt.setTimestamp(i, t);
326 }
327
328 public void setAsciiStream(int i1, InputStream is, int i2)
329 throws SQLException {
330 _stmnt.setAsciiStream(i1, is, i2);
331 }
332
333 public void setUnicodeStream(int i1, InputStream is, int i2)
334 throws SQLException {
335 _stmnt.setUnicodeStream(i1, is, i2);
336 }
337
338 public void setBinaryStream(int i1, InputStream is, int i2)
339 throws SQLException {
340 _stmnt.setBinaryStream(i1, is, i2);
341 }
342
343 public void clearParameters() throws SQLException {
344 _stmnt.clearParameters();
345 }
346
347 public void setObject(int i1, Object o, int i2, int i3)
348 throws SQLException {
349 _stmnt.setObject(i1, o, i2, i3);
350 }
351
352 public void setObject(int i1, Object o, int i2) throws SQLException {
353 _stmnt.setObject(i1, o, i2);
354 }
355
356 public void setObject(int i, Object o) throws SQLException {
357 _stmnt.setObject(i, o);
358 }
359
360 public boolean execute() throws SQLException {
361 return _stmnt.execute();
362 }
363
364 public void addBatch() throws SQLException {
365 _stmnt.addBatch();
366 }
367
368 public void setCharacterStream(int i1, Reader r, int i2)
369 throws SQLException {
370 _stmnt.setCharacterStream(i1, r, i2);
371 }
372
373 public void setRef(int i, Ref r) throws SQLException {
374 _stmnt.setRef(i, r);
375 }
376
377 public void setBlob(int i, Blob b) throws SQLException {
378 _stmnt.setBlob(i, b);
379 }
380
381 public void setClob(int i, Clob c) throws SQLException {
382 _stmnt.setClob(i, c);
383 }
384
385 public void setArray(int i, Array a) throws SQLException {
386 _stmnt.setArray(i, a);
387 }
388
389 public ResultSetMetaData getMetaData() throws SQLException {
390 return _stmnt.getMetaData();
391 }
392
393 public void setDate(int i, Date d, Calendar c) throws SQLException {
394 _stmnt.setDate(i, d, c);
395 }
396
397 public void setTime(int i, Time t, Calendar c) throws SQLException {
398 _stmnt.setTime(i, t, c);
399 }
400
401 public void setTimestamp(int i, Timestamp t, Calendar c)
402 throws SQLException {
403 _stmnt.setTimestamp(i, t, c);
404 }
405
406 public void setNull(int i1, int i2, String s) throws SQLException {
407 _stmnt.setNull(i1, i2, s);
408 }
409
410 // JDBC 3.0 (unsupported) method follow; these are required to be able
411 // to compile against JDK 1.4
412
413 public boolean getMoreResults(int i) throws SQLException {
414 throw new UnsupportedOperationException();
415 }
416
417 public ResultSet getGeneratedKeys() throws SQLException {
418 throw new UnsupportedOperationException();
419 }
420
421 public int executeUpdate(String s, int i) throws SQLException {
422 throw new UnsupportedOperationException();
423 }
424
425 public int executeUpdate(String s, int[] ia) throws SQLException {
426 throw new UnsupportedOperationException();
427 }
428
429 public int executeUpdate(String s, String[] sa) throws SQLException {
430 throw new UnsupportedOperationException();
431 }
432
433 public boolean execute(String s, int i) throws SQLException {
434 throw new UnsupportedOperationException();
435 }
436
437 public boolean execute(String s, int[] ia) throws SQLException {
438 throw new UnsupportedOperationException();
439 }
440
441 public boolean execute(String s, String[] sa) throws SQLException {
442 throw new UnsupportedOperationException();
443 }
444
445 public int getResultSetHoldability() throws SQLException {
446 throw new UnsupportedOperationException();
447 }
448
449 public void setURL(int i, URL url) throws SQLException {
450 throw new UnsupportedOperationException();
451 }
452
453 public ParameterMetaData getParameterMetaData() throws SQLException {
454 throw new UnsupportedOperationException();
455 }
456 }