1 package in.co.daffodil.db.jdbc; 2 3 import java.sql.*; 4 import java.math.BigDecimal ; 5 import com.daffodilwoods.daffodildb.utils.field.*; 6 import com.daffodilwoods.database.resource.*; 7 8 11 12 public class DTConvertor { 13 14 static final int BOOLEAN = 16; 15 16 static DTConvertor dtConvertor; 17 static { 18 dtConvertor = new DTConvertor(); 19 } 20 21 private DTConvertor() { 22 } 23 24 public static DTConvertor getInstance() { 25 return dtConvertor; } 27 28 public Object getObject(Object obj, int type) throws SQLException { 29 if (obj == null) 30 return null; 31 try { 32 if (obj.getClass().getName().equalsIgnoreCase("com.daffodilwoods.daffodildb.utils.field.FieldBase")) { 33 throw new SQLException("Invalid Object is passed for Conversion"); 34 } 35 switch (type) { 36 case Types.TINYINT: 37 case Types.SMALLINT: 38 case Types.INTEGER: 39 return getInteger(obj); 40 case Types.BIGINT: 41 return getBigInt(obj); 42 case Types.REAL: 43 return getReal(obj); 44 case Types.FLOAT: 45 return getFloat(obj); 46 case Types.DOUBLE: 47 return getDouble(obj); 48 case Types.DECIMAL: 49 Object a = getDecimal(obj); 50 return a; 51 case Types.NUMERIC: 52 return getNumeric(obj); 53 case BOOLEAN: 54 return getBoolean(obj); 55 case Types.BIT: 56 return getBit(obj); 57 case Types.CHAR: 58 return getChar(obj); 59 case Types.VARCHAR: 60 return getVarchar(obj); 61 case Types.LONGVARCHAR: 62 return getLongVarchar(obj); 63 case Types.BINARY: 64 return getBinary(obj); 65 case Types.VARBINARY: 66 return getVarBinary(obj); 67 case Types.LONGVARBINARY: 68 return getLongVarBinary(obj); 69 case Types.DATE: 70 return getDate(obj); 71 case Types.TIME: 72 return getTime(obj); 73 case Types.TIMESTAMP: 74 return getTimeStamp(obj); 75 case Types.CLOB: 76 return getClob(obj); 77 case Types.BLOB: 78 return getBlob(obj); 79 case Types.JAVA_OBJECT: 80 return getJavaObject(obj); 81 } 82 return obj; 83 } catch (SQLException sqe) { 84 throw sqe; 85 } 86 87 } 88 89 private Byte getTinyInt(Object obj) throws SQLException { 90 if (obj instanceof Byte ) 91 return ( (Byte ) obj); 92 if (obj instanceof Short ) 93 return new Byte ( ( (Short ) obj).byteValue()); 94 if (obj instanceof Integer ) 95 return new Byte ( ( (Integer ) obj).byteValue()); 96 if (obj instanceof Long ) 97 return new Byte ( ( (Long ) obj).byteValue()); 98 if (obj instanceof Float ) 99 return new Byte ( ( (Float ) obj).byteValue()); 100 if (obj instanceof Double ) 101 return new Byte ( ( (Double ) obj).byteValue()); 102 if (obj instanceof BigDecimal ) 103 return new Byte ( ( (BigDecimal ) obj).byteValue()); 104 if (obj instanceof Boolean ) 105 return Boolean.TRUE.equals(obj) ? new Byte ( (byte) 1) : new Byte ( (byte) 0); 106 if (obj instanceof String ) { 107 String st = ( (String ) obj).trim(); 108 return new Byte (st); 109 } 110 throw new SQLException(obj.getClass() + " can't be conveted to Byte"); 111 } 112 113 private Short getSmallInt(Object obj) throws SQLException { 114 if (obj instanceof Byte ) 115 return new Short ( ( (Byte ) obj).shortValue()); 116 if (obj instanceof Short ) 117 return ( (Short ) obj); 118 if (obj instanceof Integer ) 119 return new Short ( ( (Integer ) obj).shortValue()); 120 if (obj instanceof Long ) 121 return new Short ( ( (Long ) obj).shortValue()); 122 if (obj instanceof Float ) 123 return new Short ( ( (Float ) obj).shortValue()); 124 if (obj instanceof Double ) 125 return new Short ( ( (Double ) obj).shortValue()); 126 if (obj instanceof BigDecimal ) 127 return new Short ( ( (BigDecimal ) obj).shortValue()); 128 if (obj instanceof Boolean ) 129 return Boolean.TRUE.equals(obj) ? new Short ( (short) 1) : new Short ( (short) 0); 130 if (obj instanceof String ) { 131 String st = ( (String ) obj).trim(); 132 return new Short (st); 133 } 134 throw new SQLException(obj.getClass() + " can't be conveted to Short"); 135 } 136 137 private Integer getInteger(Object obj) throws SQLException { 138 if (obj instanceof Byte ) 139 return new Integer ( ( (Byte ) obj).intValue()); 140 if (obj instanceof Short ) 141 return new Integer ( ( (Short ) obj).intValue()); 142 if (obj instanceof Integer ) 143 return (Integer ) obj; 144 if (obj instanceof Long ) 145 return new Integer ( ( (Long ) obj).intValue()); 146 if (obj instanceof Float ) 147 return new Integer ( ( (Float ) obj).intValue()); 148 if (obj instanceof Double ) 149 return new Integer ( ( (Double ) obj).intValue()); 150 if (obj instanceof BigDecimal ) 151 return new Integer ( ( (BigDecimal ) obj).intValue()); 152 if (obj instanceof Boolean ) 153 return Boolean.TRUE.equals(obj) ? new Integer ( (int) 1) : new Integer ( (int) 0); 154 if (obj instanceof String ) { 155 String st = ( (String ) obj).trim(); 156 return new Integer (st); 157 } 158 throw new SQLException(obj.getClass() + " can't be conveted to Integer"); 159 } 160 161 private Long getBigInt(Object obj) throws SQLException { 162 if (obj instanceof Byte ) 163 return new Long ( ( (Byte ) obj).longValue()); 164 if (obj instanceof Short ) 165 return new Long ( ( (Short ) obj).longValue()); 166 if (obj instanceof Integer ) 167 return new Long ( ( (Integer ) obj).longValue()); 168 if (obj instanceof Long ) 169 return new Long ( ( (Long ) obj).longValue()); 170 if (obj instanceof Float ) 171 return new Long ( ( (Float ) obj).longValue()); 172 if (obj instanceof Double ) 173 return new Long ( ( (Double ) obj).longValue()); 174 if (obj instanceof BigDecimal ) 175 return new Long ( ( (BigDecimal ) obj).longValue()); 176 if (obj instanceof Boolean ) 177 return Boolean.TRUE.equals(obj) ? new Long ( (long) 1) : new Long ( (long) 0); 178 if (obj instanceof String ) { 179 String st = ( (String ) obj).trim(); 180 return new Long (st); 181 } 182 throw new SQLException(obj.getClass() + " can't be conveted to Long"); 183 } 184 185 private Float getReal(Object obj) throws SQLException { 186 if (obj instanceof Byte ) 187 return new Float ( ( (Byte ) obj).floatValue()); 188 if (obj instanceof Short ) 189 return new Float ( ( (Short ) obj).floatValue()); 190 if (obj instanceof Integer ) 191 return new Float ( ( (Integer ) obj).floatValue()); 192 if (obj instanceof Long ) 193 return new Float ( ( (Long ) obj).floatValue()); 194 if (obj instanceof Float ) 195 return ( (Float ) obj); 196 if (obj instanceof Double ) 197 return new Float ( ( (Double ) obj).floatValue()); 198 if (obj instanceof BigDecimal ) 199 return new Float ( ( (BigDecimal ) obj).floatValue()); 200 if (obj instanceof Boolean ) 201 return Boolean.TRUE.equals(obj) ? new Float ( (float) 1) : new Float ( (float) 0); 202 if (obj instanceof String ) { 203 String st = ( (String ) obj).trim(); 204 return new Float (st); 205 } 206 throw new SQLException(obj.getClass() + " can't be conveted to Float"); 207 } 208 209 private Double getFloat(Object obj) throws SQLException { 210 if (obj instanceof Byte ) 211 return new Double ( ( (Byte ) obj).doubleValue()); 212 if (obj instanceof Short ) 213 return new Double ( ( (Short ) obj).doubleValue()); 214 if (obj instanceof Integer ) 215 return new Double ( ( (Integer ) obj).doubleValue()); 216 if (obj instanceof Long ) 217 return new Double ( ( (Long ) obj).doubleValue()); 218 if (obj instanceof Float ) 219 return new Double ( ( (Float ) obj).doubleValue()); 220 if (obj instanceof Double ) 221 return ( (Double ) obj); 222 if (obj instanceof BigDecimal ) 223 return new Double ( ( (BigDecimal ) obj).doubleValue()); 224 if (obj instanceof Boolean ) 225 return Boolean.TRUE.equals(obj) ? new Double ( (double) 1) : new Double ( (double) 0); 226 if (obj instanceof String ) { 227 String st = ( (String ) obj).trim(); 228 return new Double (st); 229 } 230 throw new SQLException(obj.getClass() + " can't be conveted to Double"); 231 } 232 233 private Double getDouble(Object obj) throws SQLException { 234 if (obj instanceof Byte ) 235 return new Double ( ( (Byte ) obj).doubleValue()); 236 if (obj instanceof Short ) 237 return new Double ( ( (Short ) obj).doubleValue()); 238 if (obj instanceof Integer ) 239 return new Double ( ( (Integer ) obj).doubleValue()); 240 if (obj instanceof Long ) 241 return new Double ( ( (Long ) obj).doubleValue()); 242 if (obj instanceof Float ) 243 return new Double ( ( (Float ) obj).doubleValue()); 244 if (obj instanceof Double ) 245 return ( (Double ) obj); 246 if (obj instanceof BigDecimal ) 247 return new Double ( ( (BigDecimal ) obj).doubleValue()); 248 if (obj instanceof Boolean ) 249 return Boolean.TRUE.equals(obj) ? new Double ( (double) 1) : new Double ( (double) 0); 250 if (obj instanceof String ) { 251 String st = ( (String ) obj).trim(); 252 return new Double (st); 253 } 254 throw new SQLException(obj.getClass() + " can't be conveted to Double"); 255 } 256 257 private BigDecimal getDecimal(Object obj) throws SQLException { 258 if (obj instanceof Byte ) 259 return new BigDecimal ( ( (Byte ) obj).doubleValue()); 260 if (obj instanceof Short ) 261 return new BigDecimal ( ( (Short ) obj).doubleValue()); 262 if (obj instanceof Integer ) 263 return new BigDecimal ( ( (Integer ) obj).doubleValue()); 264 if (obj instanceof Long ) 265 return new BigDecimal ( ( (Long ) obj).doubleValue()); 266 if (obj instanceof Float ) 267 return new BigDecimal ( ( (Float ) obj).doubleValue()); 268 if (obj instanceof Double ) 269 return new BigDecimal ( ( (Double ) obj).doubleValue()); 270 if (obj instanceof BigDecimal ) 271 return (BigDecimal ) obj; 272 if (obj instanceof Boolean ) 273 return Boolean.TRUE.equals(obj) ? new BigDecimal ( (double) 1) : new BigDecimal ( (double) 0); 274 if (obj instanceof String ) { 275 String st = ( (String ) obj).trim(); 276 return new BigDecimal (st); 277 } 278 throw new SQLException(obj.getClass() + " can't be conveted to BigDecimal"); 279 } 280 281 private BigDecimal getNumeric(Object obj) throws SQLException { 282 if (obj instanceof Byte ) 283 return new BigDecimal ( ( (Byte ) obj).doubleValue()); 284 if (obj instanceof Short ) 285 return new BigDecimal ( ( (Short ) obj).doubleValue()); 286 if (obj instanceof Integer ) 287 return new BigDecimal ( ( (Integer ) obj).doubleValue()); 288 if (obj instanceof Long ) 289 return new BigDecimal ( ( (Long ) obj).doubleValue()); 290 if (obj instanceof Float ) 291 return new BigDecimal ( ( (Float ) obj).doubleValue()); 292 if (obj instanceof Double ) 293 return new BigDecimal ( ( (Double ) obj).doubleValue()); 294 if (obj instanceof BigDecimal ) 295 return (BigDecimal ) obj; 296 if (obj instanceof Boolean ) 297 return Boolean.TRUE.equals(obj) ? new BigDecimal ( (double) 1) : new BigDecimal ( (double) 0); 298 if (obj instanceof String ) { 299 String st = ( (String ) obj).trim(); 300 return new BigDecimal (st); 301 } 302 throw new SQLException(obj.getClass() + " can't be conveted to BigDecimal"); 303 } 304 305 private Boolean getBit(Object obj) throws SQLException { 306 if (obj instanceof Byte ) 307 return Utilities.getBooleanValue(obj.hashCode() != 0); 308 if (obj instanceof Short ) 309 return new Boolean (obj.hashCode() != 0); 310 if (obj instanceof Integer ) 311 return new Boolean (obj.hashCode() != 0); 312 if (obj instanceof Long ) 313 return new Boolean (obj.hashCode() != 0); 314 if (obj instanceof Float ) 315 return new Boolean (obj.hashCode() != 0); 316 if (obj instanceof Double ) 317 return new Boolean (obj.hashCode() != 0); 318 if (obj instanceof BigDecimal ) { 319 BigDecimal bd = (BigDecimal ) obj; 320 BigDecimal bd1 = new BigDecimal (0); 321 return new Boolean (bd.compareTo(bd1) != 0); 322 } 323 if (obj instanceof Boolean ) 324 return ( (Boolean ) obj); 325 if (obj instanceof String ) { 326 String str = (String ) obj; 327 str = str.trim(); 328 return new Boolean (! (str.equalsIgnoreCase("false") || str.equalsIgnoreCase("0"))); 329 } 330 throw new SQLException(obj.getClass() + " can't be conveted to Boolean"); 331 } 332 333 private Boolean getBoolean(Object obj) throws SQLException { 334 if (obj instanceof Byte ) 335 return Utilities.getBooleanValue(obj.hashCode() != 0); 336 if (obj instanceof Short ) 337 return new Boolean (obj.hashCode() != 0); 338 if (obj instanceof Integer ) 339 return new Boolean (obj.hashCode() != 0); 340 if (obj instanceof Long ) 341 return new Boolean (obj.hashCode() != 0); 342 if (obj instanceof Float ) 343 return new Boolean (obj.hashCode() != 0); 344 if (obj instanceof Double ) 345 return new Boolean (obj.hashCode() != 0); 346 if (obj instanceof BigDecimal ) { 347 BigDecimal bd = (BigDecimal ) obj; 348 BigDecimal bd1 = new BigDecimal (0); 349 return new Boolean (bd.compareTo(bd1) != 0); 350 } 351 if (obj instanceof Boolean ) 352 return ( (Boolean ) obj); 353 if (obj instanceof String ) { 354 String str = (String ) obj; 355 str = str.trim(); 356 return new Boolean (! (str.equalsIgnoreCase("false") || str.equalsIgnoreCase("0"))); 357 } 358 throw new SQLException(obj.getClass() + " can't be conveted to Boolean"); 359 } 360 361 private String getChar(Object obj) throws SQLException { 362 if (obj instanceof Byte ) 363 return ( (Byte ) obj).toString(); 364 if (obj instanceof Short ) 365 return ( (Short ) obj).toString(); 366 if (obj instanceof Integer ) 367 return ( (Integer ) obj).toString(); 368 if (obj instanceof Long ) 369 return ( (Long ) obj).toString(); 370 if (obj instanceof Float ) 371 return ( (Float ) obj).toString(); 372 if (obj instanceof Double ) 373 return ( (Double ) obj).toString(); 374 if (obj instanceof BigDecimal ) 375 return ( (BigDecimal ) obj).toString(); 376 if (obj instanceof Boolean ) 377 return ( (Boolean ) obj).toString(); 378 if (obj instanceof String ) 379 return (String ) obj; 380 if (obj instanceof java.sql.Date ) 381 return ( (Date) obj).toString(); 382 if (obj instanceof java.sql.Time ) 383 return ( (Time) obj).toString(); 384 if (obj instanceof java.sql.Timestamp ) 385 return ( (Timestamp) obj).toString(); 386 if (obj instanceof Clob) { 387 return ( (Clob) obj).getSubString(1, (int) ( (Clob) obj).length()); 388 } 389 throw new SQLException(obj.getClass() + " can't be conveted to String"); 390 } 391 392 private String getVarchar(Object obj) throws SQLException { 393 if (obj instanceof Byte ) 394 return ( (Byte ) obj).toString(); 395 if (obj instanceof Short ) 396 return ( (Short ) obj).toString(); 397 if (obj instanceof Integer ) 398 return ( (Integer ) obj).toString(); 399 if (obj instanceof Long ) 400 return ( (Long ) obj).toString(); 401 if (obj instanceof Float ) 402 return ( (Float ) obj).toString(); 403 if (obj instanceof Double ) 404 return ( (Double ) obj).toString(); 405 if (obj instanceof BigDecimal ) 406 return ( (BigDecimal ) obj).toString(); 407 if (obj instanceof Boolean ) 408 return ( (Boolean ) obj).toString(); 409 if (obj instanceof String ) 410 return (String ) obj; 411 if (obj instanceof java.sql.Date ) 412 return ( (Date) obj).toString(); 413 if (obj instanceof java.sql.Time ) 414 return ( (Time) obj).toString(); 415 if (obj instanceof java.sql.Timestamp ) 416 return ( (Timestamp) obj).toString(); 417 if (obj instanceof Clob) { 418 return ( (Clob) obj).getSubString(1, (int) ( (Clob) obj).length()); 419 } 420 if (obj instanceof Blob) { 421 return ( (Blob) obj).getClass() + ""; 422 } 423 throw new SQLException(obj.getClass() + " can't be conveted to String"); 424 } 425 426 private String getLongVarchar(Object obj) throws SQLException { 427 if (obj instanceof Byte ) 428 return ( (Byte ) obj).toString(); 429 if (obj instanceof Short ) 430 return ( (Short ) obj).toString(); 431 if (obj instanceof Integer ) 432 return ( (Integer ) obj).toString(); 433 if (obj instanceof Long ) 434 return ( (Long ) obj).toString(); 435 if (obj instanceof Float ) 436 return ( (Float ) obj).toString(); 437 if (obj instanceof Double ) 438 return ( (Double ) obj).toString(); 439 if (obj instanceof BigDecimal ) 440 return ( (BigDecimal ) obj).toString(); 441 if (obj instanceof Boolean ) 442 return ( (Boolean ) obj).toString(); 443 if (obj instanceof String ) 444 return (String ) obj; 445 if (obj instanceof java.sql.Date ) 446 return ( (Date) obj).toString(); 447 if (obj instanceof java.sql.Time ) 448 return ( (Time) obj).toString(); 449 if (obj instanceof java.sql.Timestamp ) 450 return ( (Timestamp) obj).toString(); 451 if (obj instanceof Clob) { 452 return ( (Clob) obj).getSubString(1, (int) ( (Clob) obj).length()); 453 } 454 throw new SQLException(obj.getClass() + " can't be conveted to String"); 455 } 456 457 private byte[] getBinary(Object obj) throws SQLException { 458 if (obj instanceof String ) 459 return ( (String ) obj).getBytes(); 460 if (obj instanceof byte[]) 461 return (byte[]) obj; 462 if (obj instanceof Blob) 463 return ( (Blob) obj).getBytes(1, (int) ( (Blob) obj).length()); 464 if (obj instanceof Clob) 465 return ( (Clob) obj).getSubString(1, (int) ( (Clob) obj).length()).getBytes(); 466 throw new SQLException(obj.getClass() + " can't be conveted to byte[]"); 467 } 468 469 private byte[] getVarBinary(Object obj) throws SQLException { 470 if (obj instanceof String ) 471 return ( (String ) obj).getBytes(); 472 if (obj instanceof byte[]) 473 return (byte[]) obj; 474 if (obj instanceof Blob) 475 return ( (Blob) obj).getBytes(1, (int) ( (Blob) obj).length()); 476 if (obj instanceof Clob) 477 return ( (Clob) obj).getSubString(1, (int) ( (Clob) obj).length()).getBytes(); 478 throw new SQLException(obj.getClass() + " can't be conveted to byte[]"); 479 } 480 481 private byte[] getLongVarBinary(Object obj) throws SQLException { 482 if (obj instanceof String ) 483 return ( (String ) obj).getBytes(); 484 if (obj instanceof byte[]) 485 return (byte[]) obj; 486 if (obj instanceof Blob) 487 return ( (Blob) obj).getBytes(1, (int) ( (Blob) obj).length()); 488 if (obj instanceof Clob) 489 return ( (Clob) obj).getSubString(1, (int) ( (Clob) obj).length()).getBytes(); 490 throw new SQLException(obj.getClass() + " can't be conveted to byte[]"); 491 } 492 493 private Date getDate(Object obj) throws SQLException { 494 if (obj instanceof String ) { 495 String st = ( (String ) obj).trim(); 496 return Date.valueOf(st); 497 } 498 if (obj instanceof Date) 499 return ( (Date) obj); 500 if (obj instanceof java.sql.Timestamp ) { 501 String sMfgStringVal = obj.toString(); 502 sMfgStringVal = sMfgStringVal.substring(0, sMfgStringVal.indexOf(' ')); 503 return java.sql.Date.valueOf(sMfgStringVal); 504 } 505 throw new SQLException(obj.getClass() + " can't be conveted to Date"); 506 } 507 508 private Time getTime(Object obj) throws SQLException { 509 if (obj instanceof String ) { 510 String st = ( (String ) obj).trim(); 511 return Time.valueOf(st); 512 } 513 if (obj instanceof java.sql.Time ) 514 return ( (Time) obj); 515 if (obj instanceof java.sql.Timestamp ) 516 return new Time( ( (Timestamp) obj).getTime()); 517 throw new SQLException(obj.getClass() + " can't be conveted to Time"); 518 } 519 520 private Timestamp getTimeStamp(Object obj) throws SQLException { 521 if (obj instanceof String ) { 522 String st = ( (String ) obj).trim(); 523 return Timestamp.valueOf(st); 524 } 525 if (obj instanceof Date) 526 return new Timestamp( ( (Date) obj).getTime()); 527 if (obj instanceof Time) 528 return new Timestamp( ( (Time) obj).getTime()); 529 if (obj instanceof java.sql.Timestamp ) 530 return (Timestamp) obj; 531 throw new SQLException(obj.getClass() + " can't be conveted to Timestamp"); 532 } 533 534 private Clob getClob(Object obj) throws SQLException { 535 if (obj instanceof java.sql.Clob ) 536 return ( (Clob) obj); 537 throw new SQLException(obj.getClass() + " can't be conveted to Clob"); 538 } 539 540 private Blob getBlob(Object obj) throws SQLException { 541 if (obj instanceof java.sql.Blob ) 542 return ( (Blob) obj); 543 throw new SQLException(obj.getClass() + " can't be conveted to Blob"); 544 } 545 546 private Object getJavaObject(Object obj) { 547 return obj; 548 } 549 } 550 551 | Popular Tags |