1 19 package craftsman.spy; 20 21 import java.io.InputStream ; 22 import java.io.Reader ; 23 import java.math.BigDecimal ; 24 import java.net.URL ; 25 import java.sql.Array ; 26 import java.sql.Blob ; 27 import java.sql.CallableStatement ; 28 import java.sql.Clob ; 29 import java.sql.Connection ; 30 import java.sql.Date ; 31 import java.sql.Ref ; 32 import java.sql.SQLException ; 33 import java.sql.Time ; 34 import java.sql.Timestamp ; 35 import java.sql.Types ; 36 import java.util.ArrayList ; 37 import java.util.Calendar ; 38 import java.util.HashMap ; 39 import java.util.Map ; 40 41 46 public class SpyCallableStatement extends SpyPreparedStatement implements CallableStatement { 47 50 private CallableStatement real = null; 51 52 53 56 private boolean outputParametersLogged = false; 57 58 59 62 private ArrayList outputParametersIndex = null; 63 64 65 68 private HashMap outputParametersName = null; 69 70 71 77 protected SpyCallableStatement ( Connection c, CallableStatement cstm, String sql) { 78 super(c, cstm, sql); 79 real = cstm; 80 81 outputParametersIndex = new ArrayList (getEstimatedParametersCount()+1); 83 for (int i = 0; i < getEstimatedParametersCount()+1; i++) outputParametersIndex.add(i,null); 84 outputParametersName = new HashMap (getEstimatedParametersCount()+1); 85 } 86 87 88 94 private void registerOutputParameter (int paramIndex, int type) { 95 if ( outputParametersIndex.size()<=paramIndex) { 96 outputParametersIndex.ensureCapacity(paramIndex+1); 97 for ( int i = outputParametersIndex.size(); i < paramIndex; i++) outputParametersIndex.add(i,null); 98 outputParametersIndex.add(paramIndex,new Integer (type)); 99 } else { 100 outputParametersIndex.set(paramIndex,new Integer (type)); 101 } 102 } 103 104 105 111 private void registerOutputParameter (String paramName, int type) { 112 outputParametersName.put(paramName,new Integer (type)); 113 } 114 115 116 119 public void clearParameters() throws SQLException { 120 outputParametersIndex.clear(); 121 outputParametersName.clear(); 122 super.clearParameters(); 123 } 124 125 126 129 private void logOutputParameters() { 130 136 if ( outputParametersLogged==false) { 137 StringBuffer displayableSql = new StringBuffer (preparedSql.length()); 138 139 140 if ( outputParametersIndex!=null && outputParametersName!=null) { 141 int i = 1, limit = 0, base = 0; 142 while ((limit = preparedSql.indexOf('?',limit)) != -1) { 143 displayableSql.append(preparedSql.substring(base,limit)); 144 if ( outputParametersIndex.get(i)!=null) { 145 switch ( ((Integer )outputParametersIndex.get(i)).intValue()) { 146 case Types.ARRAY: 147 case Types.BLOB: 148 case Types.CHAR: 149 case Types.CLOB: 150 case Types.DATALINK: 151 case Types.DATE: 152 case Types.JAVA_OBJECT: 153 case Types.LONGVARBINARY: 154 case Types.LONGVARCHAR: 155 case Types.REF: 156 case Types.TIME: 157 case Types.TIMESTAMP: 158 case Types.VARBINARY: 159 case Types.VARCHAR: displayableSql.append("'"); 160 try { 161 displayableSql.append(real.getString(i)); 162 } catch ( SQLException e) { 163 if ( log.isErrorEnabled()) log.error(getId()+":"+"unable to retrieve output parameter",e); 164 } 165 displayableSql.append("'"); 166 break; 167 default: try { 168 displayableSql.append(real.getString(i)); 169 } catch ( SQLException e) { 170 if ( log.isErrorEnabled()) log.error(getId()+":"+"unable to retrieve output parameter",e); 171 } 172 break; 173 } 174 } else { 175 displayableSql.append("?"); 176 } 177 i++; 178 limit++; 179 base = limit; 180 } 181 182 if (base < preparedSql.length()) { 183 displayableSql.append(preparedSql.substring(base)); 184 } 185 } 186 187 log.info(getId()+":"+displayableSql.toString()); 188 outputParametersLogged = true; 189 } 190 } 191 192 193 196 public boolean wasNull() throws SQLException { 197 return real.wasNull(); 198 } 199 200 201 204 public byte getByte(int parameterIndex) throws SQLException { 205 logOutputParameters(); 206 return real.getByte(parameterIndex); 207 } 208 209 210 213 public double getDouble(int parameterIndex) throws SQLException { 214 logOutputParameters(); 215 return real.getDouble(parameterIndex); 216 } 217 218 219 222 public float getFloat(int parameterIndex) throws SQLException { 223 logOutputParameters(); 224 return real.getFloat(parameterIndex); 225 } 226 227 228 231 public int getInt(int parameterIndex) throws SQLException { 232 logOutputParameters(); 233 return real.getInt(parameterIndex); 234 } 235 236 237 240 public long getLong(int parameterIndex) throws SQLException { 241 logOutputParameters(); 242 return real.getLong(parameterIndex); 243 } 244 245 246 249 public short getShort(int parameterIndex) throws SQLException { 250 logOutputParameters(); 251 return real.getShort(parameterIndex); 252 } 253 254 255 258 public boolean getBoolean(int parameterIndex) throws SQLException { 259 logOutputParameters(); 260 return real.getBoolean(parameterIndex); 261 } 262 263 264 267 public byte[] getBytes(int parameterIndex) throws SQLException { 268 logOutputParameters(); 269 return real.getBytes(parameterIndex); 270 } 271 272 273 276 public void registerOutParameter(int parameterIndex, int sqlType) throws SQLException { 277 registerOutputParameter(parameterIndex,sqlType); 278 real.registerOutParameter(parameterIndex,sqlType); 279 } 280 281 282 285 public void registerOutParameter(int parameterIndex, int sqlType, int scale) throws SQLException { 286 registerOutputParameter(parameterIndex,sqlType); 287 real.registerOutParameter(parameterIndex,sqlType,scale); 288 } 289 290 291 294 public Object getObject(int parameterIndex) throws SQLException { 295 logOutputParameters(); 296 return real.getObject(parameterIndex); 297 } 298 299 300 303 public String getString(int parameterIndex) throws SQLException { 304 logOutputParameters(); 305 return real.getString(parameterIndex); 306 } 307 308 309 312 public void registerOutParameter(int paramIndex, int sqlType, String typeName) throws SQLException { 313 registerOutputParameter(paramIndex,sqlType); 314 real.registerOutParameter(paramIndex,sqlType,typeName); 315 } 316 317 318 321 public byte getByte(String parameterName) throws SQLException { 322 logOutputParameters(); 323 return real.getByte(parameterName); 324 } 325 326 327 330 public double getDouble(String parameterName) throws SQLException { 331 logOutputParameters(); 332 return real.getDouble(parameterName); 333 } 334 335 336 339 public float getFloat(String parameterName) throws SQLException { 340 logOutputParameters(); 341 return real.getFloat(parameterName); 342 } 343 344 345 348 public int getInt(String parameterName) throws SQLException { 349 logOutputParameters(); 350 return real.getInt(parameterName); 351 } 352 353 354 357 public long getLong(String parameterName) throws SQLException { 358 logOutputParameters(); 359 return real.getLong(parameterName); 360 } 361 362 363 366 public short getShort(String parameterName) throws SQLException { 367 logOutputParameters(); 368 return real.getShort(parameterName); 369 } 370 371 372 375 public boolean getBoolean(String parameterName) throws SQLException { 376 logOutputParameters(); 377 return real.getBoolean(parameterName); 378 } 379 380 381 384 public byte[] getBytes(String parameterName) throws SQLException { 385 logOutputParameters(); 386 return real.getBytes(parameterName); 387 } 388 389 390 393 public void setByte(String parameterName, byte x) throws SQLException { 394 real.setByte(parameterName,x); 395 } 396 397 398 401 public void setDouble(String parameterName, double x) throws SQLException { 402 real.setDouble(parameterName,x); 403 } 404 405 406 409 public void setFloat(String parameterName, float x) throws SQLException { 410 real.setFloat(parameterName,x); 411 } 412 413 414 417 public void registerOutParameter(String parameterName, int sqlType) throws SQLException { 418 registerOutputParameter(parameterName,sqlType); 419 real.registerOutParameter(parameterName,sqlType); 420 } 421 422 423 426 public void setInt(String parameterName, int x) throws SQLException { 427 real.setInt(parameterName,x); 428 } 429 430 431 434 public void setNull(String parameterName, int sqlType) throws SQLException { 435 real.setNull(parameterName,sqlType); 436 } 437 438 439 442 public void registerOutParameter(String parameterName, int sqlType, int scale) throws SQLException { 443 registerOutputParameter(parameterName,sqlType); 444 real.registerOutParameter(parameterName,sqlType,scale); 445 } 446 447 448 451 public void setLong(String parameterName, long x) throws SQLException { 452 real.setLong(parameterName,x); 453 } 454 455 456 459 public void setShort(String parameterName, short x) throws SQLException { 460 real.setShort(parameterName,x); 461 } 462 463 464 467 public void setBoolean(String parameterName, boolean x) throws SQLException { 468 real.setBoolean(parameterName,x); 469 } 470 471 472 475 public void setBytes(String parameterName, byte[] x) throws SQLException { 476 real.setBytes(parameterName,x); 477 } 478 479 480 483 public BigDecimal getBigDecimal(int parameterIndex) throws SQLException { 484 logOutputParameters(); 485 return real.getBigDecimal(parameterIndex); 486 } 487 488 489 492 public BigDecimal getBigDecimal(int parameterIndex, int scale) throws SQLException { 493 logOutputParameters(); 494 return real.getBigDecimal(parameterIndex,scale); 495 } 496 497 498 501 public URL getURL(int parameterIndex) throws SQLException { 502 logOutputParameters(); 503 return real.getURL(parameterIndex); 504 } 505 506 507 510 public Array getArray(int i) throws SQLException { 511 logOutputParameters(); 512 return real.getArray(i); 513 } 514 515 516 519 public Blob getBlob(int i) throws SQLException { 520 logOutputParameters(); 521 return real.getBlob(i); 522 } 523 524 525 528 public Clob getClob(int i) throws SQLException { 529 logOutputParameters(); 530 return real.getClob(i); 531 } 532 533 534 537 public Date getDate(int parameterIndex) throws SQLException { 538 logOutputParameters(); 539 return real.getDate(parameterIndex); 540 } 541 542 543 546 public Ref getRef(int i) throws SQLException { 547 logOutputParameters(); 548 return real.getRef(i); 549 } 550 551 552 555 public Time getTime(int parameterIndex) throws SQLException { 556 logOutputParameters(); 557 return real.getTime(parameterIndex); 558 } 559 560 561 564 public Timestamp getTimestamp(int parameterIndex) throws SQLException { 565 logOutputParameters(); 566 return real.getTimestamp(parameterIndex); 567 } 568 569 570 573 public void setAsciiStream(String parameterName, InputStream x, int length) throws SQLException { 574 real.setAsciiStream(parameterName,x,length); 575 } 576 577 578 581 public void setBinaryStream(String parameterName, InputStream x, int length) throws SQLException { 582 real.setBinaryStream(parameterName,x,length); 583 } 584 585 586 589 public void setCharacterStream(String parameterName, Reader reader, int length) throws SQLException { 590 real.setCharacterStream(parameterName,reader,length); 591 } 592 593 594 597 public Object getObject(String parameterName) throws SQLException { 598 logOutputParameters(); 599 return real.getObject(parameterName); 600 } 601 602 603 606 public void setObject(String parameterName, Object x) throws SQLException { 607 real.setObject(parameterName,x); 608 } 609 610 611 614 public void setObject(String parameterName, Object x, int targetSqlType) throws SQLException { 615 real.setObject(parameterName,x,targetSqlType); 616 } 617 618 619 622 public void setObject(String parameterName, Object x, int targetSqlType, int scale) throws SQLException { 623 real.setObject(parameterName,x,targetSqlType,scale); 624 } 625 626 627 630 public Object getObject(int i, Map map) throws SQLException { 631 logOutputParameters(); 632 return real.getObject(i,map); 633 } 634 635 636 639 public String getString(String parameterName) throws SQLException { 640 logOutputParameters(); 641 return real.getString(parameterName); 642 } 643 644 645 648 public void registerOutParameter(String parameterName, int sqlType, String typeName) throws SQLException { 649 registerOutputParameter(parameterName,sqlType); 650 real.registerOutParameter(parameterName,sqlType,typeName); 651 } 652 653 654 657 public void setNull(String parameterName, int sqlType, String typeName) throws SQLException { 658 real.setNull(parameterName,sqlType,typeName); 659 } 660 661 662 665 public void setString(String parameterName, String x) throws SQLException { 666 real.setString(parameterName,x); 667 } 668 669 670 673 public BigDecimal getBigDecimal(String parameterName) throws SQLException { 674 logOutputParameters(); 675 return real.getBigDecimal(parameterName); 676 } 677 678 679 682 public void setBigDecimal(String parameterName, BigDecimal x) throws SQLException { 683 real.setBigDecimal(parameterName,x); 684 } 685 686 687 690 public URL getURL(String parameterName) throws SQLException { 691 logOutputParameters(); 692 return real.getURL(parameterName); 693 } 694 695 696 699 public void setURL(String parameterName, URL val) throws SQLException { 700 real.setURL(parameterName,val); 701 } 702 703 704 707 public Array getArray(String parameterName) throws SQLException { 708 logOutputParameters(); 709 return real.getArray(parameterName); 710 } 711 712 713 716 public Blob getBlob(String parameterName) throws SQLException { 717 logOutputParameters(); 718 return real.getBlob(parameterName); 719 } 720 721 722 725 public Clob getClob(String parameterName) throws SQLException { 726 logOutputParameters(); 727 return real.getClob(parameterName); 728 } 729 730 731 734 public Date getDate(String parameterName) throws SQLException { 735 logOutputParameters(); 736 return real.getDate(parameterName); 737 } 738 739 740 743 public void setDate(String parameterName, Date x) throws SQLException { 744 real.setDate(parameterName,x); 745 } 746 747 748 751 public Date getDate(int parameterIndex, Calendar cal) throws SQLException { 752 logOutputParameters(); 753 return real.getDate(parameterIndex,cal); 754 } 755 756 757 760 public Ref getRef(String parameterName) throws SQLException { 761 logOutputParameters(); 762 return real.getRef(parameterName); 763 } 764 765 766 769 public Time getTime(String parameterName) throws SQLException { 770 logOutputParameters(); 771 return real.getTime(parameterName); 772 } 773 774 775 778 public void setTime(String parameterName, Time x) throws SQLException { 779 real.setTime(parameterName,x); 780 } 781 782 783 786 public Time getTime(int parameterIndex, Calendar cal) throws SQLException { 787 logOutputParameters(); 788 return real.getTime(parameterIndex,cal); 789 } 790 791 792 795 public Timestamp getTimestamp(String parameterName) throws SQLException { 796 logOutputParameters(); 797 return real.getTimestamp(parameterName); 798 } 799 800 801 804 public void setTimestamp(String parameterName, Timestamp x) throws SQLException { 805 real.setTimestamp(parameterName,x); 806 } 807 808 809 812 public Timestamp getTimestamp(int parameterIndex, Calendar cal) throws SQLException { 813 logOutputParameters(); 814 return real.getTimestamp(parameterIndex,cal); 815 } 816 817 818 821 public Object getObject(String parameterName, Map map) throws SQLException { 822 logOutputParameters(); 823 return real.getObject(parameterName,map); 824 } 825 826 827 830 public Date getDate(String parameterName, Calendar cal) throws SQLException { 831 logOutputParameters(); 832 return real.getDate(parameterName,cal); 833 } 834 835 836 839 public Time getTime(String parameterName, Calendar cal) throws SQLException { 840 logOutputParameters(); 841 return real.getTime(parameterName,cal); 842 } 843 844 845 848 public Timestamp getTimestamp(String parameterName, Calendar cal) throws SQLException { 849 logOutputParameters(); 850 return real.getTimestamp(parameterName,cal); 851 } 852 853 854 857 public void setDate(String parameterName, Date x, Calendar cal) throws SQLException { 858 real.setDate(parameterName,x,cal); 859 } 860 861 862 865 public void setTime(String parameterName, Time x, Calendar cal) throws SQLException { 866 real.setTime(parameterName,x,cal); 867 } 868 869 870 873 public void setTimestamp(String parameterName, Timestamp x, Calendar cal) throws SQLException { 874 real.setTimestamp(parameterName,x,cal); 875 } 876 } 877 | Popular Tags |