1 19 package craftsman.spy; 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 import java.util.ArrayList ; 27 28 34 public class SpyStatement extends SpyEntity implements Statement { 35 38 private Statement real = null; 39 40 41 44 protected ArrayList batch = null; 45 46 47 53 protected SpyStatement ( Connection c, Statement stm) { 54 super ( c); 55 real = stm; 56 batch = new ArrayList (); 57 } 58 59 60 63 public int getFetchDirection() throws SQLException { 64 return real.getFetchDirection(); 65 } 66 67 68 71 public int getFetchSize() throws SQLException { 72 return real.getFetchSize(); 73 } 74 75 76 79 public int getMaxFieldSize() throws SQLException { 80 return real.getMaxFieldSize(); 81 } 82 83 84 87 public int getMaxRows() throws SQLException { 88 return real.getMaxRows(); 89 } 90 91 92 95 public int getQueryTimeout() throws SQLException { 96 return real.getQueryTimeout(); 97 } 98 99 100 103 public int getResultSetConcurrency() throws SQLException { 104 return real.getResultSetConcurrency(); 105 } 106 107 108 111 public int getResultSetHoldability() throws SQLException { 112 return real.getResultSetHoldability(); 113 } 114 115 116 119 public int getResultSetType() throws SQLException { 120 return real.getResultSetType(); 121 } 122 123 124 127 public int getUpdateCount() throws SQLException { 128 return real.getUpdateCount(); 129 } 130 131 132 135 public void cancel() throws SQLException { 136 real.cancel(); 137 } 138 139 140 143 public void clearBatch() throws SQLException { 144 batch.clear(); 145 real.clearBatch(); 146 } 147 148 149 152 public void clearWarnings() throws SQLException { 153 real.clearWarnings(); 154 } 155 156 157 160 public void close() throws SQLException { 161 real.close(); 162 } 163 164 165 168 public boolean getMoreResults() throws SQLException { 169 return real.getMoreResults(); 170 } 171 172 173 176 public int[] executeBatch() throws SQLException { 177 long end, start = System.currentTimeMillis(); 178 int[] result = null; 179 180 181 try { 182 result = real.executeBatch(); 183 end = System.currentTimeMillis(); 184 185 if ( batch.size() != result.length) { 186 if ( log.isWarnEnabled()) log.warn(getId()+":"+"expecting:"+batch.size()+" but was:"+result.length); 187 } 188 if ( log.isInfoEnabled()) { 189 log.info(getId()+":"+"executing batch..."); 190 for (int i = 0; i < result.length && i <batch.size(); i++) { 191 switch(result[i]) { 192 case SUCCESS_NO_INFO: log.info(getId()+":"+batch.get(i)+" => SUCCESS_NO_INFO"); 193 break; 194 195 case EXECUTE_FAILED: log.info(getId()+":"+batch.get(i)+" => EXECUTE_FAILED"); 196 break; 197 198 default: log.info(getId()+":"+batch.get(i)+" => "+result[i]); 199 break; 200 } 201 } 202 log.info(getId()+":"+"batch executed ("+(end-start)+" ms)"); 203 } 204 } catch ( SQLException e) { 205 end = System.currentTimeMillis(); 206 for (int i = 0; i < batch.size(); i++) { 207 log.error(getId()+":"+batch.get(i)+" => ..."); 208 } 209 if ( log.isWarnEnabled()) log.warn(getId()+":batch.size="+batch.size()); 210 if ( log.isErrorEnabled()) log.error(getId()+":"+batch+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e); 211 throw e; 212 } 213 214 return result; 215 } 216 217 218 221 public void setFetchDirection(int direction) throws SQLException { 222 real.setFetchDirection(direction); 223 } 224 225 226 229 public void setFetchSize(int rows) throws SQLException { 230 real.setFetchSize(rows); 231 } 232 233 234 237 public void setMaxFieldSize(int max) throws SQLException { 238 real.setMaxFieldSize(max); 239 } 240 241 242 245 public void setMaxRows(int max) throws SQLException { 246 real.setMaxRows(max); 247 } 248 249 250 253 public void setQueryTimeout(int seconds) throws SQLException { 254 real.setQueryTimeout(seconds); 255 } 256 257 258 261 public boolean getMoreResults(int current) throws SQLException { 262 return real.getMoreResults(current); 263 } 264 265 266 269 public void setEscapeProcessing(boolean enable) throws SQLException { 270 real.setEscapeProcessing(enable); 271 } 272 273 274 277 public int executeUpdate(String sql) throws SQLException { 278 long end, start = System.currentTimeMillis(); 279 int result = 0; 280 281 282 try { 283 result = real.executeUpdate(sql); 284 end = System.currentTimeMillis(); 285 if ( log.isInfoEnabled()) log.info(getId()+":"+sql+" => "+result+" ("+(end-start)+" ms)"); 286 } catch ( SQLException e) { 287 end = System.currentTimeMillis(); 288 if ( log.isErrorEnabled()) log.error(getId()+":"+sql+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e); 289 throw e; 290 } 291 292 return result; 293 } 294 295 296 299 public void addBatch(String sql) throws SQLException { 300 batch.add(sql); 301 real.addBatch(sql); 302 } 303 304 305 308 public void setCursorName(String name) throws SQLException { 309 real.setCursorName(name); 310 } 311 312 313 316 public boolean execute(String sql) throws SQLException { 317 long end, start = System.currentTimeMillis(); 318 boolean result = false; 319 320 321 try { 322 result = real.execute(sql); 323 end = System.currentTimeMillis(); 324 if ( log.isInfoEnabled()) log.info(getId()+":"+sql+" => "+result+" ("+(end-start)+" ms)"); 325 } catch ( SQLException e) { 326 end = System.currentTimeMillis(); 327 if ( log.isErrorEnabled()) log.error(getId()+":"+sql+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e); 328 throw e; 329 } 330 331 return result; 332 } 333 334 335 338 public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { 339 long end, start = System.currentTimeMillis(); 340 int result = 0; 341 342 343 try { 344 result = real.executeUpdate(sql,autoGeneratedKeys); 345 end = System.currentTimeMillis(); 346 if ( log.isInfoEnabled()) log.info(getId()+":"+sql+" => "+result+" ("+(end-start)+" ms)"); 347 } catch ( SQLException e) { 348 end = System.currentTimeMillis(); 349 if ( log.isErrorEnabled()) log.error(getId()+":"+sql+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e); 350 throw e; 351 } 352 353 return result; 354 } 355 356 357 360 public boolean execute(String sql, int autoGeneratedKeys) throws SQLException { 361 long end, start = System.currentTimeMillis(); 362 boolean result = false; 363 364 365 try { 366 result = real.execute(sql,autoGeneratedKeys); 367 end = System.currentTimeMillis(); 368 if ( log.isInfoEnabled()) log.info(getId()+":"+sql+" => "+result+" ("+(end-start)+" ms)"); 369 } catch ( SQLException e) { 370 end = System.currentTimeMillis(); 371 if ( log.isErrorEnabled()) log.error(getId()+":"+sql+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e); 372 throw e; 373 } 374 375 return result; 376 } 377 378 379 382 public int executeUpdate(String sql, int[] columnIndexes) throws SQLException { 383 long end, start = System.currentTimeMillis(); 384 int result = 0; 385 386 387 try { 388 result = real.executeUpdate(sql,columnIndexes); 389 end = System.currentTimeMillis(); 390 if ( log.isInfoEnabled()) log.info(getId()+":"+sql+" => "+result+" ("+(end-start)+" ms)"); 391 } catch ( SQLException e) { 392 end = System.currentTimeMillis(); 393 if ( log.isErrorEnabled()) log.error(getId()+":"+sql+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e); 394 throw e; 395 } 396 397 return result; 398 } 399 400 401 404 public boolean execute(String sql, int[] columnIndexes) throws SQLException { 405 long end, start = System.currentTimeMillis(); 406 boolean result = false; 407 408 409 try { 410 result = real.execute(sql,columnIndexes); 411 end = System.currentTimeMillis(); 412 if ( log.isInfoEnabled()) log.info(getId()+":"+sql+" => "+result+" ("+(end-start)+" ms)"); 413 } catch ( SQLException e) { 414 end = System.currentTimeMillis(); 415 if ( log.isErrorEnabled()) log.error(getId()+":"+sql+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e); 416 throw e; 417 } 418 419 return result; 420 } 421 422 423 426 public ResultSet getGeneratedKeys() throws SQLException { 427 return new SpyResultSet ( getConnection(), this, real.getGeneratedKeys()); 428 } 429 430 431 434 public ResultSet getResultSet() throws SQLException { 435 return new SpyResultSet ( getConnection(), this, real.getResultSet()); 436 } 437 438 439 442 public SQLWarning getWarnings() throws SQLException { 443 SQLWarning current, sw = real.getWarnings(); 444 445 446 if ( (current = sw)!=null) { 447 do { 448 if ( log.isInfoEnabled()) log.info(getId()+":sql warning state="+current.getSQLState()+",code="+current.getErrorCode()+",message="+current.getMessage()); 449 } while ( (current = current.getNextWarning())!=null); 450 } 451 452 return sw; 453 } 454 455 456 459 public int executeUpdate(String sql, String [] columnNames) throws SQLException { 460 long end, start = System.currentTimeMillis(); 461 int result = 0; 462 463 464 try { 465 result = real.executeUpdate(sql,columnNames); 466 end = System.currentTimeMillis(); 467 if ( log.isInfoEnabled()) log.info(getId()+":"+sql+" => "+result+" ("+(end-start)+" ms)"); 468 } catch ( SQLException e) { 469 end = System.currentTimeMillis(); 470 if ( log.isErrorEnabled()) log.error(getId()+":"+sql+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e); 471 throw e; 472 } 473 474 return result; 475 } 476 477 478 481 public boolean execute(String sql, String [] columnNames) throws SQLException { 482 long end, start = System.currentTimeMillis(); 483 boolean result = false; 484 485 486 try { 487 result = real.execute(sql,columnNames); 488 end = System.currentTimeMillis(); 489 if ( log.isInfoEnabled()) log.info(getId()+":"+sql+" => "+result+" ("+(end-start)+" ms)"); 490 } catch ( SQLException e) { 491 end = System.currentTimeMillis(); 492 if ( log.isErrorEnabled()) log.error(getId()+":"+sql+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e); 493 throw e; 494 } 495 496 return result; 497 } 498 499 500 503 public ResultSet executeQuery(String sql) throws SQLException { 504 long end, start = System.currentTimeMillis(); 505 ResultSet result = null; 506 507 508 try { 509 result = new SpyResultSet ( getConnection(), this, real.executeQuery(sql)); 510 end = System.currentTimeMillis(); 511 if ( log.isInfoEnabled()) log.info(getId()+":"+sql+" => ... ("+(end-start)+" ms)"); 512 } catch ( SQLException e) { 513 end = System.currentTimeMillis(); 514 if ( log.isErrorEnabled()) log.error(getId()+":"+sql+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e); 515 throw e; 516 } 517 518 return result; 519 } 520 } 521 | Popular Tags |