001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.lib.jdbc;
020:
021: import java.io.InputStream;
022: import java.io.Reader;
023: import java.math.BigDecimal;
024: import java.net.URL;
025: import java.sql.Array;
026: import java.sql.Blob;
027: import java.sql.Clob;
028: import java.sql.Connection;
029: import java.sql.Date;
030: import java.sql.ParameterMetaData;
031: import java.sql.PreparedStatement;
032: import java.sql.Ref;
033: import java.sql.ResultSet;
034: import java.sql.ResultSetMetaData;
035: import java.sql.SQLException;
036: import java.sql.SQLWarning;
037: import java.sql.Time;
038: import java.sql.Timestamp;
039: import java.util.Calendar;
040:
041: import org.apache.openjpa.lib.util.Closeable;
042:
043: /**
044: * Wrapper around an existing statement. Subclasses can override the
045: * methods whose behavior they mean to change. The <code>equals</code> and
046: * <code>hashCode</code> methods pass through to the base underlying data
047: * store statement.
048: *
049: * @author Abe White
050: */
051: public class DelegatingPreparedStatement implements PreparedStatement,
052: Closeable {
053:
054: private final PreparedStatement _stmnt;
055: private final DelegatingPreparedStatement _del;
056: private final Connection _conn;
057:
058: public DelegatingPreparedStatement(PreparedStatement stmnt,
059: Connection conn) {
060: _conn = conn;
061: _stmnt = stmnt;
062: if (_stmnt instanceof DelegatingPreparedStatement)
063: _del = (DelegatingPreparedStatement) _stmnt;
064: else
065: _del = null;
066: }
067:
068: protected ResultSet wrapResult(ResultSet rs, boolean wrap) {
069: if (!wrap || rs == null)
070: return rs;
071: return new DelegatingResultSet(rs, this );
072: }
073:
074: /**
075: * Return the wrapped statement.
076: */
077: public PreparedStatement getDelegate() {
078: return _stmnt;
079: }
080:
081: /**
082: * Return the base underlying data store statement.
083: */
084: public PreparedStatement getInnermostDelegate() {
085: return (_del == null) ? _stmnt : _del.getInnermostDelegate();
086: }
087:
088: public int hashCode() {
089: return getInnermostDelegate().hashCode();
090: }
091:
092: public boolean equals(Object other) {
093: if (other == this )
094: return true;
095: if (other instanceof DelegatingPreparedStatement)
096: other = ((DelegatingPreparedStatement) other)
097: .getInnermostDelegate();
098: return getInnermostDelegate().equals(other);
099: }
100:
101: public String toString() {
102: StringBuffer buf = new StringBuffer("prepstmnt ")
103: .append(hashCode());
104: appendInfo(buf);
105: return buf.toString();
106: }
107:
108: protected void appendInfo(StringBuffer buf) {
109: if (_del != null)
110: _del.appendInfo(buf);
111: }
112:
113: public ResultSet executeQuery(String str) throws SQLException {
114: return executeQuery(str, true);
115: }
116:
117: /**
118: * Execute the query, with the option of not wrapping it in a
119: * {@link DelegatingResultSet}, which is the default.
120: */
121: protected ResultSet executeQuery(String sql, boolean wrap)
122: throws SQLException {
123: ResultSet rs;
124: if (_del != null)
125: rs = _del.executeQuery(sql, false);
126: else
127: rs = _stmnt.executeQuery(sql);
128: return wrapResult(rs, wrap);
129: }
130:
131: public int executeUpdate(String str) throws SQLException {
132: return _stmnt.executeUpdate(str);
133: }
134:
135: public boolean execute(String str) throws SQLException {
136: return _stmnt.execute(str);
137: }
138:
139: public void close() throws SQLException {
140: _stmnt.close();
141: }
142:
143: public int getMaxFieldSize() throws SQLException {
144: return _stmnt.getMaxFieldSize();
145: }
146:
147: public void setMaxFieldSize(int i) throws SQLException {
148: _stmnt.setMaxFieldSize(i);
149: }
150:
151: public int getMaxRows() throws SQLException {
152: return _stmnt.getMaxRows();
153: }
154:
155: public void setMaxRows(int i) throws SQLException {
156: _stmnt.setMaxRows(i);
157: }
158:
159: public void setEscapeProcessing(boolean bool) throws SQLException {
160: _stmnt.setEscapeProcessing(bool);
161: }
162:
163: public int getQueryTimeout() throws SQLException {
164: return _stmnt.getQueryTimeout();
165: }
166:
167: public void setQueryTimeout(int i) throws SQLException {
168: _stmnt.setQueryTimeout(i);
169: }
170:
171: public void cancel() throws SQLException {
172: _stmnt.cancel();
173: }
174:
175: public SQLWarning getWarnings() throws SQLException {
176: return _stmnt.getWarnings();
177: }
178:
179: public void clearWarnings() throws SQLException {
180: _stmnt.clearWarnings();
181: }
182:
183: public void setCursorName(String str) throws SQLException {
184: _stmnt.setCursorName(str);
185: }
186:
187: public ResultSet getResultSet() throws SQLException {
188: return getResultSet(true);
189: }
190:
191: /**
192: * Get the last result set, with the option of not wrapping it in a
193: * {@link DelegatingResultSet}, which is the default.
194: */
195: protected ResultSet getResultSet(boolean wrap) throws SQLException {
196: ResultSet rs;
197: if (_del != null)
198: rs = _del.getResultSet(false);
199: else
200: rs = _stmnt.getResultSet();
201: return wrapResult(rs, wrap);
202: }
203:
204: public int getUpdateCount() throws SQLException {
205: return _stmnt.getUpdateCount();
206: }
207:
208: public boolean getMoreResults() throws SQLException {
209: return _stmnt.getMoreResults();
210: }
211:
212: public void setFetchDirection(int i) throws SQLException {
213: _stmnt.setFetchDirection(i);
214: }
215:
216: public int getFetchDirection() throws SQLException {
217: return _stmnt.getFetchDirection();
218: }
219:
220: public void setFetchSize(int i) throws SQLException {
221: _stmnt.setFetchSize(i);
222: }
223:
224: public int getFetchSize() throws SQLException {
225: return _stmnt.getFetchSize();
226: }
227:
228: public int getResultSetConcurrency() throws SQLException {
229: return _stmnt.getResultSetConcurrency();
230: }
231:
232: public int getResultSetType() throws SQLException {
233: return _stmnt.getResultSetType();
234: }
235:
236: public void addBatch(String str) throws SQLException {
237: _stmnt.addBatch(str);
238: }
239:
240: public void clearBatch() throws SQLException {
241: _stmnt.clearBatch();
242: }
243:
244: public int[] executeBatch() throws SQLException {
245: return _stmnt.executeBatch();
246: }
247:
248: public Connection getConnection() throws SQLException {
249: return _conn;
250: }
251:
252: public ResultSet executeQuery() throws SQLException {
253: return executeQuery(true);
254: }
255:
256: /**
257: * Execute the query, with the option of not wrapping it in a
258: * {@link DelegatingResultSet}, which is the default.
259: */
260: protected ResultSet executeQuery(boolean wrap) throws SQLException {
261: ResultSet rs;
262: if (_del != null)
263: rs = _del.executeQuery(false);
264: else
265: rs = _stmnt.executeQuery();
266: return wrapResult(rs, wrap);
267: }
268:
269: public int executeUpdate() throws SQLException {
270: return _stmnt.executeUpdate();
271: }
272:
273: public void setNull(int i1, int i2) throws SQLException {
274: _stmnt.setNull(i1, i2);
275: }
276:
277: public void setBoolean(int i, boolean b) throws SQLException {
278: _stmnt.setBoolean(i, b);
279: }
280:
281: public void setByte(int i, byte b) throws SQLException {
282: _stmnt.setByte(i, b);
283: }
284:
285: public void setShort(int i, short s) throws SQLException {
286: _stmnt.setShort(i, s);
287: }
288:
289: public void setInt(int i1, int i2) throws SQLException {
290: _stmnt.setInt(i1, i2);
291: }
292:
293: public void setLong(int i, long l) throws SQLException {
294: _stmnt.setLong(i, l);
295: }
296:
297: public void setFloat(int i, float f) throws SQLException {
298: _stmnt.setFloat(i, f);
299: }
300:
301: public void setDouble(int i, double d) throws SQLException {
302: _stmnt.setDouble(i, d);
303: }
304:
305: public void setBigDecimal(int i, BigDecimal bd) throws SQLException {
306: _stmnt.setBigDecimal(i, bd);
307: }
308:
309: public void setString(int i, String s) throws SQLException {
310: _stmnt.setString(i, s);
311: }
312:
313: public void setBytes(int i, byte[] b) throws SQLException {
314: _stmnt.setBytes(i, b);
315: }
316:
317: public void setDate(int i, Date d) throws SQLException {
318: _stmnt.setDate(i, d);
319: }
320:
321: public void setTime(int i, Time t) throws SQLException {
322: _stmnt.setTime(i, t);
323: }
324:
325: public void setTimestamp(int i, Timestamp t) throws SQLException {
326: _stmnt.setTimestamp(i, t);
327: }
328:
329: public void setAsciiStream(int i1, InputStream is, int i2)
330: throws SQLException {
331: _stmnt.setAsciiStream(i1, is, i2);
332: }
333:
334: public void setUnicodeStream(int i1, InputStream is, int i2)
335: throws SQLException {
336: _stmnt.setUnicodeStream(i1, is, i2);
337: }
338:
339: public void setBinaryStream(int i1, InputStream is, int i2)
340: throws SQLException {
341: _stmnt.setBinaryStream(i1, is, i2);
342: }
343:
344: public void clearParameters() throws SQLException {
345: _stmnt.clearParameters();
346: }
347:
348: public void setObject(int i1, Object o, int i2, int i3)
349: throws SQLException {
350: _stmnt.setObject(i1, o, i2, i3);
351: }
352:
353: public void setObject(int i1, Object o, int i2) throws SQLException {
354: _stmnt.setObject(i1, o, i2);
355: }
356:
357: public void setObject(int i, Object o) throws SQLException {
358: _stmnt.setObject(i, o);
359: }
360:
361: public boolean execute() throws SQLException {
362: return _stmnt.execute();
363: }
364:
365: public void addBatch() throws SQLException {
366: _stmnt.addBatch();
367: }
368:
369: public void setCharacterStream(int i1, Reader r, int i2)
370: throws SQLException {
371: _stmnt.setCharacterStream(i1, r, i2);
372: }
373:
374: public void setRef(int i, Ref r) throws SQLException {
375: _stmnt.setRef(i, r);
376: }
377:
378: public void setBlob(int i, Blob b) throws SQLException {
379: _stmnt.setBlob(i, b);
380: }
381:
382: public void setClob(int i, Clob c) throws SQLException {
383: _stmnt.setClob(i, c);
384: }
385:
386: public void setArray(int i, Array a) throws SQLException {
387: _stmnt.setArray(i, a);
388: }
389:
390: public ResultSetMetaData getMetaData() throws SQLException {
391: return _stmnt.getMetaData();
392: }
393:
394: public void setDate(int i, Date d, Calendar c) throws SQLException {
395: _stmnt.setDate(i, d, c);
396: }
397:
398: public void setTime(int i, Time t, Calendar c) throws SQLException {
399: _stmnt.setTime(i, t, c);
400: }
401:
402: public void setTimestamp(int i, Timestamp t, Calendar c)
403: throws SQLException {
404: _stmnt.setTimestamp(i, t, c);
405: }
406:
407: public void setNull(int i1, int i2, String s) throws SQLException {
408: _stmnt.setNull(i1, i2, s);
409: }
410:
411: // JDBC 3.0 (unsupported) method follow; these are required to be able
412: // to compile against JDK 1.4
413:
414: public boolean getMoreResults(int i) throws SQLException {
415: throw new UnsupportedOperationException();
416: }
417:
418: public ResultSet getGeneratedKeys() throws SQLException {
419: throw new UnsupportedOperationException();
420: }
421:
422: public int executeUpdate(String s, int i) throws SQLException {
423: throw new UnsupportedOperationException();
424: }
425:
426: public int executeUpdate(String s, int[] ia) throws SQLException {
427: throw new UnsupportedOperationException();
428: }
429:
430: public int executeUpdate(String s, String[] sa) throws SQLException {
431: throw new UnsupportedOperationException();
432: }
433:
434: public boolean execute(String s, int i) throws SQLException {
435: throw new UnsupportedOperationException();
436: }
437:
438: public boolean execute(String s, int[] ia) throws SQLException {
439: throw new UnsupportedOperationException();
440: }
441:
442: public boolean execute(String s, String[] sa) throws SQLException {
443: throw new UnsupportedOperationException();
444: }
445:
446: public int getResultSetHoldability() throws SQLException {
447: throw new UnsupportedOperationException();
448: }
449:
450: public void setURL(int i, URL url) throws SQLException {
451: throw new UnsupportedOperationException();
452: }
453:
454: public ParameterMetaData getParameterMetaData() throws SQLException {
455: throw new UnsupportedOperationException();
456: }
457: }
|