1 23 24 package org.xquark.jdbc.typing; 25 26 import java.sql.Types ; 27 28 29 38 39 public class DbType implements Cloneable 40 { 41 42 private static final String RCSRevision = "$Revision: 1.2 $"; 43 private static final String RCSName = "$Name: $"; 44 45 public static final int ORACLE_ROWID = 111; 46 public static final int BOOLEAN = 16; 48 49 50 private int jdbcType; 51 52 53 private long length = 0; 54 55 56 private int scale = 0; 57 58 59 private String nativeType; 60 61 62 private boolean isLongType = false; 63 64 65 private boolean isLOB = false; 66 67 public DbType() {} 68 69 public DbType(int jdbcType) { 70 this.jdbcType = jdbcType; 71 } 72 73 public DbType(TypeMap.SQLType sqlType) { 74 this(sqlType.getJDBCType()); 75 setNativeType(sqlType.getNativeTypeName()); 76 } 77 78 83 public DbType(int jdbcType, int length, int scale) { 84 this(jdbcType, length); 85 this.scale = scale; 86 } 87 88 public DbType(int jdbcType, int size) { 89 this.jdbcType = jdbcType; 90 this.length = size; 91 } 92 93 public DbType(int length, int scale, TypeMap.SQLType sqlType) { 94 this(sqlType); 95 if (length == 0) 97 this.length = sqlType.getPrecision(); 98 else 99 this.length = length; 100 this.scale = scale; 101 } 102 103 public Object clone () throws CloneNotSupportedException 104 { 105 return super.clone(); 106 }; 107 108 public boolean isCompatibleTo(DbType type) 109 { 110 return true; 111 } 112 113 118 public String getName() { 119 return getNameFromCode(jdbcType); 120 } 121 122 public int getPrimitiveTypeCode() { 123 return jdbcType; 124 } 125 126 public boolean isPrimitive() { 127 return true; 128 } 129 130 public boolean isUdt() { 131 return false; 132 } 133 134 139 public int getJDBCType() { 140 return jdbcType; 141 } 142 143 148 public void setJDBCType(int aCode) { 149 jdbcType = aCode; 150 } 151 152 public String name() { 153 return getNameFromCode(jdbcType); 154 } 155 156 161 protected static String getNameFromCode(int code) { 162 String retVal = null; 163 switch (code) { 164 case Types.ARRAY: 165 retVal = "ARRAY"; 166 break; 167 case Types.BIGINT: 168 retVal = "BIGINT"; 169 break; 170 case Types.BINARY: 171 retVal = "BINARY"; 172 break; 173 case Types.BIT: 174 retVal = "BIT"; 175 break; 176 case Types.BLOB: 177 retVal = "BLOB"; 178 break; 179 case Types.CHAR: 180 retVal = "CHAR"; 181 break; 182 case Types.CLOB: 183 retVal = "CLOB"; 184 break; 185 case Types.DATE: 186 retVal = "DATE"; 187 break; 188 case Types.DECIMAL: 189 retVal = "DECIMAL"; 190 break; 191 case Types.DISTINCT: 192 retVal = "DISTINCT"; 193 break; 194 case Types.DOUBLE: 195 retVal = "DOUBLE"; 196 break; 197 case Types.FLOAT: 198 retVal = "FLOAT"; 199 break; 200 case Types.INTEGER: 201 retVal = "INTEGER"; 202 break; 203 case Types.JAVA_OBJECT: 204 retVal = "JAVA_OBJECT"; 205 break; 206 case Types.LONGVARBINARY: 207 retVal = "LONGVARBINARY"; 208 break; 209 case Types.LONGVARCHAR: 210 retVal = "LONGVARCHAR"; 211 break; 212 case Types.NULL: 213 retVal = "NULL"; 214 break; 215 case Types.NUMERIC: 216 retVal = "NUMERIC"; 217 break; 218 case Types.REAL: 219 retVal = "REAL"; 220 break; 221 case Types.SMALLINT: 222 retVal = "SMALLINT"; 223 break; 224 case Types.STRUCT: 225 retVal = "STRUCT"; 226 break; 227 case Types.TIME: 228 retVal = "TIME"; 229 break; 230 case Types.TIMESTAMP: 231 retVal = "TIMESTAMP"; 232 break; 233 case Types.TINYINT: 234 retVal = "TINYINT"; 235 break; 236 case Types.VARBINARY: 237 retVal = "VARBINARY"; 238 break; 239 case Types.VARCHAR: 240 retVal = "VARCHAR"; 241 break; 242 case ORACLE_ROWID: 243 retVal = "ORACLE_ROWID"; 244 break; 245 default: 246 break; 247 } 248 249 return retVal; 250 } 251 252 public boolean isNumeric() { 253 return Types.INTEGER == jdbcType || 254 Types.DECIMAL == jdbcType || 255 Types.DOUBLE == jdbcType || 256 Types.FLOAT == jdbcType || 257 Types.REAL == jdbcType || 258 Types.NUMERIC == jdbcType || 259 Types.SMALLINT == jdbcType || 260 Types.BIGINT == jdbcType || 261 Types.TINYINT == jdbcType ; 262 } 263 264 public boolean isDecimal() { 265 return Types.DECIMAL == jdbcType; 266 } 267 268 public boolean isInteger() { 269 return Types.INTEGER == jdbcType || 270 Types.SMALLINT== jdbcType || 271 Types.BIGINT== jdbcType || 272 Types.TINYINT == jdbcType ; 273 } 274 275 public boolean isString() { 276 return Types.CHAR == jdbcType || 277 Types.CLOB == jdbcType || 278 Types.VARCHAR == jdbcType || 279 Types.LONGVARCHAR == jdbcType ; 280 } 281 282 public boolean isDataTime() { 283 return Types.DATE == jdbcType || 284 Types.TIMESTAMP == jdbcType || 285 Types.TIME == jdbcType ; 286 } 287 288 public boolean isNull() { 289 return Types.NULL == jdbcType; 290 } 291 292 public String pprint() { 293 return DbType.getNameFromCode(jdbcType) + "(" + Integer.toString(jdbcType) + ")"; 294 } 295 public long getLength() { 296 return length; 297 } 298 299 public void setLength(long length) { 300 this.length = length; 301 } 302 303 public int getScale() { 304 return scale; 305 } 306 307 public void setScale(int scale) { 308 this.scale = scale; 309 } 310 311 public boolean isLOB() { 312 return isLOB; 313 } 314 315 public void setLOB(boolean isLOB) { 316 this.isLOB = isLOB; 317 } 318 319 public boolean isLongType() { 320 return isLongType; 321 } 322 323 public void setLongType(boolean isLongType) { 324 this.isLongType = isLongType; 325 } 326 327 public String getNativeType() { 328 return nativeType; 329 } 330 331 public void setNativeType(String nativeType) { 332 this.nativeType = nativeType; 333 } 334 335 } 336 | Popular Tags |