001 package com.mockrunner.mock.jdbc; 002 003 import java.io.ByteArrayInputStream; 004 import java.io.InputStream; 005 import java.io.Reader; 006 import java.io.StringReader; 007 import java.math.BigDecimal; 008 import java.net.MalformedURLException; 009 import java.net.URL; 010 import java.sql.Array; 011 import java.sql.BatchUpdateException; 012 import java.sql.Blob; 013 import java.sql.CallableStatement; 014 import java.sql.Clob; 015 import java.sql.Connection; 016 import java.sql.Date; 017 import java.sql.Ref; 018 import java.sql.ResultSet; 019 import java.sql.SQLException; 020 import java.sql.Time; 021 import java.sql.Timestamp; 022 import java.util.ArrayList; 023 import java.util.Calendar; 024 import java.util.Collections; 025 import java.util.HashMap; 026 import java.util.HashSet; 027 import java.util.List; 028 import java.util.Map; 029 import java.util.Set; 030 031 import com.mockrunner.jdbc.AbstractOutParameterResultSetHandler; 032 import com.mockrunner.util.common.StreamUtil; 033 034 /** 035 * Mock implementation of <code>CallableStatement</code>. 036 */ 037 public class MockCallableStatement extends MockPreparedStatement implements CallableStatement 038 { 039 private AbstractOutParameterResultSetHandler resultSetHandler; 040 private Map paramObjects = new HashMap(); 041 private Set outParameterSetIndexed = new HashSet(); 042 private Set outParameterSetNamed = new HashSet(); 043 private List batchParameters = new ArrayList(); 044 private boolean wasNull = false; 045 046 public MockCallableStatement(Connection connection, String sql) 047 { 048 super(connection, sql); 049 } 050 051 public MockCallableStatement(Connection connection, String sql, int resultSetType, int resultSetConcurrency) 052 { 053 super(connection, sql, resultSetType, resultSetConcurrency); 054 } 055 056 public MockCallableStatement(Connection connection, String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) 057 { 058 super(connection, sql, resultSetType, resultSetConcurrency, resultSetHoldability); 059 } 060 061 public void setCallableStatementResultSetHandler(AbstractOutParameterResultSetHandler resultSetHandler) 062 { 063 super.setPreparedStatementResultSetHandler(resultSetHandler); 064 this.resultSetHandler = resultSetHandler; 065 } 066 067 public Map getNamedParameterMap() 068 { 069 return Collections.unmodifiableMap(paramObjects); 070 } 071 072 public Map getParameterMap() 073 { 074 Map parameterMap = new HashMap(getIndexedParameterMap()); 075 parameterMap.putAll(getNamedParameterMap()); 076 return Collections.unmodifiableMap(parameterMap); 077 } 078 079 public Object getParameter(String name) 080 { 081 return paramObjects.get(name); 082 } 083 084 public Set getNamedRegisteredOutParameterSet() 085 { 086 return Collections.unmodifiableSet(outParameterSetNamed); 087 } 088 089 public boolean isOutParameterRegistered(int index) 090 { 091 return outParameterSetIndexed.contains(new Integer(index)); 092 } 093 094 public Set getIndexedRegisteredOutParameterSet() 095 { 096 return Collections.unmodifiableSet(outParameterSetIndexed); 097 } 098 099 public boolean isOutParameterRegistered(String parameterName) 100 { 101 return outParameterSetNamed.contains(parameterName); 102 } 103 104 public ResultSet executeQuery() throws SQLException 105 { 106 return executeQuery(getParameterMap()); 107 } 108 109 public int executeUpdate() throws SQLException 110 { 111 return executeUpdate(getParameterMap()); 112 } 113 114 public void addBatch() throws SQLException 115 { 116 batchParameters.add(new HashMap(getParameterMap())); 117 } 118 119 public int[] executeBatch() throws SQLException 120 { 121 int[] results = new int[batchParameters.size()]; 122 if(isQuery(getSQL())) 123 { 124 throw new BatchUpdateException("SQL " + getSQL() + " returned a ResultSet.", null); 125 } 126 for(int ii = 0; ii < results.length; ii++) 127 { 128 Map currentParameters = (Map)batchParameters.get(ii); 129 results[ii] = executeUpdate(currentParameters); 130 } 131 return results; 132 } 133 134 public void registerOutParameter(int parameterIndex, int sqlType) throws SQLException 135 { 136 outParameterSetIndexed.add(new Integer(parameterIndex)); 137 } 138 139 public void registerOutParameter(int parameterIndex, int sqlType, int scale) throws SQLException 140 { 141 registerOutParameter(parameterIndex, sqlType); 142 } 143 144 public void registerOutParameter(int parameterIndex, int sqlType, String typeName) throws SQLException 145 { 146 registerOutParameter(parameterIndex, sqlType); 147 } 148 149 public void registerOutParameter(String parameterName, int sqlType) throws SQLException 150 { 151 outParameterSetNamed.add(parameterName); 152 } 153 154 public void registerOutParameter(String parameterName, int sqlType, int scale) throws SQLException 155 { 156 registerOutParameter(parameterName, sqlType); 157 } 158 159 public void registerOutParameter(String parameterName, int sqlType, String typeName) throws SQLException 160 { 161 registerOutParameter(parameterName, sqlType); 162 } 163 164 public boolean wasNull() throws SQLException 165 { 166 return wasNull; 167 } 168 169 public byte getByte(int parameterIndex) throws SQLException 170 { 171 Object value = getObject(parameterIndex); 172 if(null != value) 173 { 174 if(value instanceof Number) return ((Number)value).byteValue(); 175 return new Byte(value.toString()).byteValue(); 176 } 177 return 0; 178 } 179 180 public double getDouble(int parameterIndex) throws SQLException 181 { 182 Object value = getObject(parameterIndex); 183 if(null != value) 184 { 185 if(value instanceof Number) return ((Number)value).doubleValue(); 186 return new Double(value.toString()).doubleValue(); 187 } 188 return 0; 189 } 190 191 public float getFloat(int parameterIndex) throws SQLException 192 { 193 Object value = getObject(parameterIndex); 194 if(null != value) 195 { 196 if(value instanceof Number) return ((Number)value).floatValue(); 197 return new Float(value.toString()).floatValue(); 198 } 199 return 0; 200 } 201 202 public int getInt(int parameterIndex) throws SQLException 203 { 204 Object value = getObject(parameterIndex); 205 if(null != value) 206 { 207 if(value instanceof Number) return ((Number)value).intValue(); 208 return new Integer(value.toString()).intValue(); 209 } 210 return 0; 211 } 212 213 public long getLong(int parameterIndex) throws SQLException 214 { 215 Object value = getObject(parameterIndex); 216 if(null != value) 217 { 218 if(value instanceof Number) return ((Number)value).longValue(); 219 return new Long(value.toString()).longValue(); 220 } 221 return 0; 222 } 223 224 public short getShort(int parameterIndex) throws SQLException 225 { 226 Object value = getObject(parameterIndex); 227 if(null != value) 228 { 229 if(value instanceof Number) return ((Number)value).shortValue(); 230 return new Short(value.toString()).shortValue(); 231 } 232 return 0; 233 } 234 235 public boolean getBoolean(int parameterIndex) throws SQLException 236 { 237 Object value = getObject(parameterIndex); 238 if(null != value) 239 { 240 if(value instanceof Boolean) return ((Boolean)value).booleanValue(); 241 return new Boolean(value.toString()).booleanValue(); 242 } 243 return false; 244 } 245 246 public byte[] getBytes(int parameterIndex) throws SQLException 247 { 248 Object value = getObject(parameterIndex); 249 if(null != value) 250 { 251 if(value instanceof byte[]) return (byte[])value; 252 return value.toString().getBytes(); 253 } 254 return null; 255 } 256 257 public Object getObject(int parameterIndex) throws SQLException 258 { 259 wasNull = false; 260 Map outParameter = getOutParameterMap(); 261 Object returnValue = null; 262 if(null != outParameter) 263 { 264 returnValue = outParameter.get(new Integer(parameterIndex)); 265 } 266 if(null == returnValue) wasNull = true; 267 return returnValue; 268 } 269 270 public String getString(int parameterIndex) throws SQLException 271 { 272 Object value = getObject(parameterIndex); 273 if(null != value) return value.toString(); 274 return null; 275 } 276 277 public byte getByte(String parameterName) throws SQLException 278 { 279 Object value = getObject(parameterName); 280 if(null != value) 281 { 282 if(value instanceof Number) return ((Number)value).byteValue(); 283 return new Byte(value.toString()).byteValue(); 284 } 285 return 0; 286 } 287 288 public double getDouble(String parameterName) throws SQLException 289 { 290 Object value = getObject(parameterName); 291 if(null != value) 292 { 293 if(value instanceof Number) return ((Number)value).doubleValue(); 294 return new Double(value.toString()).doubleValue(); 295 } 296 return 0; 297 } 298 299 public float getFloat(String parameterName) throws SQLException 300 { 301 Object value = getObject(parameterName); 302 if(null != value) 303 { 304 if(value instanceof Number) return ((Number)value).floatValue(); 305 return new Float(value.toString()).floatValue(); 306 } 307 return 0; 308 } 309 310 public int getInt(String parameterName) throws SQLException 311 { 312 Object value = getObject(parameterName); 313 if(null != value) 314 { 315 if(value instanceof Number) return ((Number)value).intValue(); 316 return new Integer(value.toString()).intValue(); 317 } 318 return 0; 319 } 320 321 public long getLong(String parameterName) throws SQLException 322 { 323 Object value = getObject(parameterName); 324 if(null != value) 325 { 326 if(value instanceof Number) return ((Number)value).longValue(); 327 return new Long(value.toString()).longValue(); 328 } 329 return 0; 330 } 331 332 public short getShort(String parameterName) throws SQLException 333 { 334 Object value = getObject(parameterName); 335 if(null != value) 336 { 337 if(value instanceof Number) return ((Number)value).shortValue(); 338 return new Short(value.toString()).shortValue(); 339 } 340 return 0; 341 } 342 343 public boolean getBoolean(String parameterName) throws SQLException 344 { 345 Object value = getObject(parameterName); 346 if(null != value) 347 { 348 if(value instanceof Boolean) return ((Boolean)value).booleanValue(); 349 return new Boolean(value.toString()).booleanValue(); 350 } 351 return false; 352 } 353 354 public byte[] getBytes(String parameterName) throws SQLException 355 { 356 Object value = getObject(parameterName); 357 if(null != value) 358 { 359 if(value instanceof byte[]) return (byte[])value; 360 return value.toString().getBytes(); 361 } 362 return null; 363 } 364 365 public void setByte(String parameterName, byte byteValue) throws SQLException 366 { 367 setObject(parameterName, new Byte(byteValue)); 368 } 369 370 public void setDouble(String parameterName, double doubleValue) throws SQLException 371 { 372 setObject(parameterName, new Double(doubleValue)); 373 } 374 375 public void setFloat(String parameterName, float floatValue) throws SQLException 376 { 377 setObject(parameterName, new Float(floatValue)); 378 } 379 380 public void setInt(String parameterName, int intValue) throws SQLException 381 { 382 setObject(parameterName, new Integer(intValue)); 383 } 384 385 public void setNull(String parameterName, int sqlType) throws SQLException 386 { 387 setObject(parameterName, null); 388 } 389 390 public void setLong(String parameterName, long longValue) throws SQLException 391 { 392 setObject(parameterName, new Long(longValue)); 393 } 394 395 public void setShort(String parameterName, short shortValue) throws SQLException 396 { 397 setObject(parameterName, new Short(shortValue)); 398 } 399 400 public void setBoolean(String parameterName, boolean booleanValue) throws SQLException 401 { 402 setObject(parameterName, new Boolean(booleanValue)); 403 } 404 405 public void setBytes(String parameterName, byte[] byteArray) throws SQLException 406 { 407 setObject(parameterName, byteArray); 408 } 409 410 public BigDecimal getBigDecimal(int parameterIndex) throws SQLException 411 { 412 Object value = getObject(parameterIndex); 413 if(null != value) 414 { 415 if(value instanceof Number) return new BigDecimal(((Number)value).doubleValue()); 416 return new BigDecimal(value.toString()); 417 } 418 return null; 419 } 420 421 public BigDecimal getBigDecimal(int parameterIndex, int scale) throws SQLException 422 { 423 return getBigDecimal(parameterIndex); 424 } 425 426 public URL getURL(int parameterIndex) throws SQLException 427 { 428 Object value = getObject(parameterIndex); 429 if(null != value) 430 { 431 if(value instanceof URL) return (URL)value; 432 try 433 { 434 return new URL(value.toString()); 435 } 436 catch(MalformedURLException exc) 437 { 438 439 } 440 } 441 return null; 442 } 443 444 public Array getArray(int parameterIndex) throws SQLException 445 { 446 Object value = getObject(parameterIndex); 447 if(null != value) 448 { 449 if(value instanceof Array) return (Array)value; 450 return new MockArray(value); 451 } 452 return null; 453 } 454 455 public Blob getBlob(int parameterIndex) throws SQLException 456 { 457 Object value = getObject(parameterIndex); 458 if(null != value) 459 { 460 if(value instanceof Blob) return (Blob)value; 461 return new MockBlob(getBytes(parameterIndex)); 462 } 463 return null; 464 } 465 466 public Clob getClob(int parameterIndex) throws SQLException 467 { 468 Object value = getObject(parameterIndex); 469 if(null != value) 470 { 471 if(value instanceof Clob) return (Clob)value; 472 return new MockClob(getString(parameterIndex)); 473 } 474 return null; 475 } 476 477 public Date getDate(int parameterIndex) throws SQLException 478 { 479 Object value = getObject(parameterIndex); 480 if(null != value) 481 { 482 if(value instanceof Date) return (Date)value; 483 return Date.valueOf(value.toString()); 484 } 485 return null; 486 } 487 488 public Ref getRef(int parameterIndex) throws SQLException 489 { 490 Object value = getObject(parameterIndex); 491 if(null != value) 492 { 493 if(value instanceof Ref) return (Ref)value; 494 return new MockRef(value); 495 } 496 return null; 497 } 498 499 public Time getTime(int parameterIndex) throws SQLException 500 { 501 Object value = getObject(parameterIndex); 502 if(null != value) 503 { 504 if(value instanceof Time) return (Time)value; 505 return Time.valueOf(value.toString()); 506 } 507 return null; 508 } 509 510 public Timestamp getTimestamp(int parameterIndex) throws SQLException 511 { 512 Object value = getObject(parameterIndex); 513 if(null != value) 514 { 515 if(value instanceof Timestamp) return (Timestamp)value; 516 return Timestamp.valueOf(value.toString()); 517 } 518 return null; 519 } 520 521 public void setAsciiStream(String parameterName, InputStream stream, int length) throws SQLException 522 { 523 setBinaryStream(parameterName, stream, length); 524 } 525 526 public void setBinaryStream(String parameterName, InputStream stream, int length) throws SQLException 527 { 528 byte[] data = StreamUtil.getStreamAsByteArray(stream, length); 529 setObject(parameterName, new ByteArrayInputStream(data)); 530 } 531 532 public void setCharacterStream(String parameterName, Reader reader, int length) throws SQLException 533 { 534 String data = StreamUtil.getReaderAsString(reader, length); 535 setObject(parameterName, new StringReader(data)); 536 } 537 538 public Object getObject(String parameterName) throws SQLException 539 { 540 wasNull = false; 541 Map outParameter = getOutParameterMap(); 542 Object returnValue = null; 543 if(null != outParameter) 544 { 545 returnValue = outParameter.get(parameterName); 546 } 547 if(null == returnValue) wasNull = true; 548 return returnValue; 549 } 550 551 public void setObject(String parameterName, Object object) throws SQLException 552 { 553 paramObjects.put(parameterName, object); 554 } 555 556 public void setObject(String parameterName, Object object, int targetSqlType) throws SQLException 557 { 558 setObject(parameterName, object); 559 } 560 561 public void setObject(String parameterName, Object object, int targetSqlType, int scale) throws SQLException 562 { 563 setObject(parameterName, object); 564 } 565 566 public Object getObject(int parameterIndex, Map map) throws SQLException 567 { 568 return getObject(parameterIndex); 569 } 570 571 public String getString(String parameterName) throws SQLException 572 { 573 Object value = getObject(parameterName); 574 if(null != value) return value.toString(); 575 return null; 576 } 577 578 public void setNull(String parameterName, int sqlType, String typeName) throws SQLException 579 { 580 setNull(parameterName, sqlType); 581 } 582 583 public void setString(String parameterName, String string) throws SQLException 584 { 585 setObject(parameterName, string); 586 } 587 588 public BigDecimal getBigDecimal(String parameterName) throws SQLException 589 { 590 Object value = getObject(parameterName); 591 if(null != value) 592 { 593 if(value instanceof Number) return new BigDecimal(((Number)value).doubleValue()); 594 return new BigDecimal(value.toString()); 595 } 596 return null; 597 } 598 599 public void setBigDecimal(String parameterName, BigDecimal bigDecimal) throws SQLException 600 { 601 setObject(parameterName, bigDecimal); 602 } 603 604 public URL getURL(String parameterName) throws SQLException 605 { 606 Object value = getObject(parameterName); 607 if(null != value) 608 { 609 if(value instanceof URL) return (URL)value; 610 try 611 { 612 return new URL(value.toString()); 613 } 614 catch(MalformedURLException exc) 615 { 616 617 } 618 } 619 return null; 620 } 621 622 public void setURL(String parameterName, URL url) throws SQLException 623 { 624 setObject(parameterName, url); 625 } 626 627 public Array getArray(String parameterName) throws SQLException 628 { 629 Object value = getObject(parameterName); 630 if(null != value) 631 { 632 if(value instanceof Array) return (Array)value; 633 return new MockArray(value); 634 } 635 return null; 636 } 637 638 public Blob getBlob(String parameterName) throws SQLException 639 { 640 Object value = getObject(parameterName); 641 if(null != value) 642 { 643 if(value instanceof Blob) return (Blob)value; 644 return new MockBlob(getBytes(parameterName)); 645 } 646 return null; 647 } 648 649 public Clob getClob(String parameterName) throws SQLException 650 { 651 Object value = getObject(parameterName); 652 if(null != value) 653 { 654 if(value instanceof Clob) return (Clob)value; 655 return new MockClob(getString(parameterName)); 656 } 657 return null; 658 } 659 660 public Date getDate(String parameterName) throws SQLException 661 { 662 Object value = getObject(parameterName); 663 if(null != value) 664 { 665 if(value instanceof Date) return (Date)value; 666 return Date.valueOf(value.toString()); 667 } 668 return null; 669 } 670 671 public void setDate(String parameterName, Date date) throws SQLException 672 { 673 setObject(parameterName, date); 674 } 675 676 public Date getDate(int parameterIndex, Calendar calendar) throws SQLException 677 { 678 return getDate(parameterIndex); 679 } 680 681 public Ref getRef(String parameterName) throws SQLException 682 { 683 Object value = getObject(parameterName); 684 if(null != value) 685 { 686 if(value instanceof Ref) return (Ref)value; 687 return new MockRef(value); 688 } 689 return null; 690 } 691 692 public Time getTime(String parameterName) throws SQLException 693 { 694 Object value = getObject(parameterName); 695 if(null != value) 696 { 697 if(value instanceof Time) return (Time)value; 698 return Time.valueOf(value.toString()); 699 } 700 return null; 701 } 702 703 public void setTime(String parameterName, Time time) throws SQLException 704 { 705 setObject(parameterName, time); 706 } 707 708 public Time getTime(int parameterIndex, Calendar calendar) throws SQLException 709 { 710 return getTime(parameterIndex); 711 } 712 713 public Timestamp getTimestamp(String parameterName) throws SQLException 714 { 715 Object value = getObject(parameterName); 716 if(null != value) 717 { 718 if(value instanceof Timestamp) return (Timestamp)value; 719 return Timestamp.valueOf(value.toString()); 720 } 721 return null; 722 } 723 724 public void setTimestamp(String parameterName, Timestamp timestamp) throws SQLException 725 { 726 setObject(parameterName, timestamp); 727 } 728 729 public Timestamp getTimestamp(int parameterIndex, Calendar calendar) throws SQLException 730 { 731 return getTimestamp(parameterIndex); 732 } 733 734 public Object getObject(String parameterName, Map map) throws SQLException 735 { 736 return getObject(parameterName); 737 } 738 739 public Date getDate(String parameterName, Calendar calendar) throws SQLException 740 { 741 return getDate(parameterName); 742 } 743 744 public Time getTime(String parameterName, Calendar calendar) throws SQLException 745 { 746 return getTime(parameterName); 747 } 748 749 public Timestamp getTimestamp(String parameterName, Calendar calendar) throws SQLException 750 { 751 return getTimestamp(parameterName); 752 } 753 754 public void setDate(String parameterName, Date date, Calendar calendar) throws SQLException 755 { 756 setDate(parameterName, date); 757 } 758 759 public void setTime(String parameterName, Time time, Calendar calendar) throws SQLException 760 { 761 setTime(parameterName, time); 762 } 763 764 public void setTimestamp(String parameterName, Timestamp timestamp, Calendar calendar) throws SQLException 765 { 766 setTimestamp(parameterName, timestamp); 767 } 768 769 private Map getOutParameterMap() 770 { 771 Map outParameter = resultSetHandler.getOutParameter(getSQL(), getParameterMap()); 772 if(null == outParameter) 773 { 774 outParameter = resultSetHandler.getOutParameter(getSQL()); 775 } 776 if(null == outParameter) 777 { 778 outParameter = resultSetHandler.getGlobalOutParameter(); 779 } 780 return outParameter; 781 } 782 }