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.sql.Connection;
22 import java.sql.ResultSet;
23 import java.sql.SQLException;
24 import java.sql.SQLWarning;
25 import java.sql.Statement;
26
27 import org.apache.openjpa.lib.util.Closeable;
28
29 /**
30 * Wrapper around an existing statement. Subclasses can override the
31 * methods whose behavior they mean to change. The <code>equals</code> and
32 * <code>hashCode</code> methods pass through to the base underlying data
33 * store statement.
34 *
35 * @author Abe White
36 */
37 public class DelegatingStatement implements Statement, Closeable {
38
39 private final Statement _stmnt;
40 private final DelegatingStatement _del;
41 private final Connection _conn;
42
43 public DelegatingStatement(Statement stmnt, Connection conn) {
44 _conn = conn;
45 _stmnt = stmnt;
46 if (stmnt instanceof DelegatingStatement)
47 _del = (DelegatingStatement) stmnt;
48 else
49 _del = null;
50 }
51
52 protected ResultSet wrapResult(ResultSet rs, boolean wrap) {
53 if (!wrap || rs == null)
54 return rs;
55 return new DelegatingResultSet(rs, this);
56 }
57
58 /**
59 * Return the wrapped statement.
60 */
61 public Statement getDelegate() {
62 return _stmnt;
63 }
64
65 /**
66 * Return the base underlying data store statement.
67 */
68 public Statement getInnermostDelegate() {
69 return (_del == null) ? _stmnt : _del.getInnermostDelegate();
70 }
71
72 public int hashCode() {
73 return getInnermostDelegate().hashCode();
74 }
75
76 public boolean equals(Object other) {
77 if (other == this)
78 return true;
79 if (other instanceof DelegatingStatement)
80 other = ((DelegatingStatement) other).getInnermostDelegate();
81 return getInnermostDelegate().equals(other);
82 }
83
84 public String toString() {
85 StringBuffer buf = new StringBuffer("stmnt ").append(hashCode());
86 appendInfo(buf);
87 return buf.toString();
88 }
89
90 protected void appendInfo(StringBuffer buf) {
91 if (_del != null)
92 _del.appendInfo(buf);
93 }
94
95 public ResultSet executeQuery(String str) throws SQLException {
96 return executeQuery(str, true);
97 }
98
99 /**
100 * Execute the query, with the option of not wrapping it in a
101 * {@link DelegatingResultSet}, which is the default.
102 */
103 protected ResultSet executeQuery(String sql, boolean wrap)
104 throws SQLException {
105 ResultSet rs;
106 if (_del != null)
107 rs = _del.executeQuery(sql, false);
108 else
109 rs = _stmnt.executeQuery(sql);
110 return wrapResult(rs, wrap);
111 }
112
113 public int executeUpdate(String str) throws SQLException {
114 return _stmnt.executeUpdate(str);
115 }
116
117 public boolean execute(String str) throws SQLException {
118 return _stmnt.execute(str);
119 }
120
121 public void close() throws SQLException {
122 _stmnt.close();
123 }
124
125 public int getMaxFieldSize() throws SQLException {
126 return _stmnt.getMaxFieldSize();
127 }
128
129 public void setMaxFieldSize(int i) throws SQLException {
130 _stmnt.setMaxFieldSize(i);
131 }
132
133 public int getMaxRows() throws SQLException {
134 return _stmnt.getMaxRows();
135 }
136
137 public void setMaxRows(int i) throws SQLException {
138 _stmnt.setMaxRows(i);
139 }
140
141 public void setEscapeProcessing(boolean bool) throws SQLException {
142 _stmnt.setEscapeProcessing(bool);
143 }
144
145 public int getQueryTimeout() throws SQLException {
146 return _stmnt.getQueryTimeout();
147 }
148
149 public void setQueryTimeout(int i) throws SQLException {
150 _stmnt.setQueryTimeout(i);
151 }
152
153 public void cancel() throws SQLException {
154 _stmnt.cancel();
155 }
156
157 public SQLWarning getWarnings() throws SQLException {
158 return _stmnt.getWarnings();
159 }
160
161 public void clearWarnings() throws SQLException {
162 _stmnt.clearWarnings();
163 }
164
165 public void setCursorName(String str) throws SQLException {
166 _stmnt.setCursorName(str);
167 }
168
169 public ResultSet getResultSet() throws SQLException {
170 return getResultSet(true);
171 }
172
173 /**
174 * Get the last result set, with the option of not wrapping it in a
175 * {@link DelegatingResultSet}, which is the default.
176 */
177 protected ResultSet getResultSet(boolean wrap) throws SQLException {
178 ResultSet rs;
179 if (_del != null)
180 rs = _del.getResultSet(false);
181 else
182 rs = _stmnt.getResultSet();
183 return wrapResult(rs, wrap);
184 }
185
186 public int getUpdateCount() throws SQLException {
187 return _stmnt.getUpdateCount();
188 }
189
190 public boolean getMoreResults() throws SQLException {
191 return _stmnt.getMoreResults();
192 }
193
194 public void setFetchDirection(int i) throws SQLException {
195 _stmnt.setFetchDirection(i);
196 }
197
198 public int getFetchDirection() throws SQLException {
199 return _stmnt.getFetchDirection();
200 }
201
202 public void setFetchSize(int i) throws SQLException {
203 _stmnt.setFetchSize(i);
204 }
205
206 public int getFetchSize() throws SQLException {
207 return _stmnt.getFetchSize();
208 }
209
210 public int getResultSetConcurrency() throws SQLException {
211 return _stmnt.getResultSetConcurrency();
212 }
213
214 public int getResultSetType() throws SQLException {
215 return _stmnt.getResultSetType();
216 }
217
218 public void addBatch(String str) throws SQLException {
219 _stmnt.addBatch(str);
220 }
221
222 public void clearBatch() throws SQLException {
223 _stmnt.clearBatch();
224 }
225
226 public int[] executeBatch() throws SQLException {
227 return _stmnt.executeBatch();
228 }
229
230 public Connection getConnection() throws SQLException {
231 return _conn;
232 }
233
234 // JDBC 3.0 (unsupported) method follow; these are required to be able
235 // to compile against JDK 1.4
236
237 public boolean getMoreResults(int i) throws SQLException {
238 throw new UnsupportedOperationException();
239 }
240
241 public ResultSet getGeneratedKeys() throws SQLException {
242 throw new UnsupportedOperationException();
243 }
244
245 public int executeUpdate(String s, int i) throws SQLException {
246 throw new UnsupportedOperationException();
247 }
248
249 public int executeUpdate(String s, int[] ia) throws SQLException {
250 throw new UnsupportedOperationException();
251 }
252
253 public int executeUpdate(String s, String[] sa) throws SQLException {
254 throw new UnsupportedOperationException();
255 }
256
257 public boolean execute(String s, int i) throws SQLException {
258 throw new UnsupportedOperationException();
259 }
260
261 public boolean execute(String s, int[] ia) throws SQLException {
262 throw new UnsupportedOperationException();
263 }
264
265 public boolean execute(String s, String[] sa) throws SQLException {
266 throw new UnsupportedOperationException();
267 }
268
269 public int getResultSetHoldability() throws SQLException {
270 throw new UnsupportedOperationException();
271 }
272 }