1 5 package com.teamkonzept.db; 6 7 import java.io.*; 8 import java.math.*; 9 import java.sql.*; 10 import java.text.*; 11 import java.util.Enumeration ; 12 import java.util.Hashtable ; 13 14 public class TKSQLTypeConverter 15 { 16 public static PrintStream LOG = System.out; 17 18 public static final int HASHTABLE_SIZE = 19; 19 20 public static int HASH_CODE = TKSQLTypeConverter.class.getName().hashCode(); 21 22 final static Object [][] TYPE_NAMES_TABLE = { 23 { new Integer ( Types.BIGINT ), "BIGINT" }, 24 { new Integer ( Types.BINARY ), "BINARY" }, 25 { new Integer ( Types.BIT ), "BIT" }, 26 { new Integer ( Types.CHAR ), "CHAR" }, 27 { new Integer ( Types.DATE ), "DATE" }, 28 { new Integer ( Types.DECIMAL ), "DECIMAL" }, 29 { new Integer ( Types.DOUBLE ), "DOUBLE" }, 30 { new Integer ( Types.FLOAT ), "FLOAT" }, 31 { new Integer ( Types.INTEGER ), "INTEGER" }, 32 { new Integer ( Types.LONGVARBINARY ), "LONGVARBINARY" }, 33 { new Integer ( Types.LONGVARCHAR ), "LONGVARCHAR" }, 34 { new Integer ( Types.NUMERIC ), "NUMERIC" }, 35 { new Integer ( Types.REAL ), "REAL" }, 36 { new Integer ( Types.SMALLINT ), "SMALLINT" }, 37 { new Integer ( Types.TIME ), "TIME" }, 38 { new Integer ( Types.TIMESTAMP ), "TIMESTAMP" }, 39 { new Integer ( Types.TINYINT ), "TINYINT" }, 40 { new Integer ( Types.VARBINARY ), "VARBINARY" }, 41 { new Integer ( Types.VARCHAR ), "VARCHAR" } 42 }; 43 final static Hashtable TYPE_NAMES = initHash(); 44 45 static class TypeInfo { 46 47 int DATA_TYPE; 48 49 String LITERAL_PREFIX; 50 String LITERAL_SUFFIX; 51 int PRECISION; 52 short MINIMUM_SCALE; 53 short MAXIMUM_SCALE; 54 boolean UNSIGNED_ATTRIBUTE; 55 int NUM_PREC_RADIX; 56 57 static int counter = 0; 58 59 TypeInfo( final ResultSet rs ) throws SQLException { 60 DATA_TYPE = rs.getInt( "DATA_TYPE" ); 61 LITERAL_PREFIX = rs.getString( "LITERAL_PREFIX" ); 62 LITERAL_SUFFIX = rs.getString( "LITERAL_SUFFIX" ); 63 PRECISION = (int) rs.getLong( "PRECISION" ); 64 MINIMUM_SCALE = rs.getShort( "MINIMUM_SCALE" ); 65 MAXIMUM_SCALE = rs.getShort( "MAXIMUM_SCALE" ); 66 UNSIGNED_ATTRIBUTE = rs.getBoolean( "UNSIGNED_ATTRIBUTE" ); 67 NUM_PREC_RADIX = rs.getInt( "NUM_PREC_RADIX" ); 68 } 69 TypeInfo( final int DATA_TYPE, 70 final String LITERAL_PREFIX, final String LITERAL_SUFFIX, 71 final int PRECISION, 72 final int MINIMUM_SCALE, final int MAXIMUM_SCALE, 73 final boolean UNSIGNED_ATTRIBUTE, 74 final int NUM_PREC_RADIX ) 75 { 76 this.DATA_TYPE = DATA_TYPE; 77 this.LITERAL_PREFIX = LITERAL_PREFIX; 78 this.LITERAL_SUFFIX = LITERAL_SUFFIX; 79 this.PRECISION = PRECISION; 80 this.MINIMUM_SCALE = (new Integer ( MINIMUM_SCALE )).shortValue(); 81 this.MAXIMUM_SCALE = (new Integer ( MAXIMUM_SCALE )).shortValue(); 82 this.UNSIGNED_ATTRIBUTE = UNSIGNED_ATTRIBUTE; 83 this.NUM_PREC_RADIX = NUM_PREC_RADIX; 84 85 } 86 public boolean equals( final Object obj ) { 87 if (obj != null && getClass() == obj.getClass()) 88 { 89 return DATA_TYPE == ( (TypeInfo) obj ).DATA_TYPE; 90 } 91 return false; 92 } 93 94 public int hashCode() 95 { 96 return DATA_TYPE + HASH_CODE; 97 } 98 99 public String toString() { 100 final String type_name = (String )TKSQLTypeConverter.TYPE_NAMES.get( new Integer ( DATA_TYPE ) ); 101 return "TypeInfo( " + DATA_TYPE + ( type_name != null ? " /* " + type_name + " */ " : "" ) + ",\t" 102 + ( LITERAL_PREFIX != null ? "\"" + LITERAL_PREFIX + "\"" : "null" ) + ",\t" 103 + ( LITERAL_SUFFIX != null ? "\"" + LITERAL_SUFFIX + "\"" : "null" ) + ",\t" 104 + PRECISION + ",\t" + MINIMUM_SCALE + ",\t" + MAXIMUM_SCALE + ",\t" 105 + UNSIGNED_ATTRIBUTE + ",\t" + NUM_PREC_RADIX + " )"; 106 } 107 } 108 109 protected final DatabaseMetaData meta_data; 110 protected final Hashtable db_types = new Hashtable ( HASHTABLE_SIZE ); 111 112 public TKSQLTypeConverter( final Connection conn ) throws SQLException 113 { 114 this.meta_data = conn.getMetaData(); 115 116 120 final ResultSet type_rs = meta_data.getTypeInfo(); 121 while ( type_rs.next() ) { 122 123 final TypeInfo db_type = new TypeInfo( type_rs ); 124 final Integer data_type = new Integer ( db_type.DATA_TYPE ); 125 if ( ! db_types.containsKey( data_type ) ) 126 db_types.put( data_type, db_type ); 127 } 128 type_rs.close(); 129 } 130 protected TKSQLTypeConverter( final TypeInfo[] meta_data ) 131 { 132 for ( int i = 0; i < meta_data.length; i++ ) { 133 final Integer data_type = new Integer ( meta_data[i].DATA_TYPE ); 134 if ( ! db_types.containsKey( data_type ) ) 135 db_types.put( data_type, meta_data[i] ); 136 } 137 this.meta_data = null; 138 } 139 144 private static Hashtable initHash() 145 { 146 final Hashtable table = new Hashtable ( TYPE_NAMES_TABLE.length ); 147 148 for ( int i = 0; i < TYPE_NAMES_TABLE.length; i++ ) { 149 table.put( TYPE_NAMES_TABLE[i][0], TYPE_NAMES_TABLE[i][1] ); 150 } 151 152 return table; 153 } 154 155 162 public boolean supportsConvert( final int from_type, final int to_type ) 163 throws SQLException 164 { 165 return meta_data.supportsConvert( from_type, to_type ); 166 } 167 175 protected static String escapeQuotes( final String str, 176 final TypeInfo info ) 177 { 178 179 if ( info.LITERAL_PREFIX != null ) { 180 final String buf = doubleQuotes( str, info.LITERAL_PREFIX ); 181 182 if ( info.LITERAL_SUFFIX != null && 183 ! info.LITERAL_PREFIX.equals( info.LITERAL_SUFFIX ) ) { 184 return doubleQuotes( buf, info.LITERAL_SUFFIX ); 185 } else { 186 return buf; 187 } 188 } else if ( info.LITERAL_SUFFIX != null ) { 189 return doubleQuotes( str, info.LITERAL_SUFFIX ); 190 } else { 191 return str; 192 } 193 } 194 201 protected static String doubleQuotes( final String literal, 202 final String quote ) 203 { 204 final StringBuffer buf = new StringBuffer (); 205 206 int start = 0; 207 int end; 208 while ( ( end = literal.indexOf( quote, start ) ) > 0 ) { 209 buf.append( literal.substring( start, end ) + quote + quote ); 210 start = end + 1; 211 } 212 buf.append( literal.substring( start, literal.length() ) ); 213 214 return buf.toString(); 215 } 216 223 protected static String createStringLiteral( final TypeInfo info, final String str ) 224 { 225 final StringBuffer result = new StringBuffer (); 226 227 if ( info.LITERAL_PREFIX != null ) 228 result.append( info.LITERAL_PREFIX ); 229 230 result.append( escapeQuotes( str, info ) ); 231 232 if ( info.LITERAL_SUFFIX != null ) 233 result.append( info.LITERAL_SUFFIX ); 234 235 return result.toString(); 236 } 237 238 248 protected static NumberFormat getNumberFormat( final TypeInfo info ) 249 { 250 final DecimalFormatSymbols symbols = new DecimalFormatSymbols(); 252 symbols.setMinusSign( '-' ); 253 symbols.setDecimalSeparator( '.' ); 254 symbols.setZeroDigit( '0' ); 255 256 final StringBuffer pattern = new StringBuffer (); 258 if ( info.LITERAL_PREFIX != null ) 259 pattern.append( info.LITERAL_PREFIX ); 260 pattern.append( "#.#" ); 261 if ( info.LITERAL_SUFFIX != null ) 262 pattern.append( info.LITERAL_SUFFIX ); 263 264 final DecimalFormat format = new DecimalFormat( pattern.toString(), symbols ); 266 format.setMinimumFractionDigits( info.MINIMUM_SCALE ); 267 format.setMaximumFractionDigits( info.MAXIMUM_SCALE ); 268 format.setMinimumIntegerDigits( 0 ); 269 format.setMaximumIntegerDigits( info.PRECISION - info.MINIMUM_SCALE ); 270 format.setGroupingUsed( false ); 271 272 273 return format; 274 } 275 295 protected String standardNumberConversion( final int[] from_types, 296 final int to_type, 297 final Number num ) 298 throws SQLException, 299 TKIllegalConversionException 300 { 301 for ( int i = 0; i < from_types.length; i++ ) { 302 if ( supportsConvert( from_types[i], to_type ) ) { 303 final TypeInfo info = (TypeInfo) db_types.get( new Integer ( from_types[i] ) ); 304 final String type_name = (String ) TYPE_NAMES.get( new Integer ( to_type ) ); 305 final String literal = createNumberLiteral( info, num );; 306 307 return "{fn CONVERT(" + literal + ", " + type_name + ") }"; 308 } 309 } 310 311 final TypeInfo info = (TypeInfo) db_types.get( new Integer ( from_types[0] ) ); 312 313 switch ( to_type ) { 314 315 case Types.CHAR: 316 case Types.VARCHAR: 317 case Types.LONGVARCHAR: 318 return createStringLiteral( info, createNumberLiteral( info, num ) ); 319 320 case Types.BIT: 321 final boolean state; 322 323 if ( num instanceof BigDecimal || 324 num instanceof Float || 325 num instanceof Double ) 326 state = num.doubleValue() != 0 ? true : false; 327 else 328 state = num.longValue() != 0 ? true : false; 329 330 return createBitLiteral( info, state ); 331 332 default: 333 throw new TKIllegalConversionException( num.getClass().getName(), 334 "java.sql." + TYPE_NAMES.get( new Integer ( to_type ) ) ); 335 } 336 } 337 345 protected static String createNumberLiteral( final TypeInfo info, final Number num ) 346 { 347 switch ( info.DATA_TYPE ) { 348 349 case Types.NUMERIC: 350 case Types.DECIMAL: 351 if ( num instanceof BigDecimal ) { 352 BigDecimal dec = (BigDecimal) num; 353 final int scale = dec.scale(); 354 355 if ( scale < info.MINIMUM_SCALE ) 356 dec = dec.setScale( info.MINIMUM_SCALE ); 357 else if ( scale > info.MAXIMUM_SCALE ) 358 dec = dec.setScale( info.MAXIMUM_SCALE, BigDecimal.ROUND_HALF_DOWN ); 359 360 final StringBuffer literal = new StringBuffer (); 361 if ( info.LITERAL_PREFIX != null ) 362 literal.append( info.LITERAL_PREFIX ); 363 literal.append( dec.toString() ); 364 if ( info.LITERAL_SUFFIX != null ) 365 literal.append( info.LITERAL_SUFFIX ); 366 return literal.toString(); 367 } 368 369 case Types.FLOAT: 370 case Types.DOUBLE: 371 case Types.REAL: 372 return getNumberFormat( info ).format( num.doubleValue() ); 373 374 default: 375 return getNumberFormat( info ).format( num.longValue() ); 376 } 377 } 378 386 protected static String createBitLiteral( final TypeInfo info, final boolean state ) 387 { 388 final StringBuffer literal = new StringBuffer (); 389 390 if ( info.LITERAL_PREFIX != null ) 391 literal.append( info.LITERAL_PREFIX ); 392 393 literal.append( state ? "1" : "0" ); 394 395 if ( info.LITERAL_SUFFIX != null ) 396 literal.append( info.LITERAL_SUFFIX ); 397 398 return literal.toString(); 399 } 400 408 protected static String createBytestringLiteral( final TypeInfo info, final Byte [] data ) 409 { 410 final StringBuffer literal = new StringBuffer (); 411 412 if ( info.LITERAL_PREFIX != null ) 413 literal.append( info.LITERAL_PREFIX ); 414 415 416 for ( int i = 0; i < data.length; i++ ) 417 literal.append( Integer.toHexString( data[i].intValue() ) ); 418 419 if ( info.LITERAL_SUFFIX != null ) 420 literal.append( info.LITERAL_SUFFIX ); 421 422 return literal.toString(); 423 } 424 430 protected static String createDateLiteral( final java.sql.Date date ) 431 { 432 return "{d '" + date.toString() + "'}"; 433 } 434 440 protected static String createTimeLiteral( final Time time ) 441 { 442 return "{t '" + time.toString() + "'}"; 443 } 444 450 protected static String createTimestampLiteral( final Timestamp timestamp ) 451 { 452 return "{ts '" + timestamp.toString() + "'}"; 453 } 454 467 public String convert( final int type, final Object obj ) 468 throws SQLException, 469 TKIllegalConversionException 470 { 471 if ( obj instanceof String ) 472 return convertString( type, (String )obj ); 473 474 if ( obj instanceof BigDecimal ) 475 return convertBigDecimal( type, (BigDecimal)obj ); 476 477 if ( obj instanceof Boolean ) 478 return convertBoolean( type, (Boolean )obj ); 479 480 if ( obj instanceof Byte ) 481 return convertByte( type, (Byte )obj ); 482 483 if ( obj instanceof Short ) 484 return convertShort( type, (Short )obj ); 485 486 if ( obj instanceof Integer ) 487 return convertInteger( type, (Integer )obj ); 488 489 if ( obj instanceof Long ) 490 return convertLong( type, (Long )obj ); 491 492 if ( obj instanceof Float ) 493 return convertFloat( type, (Float )obj ); 494 495 if ( obj instanceof Double ) 496 return convertDouble( type, (Double )obj ); 497 498 if ( obj instanceof Byte [] ) 499 return convertByteArray( type, (Byte [])obj ); 500 501 if ( obj instanceof java.sql.Date ) 502 return convertDate( type, (java.sql.Date )obj ); 503 504 if ( obj instanceof Time ) 505 return convertTime( type, (Time)obj ); 506 507 if ( obj instanceof Timestamp ) 508 return convertTimestamp( type, (Timestamp)obj ); 509 510 throw new TKIllegalConversionException( obj.getClass().getName(), 511 "java.sql." + TYPE_NAMES.get( new Integer ( type ) ) ); 512 } 513 530 protected String convertNumber( final int[] from_types, 531 final int to_type, 532 final Number num ) 533 throws SQLException, 534 TKIllegalConversionException 535 { 536 final TypeInfo type_info = (TypeInfo) db_types.get( new Integer ( to_type ) ); 537 if ( type_info == null ) 538 throw new TKIllegalConversionException( 539 num.getClass().getName(), 540 "JDBC-Type java.sql.Types." + TYPE_NAMES.get( new Integer ( to_type ) ) 541 ); 542 543 switch ( to_type ) { 544 545 case Types.NUMERIC: 546 case Types.DECIMAL: 547 case Types.DOUBLE: 548 case Types.FLOAT: 549 case Types.REAL: 550 case Types.INTEGER: 551 case Types.BIGINT: 552 case Types.SMALLINT: 553 case Types.TINYINT: 554 return createNumberLiteral( type_info, num ); 555 556 default: 557 562 return standardNumberConversion( from_types, to_type, num ); 563 } 564 } 565 577 public String convertString( final int type, final String str ) 578 throws SQLException, 579 TKIllegalConversionException 580 { 581 final TypeInfo type_info = (TypeInfo) db_types.get( new Integer ( type ) ); 582 if ( type_info == null ) 583 throw new TKIllegalConversionException( 584 "java.lang.String", 585 "JDBC-Type java.sql.Types." + TYPE_NAMES.get( new Integer ( type ) ) 586 ); 587 588 switch ( type ) { 589 590 case Types.CHAR: 591 case Types.VARCHAR: 592 case Types.LONGVARCHAR: 593 return createStringLiteral( type_info , str ); 594 595 default: 596 597 601 final int[] from_types = { Types.CHAR, 602 Types.VARCHAR, 603 Types.LONGVARCHAR 604 }; 605 for ( int i = 0; i < from_types.length; i++ ) { 606 if ( supportsConvert( from_types[i], type ) ) { 607 final TypeInfo info = (TypeInfo) db_types.get( new Integer ( from_types[i] ) ); 608 final String literal = createStringLiteral( info, str ); 609 final String type_name = (String ) TYPE_NAMES.get( new Integer ( type ) ); 610 611 return "{fn CONVERT( " + literal + ", " + type_name + ") }"; 612 } 613 } 614 615 return str; 616 } 617 } 618 630 public String convertBoolean( final int type, final Boolean state ) 631 throws SQLException, 632 TKIllegalConversionException 633 { 634 final TypeInfo type_info = (TypeInfo) db_types.get( new Integer ( type ) ); 635 if ( type_info == null ) 636 throw new TKIllegalConversionException( 637 "java.lang.Boolean", 638 "JDBC-Type java.sql.Types." + TYPE_NAMES.get( new Integer ( type ) ) 639 ); 640 641 switch ( type ) { 642 643 case Types.BIT: 644 return createBitLiteral( type_info, state.booleanValue() ); 645 646 default: 647 if ( supportsConvert( Types.BIT, type ) ) { 648 final String type_name = (String ) TYPE_NAMES.get( new Integer ( type ) ); 649 650 return "{fn CONVERT( " + createBitLiteral( type_info, state.booleanValue() ) 651 + ", " + type_name + ") }"; 652 } else { 653 throw new TKIllegalConversionException( "java.lang.Boolean", 654 "JDBC-Type java.sql.Types." + TYPE_NAMES.get( new Integer ( type ) ) ); 655 } 656 } 657 } 658 670 public String convertByte( final int type, final Byte num ) 671 throws SQLException, 672 TKIllegalConversionException 673 { 674 final int[] from_types = { Types.TINYINT, Types.SMALLINT, Types.INTEGER, 675 Types.BIGINT, Types.FLOAT, Types.DOUBLE, 676 Types.REAL, Types.NUMERIC, Types.DECIMAL 677 }; 678 return convertNumber( from_types, type, num ); 679 } 680 692 public String convertByteArray( final int type, final Byte [] data ) 693 throws SQLException, 694 TKIllegalConversionException 695 { 696 final TypeInfo type_info = (TypeInfo) db_types.get( new Integer ( type ) ); 697 if ( type_info == null ) 698 throw new TKIllegalConversionException( 699 "java.lang.Byte[]", 700 "JDBC-Type java.sql.Types." + TYPE_NAMES.get( new Integer ( type ) ) 701 ); 702 703 switch ( type ) { 704 705 case Types.BINARY: 706 case Types.VARBINARY: 707 case Types.LONGVARBINARY: 708 return createBytestringLiteral( type_info, data ); 709 710 default: 711 712 final int[] from_types = { Types.BINARY, Types.VARBINARY, Types.LONGVARBINARY }; 713 714 for ( int i = 0; i < from_types.length; i++ ) { 715 if ( supportsConvert( from_types[i], type ) ) { 716 final TypeInfo info = (TypeInfo) db_types.get( new Integer ( from_types[i] ) ); 717 final String type_name = (String ) TYPE_NAMES.get( new Integer ( type ) ); 718 final String literal = createBytestringLiteral( info, data ); 719 720 return "{fn CONVERT( " + literal + ", " + type_name + ") }"; 721 } 722 } 723 throw new TKIllegalConversionException( "java.lang.Byte[]", 724 "JDBC-Type java.sql.Types." + TYPE_NAMES.get( new Integer ( type ) ) ); 725 } 726 } 727 739 public String convertShort( final int type, final Short num ) 740 throws SQLException, 741 TKIllegalConversionException 742 { 743 final int[] from_types = { Types.SMALLINT, Types.INTEGER, Types.BIGINT, 744 Types.FLOAT, Types.DOUBLE, Types.REAL, 745 Types.NUMERIC, Types.DECIMAL 746 }; 747 return convertNumber( from_types, type, num ); 748 } 749 761 public String convertInteger( final int type, final Integer num ) 762 throws SQLException, 763 TKIllegalConversionException 764 { 765 final int[] from_types = { Types.INTEGER, Types.BIGINT, 766 Types.FLOAT, Types.DOUBLE, Types.REAL, 767 Types.NUMERIC, Types.DECIMAL 768 }; 769 return convertNumber( from_types, type, num ); 770 } 771 783 public String convertLong( final int type, final Long num ) 784 throws SQLException, 785 TKIllegalConversionException 786 { 787 final int[] from_types = { Types.BIGINT, 788 Types.FLOAT, Types.DOUBLE, Types.REAL, 789 Types.NUMERIC, Types.DECIMAL 790 }; 791 return convertNumber( from_types, type, num ); 792 } 793 805 public String convertFloat( final int type, final Float num ) 806 throws SQLException, 807 TKIllegalConversionException 808 { 809 final int[] from_types = { Types.FLOAT, Types.DOUBLE, Types.REAL, 810 Types.NUMERIC, Types.DECIMAL 811 }; 812 return convertNumber( from_types, type, num ); 813 } 814 826 public String convertDouble( final int type, final Double num ) 827 throws SQLException, 828 TKIllegalConversionException 829 { 830 final int[] from_types = { Types.DOUBLE, Types.REAL, 831 Types.NUMERIC, Types.DECIMAL 832 }; 833 return convertNumber( from_types, type, num ); 834 } 835 848 public String convertBigDecimal( final int type, final BigDecimal num ) 849 throws SQLException, 850 TKIllegalConversionException 851 { 852 final int[] from_types = { Types.NUMERIC, Types.DECIMAL }; 853 854 return convertNumber( from_types, type, num ); 855 } 856 869 public String convertDate( final int type, final java.sql.Date date ) 870 throws SQLException, 871 TKIllegalConversionException 872 { 873 if ( type == Types.DATE ) { 874 return createDateLiteral( date ); 875 } 876 877 final TypeInfo type_info = (TypeInfo) db_types.get( new Integer ( type ) ); 878 if ( type_info == null ) 879 throw new TKIllegalConversionException( 880 "java.sql.Date", 881 "JDBC-Type java.sql.Types." + TYPE_NAMES.get( new Integer ( type ) ) 882 ); 883 884 if ( supportsConvert( Types.DATE, type ) ) { 885 final String type_name = (String ) TYPE_NAMES.get( new Integer ( type ) ); 886 887 return "{fn CONVERT( " + createDateLiteral( date ) + ", " + type_name + ") }"; 888 } else { 889 throw new TKIllegalConversionException( "java.sql.Date", 890 "JDBC-Type java.sql.Types." + TYPE_NAMES.get( new Integer ( type ) ) ); 891 } 892 } 893 906 public String convertTime( final int type, final Time time ) 907 throws SQLException, 908 TKIllegalConversionException 909 { 910 if ( type == Types.TIME ) { 911 return createTimeLiteral( time ); 912 } 913 914 final TypeInfo type_info = (TypeInfo) db_types.get( new Integer ( type ) ); 915 if ( type_info == null ) 916 throw new TKIllegalConversionException( 917 "java.sql.Time", 918 "JDBC-Type java.sql.Types." + TYPE_NAMES.get( new Integer ( type ) ) 919 ); 920 921 if ( supportsConvert( Types.TIME, type ) ) { 922 final String type_name = (String ) TYPE_NAMES.get( new Integer ( type ) ); 923 924 return "{fn CONVERT( " + createTimeLiteral( time ) + ", " + type_name + ") }"; 925 } else { 926 throw new TKIllegalConversionException( "java.sql.Time", 927 "java.sql." + TYPE_NAMES.get( new Integer ( type ) ) ); 928 } 929 } 930 943 public String convertTimestamp( final int type, final Timestamp timestamp ) 944 throws SQLException, 945 TKIllegalConversionException 946 { 947 if ( type == Types.TIMESTAMP ) { 948 return createTimestampLiteral( timestamp ); 949 } 950 951 final TypeInfo type_info = (TypeInfo) db_types.get( new Integer ( type ) ); 952 if ( type_info == null ) 953 throw new TKIllegalConversionException( 954 "java.sql.Timestamp", 955 "JDBC-Type java.sql.Types." + TYPE_NAMES.get( new Integer ( type ) ) 956 ); 957 958 if ( supportsConvert( Types.TIMESTAMP, type ) ) { 959 final String type_name = (String ) TYPE_NAMES.get( new Integer ( type ) ); 960 961 return "{fn CONVERT( " + createTimestampLiteral( timestamp ) + ", " + type_name + ") }"; 962 } else { 963 throw new TKIllegalConversionException( "java.sql.Timestamp", 964 "java.sql." + TYPE_NAMES.get( new Integer ( type ) ) ); 965 } 966 } 967 public String toString() 968 { 969 final StringBuffer buf = new StringBuffer (); 970 String lineSep = System.getProperty("line.seperator"); 971 972 buf.append( "{\t" ); 973 final Enumeration types_enum = db_types.elements(); 974 while ( types_enum.hasMoreElements() ) { 975 buf.append( ((TypeInfo)types_enum.nextElement()).toString() ); 976 if ( types_enum.hasMoreElements() ) 977 buf.append( "," ); 978 buf.append( lineSep ); 979 buf.append( "\t" ); 980 } 981 buf.append( lineSep ); 982 buf.append( "}" ); 983 return buf.toString(); 984 } 985 } 988 989 | Popular Tags |