1 21 22 package org.apache.derby.client.am; 23 24 import org.apache.derby.shared.common.reference.SQLState; 25 26 43 final class CrossConverters { 44 45 48 public static final int UNKNOWN_LENGTH = Integer.MIN_VALUE; 49 50 private final static java.math.BigDecimal bdMaxByteValue__ = 51 java.math.BigDecimal.valueOf(Byte.MAX_VALUE); 52 private final static java.math.BigDecimal bdMinByteValue__ = 53 java.math.BigDecimal.valueOf(Byte.MIN_VALUE); 54 private final static java.math.BigDecimal bdMaxShortValue__ = 55 java.math.BigDecimal.valueOf(Short.MAX_VALUE); 56 private final static java.math.BigDecimal bdMinShortValue__ = 57 java.math.BigDecimal.valueOf(Short.MIN_VALUE); 58 private final static java.math.BigDecimal bdMaxIntValue__ = 59 java.math.BigDecimal.valueOf(Integer.MAX_VALUE); 60 private final static java.math.BigDecimal bdMinIntValue__ = 61 java.math.BigDecimal.valueOf(Integer.MIN_VALUE); 62 private final static java.math.BigDecimal bdMaxLongValue__ = 63 java.math.BigDecimal.valueOf(Long.MAX_VALUE); 64 private final static java.math.BigDecimal bdMinLongValue__ = 65 java.math.BigDecimal.valueOf(Long.MIN_VALUE); 66 private final static java.math.BigDecimal bdMaxFloatValue__ = 67 new java.math.BigDecimal (Float.MAX_VALUE); 68 private final static java.math.BigDecimal bdMinFloatValue__ = 69 new java.math.BigDecimal (-Float.MAX_VALUE); 70 private final static java.math.BigDecimal bdMaxDoubleValue__ = 71 new java.math.BigDecimal (Double.MAX_VALUE); 72 private final static java.math.BigDecimal bdMinDoubleValue__ = 73 new java.math.BigDecimal (-Double.MAX_VALUE); 74 75 private final static java.math.BigDecimal bdZero__ = java.math.BigDecimal.valueOf(0); 77 private final static java.math.BigDecimal bdOne__ = java.math.BigDecimal.valueOf(1); 78 79 81 Agent agent_; 82 83 85 CrossConverters(Agent agent) { 86 agent_ = agent; 87 } 88 89 93 95 final Object setObject(int targetType, boolean source) throws SqlException { 99 return setObject(targetType, (short) (source ? 1 : 0)); 100 } 101 102 final Object setObject(int targetType, byte source) throws SqlException { 105 return setObject(targetType, (short) source); 106 } 107 108 final Object setObject(int targetType, short source) throws SqlException { 111 switch (targetType) { 112 case Types.SMALLINT: 113 return new Short (source); 114 115 case Types.INTEGER: 116 return new Integer (source); 117 118 case Types.BIGINT: 119 return new Long (source); 120 121 case Types.REAL: 122 return new Float (source); 123 124 case Types.DOUBLE: 125 return new Double (source); 126 127 case Types.DECIMAL: 128 return java.math.BigDecimal.valueOf(source); 129 130 case Types.CHAR: 131 case Types.VARCHAR: 132 case Types.LONGVARCHAR: 133 return String.valueOf(source); 134 135 default: 136 throw new SqlException(agent_.logWriter_, 137 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH), 138 "byte", Types.getTypeString(targetType)); 139 } 140 } 141 142 final Object setObject(int targetType, int source) throws SqlException { 145 switch (targetType) { 146 case Types.SMALLINT: 147 if (Configuration.rangeCheckCrossConverters && 148 (source > Short.MAX_VALUE || source < Short.MIN_VALUE)) { 149 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 150 } 151 return new Short ((short) source); 152 153 case Types.INTEGER: 154 return new Integer (source); 155 156 case Types.BIGINT: 157 return new Long (source); 158 159 case Types.REAL: 160 return new Float (source); 161 162 case Types.DOUBLE: 163 return new Double (source); 164 165 case Types.DECIMAL: 166 return java.math.BigDecimal.valueOf(source); 167 168 case Types.CHAR: 169 case Types.VARCHAR: 170 case Types.LONGVARCHAR: 171 return String.valueOf(source); 172 173 default: 174 throw new SqlException(agent_.logWriter_, 175 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH), 176 "int", Types.getTypeString(targetType)); 177 } 178 } 179 180 final boolean setBooleanFromObject(Object source, int sourceType) throws SqlException { 183 switch (sourceType) { 184 case Types.SMALLINT: 185 return getBooleanFromShort(((Short ) source).shortValue()); 186 case Types.INTEGER: 187 return getBooleanFromInt(((Integer ) source).intValue()); 188 case Types.BIGINT: 189 return getBooleanFromLong(((java.math.BigInteger ) source).longValue()); 190 case Types.REAL: 191 return getBooleanFromFloat(((Float ) source).floatValue()); 192 case Types.DOUBLE: 193 return getBooleanFromDouble(((Double ) source).doubleValue()); 194 case Types.DECIMAL: 195 return getBooleanFromLong(((java.math.BigDecimal ) source).longValue()); 196 case Types.CHAR: 197 case Types.VARCHAR: 198 case Types.LONGVARCHAR: 199 return getBooleanFromString((String ) source); 200 default: 201 throw new ColumnTypeConversionException(agent_.logWriter_, 202 Types.getTypeString(sourceType), "boolean"); 203 } 204 } 205 206 final byte setByteFromObject(Object source, int sourceType) throws SqlException { 209 switch (sourceType) { 210 case Types.SMALLINT: 211 return getByteFromShort(((Short ) source).shortValue()); 212 case Types.INTEGER: 213 return getByteFromInt(((Integer ) source).intValue()); 214 case Types.BIGINT: 215 return getByteFromLong(((java.math.BigInteger ) source).longValue()); 216 case Types.REAL: 217 return getByteFromFloat(((Float ) source).floatValue()); 218 case Types.DOUBLE: 219 return getByteFromDouble(((Double ) source).doubleValue()); 220 case Types.DECIMAL: 221 return getByteFromLong(((java.math.BigDecimal ) source).longValue()); 222 case Types.CHAR: 223 case Types.VARCHAR: 224 case Types.LONGVARCHAR: 225 return getByteFromString((String ) source); 226 default: 227 throw new ColumnTypeConversionException(agent_.logWriter_, 228 Types.getTypeString(sourceType), "byte"); 229 } 230 } 231 232 final Object setObject(int targetType, long source) throws SqlException { 235 switch (targetType) { 236 case Types.SMALLINT: 237 if (Configuration.rangeCheckCrossConverters && 238 (source > Short.MAX_VALUE || source < Short.MIN_VALUE)) { 239 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 240 } 241 return new Short ((short) source); 242 243 case Types.INTEGER: 244 if (Configuration.rangeCheckCrossConverters && 245 (source > Integer.MAX_VALUE || source < Integer.MIN_VALUE)) { 246 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 247 } 248 return new Integer ((int) source); 249 250 case Types.BIGINT: 251 return new Long (source); 252 253 case Types.REAL: 254 return new Float (source); 255 256 case Types.DOUBLE: 257 return new Double (source); 258 259 case Types.DECIMAL: 260 return java.math.BigDecimal.valueOf(source); 261 262 case Types.CHAR: 263 case Types.VARCHAR: 264 case Types.LONGVARCHAR: 265 return String.valueOf(source); 266 267 default: 268 throw new SqlException(agent_.logWriter_, 269 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH), 270 "long", Types.getTypeString(targetType)); 271 } 272 } 273 274 final Object setObject(int targetType, float source) throws SqlException { 277 switch (targetType) { 278 case Types.SMALLINT: 279 if (Configuration.rangeCheckCrossConverters && 280 (source > Short.MAX_VALUE || source < Short.MIN_VALUE)) { 281 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 282 } 283 return new Short ((short) source); 284 285 case Types.INTEGER: 286 if (Configuration.rangeCheckCrossConverters && 287 (source > Integer.MAX_VALUE || source < Integer.MIN_VALUE)) { 288 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 289 } 290 return new Integer ((int) source); 291 292 case Types.BIGINT: 293 if (Configuration.rangeCheckCrossConverters && 294 (source > Long.MAX_VALUE || source < Long.MIN_VALUE)) { 295 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 296 } 297 return new Long ((long) source); 298 299 case Types.REAL: 300 if (Configuration.rangeCheckCrossConverters && 301 (source == Float.POSITIVE_INFINITY || source == Float.NEGATIVE_INFINITY)) { 312 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 313 } 314 return new Float (source); 315 316 case Types.DOUBLE: 317 if (Configuration.rangeCheckCrossConverters && 318 (source == Double.POSITIVE_INFINITY || source == Double.NEGATIVE_INFINITY)) { 327 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 328 } 329 return new Double (String.valueOf(source)); 331 332 case Types.DECIMAL: 333 return new java.math.BigDecimal (String.valueOf(source)); 337 case Types.CHAR: 338 case Types.VARCHAR: 339 case Types.LONGVARCHAR: 340 return String.valueOf(source); 341 342 default: 343 throw new SqlException(agent_.logWriter_, 344 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH), 345 "float", Types.getTypeString(targetType)); 346 } 347 } 348 349 final Object setObject(int targetType, double source) throws SqlException { 352 switch (targetType) { 353 case Types.SMALLINT: 354 if (Configuration.rangeCheckCrossConverters && 355 (source > Short.MAX_VALUE || source < Short.MIN_VALUE)) { 356 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 357 } 358 return new Short ((short) source); 359 360 case Types.INTEGER: 361 if (Configuration.rangeCheckCrossConverters && 362 (source > Integer.MAX_VALUE || source < Integer.MIN_VALUE)) { 363 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 364 } 365 return new Integer ((int) source); 366 367 case Types.BIGINT: 368 if (Configuration.rangeCheckCrossConverters && 369 (source > Long.MAX_VALUE || source < Long.MIN_VALUE)) { 370 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 371 } 372 return new Long ((long) source); 373 374 case Types.REAL: 375 if (Configuration.rangeCheckCrossConverters && 376 (source > Float.MAX_VALUE || source < -Float.MAX_VALUE)) { 377 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 378 } 379 return new Float ((float) source); 380 381 case Types.DOUBLE: 382 if (Configuration.rangeCheckCrossConverters && 383 (source == Double.POSITIVE_INFINITY || source == Double.NEGATIVE_INFINITY)) { 394 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 395 } 396 return new Double (source); 397 398 case Types.DECIMAL: 399 return new java.math.BigDecimal (String.valueOf(source)); case Types.CHAR: 401 case Types.VARCHAR: 402 case Types.LONGVARCHAR: 403 return String.valueOf(source); 404 405 default: 406 throw new SqlException(agent_.logWriter_, 407 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH), 408 "double", Types.getTypeString(targetType)); 409 } 410 } 411 412 final Object setObject(int targetType, java.math.BigDecimal source) throws SqlException { 415 switch (targetType) { 416 case Types.SMALLINT: 417 if (Configuration.rangeCheckCrossConverters && 418 (source.compareTo(bdMaxShortValue__) == 1 || source.compareTo(bdMinShortValue__) == -1)) { 419 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 420 } 421 return new Short (source.shortValue()); 422 423 case Types.INTEGER: 424 if (Configuration.rangeCheckCrossConverters && 425 (source.compareTo(bdMaxIntValue__) == 1 || source.compareTo(bdMinIntValue__) == -1)) { 426 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 427 } 428 return new Integer (source.intValue()); 429 430 case Types.BIGINT: 431 if (Configuration.rangeCheckCrossConverters && 432 (source.compareTo(bdMaxLongValue__) == 1 || source.compareTo(bdMinLongValue__) == -1)) { 433 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 434 } 435 return new Long (source.longValue()); 436 437 case Types.REAL: 438 if (Configuration.rangeCheckCrossConverters && 439 (source.compareTo(bdMaxFloatValue__) == 1 || source.compareTo(bdMinFloatValue__) == -1)) { 440 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 441 } 442 return new Float (source.floatValue()); 443 444 case Types.DOUBLE: 445 if (Configuration.rangeCheckCrossConverters && 446 (source.compareTo(bdMaxDoubleValue__) == 1 || source.compareTo(bdMinDoubleValue__) == -1)) { 447 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 448 } 449 return new Double (source.doubleValue()); 450 451 case Types.DECIMAL: 452 return source; 453 454 case Types.CHAR: 455 case Types.VARCHAR: 456 case Types.LONGVARCHAR: 457 return String.valueOf(source); 458 459 default: 460 throw new SqlException(agent_.logWriter_, 461 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH), 462 "java.Math.BigDecimal", Types.getTypeString(targetType)); 463 } 464 } 465 466 final Object setObject(int targetType, java.sql.Date source) throws SqlException { 469 switch (targetType) { 470 471 case java.sql.Types.DATE: 472 return source; 473 474 case java.sql.Types.TIMESTAMP: 475 return new java.sql.Timestamp (source.getTime()); 476 477 case java.sql.Types.CHAR: 478 case java.sql.Types.VARCHAR: 479 case java.sql.Types.LONGVARCHAR: 480 return String.valueOf(source); 481 482 default: 483 throw new SqlException(agent_.logWriter_, 484 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH), 485 "java.sql.Date", Types.getTypeString(targetType)); 486 } 487 } 488 489 final Object setObject(int targetType, java.sql.Time source) throws SqlException { 492 switch (targetType) { 493 494 case java.sql.Types.TIME: 495 return source; 496 497 case java.sql.Types.CHAR: 498 case java.sql.Types.VARCHAR: 499 case java.sql.Types.LONGVARCHAR: 500 return String.valueOf(source); 501 502 default: 503 throw new SqlException(agent_.logWriter_, 504 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH), 505 "java.sql.Time", Types.getTypeString(targetType)); 506 } 507 } 508 509 final Object setObject(int targetType, java.sql.Timestamp source) throws SqlException { 512 switch (targetType) { 513 514 case java.sql.Types.TIMESTAMP: 515 return source; 516 517 case java.sql.Types.TIME: 518 return new java.sql.Time (source.getTime()); 519 520 case java.sql.Types.DATE: 521 return new java.sql.Date (source.getTime()); 522 523 case java.sql.Types.CHAR: 524 case java.sql.Types.VARCHAR: 525 case java.sql.Types.LONGVARCHAR: 526 return String.valueOf(source); 527 528 default: 529 throw new SqlException(agent_.logWriter_, 530 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH), 531 "java.sql.Timestamp", Types.getTypeString(targetType)); 532 } 533 } 534 535 final Object setObject(int targetDriverType, String source) throws SqlException { 540 try { 541 switch (targetDriverType) { 542 case Types.SMALLINT: 543 return Short.valueOf(source); 544 545 case Types.INTEGER: 546 return Integer.valueOf(source); 547 548 case Types.BIGINT: 549 return Long.valueOf(source); 550 551 case Types.REAL: 552 return Float.valueOf(source); 553 554 case Types.DOUBLE: 555 return Double.valueOf(source); 556 557 case Types.DECIMAL: 558 return new java.math.BigDecimal (source); 559 560 case java.sql.Types.DATE: 561 return date_valueOf(source); 562 563 case java.sql.Types.TIME: 564 return time_valueOf(source); 565 566 case java.sql.Types.TIMESTAMP: 567 return timestamp_valueOf(source); 568 569 case Types.CHAR: 570 case Types.VARCHAR: 571 case Types.LONGVARCHAR: 572 return source; 573 574 case Types.CLOB: 575 return new Clob(agent_, source); 576 577 case Types.BINARY: 580 case Types.VARBINARY: 581 case Types.LONGVARBINARY: 582 case Types.BLOB: 583 default: 584 throw new SqlException(agent_.logWriter_, 585 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH), 586 "String", Types.getTypeString(targetDriverType)); 587 } 588 } catch (java.lang.NumberFormatException e) { 589 throw new SqlException(agent_.logWriter_, 590 new ClientMessageId 591 (SQLState.LANG_FORMAT_EXCEPTION), 592 Types.getTypeString(targetDriverType), 593 e); 594 } 595 } 596 597 601 public static int getInputJdbcType(int jdbcType) { 602 switch (jdbcType) { 603 case java.sql.Types.BIT: 604 case java.sql.Types.BOOLEAN: 605 case java.sql.Types.TINYINT: 606 case java.sql.Types.SMALLINT: 607 return java.sql.Types.INTEGER; 608 case java.sql.Types.NUMERIC: 609 return java.sql.Types.DECIMAL; 610 case java.sql.Types.FLOAT: 611 return java.sql.Types.DOUBLE; 612 default: 613 return jdbcType; 614 } 615 616 } 617 618 619 621 622 final Object setObject(int targetType, byte[] source) throws SqlException { 625 switch (targetType) { 626 case Types.BINARY: 627 case Types.VARBINARY: 628 case Types.LONGVARBINARY: 629 return source; 630 case Types.BLOB: 631 return new Blob(source, agent_, 0); 632 default: 633 throw new SqlException(agent_.logWriter_, 634 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH), 635 "byte[]", Types.getTypeString(targetType)); 636 } 637 } 638 639 final Object setObject(int targetType, java.io.Reader source, int length) throws SqlException { 642 switch (targetType) { 643 case Types.CHAR: 644 case Types.VARCHAR: 645 case Types.LONGVARCHAR: 646 return setStringFromReader(source, length); 647 case Types.CLOB: 648 if (length == CrossConverters.UNKNOWN_LENGTH) { 649 return new Clob(agent_, source); 650 } 651 return new Clob(agent_, source, length); 652 case Types.BINARY: 655 case Types.VARBINARY: 656 case Types.LONGVARBINARY: 657 case Types.BLOB: 658 default: 659 throw new SqlException(agent_.logWriter_, 660 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH), 661 "java.io.Reader", Types.getTypeString(targetType)); 662 } 663 } 664 665 private final String setStringFromReader(java.io.Reader r, int length) throws SqlException { 667 java.io.StringWriter sw = new java.io.StringWriter (); 668 try { 669 int read = r.read(); 670 int totalRead = 0; 671 while (read != -1) { 672 totalRead++; 673 sw.write(read); 674 read = r.read(); 675 } 676 if (length != CrossConverters.UNKNOWN_LENGTH && 677 length != totalRead) { 678 throw new SqlException(agent_.logWriter_, 679 new ClientMessageId (SQLState.READER_UNDER_RUN)); 680 } 681 return sw.toString(); 682 } catch (java.io.IOException e) { 683 throw SqlException.javaException(agent_.logWriter_, e); 684 } 685 } 686 687 final Object setObjectFromCharacterStream(int targetType, java.io.InputStream source, String encoding, int length) throws SqlException { 691 switch (targetType) { 692 case Types.CHAR: 693 case Types.VARCHAR: 694 case Types.LONGVARCHAR: 695 return setStringFromStream(source, encoding, length); 696 case Types.CLOB: 697 if (length == CrossConverters.UNKNOWN_LENGTH) { 698 return new Clob(agent_, source, encoding); 699 } 700 return new Clob(agent_, source, encoding, length); 701 case Types.BINARY: 702 case Types.VARBINARY: 703 case Types.LONGVARBINARY: 704 case Types.BLOB: 705 default: 706 throw new SqlException(agent_.logWriter_, 707 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH), 708 "java.io.InputStream", Types.getTypeString(targetType)); 709 } 710 } 711 712 713 private final String setStringFromStream(java.io.InputStream is, String encoding, int length) throws SqlException { 715 try { 716 java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream (); 717 int totalRead = 0; 718 719 try { 720 int read = is.read(); 721 while (read != -1) { 722 totalRead++; 723 baos.write(read); 724 read = is.read(); 725 } 726 } catch (java.io.IOException e) { 727 throw new SqlException(agent_.logWriter_, 728 new ClientMessageId(SQLState.JAVA_EXCEPTION), 729 e.getClass().getName(), e.getMessage(), e); 730 } 731 732 if (length != CrossConverters.UNKNOWN_LENGTH && 733 length != totalRead) { 734 throw new SqlException(agent_.logWriter_, 735 new ClientMessageId (SQLState.READER_UNDER_RUN)); 736 } 737 738 return new String (baos.toByteArray(), encoding); 739 } catch (java.io.UnsupportedEncodingException e) { 740 throw new SqlException(agent_.logWriter_, 741 new ClientMessageId (SQLState.UNSUPPORTED_ENCODING), 742 "java.io.InputStream", "String", e); 743 } 744 } 745 746 final Object setObject(int targetType, java.sql.Blob source) throws SqlException { 749 switch (targetType) { 750 case Types.BLOB: 751 return source; 752 case Types.BINARY: 753 case Types.VARBINARY: 754 case Types.LONGVARBINARY: 755 try { 756 return source.getBytes(1L, (int) source.length()); 757 } catch (java.sql.SQLException e) { 758 throw new SqlException(e); 759 } 760 default: 761 throw new SqlException(agent_.logWriter_, 762 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH), 763 "java.sql.Blob", Types.getTypeString(targetType)); 764 } 765 } 766 767 final Object setObjectFromBinaryStream(int targetType, java.io.InputStream source, int length) throws SqlException { 770 switch (targetType) { 771 case Types.BINARY: 772 case Types.VARBINARY: 773 case Types.LONGVARBINARY: 774 return setBytesFromStream(source, length); 775 case Types.BLOB: 776 if (length == CrossConverters.UNKNOWN_LENGTH) { 777 return new Blob(agent_, source); 778 } 779 return new Blob(agent_, source, length); 780 default: 781 throw new SqlException(agent_.logWriter_, 782 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH), 783 "java.io.InputStream", Types.getTypeString(targetType)); 784 } 785 } 786 787 private final byte[] setBytesFromStream(java.io.InputStream is, int length) throws SqlException { 789 java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream (); 790 int totalRead = 0; 791 792 try { 793 int read = is.read(); 794 while (read != -1) { 795 totalRead++; 796 baos.write(read); 797 read = is.read(); 798 } 799 800 if (length != CrossConverters.UNKNOWN_LENGTH && 801 length != totalRead) { 802 throw new SqlException(agent_.logWriter_, 803 new ClientMessageId (SQLState.READER_UNDER_RUN)); 804 } 805 } catch (java.io.IOException e) { 806 throw SqlException.javaException(agent_.logWriter_, e); 807 } 808 return baos.toByteArray(); 809 } 810 811 final Object setObject(int targetType, java.sql.Clob source) throws SqlException { 814 switch (targetType) { 815 case Types.CLOB: 816 return source; 817 case Types.CHAR: 818 case Types.VARCHAR: 819 case Types.LONGVARCHAR: 820 return source.toString(); 821 default: 822 throw new SqlException(agent_.logWriter_, 823 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH), 824 "java.sql.Clob", Types.getTypeString(targetType)); 825 } 826 } 827 828 final Object setObject(int targetType, Object source) throws SqlException { 831 if (source == null) { 832 return null; 833 } else if (source instanceof Boolean ) { 834 return setObject(targetType, ((Boolean ) source).booleanValue()); 835 } else if (source instanceof Integer ) { 836 return setObject(targetType, ((Integer ) source).intValue()); 837 } else if (source instanceof Long ) { 838 return setObject(targetType, ((Long ) source).longValue()); 839 } else if (source instanceof Float ) { 840 return setObject(targetType, ((Float ) source).floatValue()); 841 } else if (source instanceof Double ) { 842 return setObject(targetType, ((Double ) source).doubleValue()); 843 } else if (source instanceof java.math.BigDecimal ) { 844 return setObject(targetType, (java.math.BigDecimal ) source); 845 } else if (source instanceof java.sql.Date ) { 846 return setObject(targetType, (java.sql.Date ) source); 847 } else if (source instanceof java.sql.Time ) { 848 return setObject(targetType, (java.sql.Time ) source); 849 } else if (source instanceof java.sql.Timestamp ) { 850 return setObject(targetType, (java.sql.Timestamp ) source); 851 } else if (source instanceof String ) { 852 return setObject(targetType, (String ) source); 853 } else if (source instanceof byte[]) { 854 return setObject(targetType, (byte[]) source); 855 } else if (source instanceof java.sql.Blob ) { 856 return setObject(targetType, (java.sql.Blob ) source); 857 } else if (source instanceof java.sql.Clob ) { 858 return setObject(targetType, (java.sql.Clob ) source); 859 } else if (source instanceof java.sql.Array ) { 860 return setObject(targetType, (java.sql.Array ) source); 861 } else if (source instanceof java.sql.Ref ) { 862 return setObject(targetType, (java.sql.Ref ) source); 863 } else if (source instanceof Short ) { 864 return setObject(targetType, ((Short ) source).shortValue()); 865 } else if (source instanceof Byte ) { 866 return setObject(targetType, ((Byte ) source).byteValue()); 867 } else { 868 throw new SqlException(agent_.logWriter_, 869 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH), 870 source.getClass().getName(), Types.getTypeString(targetType)); 871 } 872 } 873 874 879 881 final boolean getBooleanFromByte(byte source) throws SqlException { 882 return source != 0; 883 } 884 885 final boolean getBooleanFromShort(short source) throws SqlException { 886 return source != 0; 887 } 888 889 final boolean getBooleanFromInt(int source) throws SqlException { 890 return source != 0; 891 } 892 893 final boolean getBooleanFromLong(long source) throws SqlException { 894 return source != 0; 895 } 896 897 final boolean getBooleanFromFloat(float source) throws SqlException { 898 return source != 0; 899 } 900 901 final boolean getBooleanFromDouble(double source) throws SqlException { 902 return source != 0; 903 } 904 905 final boolean getBooleanFromBigDecimal(java.math.BigDecimal source) throws SqlException { 906 return source.intValue() != 0; 907 } 908 909 final boolean getBooleanFromString(String source) throws SqlException { 911 return !(source.trim().equals("0") || source.trim().equals("false")); 912 } 913 914 916 final byte getByteFromShort(short source) throws SqlException { 917 if (Configuration.rangeCheckCrossConverters && 918 (source > java.lang.Byte.MAX_VALUE || source < java.lang.Byte.MIN_VALUE)) { 919 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 920 } 921 922 return (byte) source; 923 } 924 925 final byte getByteFromInt(int source) throws SqlException { 926 if (Configuration.rangeCheckCrossConverters && 927 (source > java.lang.Byte.MAX_VALUE || source < java.lang.Byte.MIN_VALUE)) { 928 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 929 } 930 931 return (byte) source; 932 } 933 934 final byte getByteFromLong(long source) throws SqlException { 935 if (Configuration.rangeCheckCrossConverters && 936 (source > java.lang.Byte.MAX_VALUE || source < java.lang.Byte.MIN_VALUE)) { 937 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 938 } 939 940 return (byte) source; 941 } 942 943 final byte getByteFromFloat(float source) throws SqlException { 944 if (Configuration.rangeCheckCrossConverters && 945 (source > java.lang.Byte.MAX_VALUE || source < java.lang.Byte.MIN_VALUE)) { 946 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 947 } 948 949 return (byte) source; 950 } 951 952 final byte getByteFromDouble(double source) throws SqlException { 953 if (Configuration.rangeCheckCrossConverters && 954 (source > java.lang.Byte.MAX_VALUE || source < java.lang.Byte.MIN_VALUE)) { 955 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 956 } 957 958 return (byte) source; 959 } 960 961 final byte getByteFromBigDecimal(java.math.BigDecimal source) throws SqlException { 962 if (Configuration.rangeCheckCrossConverters && 963 (source.compareTo(bdMaxByteValue__) == 1 || source.compareTo(bdMinByteValue__) == -1)) { 964 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 965 } 966 return (byte) source.intValue(); 967 } 968 969 final byte getByteFromBoolean(boolean source) throws SqlException { 970 return source ? (byte) 1 : (byte) 0; 971 } 972 973 final byte getByteFromString(String source) throws SqlException { 974 try { 975 return parseByte(source); 976 } catch (java.lang.NumberFormatException e) { 977 throw new SqlException(agent_.logWriter_, 978 new ClientMessageId 979 (SQLState.LANG_FORMAT_EXCEPTION), "byte", e); 980 } 981 } 982 983 985 final short getShortFromInt(int source) throws SqlException { 986 if (Configuration.rangeCheckCrossConverters && 987 (source > java.lang.Short.MAX_VALUE || source < java.lang.Short.MIN_VALUE)) { 988 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 989 } 990 991 return (short) source; 992 } 993 994 final short getShortFromLong(long source) throws SqlException { 995 if (Configuration.rangeCheckCrossConverters && 996 (source > java.lang.Short.MAX_VALUE || source < java.lang.Short.MIN_VALUE)) { 997 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 998 } 999 1000 return (short) source; 1001 } 1002 1003 final short getShortFromFloat(float source) throws SqlException { 1004 if (Configuration.rangeCheckCrossConverters && 1005 (source > java.lang.Short.MAX_VALUE || source < java.lang.Short.MIN_VALUE)) { 1006 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 1007 } 1008 1009 return (short) source; 1010 } 1011 1012 final short getShortFromDouble(double source) throws SqlException { 1013 if (Configuration.rangeCheckCrossConverters && 1014 (source > java.lang.Short.MAX_VALUE || source < java.lang.Short.MIN_VALUE)) { 1015 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 1016 } 1017 1018 return (short) source; 1019 } 1020 1021 final short getShortFromBigDecimal(java.math.BigDecimal source) throws SqlException { 1022 if (Configuration.rangeCheckCrossConverters && 1023 (source.compareTo(bdMaxShortValue__) == 1 || source.compareTo(bdMinShortValue__) == -1)) { 1024 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 1025 } 1026 return (short) source.intValue(); 1027 } 1028 1029 final short getShortFromBoolean(boolean source) throws SqlException { 1030 return source ? (short) 1 : (short) 0; 1031 } 1032 1033 final short getShortFromString(String source) throws SqlException { 1034 try { 1035 return parseShort(source); 1036 } catch (java.lang.NumberFormatException e) { 1037 throw new SqlException(agent_.logWriter_, 1038 new ClientMessageId 1039 (SQLState.LANG_FORMAT_EXCEPTION), 1040 "short", e); 1041 } 1042 } 1043 1044 1046 final int getIntFromLong(long source) throws SqlException { 1047 if (Configuration.rangeCheckCrossConverters && 1048 (source > java.lang.Integer.MAX_VALUE || source < java.lang.Integer.MIN_VALUE)) { 1049 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 1050 } 1051 1052 return (int) source; 1053 } 1054 1055 final int getIntFromFloat(float source) throws SqlException { 1056 if (Configuration.rangeCheckCrossConverters && 1057 (source > java.lang.Integer.MAX_VALUE || source < java.lang.Integer.MIN_VALUE)) { 1058 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 1059 } 1060 1061 return (int) source; 1062 } 1063 1064 final int getIntFromDouble(double source) throws SqlException { 1065 if (Configuration.rangeCheckCrossConverters && 1066 (source > java.lang.Integer.MAX_VALUE || source < java.lang.Integer.MIN_VALUE)) { 1067 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 1068 } 1069 1070 return (int) source; 1071 } 1072 1073 final int getIntFromBigDecimal(java.math.BigDecimal source) throws SqlException { 1074 if (Configuration.rangeCheckCrossConverters && 1075 (source.compareTo(bdMaxIntValue__) == 1 || source.compareTo(bdMinIntValue__) == -1)) { 1076 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 1077 } 1078 return source.intValue(); 1079 } 1080 1081 final int getIntFromBoolean(boolean source) throws SqlException { 1082 return source ? (int) 1 : (int) 0; 1083 } 1084 1085 final int getIntFromString(String source) throws SqlException { 1086 try { 1087 return parseInt(source); 1088 } catch (java.lang.NumberFormatException e) { 1089 throw new SqlException(agent_.logWriter_, 1090 new ClientMessageId (SQLState.LANG_FORMAT_EXCEPTION), 1091 "int", e); 1092 } 1093 } 1094 1095 1097 final long getLongFromFloat(float source) throws SqlException { 1098 if (Configuration.rangeCheckCrossConverters && 1099 (source > java.lang.Long.MAX_VALUE || source < java.lang.Long.MIN_VALUE)) { 1100 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 1101 } 1102 1103 return (long) source; 1104 } 1105 1106 final long getLongFromDouble(double source) throws SqlException { 1107 if (Configuration.rangeCheckCrossConverters && 1108 (source > java.lang.Long.MAX_VALUE || source < java.lang.Long.MIN_VALUE)) { 1109 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 1110 } 1111 1112 return (long) source; 1113 } 1114 1115 final long getLongFromBigDecimal(java.math.BigDecimal source) throws SqlException { 1116 if (Configuration.rangeCheckCrossConverters && 1117 (source.compareTo(bdMaxLongValue__) == 1 || source.compareTo(bdMinLongValue__) == -1)) { 1118 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 1119 } 1120 return source.longValue(); 1121 } 1122 1123 final long getLongFromBoolean(boolean source) throws SqlException { 1124 return source ? (long) 1 : (long) 0; 1125 } 1126 1127 final long getLongFromString(String source) throws SqlException { 1128 try { 1129 return parseLong(source); 1130 } catch (java.lang.NumberFormatException e) { 1131 throw new SqlException(agent_.logWriter_, 1132 new ClientMessageId (SQLState.LANG_FORMAT_EXCEPTION), 1133 "long", e); 1134 } 1135 } 1136 1137 1139 final float getFloatFromDouble(double source) throws SqlException { 1140 if (Configuration.rangeCheckCrossConverters && 1141 Float.isInfinite((float)source)) { 1142 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 1143 } 1144 1145 return (float) source; 1146 } 1147 1148 final float getFloatFromBigDecimal(java.math.BigDecimal source) throws SqlException { 1149 if (Configuration.rangeCheckCrossConverters && 1150 (source.compareTo(bdMaxFloatValue__) == 1 || source.compareTo(bdMinFloatValue__) == -1)) { 1151 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 1152 } 1153 return source.floatValue(); 1154 } 1155 1156 final float getFloatFromBoolean(boolean source) throws SqlException { 1157 return source ? (float) 1 : (float) 0; 1158 } 1159 1160 final float getFloatFromString(String source) throws SqlException { 1161 try { 1162 return Float.parseFloat(source.trim()); 1163 } catch (java.lang.NumberFormatException e) { 1164 throw new SqlException(agent_.logWriter_, 1165 new ClientMessageId (SQLState.LANG_FORMAT_EXCEPTION), 1166 "float", e); 1167 } 1168 } 1169 1170 1172 final double getDoubleFromBigDecimal(java.math.BigDecimal source) throws SqlException { 1173 if (Configuration.rangeCheckCrossConverters && 1174 (source.compareTo(bdMaxDoubleValue__) == 1 || source.compareTo(bdMinDoubleValue__) == -1)) { 1175 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); 1176 } 1177 return source.doubleValue(); 1178 } 1179 1180 final double getDoubleFromBoolean(boolean source) throws SqlException { 1181 return source ? (double) 1 : (double) 0; 1182 } 1183 1184 final double getDoubleFromString(String source) throws SqlException { 1185 try { 1186 return Double.parseDouble(source.trim()); 1187 } catch (java.lang.NumberFormatException e) { 1188 throw new SqlException(agent_.logWriter_, 1189 new ClientMessageId (SQLState.LANG_FORMAT_EXCEPTION), 1190 "double", e); 1191 } 1192 } 1193 1194 1196 final java.math.BigDecimal getBigDecimalFromBoolean(boolean source) throws SqlException { 1197 return source ? bdOne__ : bdZero__; 1198 } 1199 1200 final java.math.BigDecimal getBigDecimalFromString(String source) throws SqlException { 1201 try { 1202 return new java.math.BigDecimal (source.trim()); 1205 } catch (java.lang.NumberFormatException e) { 1206 throw new SqlException(agent_.logWriter_, 1207 new ClientMessageId (SQLState.LANG_FORMAT_EXCEPTION), 1208 "java.math.BigDecimal", e); 1209 } 1210 } 1211 1212 1214 final String getStringFromBoolean(boolean source) throws SqlException { 1215 return source ? "1" : "0"; 1216 } 1217 1218 final String getStringFromBytes(byte[] bytes) throws SqlException { 1219 StringBuffer stringBuffer = new StringBuffer (bytes.length * 2); 1220 for (int i = 0; i < bytes.length; i++) { 1221 String hexForByte = Integer.toHexString(bytes[i] & 0xff); 1222 if (hexForByte.length() == 1) { 1224 stringBuffer.append('0'); 1225 } 1226 stringBuffer.append(hexForByte); 1227 } 1228 return stringBuffer.toString(); 1229 } 1230 1231 1232 1234 1236 final java.sql.Date getDateFromString(String source) throws SqlException { 1237 try { 1238 return date_valueOf(source); 1239 } catch (java.lang.IllegalArgumentException e) { throw new SqlException(agent_.logWriter_, 1241 new ClientMessageId (SQLState.LANG_DATE_SYNTAX_EXCEPTION), e); 1242 } 1243 } 1244 1245 final java.sql.Date getDateFromTime(java.sql.Time source) throws SqlException { 1246 return new java.sql.Date (source.getTime()); 1247 } 1248 1249 final java.sql.Date getDateFromTimestamp(java.sql.Timestamp source) throws SqlException { 1250 return new java.sql.Date (source.getTime()); 1251 } 1252 1253 1255 final java.sql.Time getTimeFromString(String source) throws SqlException { 1256 try { 1257 return time_valueOf(source); 1258 } catch (java.lang.IllegalArgumentException e) { throw new SqlException(agent_.logWriter_, 1260 new ClientMessageId (SQLState.LANG_DATE_SYNTAX_EXCEPTION), e); 1261 } 1262 } 1263 1264 final java.sql.Time getTimeFromTimestamp(java.sql.Timestamp source) throws SqlException { 1265 return new java.sql.Time (source.getTime()); 1266 } 1267 1268 1270 final java.sql.Timestamp getTimestampFromString(String source) throws SqlException { 1271 try { 1272 return timestamp_valueOf(source); 1273 } catch (java.lang.IllegalArgumentException e) { throw new SqlException(agent_.logWriter_, 1275 new ClientMessageId (SQLState.LANG_DATE_SYNTAX_EXCEPTION), e); 1276 } 1277 } 1278 1279 final java.sql.Timestamp getTimestampFromTime(java.sql.Time source) throws SqlException { 1280 return new java.sql.Timestamp (source.getTime()); 1281 } 1282 1283 final java.sql.Timestamp getTimestampFromDate(java.sql.Date source) throws SqlException { 1284 return new java.sql.Timestamp (source.getTime()); 1285 } 1286 1287 final java.sql.Date date_valueOf(String s) throws java.lang.IllegalArgumentException { 1288 String formatError = "JDBC Date format must be yyyy-mm-dd"; 1289 if (s == null) { 1290 throw new java.lang.IllegalArgumentException (formatError); 1291 } 1292 s = s.trim(); 1293 return java.sql.Date.valueOf(s); 1294 } 1295 1296 1297 final java.sql.Time time_valueOf(String s) throws java.lang.IllegalArgumentException , NumberFormatException { 1298 String formatError = "JDBC Time format must be hh:mm:ss"; 1299 if (s == null) { 1300 throw new java.lang.IllegalArgumentException (); 1301 } 1302 s = s.trim(); 1303 return java.sql.Time.valueOf(s); 1304 } 1305 1306 final java.sql.Timestamp timestamp_valueOf(String s) throws java.lang.IllegalArgumentException , NumberFormatException { 1307 String formatError = "JDBC Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff"; 1308 if (s == null) { 1309 throw new java.lang.IllegalArgumentException (); 1310 } 1311 1312 s = s.trim(); 1313 return java.sql.Timestamp.valueOf(s); 1314 } 1315 1316 private final byte parseByte(String s) throws NumberFormatException { 1317 int i = parseInt(s); 1318 if (i < Byte.MIN_VALUE || i > Byte.MAX_VALUE) { 1319 throw new NumberFormatException (); 1320 } 1321 return (byte) i; 1322 } 1323 1324 private final short parseShort(String s) throws NumberFormatException { 1325 int i = parseInt(s); 1326 if (i < Short.MIN_VALUE || i > Short.MAX_VALUE) { 1327 throw new NumberFormatException (); 1328 } 1329 return (short) i; 1330 } 1331 1332 private final int parseInt(String s) throws NumberFormatException { 1334 if (s == null) { 1335 throw new NumberFormatException ("null"); 1336 } 1337 1338 int result = 0; 1339 boolean negative = false; 1340 int i = 0; 1341 int max = s.length(); 1342 int limit; 1343 int multmin; 1344 int digit; 1345 1346 if (max == 0) { 1347 throw new NumberFormatException (s); 1348 } 1349 1350 if (s.charAt(0) == '-') { 1351 negative = true; 1352 limit = Integer.MIN_VALUE; 1353 i++; 1354 } else { 1355 limit = -Integer.MAX_VALUE; 1356 } 1357 multmin = limit / 10; 1358 if (i < max) { 1360 digit = Character.digit(s.charAt(i++), 10); 1361 if (digit < 0) { 1362 throw new NumberFormatException (s); 1363 } else { 1364 result = -digit; 1365 } 1366 } 1367 while (i < max) { 1369 char c = s.charAt(i++); 1370 if (c == ' ') { 1371 skipPadding(s, i, max); 1372 break; 1373 } 1374 digit = Character.digit(c, 10); 1376 if (digit < 0) { 1377 throw new NumberFormatException (s); 1378 } 1379 if (result < multmin) { 1380 throw new NumberFormatException (s); 1381 } 1382 result *= 10; 1383 if (result < limit + digit) { 1384 throw new NumberFormatException (s); 1385 } 1386 result -= digit; 1387 } 1388 if (negative) { 1389 if (i > 1) { 1390 return result; 1391 } else { throw new NumberFormatException (s); 1393 } 1394 } else { 1395 return -result; 1396 } 1397 } 1398 1399 private final long parseLong(String s) throws NumberFormatException { 1400 if (s == null) { 1401 throw new NumberFormatException ("null"); 1402 } 1403 1404 long result = 0; 1405 boolean negative = false; 1406 int i = 0, max = s.length(); 1407 long limit; 1408 long multmin; 1409 int digit; 1410 1411 if (max == 0) { 1412 throw new NumberFormatException (s); 1413 } 1414 1415 if (s.charAt(0) == '-') { 1416 negative = true; 1417 limit = Long.MIN_VALUE; 1418 i++; 1419 } else { 1420 limit = -Long.MAX_VALUE; 1421 } 1422 multmin = limit / 10; 1423 if (i < max) { 1424 digit = Character.digit(s.charAt(i++), 10); 1425 if (digit < 0) { 1426 throw new NumberFormatException (s); 1427 } else { 1428 result = -digit; 1429 } 1430 } 1431 while (i < max) { 1432 char c = s.charAt(i++); 1433 if (c == ' ') { 1434 skipPadding(s, i, max); 1435 break; 1436 } 1437 digit = Character.digit(c, 10); 1439 if (digit < 0) { 1440 throw new NumberFormatException (s); 1441 } 1442 if (result < multmin) { 1443 throw new NumberFormatException (s); 1444 } 1445 result *= 10; 1446 if (result < limit + digit) { 1447 throw new NumberFormatException (s); 1448 } 1449 result -= digit; 1450 } 1451 if (negative) { 1452 if (i > 1) { 1453 return result; 1454 } else { throw new NumberFormatException (s); 1456 } 1457 } else { 1458 return -result; 1459 } 1460 } 1461 1462 private final void skipPadding(String s, int i, int length) throws NumberFormatException { 1463 while (i < length) { 1464 if (s.charAt(i++) != ' ') { 1465 throw new NumberFormatException (s); 1466 } 1467 } 1468 } 1469} 1470 | Popular Tags |