1 24 25 package com.mckoi.database.global; 26 27 import java.io.*; 28 import java.util.Date ; 29 import java.math.BigDecimal ; 30 31 40 41 public class ColumnDescription { 42 43 45 48 private String name; 49 50 53 private int type; 54 55 60 private int size; 61 62 67 private int scale = -1; 68 69 74 private int sql_type = -9332; 75 76 81 private boolean not_null; 82 83 87 private boolean unique; 88 89 96 private int unique_group; 97 98 101 public ColumnDescription(String name, int type, int size, boolean not_null) { 102 this.name = name; 103 this.type = type; 104 this.size = size; 105 this.not_null = not_null; 106 this.unique = false; 107 this.unique_group = -1; 108 } 109 110 public ColumnDescription(String name, int type, boolean not_null) { 111 this(name, type, -1, not_null); 112 } 113 114 public ColumnDescription(ColumnDescription cd) { 115 this(cd.getName(), cd.getType(), cd.getSize(), cd.isNotNull()); 116 if (cd.isUnique()) { 117 setUnique(); 118 } 119 setUniqueGroup(cd.getUniqueGroup()); 120 setScale(cd.getScale()); 121 setSQLType(cd.getSQLType()); 122 } 123 124 public ColumnDescription(String name, ColumnDescription cd) { 125 this(name, cd.getType(), cd.getSize(), cd.isNotNull()); 126 if (cd.isUnique()) { 127 setUnique(); 128 } 129 setUniqueGroup(cd.getUniqueGroup()); 130 setScale(cd.getScale()); 131 setSQLType(cd.getSQLType()); 132 } 133 134 139 public void setUnique() { 140 this.unique = true; 141 } 142 143 149 public void setUniqueGroup(int group) { 150 this.unique_group = group; 151 } 152 153 167 public void setSQLType(int sql_type) { 168 this.sql_type = sql_type; 169 } 170 171 174 public void setScale(int scale) { 175 this.scale = scale; 176 } 177 178 183 public String getName() { 184 return name; 185 } 186 187 191 public int getType() { 192 return type; 193 } 194 195 198 public boolean isNumericType() { 199 return (type == Types.DB_NUMERIC); 200 } 201 202 208 public int getSQLType() { 209 if (sql_type == -9332) { 210 if (type == Types.DB_NUMERIC) { 212 return SQLTypes.NUMERIC; 213 } 214 else if (type == Types.DB_STRING) { 215 return SQLTypes.LONGVARCHAR; 216 } 217 else if (type == Types.DB_BOOLEAN) { 218 return SQLTypes.BIT; 219 } 220 else if (type == Types.DB_TIME) { 221 return SQLTypes.TIMESTAMP; 222 } 223 else if (type == Types.DB_BLOB) { 224 return SQLTypes.LONGVARBINARY; 225 } 226 else if (type == Types.DB_OBJECT) { 227 return SQLTypes.JAVA_OBJECT; 228 } 229 else { 230 throw new Error ("Unrecognised internal type."); 231 } 232 } 233 return sql_type; 234 } 235 236 240 public String getSQLTypeName() { 241 int type = getSQLType(); 242 switch (type) { 243 case(SQLTypes.BIT): 244 return "BIT"; 245 case(SQLTypes.TINYINT): 246 return "TINYINT"; 247 case(SQLTypes.SMALLINT): 248 return "SMALLINT"; 249 case(SQLTypes.INTEGER): 250 return "INTEGER"; 251 case(SQLTypes.BIGINT): 252 return "BIGINT"; 253 case(SQLTypes.FLOAT): 254 return "FLOAT"; 255 case(SQLTypes.REAL): 256 return "REAL"; 257 case(SQLTypes.DOUBLE): 258 return "DOUBLE"; 259 case(SQLTypes.NUMERIC): 260 return "NUMERIC"; 261 case(SQLTypes.DECIMAL): 262 return "DECIMAL"; 263 case(SQLTypes.CHAR): 264 return "CHAR"; 265 case(SQLTypes.VARCHAR): 266 return "VARCHAR"; 267 case(SQLTypes.LONGVARCHAR): 268 return "LONGVARCHAR"; 269 case(SQLTypes.DATE): 270 return "DATE"; 271 case(SQLTypes.TIME): 272 return "TIME"; 273 case(SQLTypes.TIMESTAMP): 274 return "TIMESTAMP"; 275 case(SQLTypes.BINARY): 276 return "BINARY"; 277 case(SQLTypes.VARBINARY): 278 return "VARBINARY"; 279 case(SQLTypes.LONGVARBINARY): 280 return "LONGVARBINARY"; 281 case(SQLTypes.NULL): 282 return "NULL"; 283 case(SQLTypes.OTHER): 284 return "OTHER"; 285 case(SQLTypes.JAVA_OBJECT): 286 return "JAVA_OBJECT"; 287 case(SQLTypes.DISTINCT): 288 return "DISTINCT"; 289 case(SQLTypes.STRUCT): 290 return "STRUCT"; 291 case(SQLTypes.ARRAY): 292 return "ARRAY"; 293 case(SQLTypes.BLOB): 294 return "BLOB"; 295 case(SQLTypes.CLOB): 296 return "CLOB"; 297 case(SQLTypes.REF): 298 return "REF"; 299 case(SQLTypes.BOOLEAN): 300 return "BOOLEAN"; 301 default: 302 return null; 303 } 304 } 305 306 307 308 311 public Class classType() { 312 return TypeUtil.toClass(type); 313 } 329 330 334 public int getSize() { 335 return size; 336 } 337 338 341 public int getScale() { 342 return scale; 343 } 344 345 349 public boolean isNotNull() { 350 return not_null; 351 } 352 353 357 public boolean isUnique() { 358 return unique; 359 } 360 361 365 public int getUniqueGroup() { 366 return unique_group; 367 } 368 369 375 public boolean isQuantifiable() { 376 if (type == Types.DB_BLOB || 377 type == Types.DB_OBJECT) { 378 return false; 379 } 380 return true; 381 } 382 383 387 public boolean equals(Object ob) { 388 ColumnDescription cd = (ColumnDescription) ob; 389 return (name.equals(cd.name) && 390 type == cd.type && 391 size == cd.size && 392 not_null == cd.not_null && 393 unique == cd.unique && 394 unique_group == cd.unique_group); 395 } 396 397 401 public void writeTo(DataOutputStream out) throws IOException { 402 out.writeUTF(name); 403 out.writeInt(type); 404 out.writeInt(size); 405 out.writeBoolean(not_null); 406 out.writeBoolean(unique); 407 out.writeInt(unique_group); 408 out.writeInt(sql_type); 409 out.writeInt(scale); 410 } 411 412 416 public static ColumnDescription readFrom(DataInputStream in) 417 throws IOException { 418 String name = in.readUTF(); 419 int type = in.readInt(); 420 int size = in.readInt(); 421 boolean not_null = in.readBoolean(); 422 boolean unique = in.readBoolean(); 423 int unique_group = in.readInt(); 424 425 ColumnDescription col_desc = new ColumnDescription(name, 426 type, size, not_null); 427 if (unique) col_desc.setUnique(); 428 col_desc.setUniqueGroup(unique_group); 429 col_desc.setSQLType(in.readInt()); 430 col_desc.setScale(in.readInt()); 431 432 return col_desc; 433 } 434 435 } 436 | Popular Tags |