1 24 25 package com.mckoi.database.global; 26 27 import java.text.SimpleDateFormat ; 28 import java.text.ParseException ; 29 import java.text.DateFormat ; 30 import com.mckoi.util.BigNumber; 31 import java.util.Date ; 32 33 39 40 public class CastHelper { 41 42 45 private static BigNumber BD_ZERO = BigNumber.fromLong(0); 46 private static BigNumber BD_ONE = BigNumber.fromLong(1); 47 48 51 private static DateFormat [] date_format_sql; 52 private static DateFormat [] time_format_sql; 53 private static DateFormat [] ts_format_sql; 54 55 static { 56 date_format_sql = new DateFormat [1]; 58 date_format_sql[0] = new SimpleDateFormat ("yyyy-MM-dd"); 59 60 time_format_sql = new DateFormat [4]; 61 time_format_sql[0] = new SimpleDateFormat ("HH:mm:ss.S z"); 62 time_format_sql[1] = new SimpleDateFormat ("HH:mm:ss.S"); 63 time_format_sql[2] = new SimpleDateFormat ("HH:mm:ss z"); 64 time_format_sql[3] = new SimpleDateFormat ("HH:mm:ss"); 65 66 ts_format_sql = new DateFormat [4]; 67 ts_format_sql[0] = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss.S z"); 68 ts_format_sql[1] = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss.S"); 69 ts_format_sql[2] = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss z"); 70 ts_format_sql[3] = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); 71 } 72 73 74 75 79 private static Object toJavaObject(Object ob) { 80 try { 81 return ObjectTranslator.serialize(ob); 82 } 83 catch (Throwable e) { 84 throw new Error ("Can't serialize object " + ob.getClass()); 85 } 86 } 87 88 91 private static String formatDateAsString(Date d) { 92 synchronized (ts_format_sql) { 93 return ts_format_sql[1].format(d); 96 } 97 } 98 99 103 private static String paddedString(String str, int size) { 104 if (size == -1) { 105 return str; 106 } 107 int dif = size - str.length(); 108 if (dif > 0) { 109 StringBuffer buf = new StringBuffer (str); 110 for (int n = 0; n < dif; ++n) { 111 buf.append(' '); 112 } 113 return new String (buf); 114 } 115 else if (dif < 0) { 116 return str.substring(0, size); 117 } 118 return str; 119 } 120 121 124 private static Date toDate(long time) { 125 return new Date (time); 126 } 127 128 131 private static BigNumber toBigNumber(String str) { 132 try { 133 return BigNumber.fromString(str); 134 } 135 catch (Throwable e) { 136 return BD_ZERO; 137 } 138 } 139 140 143 private static String dateErrorString(String msg, DateFormat [] df) { 144 String pattern = ""; 145 if (df[0] instanceof SimpleDateFormat ) { 146 SimpleDateFormat sdf = (SimpleDateFormat ) df[0]; 147 pattern = "(" + sdf.toPattern() + ")"; 148 } 149 return msg + pattern; 150 } 151 152 155 public static Date toDate(String str) { 156 synchronized(date_format_sql) { 157 for (int i = 0; i < date_format_sql.length; ++i) { 158 try { 159 return date_format_sql[i].parse(str); 160 } 161 catch (ParseException e) {} 162 } 163 throw new RuntimeException ( 164 dateErrorString("Unable to parse string as a date ", 165 date_format_sql)); 166 } 167 } 168 169 172 public static Date toTime(String str) { 173 synchronized(time_format_sql) { 174 for (int i = 0; i < time_format_sql.length; ++i) { 175 try { 176 return time_format_sql[i].parse(str); 177 } 178 catch (ParseException e) {} 179 } 180 throw new RuntimeException ( 181 dateErrorString("Unable to parse string as a time ", 182 time_format_sql)); 183 } 184 } 185 186 189 public static Date toTimeStamp(String str) { 190 synchronized(ts_format_sql) { 191 for (int i = 0; i < ts_format_sql.length; ++i) { 192 try { 193 return ts_format_sql[i].parse(str); 194 } 195 catch (ParseException e) {} 196 } 197 throw new RuntimeException ( 198 dateErrorString("Unable to parse string as a timestamp ", 199 ts_format_sql)); 200 } 201 } 202 203 204 205 206 207 208 229 public static Object castObjectToSQLType(Object ob, 230 int sql_type, int sql_size, int sql_scale, String sql_type_string) { 231 232 236 241 if (ob instanceof ByteLongObject) { 244 if ( sql_type != SQLTypes.JAVA_OBJECT && 245 sql_type != SQLTypes.BLOB && 246 sql_type != SQLTypes.BINARY && 247 sql_type != SQLTypes.VARBINARY && 248 sql_type != SQLTypes.LONGVARBINARY ) { 249 try { 251 ob = ObjectTranslator.deserialize((ByteLongObject) ob); 252 } 253 catch (Throwable e) { 254 throw new Error ("Can't cast a BLOB to " + sql_type_string); 257 } 258 } 259 else { 260 return ob; 263 } 264 } 265 266 if (ob instanceof BlobRef) { 268 if (sql_type == SQLTypes.BINARY || 269 sql_type == SQLTypes.BLOB || 270 sql_type == SQLTypes.JAVA_OBJECT || 271 sql_type == SQLTypes.VARBINARY || 272 sql_type == SQLTypes.LONGVARBINARY) { 273 return ob; 274 } 275 } 276 277 if (ob instanceof ClobRef) { 279 if (sql_type == SQLTypes.VARCHAR || 280 sql_type == SQLTypes.LONGVARCHAR || 281 sql_type == SQLTypes.CLOB) { 282 return ob; 283 } 284 } 285 286 if (ob == null) { 288 switch (sql_type) { 289 case(SQLTypes.BIT): 290 case(SQLTypes.TINYINT): 292 case(SQLTypes.SMALLINT): 294 case(SQLTypes.INTEGER): 296 case(SQLTypes.BIGINT): 298 case(SQLTypes.FLOAT): 300 case(SQLTypes.REAL): 302 case(SQLTypes.DOUBLE): 304 case(SQLTypes.NUMERIC): 306 case(SQLTypes.DECIMAL): 308 case(SQLTypes.CHAR): 310 case(SQLTypes.VARCHAR): 312 case(SQLTypes.LONGVARCHAR): 314 case(SQLTypes.CLOB): 316 case(SQLTypes.DATE): 318 case(SQLTypes.TIME): 320 case(SQLTypes.TIMESTAMP): 322 case(SQLTypes.NULL): 324 326 case(SQLTypes.BINARY): 327 case(SQLTypes.VARBINARY): 329 case(SQLTypes.LONGVARBINARY): 331 case(SQLTypes.BLOB): 333 335 336 case(SQLTypes.JAVA_OBJECT): 337 339 case(SQLTypes.BOOLEAN): 340 return null; 341 default: 342 throw new Error ("Can't cast NULL to " + sql_type_string); 343 } 344 } 345 346 if (ob instanceof Number ) { 348 Number n = (Number ) ob; 349 switch (sql_type) { 350 case(SQLTypes.BIT): 351 return n.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE; 352 case(SQLTypes.TINYINT): 353 case(SQLTypes.SMALLINT): 355 case(SQLTypes.INTEGER): 357 return BigNumber.fromLong(n.intValue()); 359 case(SQLTypes.BIGINT): 360 return BigNumber.fromLong(n.longValue()); 362 case(SQLTypes.FLOAT): 363 return BigNumber.fromString(Double.toString(n.doubleValue())); 364 case(SQLTypes.REAL): 365 return BigNumber.fromString(n.toString()); 366 case(SQLTypes.DOUBLE): 367 return BigNumber.fromString(Double.toString(n.doubleValue())); 368 case(SQLTypes.NUMERIC): 369 case(SQLTypes.DECIMAL): 371 return BigNumber.fromString(n.toString()); 372 case(SQLTypes.CHAR): 373 return StringObject.fromString(paddedString(n.toString(), sql_size)); 374 case(SQLTypes.VARCHAR): 375 return StringObject.fromString(n.toString()); 376 case(SQLTypes.LONGVARCHAR): 377 return StringObject.fromString(n.toString()); 378 case(SQLTypes.DATE): 379 return toDate(n.longValue()); 380 case(SQLTypes.TIME): 381 return toDate(n.longValue()); 382 case(SQLTypes.TIMESTAMP): 383 return toDate(n.longValue()); 384 case(SQLTypes.BLOB): 385 case(SQLTypes.BINARY): 387 case(SQLTypes.VARBINARY): 389 case(SQLTypes.LONGVARBINARY): 391 return new ByteLongObject(n.toString().getBytes()); 392 case(SQLTypes.NULL): 393 return null; 394 case(SQLTypes.JAVA_OBJECT): 395 return toJavaObject(ob); 396 case(SQLTypes.BOOLEAN): 397 return n.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE; 398 default: 399 throw new Error ("Can't cast number to " + sql_type_string); 400 } 401 } 403 if (ob instanceof StringObject || ob instanceof String ) { 405 String str = ob.toString(); 406 switch (sql_type) { 407 case(SQLTypes.BIT): 408 return str.equalsIgnoreCase("true") ? Boolean.TRUE : Boolean.FALSE; 409 case(SQLTypes.TINYINT): 410 case(SQLTypes.SMALLINT): 412 case(SQLTypes.INTEGER): 414 return BigNumber.fromLong(toBigNumber(str).intValue()); 416 case(SQLTypes.BIGINT): 417 return BigNumber.fromLong(toBigNumber(str).longValue()); 419 case(SQLTypes.FLOAT): 420 return BigNumber.fromString( 421 Double.toString(toBigNumber(str).doubleValue())); 422 case(SQLTypes.REAL): 423 return toBigNumber(str); 424 case(SQLTypes.DOUBLE): 425 return BigNumber.fromString( 426 Double.toString(toBigNumber(str).doubleValue())); 427 case(SQLTypes.NUMERIC): 428 case(SQLTypes.DECIMAL): 430 return toBigNumber(str); 431 case(SQLTypes.CHAR): 432 return StringObject.fromString(paddedString(str, sql_size)); 433 case(SQLTypes.VARCHAR): 434 return StringObject.fromString(str); 435 case(SQLTypes.LONGVARCHAR): 436 return StringObject.fromString(str); 437 case(SQLTypes.DATE): 438 return toDate(str); 439 case(SQLTypes.TIME): 440 return toTime(str); 441 case(SQLTypes.TIMESTAMP): 442 return toTimeStamp(str); 443 case(SQLTypes.BLOB): 444 case(SQLTypes.BINARY): 446 case(SQLTypes.VARBINARY): 448 case(SQLTypes.LONGVARBINARY): 450 return new ByteLongObject(str.getBytes()); 451 case(SQLTypes.NULL): 452 return null; 453 case(SQLTypes.JAVA_OBJECT): 454 return toJavaObject(str); 455 case(SQLTypes.BOOLEAN): 456 return str.equalsIgnoreCase("true") ? Boolean.TRUE : Boolean.FALSE; 457 case(SQLTypes.CLOB): 458 return StringObject.fromString(str); 459 default: 460 throw new Error ("Can't cast string to " + sql_type_string); 461 } 462 } 464 if (ob instanceof Boolean ) { 466 Boolean b = (Boolean ) ob; 467 switch (sql_type) { 468 case(SQLTypes.BIT): 469 return b; 470 case(SQLTypes.TINYINT): 471 case(SQLTypes.SMALLINT): 473 case(SQLTypes.INTEGER): 475 case(SQLTypes.BIGINT): 477 case(SQLTypes.FLOAT): 479 case(SQLTypes.REAL): 481 case(SQLTypes.DOUBLE): 483 case(SQLTypes.NUMERIC): 485 case(SQLTypes.DECIMAL): 487 return b.equals(Boolean.TRUE) ? BD_ONE : BD_ZERO; 488 case(SQLTypes.CHAR): 489 return StringObject.fromString(paddedString(b.toString(), sql_size)); 490 case(SQLTypes.VARCHAR): 491 return StringObject.fromString(b.toString()); 492 case(SQLTypes.LONGVARCHAR): 493 return StringObject.fromString(b.toString()); 494 case(SQLTypes.NULL): 495 return null; 496 case(SQLTypes.JAVA_OBJECT): 497 return toJavaObject(ob); 498 case(SQLTypes.BOOLEAN): 499 return b; 500 default: 501 throw new Error ("Can't cast boolean to " + sql_type_string); 502 } 503 } 505 if (ob instanceof Date ) { 507 Date d = (Date ) ob; 508 switch (sql_type) { 509 case(SQLTypes.TINYINT): 510 case(SQLTypes.SMALLINT): 512 case(SQLTypes.INTEGER): 514 case(SQLTypes.BIGINT): 516 case(SQLTypes.FLOAT): 518 case(SQLTypes.REAL): 520 case(SQLTypes.DOUBLE): 522 case(SQLTypes.NUMERIC): 524 case(SQLTypes.DECIMAL): 526 return BigNumber.fromLong(d.getTime()); 527 case(SQLTypes.CHAR): 528 return StringObject.fromString(paddedString(formatDateAsString(d), sql_size)); 529 case(SQLTypes.VARCHAR): 530 return StringObject.fromString(formatDateAsString(d)); 531 case(SQLTypes.LONGVARCHAR): 532 return StringObject.fromString(formatDateAsString(d)); 533 case(SQLTypes.DATE): 534 return d; 535 case(SQLTypes.TIME): 536 return d; 537 case(SQLTypes.TIMESTAMP): 538 return d; 539 case(SQLTypes.NULL): 540 return null; 541 case(SQLTypes.JAVA_OBJECT): 542 return toJavaObject(ob); 543 default: 544 throw new Error ("Can't cast date to " + sql_type_string); 545 } 546 } 548 if (ob instanceof byte[]) { 550 switch (sql_type) { 551 case(SQLTypes.BLOB): 552 case(SQLTypes.BINARY): 554 case(SQLTypes.VARBINARY): 556 case(SQLTypes.LONGVARBINARY): 558 return new ByteLongObject((byte[]) ob); 559 default: 560 throw new Error ("Can't cast byte[] to " + sql_type_string); 561 } 562 } 563 564 if (sql_type == SQLTypes.JAVA_OBJECT) { 567 return toJavaObject(ob); 568 } 569 570 throw new RuntimeException ("Can't cast object " + ob.getClass() + " to " + 571 sql_type_string); 572 573 } 574 575 } 576 | Popular Tags |