1 24 25 package com.mckoi.database; 26 27 import java.io.*; 28 import com.mckoi.database.global.ColumnDescription; 29 import com.mckoi.database.global.SQLTypes; 30 31 36 37 public class DataTableColumnDef { 38 39 44 private byte[] constraints_format = new byte[16]; 45 46 49 private String name; 50 51 54 private int sql_type; 55 56 60 private int db_type; 61 62 65 private int size; 66 67 70 private int scale; 71 72 77 private String locale_str = ""; 78 79 83 private int str_strength; 84 85 89 private int str_decomposition; 90 91 94 private String default_expression_string; 95 96 101 106 private String foreign_key = ""; 107 108 111 private String index_desc = ""; 112 113 118 private String class_constraint = ""; 119 120 123 private Class constraining_class; 124 125 128 public TType type; 129 130 131 132 135 public DataTableColumnDef() { 136 } 137 138 141 public DataTableColumnDef(DataTableColumnDef column_def) { 142 System.arraycopy(column_def.constraints_format, 0, 143 constraints_format, 0, constraints_format.length); 144 name = column_def.name; 145 sql_type = column_def.sql_type; 146 db_type = column_def.db_type; 147 size = column_def.size; 148 scale = column_def.scale; 149 locale_str = column_def.locale_str; 150 str_strength = column_def.str_strength; 151 str_decomposition = column_def.str_decomposition; 152 if (column_def.default_expression_string != null) { 153 default_expression_string = column_def.default_expression_string; 154 } 156 foreign_key = column_def.foreign_key; 157 index_desc = column_def.index_desc; 158 class_constraint = column_def.class_constraint; 159 type = column_def.type; 160 } 161 162 164 public void setName(String name) { 165 this.name = name; 166 } 167 168 public void setNotNull(boolean status) { 169 constraints_format[0] = (byte) (status ? 1 : 0); 170 } 171 172 180 public void setSQLType(int sql_type) { 181 this.sql_type = sql_type; 182 if (sql_type == SQLTypes.BIT || 183 sql_type == SQLTypes.BOOLEAN) { 184 db_type = com.mckoi.database.global.Types.DB_BOOLEAN; 185 } 186 else if (sql_type == SQLTypes.TINYINT || 187 sql_type == SQLTypes.SMALLINT || 188 sql_type == SQLTypes.INTEGER || 189 sql_type == SQLTypes.BIGINT || 190 sql_type == SQLTypes.FLOAT || 191 sql_type == SQLTypes.REAL || 192 sql_type == SQLTypes.DOUBLE || 193 sql_type == SQLTypes.NUMERIC || 194 sql_type == SQLTypes.DECIMAL) { 195 db_type = com.mckoi.database.global.Types.DB_NUMERIC; 196 } 197 else if (sql_type == SQLTypes.CHAR || 198 sql_type == SQLTypes.VARCHAR || 199 sql_type == SQLTypes.LONGVARCHAR) { 200 db_type = com.mckoi.database.global.Types.DB_STRING; 201 } 202 else if (sql_type == SQLTypes.DATE || 203 sql_type == SQLTypes.TIME || 204 sql_type == SQLTypes.TIMESTAMP) { 205 db_type = com.mckoi.database.global.Types.DB_TIME; 206 } 207 else if (sql_type == SQLTypes.BINARY || 208 sql_type == SQLTypes.VARBINARY || 209 sql_type == SQLTypes.LONGVARBINARY) { 210 db_type = com.mckoi.database.global.Types.DB_BLOB; 211 } 212 else if (sql_type == SQLTypes.JAVA_OBJECT) { 213 db_type = com.mckoi.database.global.Types.DB_OBJECT; 214 } 215 else { 216 db_type = com.mckoi.database.global.Types.DB_UNKNOWN; 217 } 218 } 219 220 public void setDBType(int db_type) { 221 this.db_type = db_type; 222 if (db_type == com.mckoi.database.global.Types.DB_NUMERIC) { 223 sql_type = SQLTypes.NUMERIC; 224 } 225 else if (db_type == com.mckoi.database.global.Types.DB_STRING) { 226 sql_type = SQLTypes.LONGVARCHAR; 227 } 228 else if (db_type == com.mckoi.database.global.Types.DB_BOOLEAN) { 229 sql_type = SQLTypes.BIT; 230 } 231 else if (db_type == com.mckoi.database.global.Types.DB_TIME) { 232 sql_type = SQLTypes.TIMESTAMP; 233 } 234 else if (db_type == com.mckoi.database.global.Types.DB_BLOB) { 235 sql_type = SQLTypes.LONGVARBINARY; 236 } 237 else if (db_type == com.mckoi.database.global.Types.DB_OBJECT) { 238 sql_type = SQLTypes.JAVA_OBJECT; 239 } 240 else { 241 throw new Error ("Unrecognised internal type."); 242 } 243 } 244 245 public void setSize(int size) { 246 this.size = size; 247 } 248 249 public void setScale(int scale) { 250 this.scale = scale; 251 } 252 253 public void setStringLocale(String locale_str, 254 int strength, int decomposition) { 255 if (locale_str == null) { 259 this.locale_str = ""; 260 } 261 else { 262 this.locale_str = locale_str; 263 this.str_strength = strength; 264 this.str_decomposition = decomposition; 265 } 266 } 267 268 public void setDefaultExpression(Expression expression) { 269 this.default_expression_string = new String (expression.text().toString()); 270 } 271 272 275 public void setForeignKey(String foreign_key) { 276 this.foreign_key = foreign_key; 277 } 278 279 283 public void setIndexScheme(String index_scheme) { 284 index_desc = index_scheme; 285 } 286 287 291 public void setClassConstraint(String class_constraint) { 292 this.class_constraint = class_constraint; 293 try { 294 if (class_constraint.endsWith("[]")) { 296 String array_class = 297 class_constraint.substring(0, class_constraint.length() - 2); 298 Class ac; 299 if (array_class.equals("boolean")) { 301 ac = boolean.class; 302 } 303 else if (array_class.equals("byte")) { 304 ac = byte.class; 305 } 306 else if (array_class.equals("char")) { 307 ac = char.class; 308 } 309 else if (array_class.equals("short")) { 310 ac = short.class; 311 } 312 else if (array_class.equals("int")) { 313 ac = int.class; 314 } 315 else if (array_class.equals("long")) { 316 ac = long.class; 317 } 318 else if (array_class.equals("float")) { 319 ac = float.class; 320 } 321 else if (array_class.equals("double")) { 322 ac = double.class; 323 } 324 else { 325 ac = Class.forName(array_class); 327 } 328 constraining_class = 330 java.lang.reflect.Array.newInstance(ac, 0).getClass(); 331 } 332 else { 333 constraining_class = Class.forName(class_constraint); 335 } 336 } 337 catch (ClassNotFoundException e) { 338 throw new Error ("Unable to resolve class: " + class_constraint); 339 } 340 } 341 342 348 public void setFromTType(TType type) { 349 setSQLType(type.getSQLType()); 350 if (type instanceof TStringType) { 351 TStringType str_type = (TStringType) type; 352 setSize(str_type.getMaximumSize()); 353 setStringLocale(str_type.getLocaleString(), 354 str_type.getStrength(), str_type.getDecomposition()); 355 } 356 else if (type instanceof TNumericType) { 357 TNumericType num_type = (TNumericType) type; 358 setSize(num_type.getSize()); 359 setScale(num_type.getScale()); 360 } 361 else if (type instanceof TBooleanType) { 362 } 365 else if (type instanceof TDateType) { 366 } 369 else if (type instanceof TNullType) { 370 } 372 else if (type instanceof TBinaryType) { 373 TBinaryType binary_type = (TBinaryType) type; 374 setSize(binary_type.getMaximumSize()); 375 } 376 else if (type instanceof TJavaObjectType) { 377 TJavaObjectType java_object_type = (TJavaObjectType) type; 378 setClassConstraint(java_object_type.getJavaClassTypeString()); 379 } 380 else { 381 throw new Error ("Don't know how to handle this type: " + 382 type.getClass()); 383 } 384 this.type = type; 385 386 } 387 388 392 public void initTTypeInfo() { 393 if (type == null) { 394 type = createTTypeFor(getSQLType(), getSize(), getScale(), 395 getLocaleString(), getStrength(), getDecomposition(), 396 getClassConstraint()); 397 } 398 } 399 400 401 403 public String getName() { 404 return name; 405 } 406 407 public boolean isNotNull() { 408 return constraints_format[0] != 0; 409 } 410 411 public int getSQLType() { 412 return sql_type; 413 } 414 415 418 public String getSQLTypeString() { 419 return sqlTypeToString(getSQLType()); 420 } 421 422 425 public String getDBTypeString() { 426 switch (getDBType()) { 427 case com.mckoi.database.global.Types.DB_NUMERIC: 428 return "DB_NUMERIC"; 429 case com.mckoi.database.global.Types.DB_STRING: 430 return "DB_STRING"; 431 case com.mckoi.database.global.Types.DB_BOOLEAN: 432 return "DB_BOOLEAN"; 433 case com.mckoi.database.global.Types.DB_TIME: 434 return "DB_TIME"; 435 case com.mckoi.database.global.Types.DB_BLOB: 436 return "DB_BLOB"; 437 case com.mckoi.database.global.Types.DB_OBJECT: 438 return "DB_OBJECT"; 439 default: 440 return "UNKNOWN(" + getDBType() + ")"; 441 } 442 } 443 444 447 public Class classType() { 448 return com.mckoi.database.global.TypeUtil.toClass(getDBType()); 449 } 450 451 public int getDBType() { 452 return db_type; 453 } 454 455 public int getSize() { 456 return size; 457 } 458 459 public int getScale() { 460 return scale; 461 } 462 463 public String getLocaleString() { 464 return locale_str; 465 } 466 467 public int getStrength() { 468 return str_strength; 469 } 470 471 public int getDecomposition() { 472 return str_decomposition; 473 } 474 475 public Expression getDefaultExpression(TransactionSystem system) { 476 if (default_expression_string == null) { 477 return null; 478 } 479 Expression exp = Expression.parse(default_expression_string); 480 return exp; 481 } 482 483 public String getDefaultExpressionString() { 484 return default_expression_string; 485 } 486 487 490 public String getForeignKey() { 491 return foreign_key; 492 } 493 494 498 public String getIndexScheme() { 499 if (index_desc.equals("")) { 500 return "InsertSearch"; 501 } 502 return index_desc; 503 } 504 505 508 public boolean isIndexableType() { 509 if (getDBType() == com.mckoi.database.global.Types.DB_BLOB || 510 getDBType() == com.mckoi.database.global.Types.DB_OBJECT) { 511 return false; 512 } 513 return true; 514 } 515 516 520 public String getClassConstraint() { 521 return class_constraint; 522 } 523 524 528 public Class getClassConstraintAsClass() { 529 return constraining_class; 530 } 531 532 535 public TType getTType() { 536 if (type == null) { 537 throw new Error ("'type' variable was not set."); 538 } 539 return type; 540 } 541 542 559 563 public ColumnDescription columnDescriptionValue(String column_name) { 564 ColumnDescription field = 565 new ColumnDescription(column_name, getDBType(), getSize(), isNotNull()); 566 field.setScale(getScale()); 567 field.setSQLType(getSQLType()); 568 569 return field; 570 } 571 572 575 public void dump(PrintStream out) { 576 out.print(getName()); 577 out.print("("); 578 out.print(getSQLTypeString()); 579 out.print(")"); 580 } 581 582 586 boolean compatIsUnique() { 587 return constraints_format[1] != 0; 588 } 589 590 boolean compatIsPrimaryKey() { 591 return constraints_format[2] != 0; 592 } 593 594 596 600 public static String sqlTypeToString(int sql_type) { 601 switch (sql_type) { 602 case SQLTypes.BIT: 603 return "BIT"; 604 case SQLTypes.TINYINT: 605 return "TINYINT"; 606 case SQLTypes.SMALLINT: 607 return "SMALLINT"; 608 case SQLTypes.INTEGER: 609 return "INTEGER"; 610 case SQLTypes.BIGINT: 611 return "BIGINT"; 612 case SQLTypes.FLOAT: 613 return "FLOAT"; 614 case SQLTypes.REAL: 615 return "REAL"; 616 case SQLTypes.DOUBLE: 617 return "DOUBLE"; 618 case SQLTypes.NUMERIC: 619 return "NUMERIC"; 620 case SQLTypes.DECIMAL: 621 return "DECIMAL"; 622 case SQLTypes.CHAR: 623 return "CHAR"; 624 case SQLTypes.VARCHAR: 625 return "VARCHAR"; 626 case SQLTypes.LONGVARCHAR: 627 return "LONGVARCHAR"; 628 case SQLTypes.DATE: 629 return "DATE"; 630 case SQLTypes.TIME: 631 return "TIME"; 632 case SQLTypes.TIMESTAMP: 633 return "TIMESTAMP"; 634 case SQLTypes.BINARY: 635 return "BINARY"; 636 case SQLTypes.VARBINARY: 637 return "VARBINARY"; 638 case SQLTypes.LONGVARBINARY: 639 return "LONGVARBINARY"; 640 case SQLTypes.JAVA_OBJECT: 641 return "JAVA_OBJECT"; 642 case SQLTypes.NULL: 643 return "NULL"; 644 case SQLTypes.BOOLEAN: 645 return "BOOLEAN"; 646 default: 647 return "UNKNOWN(" + sql_type + ")"; 648 } 649 } 650 651 655 static TType createTTypeFor(int sql_type, int size, int scale, 656 String locale, int str_strength, int str_decomposition, 657 String java_class) { 658 switch (sql_type) { 659 case (SQLTypes.BIT): 660 case (SQLTypes.BOOLEAN): 661 return TType.BOOLEAN_TYPE; 662 663 case (SQLTypes.TINYINT): 664 case (SQLTypes.SMALLINT): 665 case (SQLTypes.INTEGER): 666 case (SQLTypes.BIGINT): 667 case (SQLTypes.FLOAT): 668 case (SQLTypes.REAL): 669 case (SQLTypes.DOUBLE): 670 case (SQLTypes.NUMERIC): 671 case (SQLTypes.DECIMAL): 672 return new TNumericType(sql_type, size, scale); 673 674 case (SQLTypes.CHAR): 675 case (SQLTypes.VARCHAR): 676 case (SQLTypes.LONGVARCHAR): 677 case (SQLTypes.CLOB): 678 return new TStringType(sql_type, size, locale, 679 str_strength, str_decomposition); 680 681 case (SQLTypes.DATE): 682 case (SQLTypes.TIME): 683 case (SQLTypes.TIMESTAMP): 684 return new TDateType(sql_type); 685 686 case (SQLTypes.BINARY): 687 case (SQLTypes.VARBINARY): 688 case (SQLTypes.LONGVARBINARY): 689 case (SQLTypes.BLOB): 690 return new TBinaryType(sql_type, size); 691 692 case (SQLTypes.JAVA_OBJECT): 693 return new TJavaObjectType(java_class); 694 695 case (SQLTypes.ARRAY): 696 return TType.ARRAY_TYPE; 697 698 case (SQLTypes.NULL): 699 return TType.NULL_TYPE; 700 701 default: 702 throw new Error ("SQL type not recognized."); 703 } 704 } 705 706 710 public static DataTableColumnDef createNumericColumn(String name) { 711 DataTableColumnDef column = new DataTableColumnDef(); 712 column.setName(name); 713 column.setSQLType(java.sql.Types.NUMERIC); 714 column.initTTypeInfo(); 715 return column; 716 } 717 718 722 public static DataTableColumnDef createBooleanColumn(String name) { 723 DataTableColumnDef column = new DataTableColumnDef(); 724 column.setName(name); 725 column.setSQLType(java.sql.Types.BIT); 726 column.initTTypeInfo(); 727 return column; 728 } 729 730 734 public static DataTableColumnDef createStringColumn(String name) { 735 DataTableColumnDef column = new DataTableColumnDef(); 736 column.setName(name); 737 column.setSQLType(java.sql.Types.VARCHAR); 738 column.setSize(Integer.MAX_VALUE); 739 column.initTTypeInfo(); 740 return column; 741 } 742 743 747 public static DataTableColumnDef createBinaryColumn(String name) { 748 DataTableColumnDef column = new DataTableColumnDef(); 749 column.setName(name); 750 column.setSQLType(java.sql.Types.LONGVARBINARY); 751 column.setSize(Integer.MAX_VALUE); 752 column.setIndexScheme("BlindSearch"); 753 column.initTTypeInfo(); 754 return column; 755 } 756 757 758 759 761 764 void write(DataOutput out) throws IOException { 765 766 out.writeInt(2); 768 out.writeUTF(name); 769 out.writeInt(constraints_format.length); 770 out.write(constraints_format); 771 out.writeInt(sql_type); 772 out.writeInt(db_type); 773 out.writeInt(size); 774 out.writeInt(scale); 775 776 if (default_expression_string != null) { 777 out.writeBoolean(true); 778 out.writeUTF(default_expression_string); 779 } 781 else { 782 out.writeBoolean(false); 783 } 784 785 out.writeUTF(foreign_key); 786 out.writeUTF(index_desc); 787 out.writeUTF(class_constraint); 789 StringBuffer other = new StringBuffer (); 791 other.append("|"); 792 other.append(locale_str); 793 other.append("|"); 794 other.append(str_strength); 795 other.append("|"); 796 other.append(str_decomposition); 797 other.append("|"); 798 out.writeUTF(new String (other)); 800 } 801 802 805 static DataTableColumnDef read(DataInput in) throws IOException { 806 807 int ver = in.readInt(); 808 809 DataTableColumnDef cd = new DataTableColumnDef(); 810 cd.name = in.readUTF(); 811 int len = in.readInt(); 812 in.readFully(cd.constraints_format, 0, len); 813 cd.sql_type = in.readInt(); 814 cd.db_type = in.readInt(); 815 cd.size = in.readInt(); 816 cd.scale = in.readInt(); 817 818 boolean b = in.readBoolean(); 819 if (b) { 820 cd.default_expression_string = in.readUTF(); 821 } 823 cd.foreign_key = in.readUTF(); 824 cd.index_desc = in.readUTF(); 825 if (ver > 1) { 826 String cc = in.readUTF(); 827 if (!cc.equals("")) { 828 cd.setClassConstraint(cc); 829 } 830 } 831 else { 832 cd.class_constraint = ""; 833 } 834 835 String other = in.readUTF(); 837 if (other.length() > 0) { 838 if (other.startsWith("|")) { 839 int cur_i = 1; 841 int next_break = other.indexOf("|", cur_i); 842 cd.locale_str = other.substring(cur_i, next_break); 843 844 cur_i = next_break + 1; 845 next_break = other.indexOf("|", cur_i); 846 cd.str_strength = 847 Integer.parseInt(other.substring(cur_i, next_break)); 848 849 cur_i = next_break + 1; 850 next_break = other.indexOf("|", cur_i); 851 cd.str_decomposition = 852 Integer.parseInt(other.substring(cur_i, next_break)); 853 854 } 855 else { 856 throw new Error ("Incorrectly formatted DataTableColumnDef data."); 857 } 858 } 859 860 cd.initTTypeInfo(); 861 862 return cd; 863 } 864 865 } 866 | Popular Tags |