1 10 11 package com.triactive.jdo.store; 12 13 import java.io.PrintWriter ; 14 import java.io.StringWriter ; 15 import java.sql.DatabaseMetaData ; 16 import java.sql.ResultSet ; 17 import java.sql.SQLException ; 18 import java.sql.Types ; 19 import javax.jdo.JDOFatalDataStoreException; 20 21 22 39 40 class TypeInfo 41 { 42 47 public static final short Types_BOOLEAN = 16; 48 49 52 public String typeName; 53 54 57 public short dataType; 58 59 62 public int precision; 63 64 68 public String literalPrefix; 69 70 74 public String literalSuffix; 75 76 79 public String createParams; 80 81 88 public int nullable; 89 90 94 public boolean caseSensitive; 95 96 105 public short searchable; 106 107 110 public boolean unsignedAttribute; 111 112 116 public boolean fixedPrecScale; 117 118 122 public boolean autoIncrement; 123 124 127 public String localTypeName; 128 129 132 public short minimumScale; 133 134 137 public short maximumScale; 138 139 142 public int numPrecRadix; 143 144 private int hash = 0; 145 146 147 161 162 public TypeInfo(ResultSet rs) throws JDOFatalDataStoreException 163 { 164 try 165 { 166 typeName = rs.getString(1); 167 dataType = rs.getShort(2); 168 precision = (int)rs.getLong(3); 169 literalPrefix = rs.getString(4); 170 literalSuffix = rs.getString(5); 171 createParams = rs.getString(6); 172 nullable = rs.getInt(7); 173 caseSensitive = rs.getBoolean(8); 174 searchable = rs.getShort(9); 175 unsignedAttribute = rs.getBoolean(10); 176 fixedPrecScale = rs.getBoolean(11); 177 autoIncrement = rs.getBoolean(12); 178 localTypeName = rs.getString(13); 179 minimumScale = rs.getShort(14); 180 maximumScale = rs.getShort(15); 181 numPrecRadix = rs.getInt(18); 182 } 183 catch (SQLException e) 184 { 185 throw new JDOFatalDataStoreException("Can't read JDBC metadata from result set", e); 186 } 187 } 188 189 190 196 197 public TypeInfo(String typeName, 198 short dataType, 199 int precision, 200 String literalPrefix, 201 String literalSuffix, 202 String createParams, 203 int nullable, 204 boolean caseSensitive, 205 short searchable, 206 boolean unsignedAttribute, 207 boolean fixedPrecScale, 208 boolean autoIncrement, 209 String localTypeName, 210 short minimumScale, 211 short maximumScale, 212 int numPrecRadix) 213 { 214 this.typeName = typeName; 215 this.dataType = dataType; 216 this.precision = precision; 217 this.literalPrefix = literalPrefix; 218 this.literalSuffix = literalSuffix; 219 this.createParams = createParams; 220 this.nullable = nullable; 221 this.caseSensitive = caseSensitive; 222 this.searchable = searchable; 223 this.unsignedAttribute = unsignedAttribute; 224 this.fixedPrecScale = fixedPrecScale; 225 this.autoIncrement = autoIncrement; 226 this.localTypeName = localTypeName; 227 this.minimumScale = minimumScale; 228 this.maximumScale = maximumScale; 229 this.numPrecRadix = numPrecRadix; 230 } 231 232 233 237 238 public boolean isCompatibleWith(ColumnInfo ci) 239 { 240 return areCompatibleTypes(dataType, ci.dataType); 241 } 242 243 244 254 255 public boolean equals(Object obj) 256 { 257 if (obj == this) 258 return true; 259 260 if (!(obj instanceof TypeInfo)) 261 return false; 262 263 TypeInfo ti = (TypeInfo)obj; 264 265 return typeName.equals(ti.typeName) && dataType == ti.dataType; 266 } 267 268 269 274 275 public int hashCode() 276 { 277 if (hash == 0) 278 hash = typeName.hashCode() ^ dataType; 279 280 return hash; 281 } 282 283 284 289 290 public String toString() 291 { 292 StringWriter sw = new StringWriter (); 293 PrintWriter pw = new PrintWriter (sw); 294 295 pw.println(this.getClass().getName()); 296 pw.print(" typeName = "); pw.println(typeName); 297 pw.print(" dataType = "); pw.println(getJDBCTypeName(dataType)); 298 pw.print(" precision = "); pw.println(precision); 299 pw.print(" literalPrefix = "); pw.println(literalPrefix); 300 pw.print(" literalSuffix = "); pw.println(literalSuffix); 301 pw.print(" createParams = "); pw.println(createParams); 302 pw.print(" nullable = "); pw.println(nullable); 303 pw.print(" caseSensitive = "); pw.println(caseSensitive); 304 pw.print(" searchable = "); pw.println(searchable); 305 pw.print(" searchable = "); pw.println(searchable); 306 pw.print(" unsignedAttribute = "); pw.println(unsignedAttribute); 307 pw.print(" fixedPrecScale = "); pw.println(fixedPrecScale); 308 pw.print(" autoIncrement = "); pw.println(autoIncrement); 309 pw.print(" localTypeName = "); pw.println(localTypeName); 310 pw.print(" minimumScale = "); pw.println(minimumScale); 311 pw.print(" maximumScale = "); pw.println(maximumScale); 312 pw.print(" numPrecRadix = "); pw.println(numPrecRadix); 313 pw.close(); 314 315 return sw.toString(); 316 } 317 318 319 332 333 public static String getJDBCTypeName(int type) 334 { 335 switch (type) 336 { 337 case Types.BIT: 338 return "Types.BIT"; 339 case Types_BOOLEAN: 340 return "Types.BOOLEAN"; 341 case Types.TINYINT: 342 return "Types.TINYINT"; 343 case Types.SMALLINT: 344 return "Types.SMALLINT"; 345 case Types.INTEGER: 346 return "Types.INTEGER"; 347 case Types.BIGINT: 348 return "Types.BIGINT"; 349 case Types.FLOAT: 350 return "Types.FLOAT"; 351 case Types.REAL: 352 return "Types.REAL"; 353 case Types.DOUBLE: 354 return "Types.DOUBLE"; 355 case Types.NUMERIC: 356 return "Types.NUMERIC"; 357 case Types.DECIMAL: 358 return "Types.DECIMAL"; 359 case Types.CHAR: 360 return "Types.CHAR"; 361 case Types.VARCHAR: 362 return "Types.VARCHAR"; 363 case Types.LONGVARCHAR: 364 return "Types.LONGVARCHAR"; 365 case Types.DATE: 366 return "Types.DATE"; 367 case Types.TIME: 368 return "Types.TIME"; 369 case Types.TIMESTAMP: 370 return "Types.TIMESTAMP"; 371 case Types.BINARY: 372 return "Types.BINARY"; 373 case Types.VARBINARY: 374 return "Types.VARBINARY"; 375 case Types.LONGVARBINARY: 376 return "Types.LONGVARBINARY"; 377 case Types.NULL: 378 return "Types.NULL"; 379 case Types.OTHER: 380 return "Types.OTHER"; 381 case Types.JAVA_OBJECT: 382 return "Types.JAVA_OBJECT"; 383 case Types.DISTINCT: 384 return "Types.DISTINCT"; 385 case Types.STRUCT: 386 return "Types.STRUCT"; 387 case Types.ARRAY: 388 return "Types.ARRAY"; 389 case Types.BLOB: 390 return "Types.BLOB"; 391 case Types.CLOB: 392 return "Types.CLOB"; 393 case Types.REF: 394 return "Types.REF"; 395 default: 396 return "Unrecognized type (" + type + ")"; 397 } 398 } 399 400 401 public static boolean areCompatibleTypes(int expected, int actual) 402 { 403 switch (expected) 404 { 405 case Types.BIT: 406 case Types_BOOLEAN: 407 return isBooleanType(actual); 408 409 case Types.TINYINT: 410 case Types.SMALLINT: 411 case Types.INTEGER: 412 case Types.BIGINT: 413 return isIntegerType(actual); 414 415 case Types.FLOAT: 416 case Types.REAL: 417 case Types.DOUBLE: 418 return isFloatingType(actual); 419 420 case Types.NUMERIC: 421 case Types.DECIMAL: 422 return isNumericType(actual); 423 424 case Types.CHAR: 425 case Types.VARCHAR: 426 case Types.LONGVARCHAR: 427 case Types.DATE: 428 case Types.TIME: 429 case Types.TIMESTAMP: 430 case Types.BINARY: 431 case Types.VARBINARY: 432 case Types.LONGVARBINARY: 433 case Types.NULL: 434 case Types.OTHER: 435 case Types.JAVA_OBJECT: 436 case Types.DISTINCT: 437 case Types.STRUCT: 438 case Types.ARRAY: 439 case Types.BLOB: 440 case Types.CLOB: 441 case Types.REF: 442 default: 443 return expected == actual; 444 } 445 } 446 447 private static boolean isBooleanType(int type) 448 { 449 switch (type) 450 { 451 case Types.BIT: 452 case Types_BOOLEAN: 453 return true; 454 455 default: 456 return false; 457 } 458 } 459 460 private static boolean isIntegerType(int type) 461 { 462 switch (type) 463 { 464 case Types.TINYINT: 465 case Types.SMALLINT: 466 case Types.INTEGER: 467 case Types.BIGINT: 468 return true; 469 470 default: 471 return isNumericType(type); 472 } 473 } 474 475 private static boolean isFloatingType(int type) 476 { 477 switch (type) 478 { 479 case Types.FLOAT: 480 case Types.REAL: 481 case Types.DOUBLE: 482 return true; 483 484 default: 485 return isNumericType(type); 486 } 487 } 488 489 private static boolean isNumericType(int type) 490 { 491 switch (type) 492 { 493 case Types.NUMERIC: 494 case Types.DECIMAL: 495 return true; 496 497 default: 498 return false; 499 } 500 } 501 } 502 | Popular Tags |