1 16 17 package org.apache.commons.dbcp; 18 19 import java.sql.Connection ; 20 import java.sql.ResultSet ; 21 import java.sql.SQLException ; 22 import java.sql.SQLWarning ; 23 import java.sql.Statement ; 24 import java.util.List ; 25 26 45 public class DelegatingStatement extends AbandonedTrace implements Statement { 46 47 protected Statement _stmt = null; 48 49 protected DelegatingConnection _conn = null; 50 51 59 public DelegatingStatement(DelegatingConnection c, Statement s) { 60 super(c); 61 _stmt = s; 62 _conn = c; 63 } 64 65 70 public Statement getDelegate() { 71 return _stmt; 72 } 73 74 public boolean equals(Object obj) { 75 Statement delegate = getInnermostDelegate(); 76 if (delegate == null) { 77 return false; 78 } 79 if (obj instanceof DelegatingStatement) { 80 DelegatingStatement s = (DelegatingStatement) obj; 81 return delegate.equals(s.getInnermostDelegate()); 82 } 83 else { 84 return delegate.equals(obj); 85 } 86 } 87 88 public int hashCode() { 89 Object obj = getInnermostDelegate(); 90 if (obj == null) { 91 return 0; 92 } 93 return obj.hashCode(); 94 } 95 96 112 public Statement getInnermostDelegate() { 113 Statement s = _stmt; 114 while(s != null && s instanceof DelegatingStatement) { 115 s = ((DelegatingStatement)s).getDelegate(); 116 if(this == s) { 117 return null; 118 } 119 } 120 return s; 121 } 122 123 124 public void setDelegate(Statement s) { 125 _stmt = s; 126 } 127 128 protected boolean _closed = false; 129 130 protected boolean isClosed() { 131 return _closed; 132 } 133 134 protected void checkOpen() throws SQLException { 135 if(isClosed()) { 136 throw new SQLException (this.getClass().getName() + " is closed."); 137 } 138 } 139 140 144 public void close() throws SQLException { 145 try { 146 try { 147 if (_conn != null) { 148 _conn.removeTrace(this); 149 _conn = null; 150 } 151 152 List resultSets = getTrace(); 157 if( resultSets != null) { 158 ResultSet [] set = (ResultSet []) resultSets.toArray(new ResultSet [resultSets.size()]); 159 for (int i = 0; i < set.length; i++) { 160 set[i].close(); 161 } 162 clearTrace(); 163 } 164 165 _stmt.close(); 166 } 167 catch (SQLException e) { 168 handleException(e); 169 } 170 } 171 finally { 172 _closed = true; 173 } 174 } 175 176 protected void handleException(SQLException e) throws SQLException { 177 if (_conn != null) { 178 _conn.handleException(e); 179 } 180 else { 181 throw e; 182 } 183 } 184 185 protected void activate() throws SQLException { 186 if(_stmt instanceof DelegatingStatement) { 187 ((DelegatingStatement)_stmt).activate(); 188 } 189 } 190 191 protected void passivate() throws SQLException { 192 if(_stmt instanceof DelegatingStatement) { 193 ((DelegatingStatement)_stmt).passivate(); 194 } 195 } 196 197 public Connection getConnection() throws SQLException { 198 checkOpen(); 199 return _conn; } 201 202 public ResultSet executeQuery(String sql) throws SQLException { 203 checkOpen(); 204 try { 205 return DelegatingResultSet.wrapResultSet(this,_stmt.executeQuery(sql)); 206 } 207 catch (SQLException e) { 208 handleException(e); 209 return null; 210 } 211 } 212 213 public ResultSet getResultSet() throws SQLException { 214 checkOpen(); 215 try { 216 return DelegatingResultSet.wrapResultSet(this,_stmt.getResultSet()); 217 } 218 catch (SQLException e) { 219 handleException(e); 220 return null; 221 } 222 } 223 224 public int executeUpdate(String sql) throws SQLException 225 { checkOpen(); try { return _stmt.executeUpdate(sql); } catch (SQLException e) { handleException(e); return 0; } } 226 227 public int getMaxFieldSize() throws SQLException 228 { checkOpen(); try { return _stmt.getMaxFieldSize(); } catch (SQLException e) { handleException(e); return 0; } } 229 230 public void setMaxFieldSize(int max) throws SQLException 231 { checkOpen(); try { _stmt.setMaxFieldSize(max); } catch (SQLException e) { handleException(e); } } 232 233 public int getMaxRows() throws SQLException 234 { checkOpen(); try { return _stmt.getMaxRows(); } catch (SQLException e) { handleException(e); return 0; } } 235 236 public void setMaxRows(int max) throws SQLException 237 { checkOpen(); try { _stmt.setMaxRows(max); } catch (SQLException e) { handleException(e); } } 238 239 public void setEscapeProcessing(boolean enable) throws SQLException 240 { checkOpen(); try { _stmt.setEscapeProcessing(enable); } catch (SQLException e) { handleException(e); } } 241 242 public int getQueryTimeout() throws SQLException 243 { checkOpen(); try { return _stmt.getQueryTimeout(); } catch (SQLException e) { handleException(e); return 0; } } 244 245 public void setQueryTimeout(int seconds) throws SQLException 246 { checkOpen(); try { _stmt.setQueryTimeout(seconds); } catch (SQLException e) { handleException(e); } } 247 248 public void cancel() throws SQLException 249 { checkOpen(); try { _stmt.cancel(); } catch (SQLException e) { handleException(e); } } 250 251 public SQLWarning getWarnings() throws SQLException 252 { checkOpen(); try { return _stmt.getWarnings(); } catch (SQLException e) { handleException(e); return null; } } 253 254 public void clearWarnings() throws SQLException 255 { checkOpen(); try { _stmt.clearWarnings(); } catch (SQLException e) { handleException(e); } } 256 257 public void setCursorName(String name) throws SQLException 258 { checkOpen(); try { _stmt.setCursorName(name); } catch (SQLException e) { handleException(e); } } 259 260 public boolean execute(String sql) throws SQLException 261 { checkOpen(); try { return _stmt.execute(sql); } catch (SQLException e) { handleException(e); return false; } } 262 263 public int getUpdateCount() throws SQLException 264 { checkOpen(); try { return _stmt.getUpdateCount(); } catch (SQLException e) { handleException(e); return 0; } } 265 266 public boolean getMoreResults() throws SQLException 267 { checkOpen(); try { return _stmt.getMoreResults(); } catch (SQLException e) { handleException(e); return false; } } 268 269 public void setFetchDirection(int direction) throws SQLException 270 { checkOpen(); try { _stmt.setFetchDirection(direction); } catch (SQLException e) { handleException(e); } } 271 272 public int getFetchDirection() throws SQLException 273 { checkOpen(); try { return _stmt.getFetchDirection(); } catch (SQLException e) { handleException(e); return 0; } } 274 275 public void setFetchSize(int rows) throws SQLException 276 { checkOpen(); try { _stmt.setFetchSize(rows); } catch (SQLException e) { handleException(e); } } 277 278 public int getFetchSize() throws SQLException 279 { checkOpen(); try { return _stmt.getFetchSize(); } catch (SQLException e) { handleException(e); return 0; } } 280 281 public int getResultSetConcurrency() throws SQLException 282 { checkOpen(); try { return _stmt.getResultSetConcurrency(); } catch (SQLException e) { handleException(e); return 0; } } 283 284 public int getResultSetType() throws SQLException 285 { checkOpen(); try { return _stmt.getResultSetType(); } catch (SQLException e) { handleException(e); return 0; } } 286 287 public void addBatch(String sql) throws SQLException 288 { checkOpen(); try { _stmt.addBatch(sql); } catch (SQLException e) { handleException(e); } } 289 290 public void clearBatch() throws SQLException 291 { checkOpen(); try { _stmt.clearBatch(); } catch (SQLException e) { handleException(e); } } 292 293 public int[] executeBatch() throws SQLException 294 { checkOpen(); try { return _stmt.executeBatch(); } catch (SQLException e) { handleException(e); return null; } } 295 296 299 300 301 public boolean getMoreResults(int current) throws SQLException 302 { checkOpen(); try { return _stmt.getMoreResults(current); } catch (SQLException e) { handleException(e); return false; } } 303 304 public ResultSet getGeneratedKeys() throws SQLException 305 { checkOpen(); try { return _stmt.getGeneratedKeys(); } catch (SQLException e) { handleException(e); return null; } } 306 307 public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException 308 { checkOpen(); try { return _stmt.executeUpdate(sql, autoGeneratedKeys); } catch (SQLException e) { handleException(e); return 0; } } 309 310 public int executeUpdate(String sql, int columnIndexes[]) throws SQLException 311 { checkOpen(); try { return _stmt.executeUpdate(sql, columnIndexes); } catch (SQLException e) { handleException(e); return 0; } } 312 313 public int executeUpdate(String sql, String columnNames[]) throws SQLException 314 { checkOpen(); try { return _stmt.executeUpdate(sql, columnNames); } catch (SQLException e) { handleException(e); return 0; } } 315 316 public boolean execute(String sql, int autoGeneratedKeys) throws SQLException 317 { checkOpen(); try { return _stmt.execute(sql, autoGeneratedKeys); } catch (SQLException e) { handleException(e); return false; } } 318 319 public boolean execute(String sql, int columnIndexes[]) throws SQLException 320 { checkOpen(); try { return _stmt.execute(sql, columnIndexes); } catch (SQLException e) { handleException(e); return false; } } 321 322 public boolean execute(String sql, String columnNames[]) throws SQLException 323 { checkOpen(); try { return _stmt.execute(sql, columnNames); } catch (SQLException e) { handleException(e); return false; } } 324 325 public int getResultSetHoldability() throws SQLException 326 { checkOpen(); try { return _stmt.getResultSetHoldability(); } catch (SQLException e) { handleException(e); return 0; } } 327 328 329 } 330 | Popular Tags |