1 23 24 package org.xquark.jdbc.typing; 25 26 import java.sql.*; 27 import java.util.*; 28 29 33 public class TypeMap extends HashMap 34 { 35 private static final String RCSRevision = "$Revision: 1.2 $"; 36 private static final String RCSName = "$Name: $"; 37 38 private SQLType OTHER_Type = new SQLType(); 39 40 43 private Map canonicalTypesMap = new HashMap(); 44 45 public TypeMap() { 46 OTHER_Type.addJDBCType(Types.OTHER, null, true, false); 47 } 48 49 public void load(Connection connection) throws SQLException 50 { 51 ResultSet rs = connection.getMetaData().getTypeInfo(); 52 while (rs.next()) 53 addType(new SQLType(rs)); 54 rs.close(); 55 } 56 57 public SQLType createType() { 58 return new SQLType(); 59 } 60 61 public void addType(SQLType nativeType) 62 { 63 String key = nativeType.getNativeTypeName().toLowerCase(); 64 if (get(key) != null) 65 throw new RuntimeException ("Internal Error: duplicate entry in type XML data file."); 66 put(key, nativeType); 67 } 68 69 public SQLType getType(String nativeType) 70 { 71 SQLType ret = (SQLType)get(nativeType.toLowerCase()); 72 if (ret == null) 73 ret = OTHER_Type; return ret; 75 } 76 77 public SQLType getType(int JDBCType) 78 { 79 SQLType targetType = (SQLType)canonicalTypesMap.get(new Integer (JDBCType)); 80 if (targetType == null) 81 throw new RuntimeException ("The JDBC type " + JDBCType + " is not supported by the database JDBC driver."); 82 return targetType; 83 } 84 85 public String getNativeTypeName(int JDBCType) 86 { 87 return getType(JDBCType).getNativeTypeName(); 88 } 89 90 public long getSize(int JDBCType) 91 { 92 return getType(JDBCType).getPrecision(); 93 } 94 95 public boolean isUnsigned(int JDBCType) 96 { 97 return getType(JDBCType).isUnsigned(); 98 } 99 100 public boolean useGetString(int JDBCType) 101 { 102 return getType(JDBCType).useGetString(); 103 } 104 105 public String getTypeParam(int JDBCType) 106 { 107 return getType(JDBCType).getTypeParam(); 108 } 109 110 private class JDBCType implements Cloneable 114 { 115 private int JDBCType; 116 private String createParam; private boolean useGetString; 118 JDBCType(int type, String createParam, boolean useGetString) { 120 JDBCType = type; 121 this.createParam = createParam; 122 this.useGetString = useGetString; 123 } 124 127 String getCreateParam() { 128 return createParam; 129 } 130 131 134 int getJDBCType() { 135 return JDBCType; 136 } 137 138 public boolean useGetString() { 139 return useGetString; 140 } 141 public void setUseGetString(boolean useGetString) { 142 this.useGetString = useGetString; 143 } 144 145 } 146 public class SQLType implements Cloneable 147 { 148 private String nativeTypeName; 149 private List JDBCTypes = new ArrayList(); private long precision; private boolean unsigned; 152 private boolean indexable = true; 153 private boolean extraction = true; private boolean storage = true; private boolean autoFill = false; 156 159 public SQLType() {}; 160 161 SQLType(ResultSet rs) throws SQLException 162 { 163 nativeTypeName = rs.getString(1); 164 addJDBCType(rs.getShort(2), rs.getString(6), false, false); 165 precision = rs.getLong(3); unsigned = rs.getBoolean(10); 167 } 168 169 String getNativeTypeName() 170 { 171 return nativeTypeName; 172 } 173 String getTypeParam() 174 { 175 return ((JDBCType)JDBCTypes.get(0)).getCreateParam(); 176 } 177 int getJDBCType() 178 { 179 return ((JDBCType)JDBCTypes.get(0)).getJDBCType(); 180 } 181 boolean useGetString() 182 { 183 return ((JDBCType)JDBCTypes.get(0)).useGetString(); 184 } 185 long getPrecision() 186 { 187 return precision; 188 } 189 boolean isUnsigned() 190 { 191 return unsigned; 192 } 193 public void setNativeTypeName(String typeName) 194 { 195 nativeTypeName = typeName; 196 } 197 public void setPrecision(long prec) 198 { 199 precision = prec; 200 } 201 public void setUnsigned(boolean unsigned) 202 { 203 this.unsigned = unsigned; 204 } 205 public String toString() 206 { 207 return "[jdbc=" + JDBCTypes + ", sql=" + nativeTypeName + ", prec=" + precision 208 + ", unsigned=" + unsigned + "]"; 209 } 210 public Object clone() 211 { 212 try { 213 return super.clone(); 214 } 215 catch (CloneNotSupportedException e) { 216 return null; 217 } 218 } 219 223 public void addJDBCType(int jDBCType, String createParams, boolean principal, boolean useGetString) 224 { 225 Integer key = new Integer (jDBCType); 226 JDBCType typeInfo = new JDBCType(jDBCType, createParams, useGetString); 227 if (principal) { 228 JDBCTypes.add(0, typeInfo); 229 canonicalTypesMap.put(key, this); 230 } 231 else { 232 JDBCTypes.add(typeInfo); 233 if (canonicalTypesMap.get(key) == null) 234 canonicalTypesMap.put(key, this); 235 } 236 } 237 238 public void addJDBCType(String jDBCType, String createParams, boolean principal, boolean useGetString) 239 { 240 addJDBCType(parseJDBCType(jDBCType), createParams, principal, useGetString); 241 } 242 243 public boolean isAutoFill() { return autoFill;} 244 245 public void setAutoFill(boolean autoFill) { this.autoFill = autoFill;} 246 247 250 public boolean isExtraction() { 251 return extraction; 252 } 253 256 public void setExtraction(boolean extraction) { 257 this.extraction = extraction; 258 } 259 262 public boolean isIndexable() { 263 return indexable; 264 } 265 268 public void setIndexable(boolean indexable) { 269 this.indexable = indexable; 270 } 271 274 public boolean isStorage() { 275 return storage; 276 } 277 280 public void setStorage(boolean storage) { 281 this.storage = storage; 282 } 283 } 284 285 public static String getJDBCTypeName(int type) 286 { 287 switch (type) 288 { 289 case Types.ARRAY : 290 return "ARRAY"; 291 case Types.BIGINT : 292 return "BIGINT"; 293 case Types.BINARY : 294 return "BINARY"; 295 case Types.BIT : 296 return "BIT"; 297 case Types.BLOB : 298 return "BLOB"; 299 case DbType.BOOLEAN : 300 return "BOOLEAN"; 301 case Types.CHAR : 302 return "CHAR"; 303 case Types.CLOB : 304 return "CLOB"; 305 case Types.DATE : 306 return "DATE"; 307 case Types.DECIMAL : 308 return "DECIMAL"; 309 case Types.DISTINCT : 310 return "DISTINCT"; 311 case Types.DOUBLE : 312 return "DOUBLE"; 313 case Types.FLOAT : 314 return "FLOAT"; 315 case Types.INTEGER : 316 return "INTEGER"; 317 case Types.JAVA_OBJECT : 318 return "JAVA_OBJECT"; 319 case Types.LONGVARBINARY : 320 return "LONGVARBINARY"; 321 case Types.LONGVARCHAR : 322 return "LONGVARCHAR"; 323 case Types.NULL : 324 return "NULL"; 325 case Types.NUMERIC : 326 return "NUMERIC"; 327 case Types.OTHER : 328 return "OTHER"; 329 case Types.REAL : 330 return "REAL"; 331 case Types.REF : 332 return "REF"; 333 case Types.SMALLINT : 334 return "SMALLINT"; 335 case Types.STRUCT : 336 return "STRUCT"; 337 case Types.TIME : 338 return "TIME"; 339 case Types.TIMESTAMP : 340 return "TIMESTAMP"; 341 case Types.TINYINT : 342 return "TINYINT"; 343 case Types.VARBINARY : 344 return "VARBINARY"; 345 case Types.VARCHAR : 346 return "VARCHAR"; 347 case DbType.ORACLE_ROWID : 348 return "ROWID"; 349 default : 350 return ""; 351 } 352 353 } 354 private static final int[] dataTypes = { 355 Types.ARRAY, 356 Types.BIGINT, 357 Types.BINARY, 358 Types.BIT, 359 Types.BLOB, 360 DbType.BOOLEAN, 361 Types.CHAR, 362 Types.CLOB, 363 Types.DATE, 364 Types.DECIMAL, 365 Types.DISTINCT, 366 Types.DOUBLE, 367 Types.FLOAT, 368 Types.INTEGER, 369 Types.JAVA_OBJECT, 370 Types.LONGVARBINARY, 371 Types.LONGVARCHAR, 372 Types.NULL, 373 Types.NUMERIC, 374 Types.OTHER, 375 Types.REAL, 376 Types.REF, 377 DbType.ORACLE_ROWID, 378 Types.SMALLINT, 379 Types.STRUCT, 380 Types.TIME, 381 Types.TIMESTAMP, 382 Types.TINYINT, 383 Types.VARBINARY, 384 Types.VARCHAR 385 }; 386 387 388 private static final String [] dataTypeStrings = 389 { 390 "ARRAY", 391 "BIGINT", 392 "BINARY", 393 "BIT", 394 "BLOB", 395 "BOOLEAN", 396 "CHAR", 397 "CLOB", 398 "DATE", 399 "DECIMAL", 400 "DISTINCT", 401 "DOUBLE", 402 "FLOAT", 403 "INTEGER", 404 "JAVA_OBJECT", 405 "LONGVARBINARY", 406 "LONGVARCHAR", 407 "NULL", 408 "NUMERIC", 409 "OTHER", 410 "REAL", 411 "REF", 412 "ROWID", 413 "SMALLINT", 414 "STRUCT", 415 "TIME", 416 "TIMESTAMP", 417 "TINYINT", 418 "VARBINARY", 419 "VARCHAR" 420 }; 421 422 public static int parseJDBCType(String type) 423 { 424 int index = Arrays.binarySearch(dataTypeStrings, type); 425 if (index < 0) 426 throw new RuntimeException ("The " + type + " JDBC type used in tableSpec.xml is unknown."); 427 return dataTypes[index]; 428 } 429 } 430 | Popular Tags |