1 package com.daffodilwoods.database.general; 2 3 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.Datatype; 4 import com.daffodilwoods.database.resource.*; 5 import com.daffodilwoods.daffodildb.server.sql99.common.StaticClass; 6 import java.sql.*; 7 import java.text.MessageFormat ; 8 import java.math.BigDecimal ; 9 import com.daffodilwoods.daffodildb.client.IgnoreValue; 10 11 public class TypeConverter implements Datatype{ 12 13 private TypeConverter() throws DException {} 14 15 public static Object ConvertObjectToDatabaseType(Object object,int databaseType,int size)throws DException{ 16 if(object instanceof IgnoreValue) 17 throw new DException("DSE4120", null); 18 if(object instanceof Exception ) 19 throw new DException("DSE0", new Object []{object}); 20 if(object == null) 21 return null; 22 switch(databaseType){ 23 case Datatype.BIGDECIMAL : 24 case Datatype.DEC : 25 case Datatype.DECIMAL : 26 case Datatype.NUMERIC : 27 if(object instanceof BigDecimal ) 28 return object; 29 if( object instanceof Boolean ) 30 return Boolean.TRUE.equals(object) ? new BigDecimal (1.0) : new BigDecimal (0.0); 31 if(object instanceof byte[]) 32 object = new String ((byte[])object); 33 34 case Datatype.BIGINT : 35 case Datatype.LONG : 36 if(object instanceof Long ) 37 return object; 38 if(object instanceof BigDecimal ) 39 return new Long (((BigDecimal )object).longValue()); 40 if( object instanceof Boolean ) 41 return Boolean.TRUE.equals(object) ? new Long (1L) : new Long (0L); 42 if(object instanceof byte[]) 43 object = new String ((byte[])object); 44 try{ 45 Double db = new Double (""+object); 46 return new Long (db.longValue()); 47 }catch(RuntimeException re){ 48 throw new DException("DSE773", new Object []{object,StaticClass.getDataTypeName(databaseType)}); 49 } 50 51 case Datatype.BOOLEAN : 52 if(object instanceof Boolean ) 53 return object ; 54 if(object instanceof byte[]) 55 object = new String ((byte[])object); 56 try{ 57 return new Boolean (""+object); 58 }catch(RuntimeException re){ 59 throw new DException("DSE773", new Object []{object,StaticClass.getDataTypeName(databaseType)}); 60 } 61 62 case Datatype.TINYINT : 63 case Datatype.BYTE : 64 if(object instanceof Byte ) 65 return object ; 66 if( object instanceof Boolean ) 67 return Boolean.TRUE.equals(object) ? new Byte ("1") : new Byte ("0"); 68 if(object instanceof byte[]) 69 object = new String ((byte[])object); 70 try{ 71 return new Byte (""+object); 72 }catch(RuntimeException re){ 73 throw new DException("DSE773", new Object []{object,StaticClass.getDataTypeName(databaseType)}); 74 } 75 76 case Datatype.BLOB : 77 case Datatype.BINARYLARGEOBJECT : 78 case Datatype.LONGVARBINARY : 79 if(object instanceof Blob) 80 return object; 81 throw new DException("DSE773", new Object []{object,StaticClass.getDataTypeName(databaseType)}); 82 case Datatype.BINARY : 83 case Datatype.VARBINARY : 84 if(object instanceof byte[]) 85 return object; 86 String str = object.toString(); 87 return str.getBytes(); 88 89 case Datatype.CHARACTERLARGEOBJECT : 90 case Datatype.CHARLARGEOBJECT : 91 case Datatype.CLOB : 92 case Datatype.LONGVARCHAR : 93 if(object instanceof Clob) 94 return object; 95 throw new DException("DSE773", new Object []{object,StaticClass.getDataTypeName(databaseType)}); 96 97 case Datatype.CHAR : 98 case Datatype.CHARACTER : 99 case Datatype.CHARACTERVARYING : 100 case Datatype.CHARVARYING : 101 case Datatype.VARCHAR : 102 if(object instanceof byte[]) 103 object = new String ((byte[])object); 104 String str1 = ""+object; 105 if(size == -1) 106 return str1; 107 if(str1.length() > size) 108 throw new SizeMisMatchException("DSE773",new Object []{object,StaticClass.getDataTypeName(databaseType)}); 109 return str1; 110 111 case Datatype.BIT : 112 case Datatype.BITVARYING : 113 if(object instanceof Boolean ) 114 object = ((Boolean )object).booleanValue() ? "1" : "0"; 115 String str11 = ""+object; 116 if(size == -1) 117 return str11; 118 if(str11.length() > size) 119 throw new SizeMisMatchException("DSE773",new Object []{object,StaticClass.getDataTypeName(databaseType)}); 120 return str11; 121 122 case Datatype.DOUBLE : 123 case Datatype.DOUBLEPRECISION : 124 case Datatype.FLOAT : 125 if(object instanceof Double ) 126 return object ; 127 if(object instanceof BigDecimal ) 128 return new Double (((BigDecimal )object).doubleValue()); 129 if( object instanceof Boolean ) 130 return Boolean.TRUE.equals(object) ? new Double (1d) : new Double (0d); 131 if(object instanceof byte[]) 132 object = new String ((byte[])object); 133 try{ 134 return new Double (""+object); 135 }catch(RuntimeException re){ 136 throw new DException("DSE773", new Object []{object,StaticClass.getDataTypeName(databaseType)}); 137 } 138 139 case Datatype.INT : 140 case Datatype.INTEGER : 141 if(object instanceof Integer ) 142 return object ; 143 if(object instanceof BigDecimal ) 144 return new Integer (((BigDecimal )object).intValue()); 145 if( object instanceof Boolean ) 146 return Boolean.TRUE.equals(object) ? new Integer (1) : new Integer (0); 147 if(object instanceof byte[]) 148 object = new String ((byte[])object); 149 try{ 150 Double db = new Double (""+object); 151 return new Integer (db.intValue()); 152 }catch(RuntimeException re){ 153 throw new DException("DSE773", new Object []{object,StaticClass.getDataTypeName(databaseType)}); 154 } 155 156 case Datatype.REAL : 157 if(object instanceof Float ) 158 return object ; 159 if(object instanceof BigDecimal ) 160 return new Float (((BigDecimal )object).floatValue()); 161 if( object instanceof Boolean ) 162 return Boolean.TRUE.equals(object) ? new Float (1f) : new Float (0f); 163 if(object instanceof byte[]) 164 object = new String ((byte[])object); 165 try{ 166 Double db = new Double (""+object); 167 return new Float (db.floatValue()); 168 }catch(RuntimeException re){ 169 throw new DException("DSE773", new Object []{object,StaticClass.getDataTypeName(databaseType)}); 170 } 171 172 case Datatype.SHORT : 173 case Datatype.SMALLINT : 174 if(object instanceof Short ) 175 return object ; 176 if(object instanceof BigDecimal ) 177 return new Short (((BigDecimal )object).shortValue()); 178 if( object instanceof Boolean ) 179 return Boolean.TRUE.equals(object) ? new Short ("1") : new Short ("0"); 180 if(object instanceof byte[]) 181 object = new String ((byte[])object); 182 try{ 183 Double db = new Double (""+object); 184 return new Short (db.shortValue()); 185 }catch(RuntimeException re){ 186 throw new DException("DSE773", new Object []{object,StaticClass.getDataTypeName(databaseType)}); 187 } 188 189 case Datatype.DATE : 190 if(object instanceof Date) 191 return ((Date)object); 192 if(object instanceof java.sql.Timestamp ) 193 return new com.daffodilwoods.daffodildb.utils.DBDate(((Timestamp)object).getTime()); 194 if(object instanceof byte[]) 195 object = new String ((byte[])object); 196 if(object instanceof String ) 197 return Date.valueOf((String )object); 198 throw new DException("DSE773", new Object []{object,StaticClass.getDataTypeName(databaseType)}); 199 200 case Datatype.TIME : 201 if(object instanceof java.sql.Time ) 202 return ((Time)object); 203 if(object instanceof java.sql.Timestamp ) 204 return new Time(((Timestamp)object).getTime()); 205 if(object instanceof java.sql.Date ) 206 return new Time(((Date)object).getTime()); 207 if(object instanceof byte[]) 208 object = new String ((byte[])object); 209 if(object instanceof String ) 210 return Time.valueOf((String )object); 211 throw new DException("DSE773", new Object []{object,StaticClass.getDataTypeName(databaseType)}); 212 213 case Datatype.TIMESTAMP : 214 if(object instanceof Date) 215 return new Timestamp(((Date)object).getTime()); 216 if(object instanceof Time) 217 return new Timestamp(((Time)object).getTime()); 218 if(object instanceof java.sql.Timestamp ) 219 return (Timestamp)object; 220 if(object instanceof byte[]) 221 object = new String ((byte[])object); 222 if(object instanceof String ) 223 return Timestamp.valueOf((String )object); 224 throw new DException("DSE773", new Object []{object,StaticClass.getDataTypeName(databaseType)}); 225 226 case Datatype.ARRAY : 227 case Datatype.MODULE : 228 case Datatype.RECORD : 229 case Datatype.RECORDSET : 230 case Datatype.REF : 231 case Datatype.STRUCT : 232 case Datatype.SUM : 233 case Datatype.TIME_WITH_TIMEZONE : 234 case Datatype.TIMESTAMP_WITH_TIMEZONE : 235 throw new DException("DSE1133", new Object []{new Integer (databaseType)}); 236 237 default : 238 throw new DException("DSE1021", new Object []{new Integer (databaseType)}); 239 } 240 } 241 242 243 244 public static Object ConvertObjectToDatabaseTypeUsedForBytes(Object object,int databaseType,int size)throws DException{ 245 if(object instanceof IgnoreValue) 246 throw new DException("DSE4120", null); 247 if(object instanceof Exception ) 248 throw new DException("DSE0", new Object []{object}); 249 if(object == null) 250 return null; 251 switch(databaseType){ 252 case Datatype.BIGDECIMAL : 253 case Datatype.DEC : 254 case Datatype.DECIMAL : 255 case Datatype.NUMERIC : 256 if(object instanceof BigDecimal ) 257 return object; 258 if( object instanceof Boolean ) 259 return Boolean.TRUE.equals(object) ? new BigDecimal (1.0) : new BigDecimal (0.0); 260 if(object instanceof byte[]) 261 object = new String ((byte[])object); 262 try{ 263 return new BigDecimal (""+object); 264 }catch(RuntimeException re){ 265 throw new DException("DSE773", new Object []{object,StaticClass.getDataTypeName(databaseType)}); 266 } 267 268 case Datatype.BIGINT : 269 case Datatype.LONG : 270 if(object instanceof Long ) 271 return object; 272 if(object instanceof BigDecimal ) 273 return new Long (((BigDecimal )object).longValue()); 274 if( object instanceof Boolean ) 275 return Boolean.TRUE.equals(object) ? new Long (1L) : new Long (0L); 276 if(object instanceof byte[]) 277 object = new String ((byte[])object); 278 try{ 279 Double db = new Double (""+object); 280 return new Long (db.longValue()); 281 }catch(RuntimeException re){ 282 throw new DException("DSE773", new Object []{object,StaticClass.getDataTypeName(databaseType)}); 283 } 284 285 case Datatype.BOOLEAN : 286 if(object instanceof Boolean ) 287 return object ; 288 if(object instanceof byte[]) 289 object = new String ((byte[])object); 290 try{ 291 return new Boolean (""+object); 292 }catch(RuntimeException re){ 293 throw new DException("DSE773", new Object []{object,StaticClass.getDataTypeName(databaseType)}); 294 } 295 296 case Datatype.TINYINT : 297 case Datatype.BYTE : 298 if(object instanceof Byte ) 299 return object ; 300 if( object instanceof Boolean ) 301 return Boolean.TRUE.equals(object) ? new Byte ("1") : new Byte ("0"); 302 if(object instanceof byte[]) 303 object = new String ((byte[])object); 304 try{ 305 return new Byte (""+object); 306 }catch(RuntimeException re){ 307 throw new DException("DSE773", new Object []{object,StaticClass.getDataTypeName(databaseType)}); 308 } 309 310 case Datatype.BLOB : 311 case Datatype.BINARYLARGEOBJECT : 312 case Datatype.LONGVARBINARY : 313 if(object instanceof Blob) 314 return object; 315 throw new DException("DSE773", new Object []{object,StaticClass.getDataTypeName(databaseType)}); 316 case Datatype.BINARY : 317 case Datatype.VARBINARY : 318 if(object instanceof byte[]) 319 return object; 320 String str = object.toString(); 321 return str.getBytes(); 322 323 case Datatype.CHARACTERLARGEOBJECT : 324 case Datatype.CHARLARGEOBJECT : 325 case Datatype.CLOB : 326 case Datatype.LONGVARCHAR : 327 if(object instanceof Clob) 328 return object; 329 throw new DException("DSE773", new Object []{object,StaticClass.getDataTypeName(databaseType)}); 330 331 case Datatype.CHAR : 332 case Datatype.CHARACTER : 333 case Datatype.CHARACTERVARYING : 334 case Datatype.CHARVARYING : 335 case Datatype.VARCHAR : 336 if(object instanceof byte[]) 337 object = new String ((byte[])object); 338 String str1 = ""+object; 339 return str1; 340 341 case Datatype.BIT : 342 case Datatype.BITVARYING : 343 if(object instanceof Boolean ) 344 object = ((Boolean )object).booleanValue() ? "1" : "0"; 345 String str11 = ""+object; 346 if(size == -1) 347 return str11; 348 if(str11.length() > size) 349 throw new SizeMisMatchException("DSE773",new Object []{object,StaticClass.getDataTypeName(databaseType)}); 350 return str11; 351 352 case Datatype.DOUBLE : 353 case Datatype.DOUBLEPRECISION : 354 case Datatype.FLOAT : 355 if(object instanceof Double ) 356 return object ; 357 if(object instanceof BigDecimal ) 358 return new Double (((BigDecimal )object).doubleValue()); 359 if( object instanceof Boolean ) 360 return Boolean.TRUE.equals(object) ? new Double (1d) : new Double (0d); 361 if(object instanceof byte[]) 362 object = new String ((byte[])object); 363 try{ 364 return new Double (""+object); 365 }catch(RuntimeException re){ 366 throw new DException("DSE773", new Object []{object,StaticClass.getDataTypeName(databaseType)}); 367 } 368 369 case Datatype.INT : 370 case Datatype.INTEGER : 371 if(object instanceof Integer ) 372 return object ; 373 if(object instanceof BigDecimal ) 374 return new Integer (((BigDecimal )object).intValue()); 375 if( object instanceof Boolean ) 376 return Boolean.TRUE.equals(object) ? new Integer (1) : new Integer (0); 377 if(object instanceof byte[]) 378 object = new String ((byte[])object); 379 try{ 380 Double db = new Double (""+object); 381 return new Integer (db.intValue()); 382 }catch(RuntimeException re){ 383 throw new DException("DSE773", new Object []{object,StaticClass.getDataTypeName(databaseType)}); 384 } 385 386 case Datatype.REAL : 387 if(object instanceof Float ) 388 return object ; 389 if(object instanceof BigDecimal ) 390 return new Float (((BigDecimal )object).floatValue()); 391 if( object instanceof Boolean ) 392 return Boolean.TRUE.equals(object) ? new Float (1f) : new Float (0f); 393 if(object instanceof byte[]) 394 object = new String ((byte[])object); 395 try{ 396 Double db = new Double (""+object); 397 return new Float (db.floatValue()); 398 }catch(RuntimeException re){ 399 throw new DException("DSE773", new Object []{object,StaticClass.getDataTypeName(databaseType)}); 400 } 401 402 case Datatype.SHORT : 403 case Datatype.SMALLINT : 404 if(object instanceof Short ) 405 return object ; 406 if(object instanceof BigDecimal ) 407 return new Short (((BigDecimal )object).shortValue()); 408 if( object instanceof Boolean ) 409 return Boolean.TRUE.equals(object) ? new Short ("1") : new Short ("0"); 410 if(object instanceof byte[]) 411 object = new String ((byte[])object); 412 try{ 413 Double db = new Double (""+object); 414 return new Short (db.shortValue()); 415 }catch(RuntimeException re){ 416 throw new DException("DSE773", new Object []{object,StaticClass.getDataTypeName(databaseType)}); 417 } 418 419 case Datatype.DATE : 420 if(object instanceof Date) 421 return ((Date)object); 422 if(object instanceof java.sql.Timestamp ) 423 return new com.daffodilwoods.daffodildb.utils.DBDate(((Timestamp)object).getTime()); 424 if(object instanceof byte[]) 425 object = new String ((byte[])object); 426 if(object instanceof String ) 427 return Date.valueOf((String )object); 428 throw new DException("DSE773", new Object []{object,StaticClass.getDataTypeName(databaseType)}); 429 430 case Datatype.TIME : 431 if(object instanceof java.sql.Time ) 432 return ((Time)object); 433 if(object instanceof java.sql.Timestamp ) 434 return new Time(((Timestamp)object).getTime()); 435 if(object instanceof java.sql.Date ) 436 return new Time(((Date)object).getTime()); 437 if(object instanceof byte[]) 438 object = new String ((byte[])object); 439 if(object instanceof String ) 440 return Time.valueOf((String )object); 441 throw new DException("DSE773", new Object []{object,StaticClass.getDataTypeName(databaseType)}); 442 443 case Datatype.TIMESTAMP : 444 if(object instanceof Date) 445 return new Timestamp(((Date)object).getTime()); 446 if(object instanceof Time) 447 return new Timestamp(((Time)object).getTime()); 448 if(object instanceof java.sql.Timestamp ) 449 return (Timestamp)object; 450 if(object instanceof byte[]) 451 object = new String ((byte[])object); 452 if(object instanceof String ) 453 return Timestamp.valueOf((String )object); 454 throw new DException("DSE773", new Object []{object,StaticClass.getDataTypeName(databaseType)}); 455 456 case Datatype.ARRAY : 457 case Datatype.MODULE : 458 case Datatype.RECORD : 459 case Datatype.RECORDSET : 460 case Datatype.REF : 461 case Datatype.STRUCT : 462 case Datatype.SUM : 463 case Datatype.TIME_WITH_TIMEZONE : 464 case Datatype.TIMESTAMP_WITH_TIMEZONE : 465 throw new DException("DSE1133", new Object []{new Integer (databaseType)}); 466 467 default : 468 throw new DException("DSE1021", new Object []{new Integer (databaseType)}); 469 } 470 } 471 472 473 public static String getDataBaseTypeName(int databaseType) throws DException { 474 switch (databaseType){ 475 case Datatype.CHARACTER : return "CHARACTER"; 476 case Datatype.INTEGER : return "INTEGER"; 477 case Datatype.LONG : return "LONG"; 478 case Datatype.SHORT : return "SHORT"; 479 case Datatype.BOOLEAN : return "BOOLEAN"; 480 case Datatype.FLOAT : return "FLOAT"; 481 case Datatype.DOUBLE : return "DOUBLE"; 482 case Datatype.DATE : return "DATE"; 483 case Datatype.MODULE : return "MODULE"; 484 case Datatype.SUM : return "SUM"; 485 case Datatype.ARRAY : return "ARRAY"; 486 case Datatype.RECORD : return "RECORD"; 487 case Datatype.RECORDSET : return "RECORDSET"; 488 case Datatype.BIGDECIMAL : return "BIGDECIMAL"; 489 case Datatype.TIME : return "TIME"; 490 case Datatype.TIMESTAMP : return "TIMESTAMP"; 491 case Datatype.BYTE : return "BYTE"; 492 case Datatype.BLOB : return "BLOB"; 493 case Datatype.CLOB : return "CLOB"; 494 case Datatype.REF : return "REF"; 495 case Datatype.STRUCT : return "STRUCT"; 496 497 case Datatype.VARCHAR : return "VARCHAR"; 498 case Datatype.CHAR : return "CHAR"; 499 case Datatype.CHARACTERVARYING :return "CHARACTER VARYING"; 500 case Datatype.CHARVARYING : return "CHAR VARYING"; 501 case Datatype.CHARACTERLARGEOBJECT : return "CHARACTER LARGE OBJECT"; 502 case Datatype.CHARLARGEOBJECT : return "CHAR LARGE OBJECT"; 503 case Datatype.BINARYLARGEOBJECT : return "BINARY LARGE OBJECT"; 504 case Datatype.BIT : return "BIT"; 505 case Datatype.BITVARYING : return "BIT VARYING"; 506 case Datatype.NUMERIC : return "NUMERIC"; 507 case Datatype.DECIMAL : return "DECIMAL"; 508 case Datatype.DEC : return "DEC"; 509 case Datatype.INT : return "INT"; 510 case Datatype.REAL : return "REAL"; 511 case Datatype.DOUBLEPRECISION : return "DOUBLE PRECISION"; 512 case Datatype.SMALLINT : return "SMALLINT"; 513 514 case Datatype.TINYINT : return "TINYINT"; 515 case Datatype.BIGINT : return "BIGINT"; 516 case Datatype.LONGVARCHAR : return "LONG VARCHAR"; 517 case Datatype.LONGVARBINARY : return "LONG VARBINARY"; 518 case Datatype.VARBINARY : return "VARBINARY"; 519 case Datatype.BINARY : return "BINARY"; 520 } 521 throw new IllegalColumnPropertyException("DSE1168", new Object []{"","Type",new Integer (databaseType)}); 522 } 523 524 } 525 | Popular Tags |