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.sql.Connection;
022: import java.sql.ResultSet;
023: import java.sql.SQLException;
024: import java.sql.SQLWarning;
025: import java.sql.Statement;
026:
027: import org.apache.openjpa.lib.util.Closeable;
028:
029: /**
030: * Wrapper around an existing statement. Subclasses can override the
031: * methods whose behavior they mean to change. The <code>equals</code> and
032: * <code>hashCode</code> methods pass through to the base underlying data
033: * store statement.
034: *
035: * @author Abe White
036: */
037: public class DelegatingStatement implements Statement, Closeable {
038:
039: private final Statement _stmnt;
040: private final DelegatingStatement _del;
041: private final Connection _conn;
042:
043: public DelegatingStatement(Statement stmnt, Connection conn) {
044: _conn = conn;
045: _stmnt = stmnt;
046: if (stmnt instanceof DelegatingStatement)
047: _del = (DelegatingStatement) stmnt;
048: else
049: _del = null;
050: }
051:
052: protected ResultSet wrapResult(ResultSet rs, boolean wrap) {
053: if (!wrap || rs == null)
054: return rs;
055: return new DelegatingResultSet(rs, this );
056: }
057:
058: /**
059: * Return the wrapped statement.
060: */
061: public Statement getDelegate() {
062: return _stmnt;
063: }
064:
065: /**
066: * Return the base underlying data store statement.
067: */
068: public Statement getInnermostDelegate() {
069: return (_del == null) ? _stmnt : _del.getInnermostDelegate();
070: }
071:
072: public int hashCode() {
073: return getInnermostDelegate().hashCode();
074: }
075:
076: public boolean equals(Object other) {
077: if (other == this )
078: return true;
079: if (other instanceof DelegatingStatement)
080: other = ((DelegatingStatement) other)
081: .getInnermostDelegate();
082: return getInnermostDelegate().equals(other);
083: }
084:
085: public String toString() {
086: StringBuffer buf = new StringBuffer("stmnt ")
087: .append(hashCode());
088: appendInfo(buf);
089: return buf.toString();
090: }
091:
092: protected void appendInfo(StringBuffer buf) {
093: if (_del != null)
094: _del.appendInfo(buf);
095: }
096:
097: public ResultSet executeQuery(String str) throws SQLException {
098: return executeQuery(str, true);
099: }
100:
101: /**
102: * Execute the query, with the option of not wrapping it in a
103: * {@link DelegatingResultSet}, which is the default.
104: */
105: protected ResultSet executeQuery(String sql, boolean wrap)
106: throws SQLException {
107: ResultSet rs;
108: if (_del != null)
109: rs = _del.executeQuery(sql, false);
110: else
111: rs = _stmnt.executeQuery(sql);
112: return wrapResult(rs, wrap);
113: }
114:
115: public int executeUpdate(String str) throws SQLException {
116: return _stmnt.executeUpdate(str);
117: }
118:
119: public boolean execute(String str) throws SQLException {
120: return _stmnt.execute(str);
121: }
122:
123: public void close() throws SQLException {
124: _stmnt.close();
125: }
126:
127: public int getMaxFieldSize() throws SQLException {
128: return _stmnt.getMaxFieldSize();
129: }
130:
131: public void setMaxFieldSize(int i) throws SQLException {
132: _stmnt.setMaxFieldSize(i);
133: }
134:
135: public int getMaxRows() throws SQLException {
136: return _stmnt.getMaxRows();
137: }
138:
139: public void setMaxRows(int i) throws SQLException {
140: _stmnt.setMaxRows(i);
141: }
142:
143: public void setEscapeProcessing(boolean bool) throws SQLException {
144: _stmnt.setEscapeProcessing(bool);
145: }
146:
147: public int getQueryTimeout() throws SQLException {
148: return _stmnt.getQueryTimeout();
149: }
150:
151: public void setQueryTimeout(int i) throws SQLException {
152: _stmnt.setQueryTimeout(i);
153: }
154:
155: public void cancel() throws SQLException {
156: _stmnt.cancel();
157: }
158:
159: public SQLWarning getWarnings() throws SQLException {
160: return _stmnt.getWarnings();
161: }
162:
163: public void clearWarnings() throws SQLException {
164: _stmnt.clearWarnings();
165: }
166:
167: public void setCursorName(String str) throws SQLException {
168: _stmnt.setCursorName(str);
169: }
170:
171: public ResultSet getResultSet() throws SQLException {
172: return getResultSet(true);
173: }
174:
175: /**
176: * Get the last result set, with the option of not wrapping it in a
177: * {@link DelegatingResultSet}, which is the default.
178: */
179: protected ResultSet getResultSet(boolean wrap) throws SQLException {
180: ResultSet rs;
181: if (_del != null)
182: rs = _del.getResultSet(false);
183: else
184: rs = _stmnt.getResultSet();
185: return wrapResult(rs, wrap);
186: }
187:
188: public int getUpdateCount() throws SQLException {
189: return _stmnt.getUpdateCount();
190: }
191:
192: public boolean getMoreResults() throws SQLException {
193: return _stmnt.getMoreResults();
194: }
195:
196: public void setFetchDirection(int i) throws SQLException {
197: _stmnt.setFetchDirection(i);
198: }
199:
200: public int getFetchDirection() throws SQLException {
201: return _stmnt.getFetchDirection();
202: }
203:
204: public void setFetchSize(int i) throws SQLException {
205: _stmnt.setFetchSize(i);
206: }
207:
208: public int getFetchSize() throws SQLException {
209: return _stmnt.getFetchSize();
210: }
211:
212: public int getResultSetConcurrency() throws SQLException {
213: return _stmnt.getResultSetConcurrency();
214: }
215:
216: public int getResultSetType() throws SQLException {
217: return _stmnt.getResultSetType();
218: }
219:
220: public void addBatch(String str) throws SQLException {
221: _stmnt.addBatch(str);
222: }
223:
224: public void clearBatch() throws SQLException {
225: _stmnt.clearBatch();
226: }
227:
228: public int[] executeBatch() throws SQLException {
229: return _stmnt.executeBatch();
230: }
231:
232: public Connection getConnection() throws SQLException {
233: return _conn;
234: }
235:
236: // JDBC 3.0 (unsupported) method follow; these are required to be able
237: // to compile against JDK 1.4
238:
239: public boolean getMoreResults(int i) throws SQLException {
240: throw new UnsupportedOperationException();
241: }
242:
243: public ResultSet getGeneratedKeys() throws SQLException {
244: throw new UnsupportedOperationException();
245: }
246:
247: public int executeUpdate(String s, int i) throws SQLException {
248: throw new UnsupportedOperationException();
249: }
250:
251: public int executeUpdate(String s, int[] ia) throws SQLException {
252: throw new UnsupportedOperationException();
253: }
254:
255: public int executeUpdate(String s, String[] sa) throws SQLException {
256: throw new UnsupportedOperationException();
257: }
258:
259: public boolean execute(String s, int i) throws SQLException {
260: throw new UnsupportedOperationException();
261: }
262:
263: public boolean execute(String s, int[] ia) throws SQLException {
264: throw new UnsupportedOperationException();
265: }
266:
267: public boolean execute(String s, String[] sa) throws SQLException {
268: throw new UnsupportedOperationException();
269: }
270:
271: public int getResultSetHoldability() throws SQLException {
272: throw new UnsupportedOperationException();
273: }
274: }
|