1 package org.apache.torque.engine.database.model; 2 3 18 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import org.apache.commons.lang.StringUtils; 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 import org.apache.torque.engine.EngineException; 27 import org.apache.torque.engine.platform.Platform; 28 import org.apache.torque.engine.platform.PlatformDefaultImpl; 29 import org.xml.sax.Attributes ; 30 31 42 public class Column 43 { 44 private static final SchemaType DEFAULT_TYPE = SchemaType.VARCHAR; 45 46 private static Log log = LogFactory.getLog(Column.class); 47 private String name; 48 private String description; 49 private Domain domain = new Domain(); 50 private String javaName = null; 51 private String javaNamingMethod; 52 private boolean isNotNull = false; 53 private boolean isProtected = false; 54 private String javaType; 55 private Table parentTable; 56 private int position; 57 private boolean isPrimaryKey = false; 58 private boolean isUnique = false; 59 private boolean isAutoIncrement = false; 60 private List referrers; 61 private String inheritanceType; 66 private boolean isInheritance; 67 private boolean isEnumeratedClasses; 68 private List inheritanceList; 69 private boolean needsTransactionInPostgres; 70 71 72 private boolean correctGetters = false; 73 74 75 private String inputValidator = null; 76 77 80 public Column() 81 { 82 this(null); 83 } 84 85 90 public Column(String name) 91 { 92 this.name = name; 93 } 94 95 101 public static String makeList(List columns) 102 { 103 Object obj = columns.get(0); 104 boolean isColumnList = (obj instanceof Column); 105 if (isColumnList) 106 { 107 obj = ((Column) obj).getName(); 108 } 109 StringBuffer buf = new StringBuffer ((String ) obj); 110 for (int i = 1; i < columns.size(); i++) 111 { 112 obj = columns.get(i); 113 if (isColumnList) 114 { 115 obj = ((Column) obj).getName(); 116 } 117 buf.append(", ").append(obj); 118 } 119 return buf.toString(); 120 } 121 122 125 public void loadFromXML(Attributes attrib) 126 { 127 String dom = attrib.getValue("domain"); 128 if (StringUtils.isNotEmpty(dom)) 129 { 130 domain = new Domain(getTable().getDatabase().getDomain(dom)); 131 } 132 else 133 { 134 domain = new Domain(getPlatform().getDomainForSchemaType(DEFAULT_TYPE)); 135 setType(attrib.getValue("type")); 136 } 137 name = attrib.getValue("name"); 139 140 javaName = attrib.getValue("javaName"); 141 javaType = attrib.getValue("javaType"); 142 if (javaType != null && javaType.length() == 0) 143 { 144 javaType = null; 145 } 146 147 javaNamingMethod = attrib.getValue("javaNamingMethod"); 150 if (javaNamingMethod == null) 151 { 152 javaNamingMethod 153 = parentTable.getDatabase().getDefaultJavaNamingMethod(); 154 } 155 156 String primaryKey = attrib.getValue("primaryKey"); 158 isPrimaryKey = ("true".equals(primaryKey)); 160 161 if ("true".equals(primaryKey)) 163 { 164 isNotNull = true; 165 } 166 167 String notNull = attrib.getValue("required"); 170 isNotNull = (notNull != null && "true".equals(notNull)); 171 172 String autoIncrement = attrib.getValue("autoIncrement"); 174 isAutoIncrement = ("true".equals(autoIncrement) 180 || (isPrimaryKey() 181 && IDMethod.NATIVE.equals(getTable().getIdMethod()) 182 && Platform.IDENTITY.equals( 183 getPlatform().getNativeIdMethod()) 184 && (!"false".equals(autoIncrement)))); 185 domain.replaceDefaultValue(attrib.getValue("default")); 187 188 domain.replaceSize(attrib.getValue("size")); 189 domain.replaceScale(attrib.getValue("scale")); 190 191 inheritanceType = attrib.getValue("inheritance"); 192 isInheritance = (inheritanceType != null 193 && !inheritanceType.equals("false")); 194 195 this.inputValidator = attrib.getValue("inputValidator"); 196 description = attrib.getValue("description"); 197 198 isProtected = ("true".equals(attrib.getValue("protected"))); 199 } 200 201 204 public String getFullyQualifiedName() 205 { 206 return (parentTable.getName() + '.' + name); 207 } 208 209 212 public String getName() 213 { 214 return name; 215 } 216 217 220 public void setName(String newName) 221 { 222 name = newName; 223 } 224 225 228 public String getDescription() 229 { 230 return description; 231 } 232 233 238 public void setDescription(String newDescription) 239 { 240 description = newDescription; 241 } 242 243 248 public String getJavaName() 249 { 250 if (javaName == null) 251 { 252 List inputs = new ArrayList (2); 253 inputs.add(name); 254 inputs.add(javaNamingMethod); 255 try 256 { 257 javaName = NameFactory.generateName(NameFactory.JAVA_GENERATOR, 258 inputs); 259 } 260 catch (EngineException e) 261 { 262 log.error(e, e); 263 } 264 } 265 return StringUtils.capitalize(javaName); 266 } 267 268 275 public String getGetterName() 276 { 277 if (("boolean".equalsIgnoreCase(getJavaNative()) && isCorrectGetters())) 278 { 279 return "is" + StringUtils.capitalize(getJavaName()); 280 } 281 else 282 { 283 return "get" + StringUtils.capitalize(getJavaName()); 284 } 285 } 286 287 294 public String getSetterName() 295 { 296 return "set" + StringUtils.capitalize(getJavaName()); 297 } 298 299 302 public String getUncapitalisedJavaName() 303 { 304 return StringUtils.uncapitalize(getJavaName()); 305 } 306 307 310 public void setJavaName(String javaName) 311 { 312 this.javaName = javaName; 313 } 314 315 318 public String getJavaType() 319 { 320 return javaType; 321 } 322 323 327 public int getPosition() 328 { 329 return position; 330 } 331 332 336 public void setPosition(int v) 337 { 338 this.position = v; 339 } 340 341 344 public void setTable(Table parent) 345 { 346 parentTable = parent; 347 } 348 349 352 public Table getTable() 353 { 354 return parentTable; 355 } 356 357 360 public String getTableName() 361 { 362 return parentTable.getName(); 363 } 364 365 369 public Inheritance addInheritance(Attributes attrib) 370 { 371 Inheritance inh = new Inheritance(); 372 inh.loadFromXML (attrib); 373 addInheritance(inh); 374 375 return inh; 376 } 377 378 382 public void addInheritance(Inheritance inh) 383 { 384 inh.setColumn(this); 385 if (inheritanceList == null) 386 { 387 inheritanceList = new ArrayList (); 388 isEnumeratedClasses = true; 389 } 390 inheritanceList.add(inh); 391 } 392 393 396 public List getChildren() 397 { 398 return inheritanceList; 399 } 400 401 405 public boolean isInheritance() 406 { 407 return isInheritance; 408 } 409 410 413 public boolean isEnumeratedClasses() 414 { 415 return isEnumeratedClasses; 416 } 417 418 421 public boolean isNotNull() 422 { 423 return isNotNull; 424 } 425 426 429 public void setNotNull(boolean status) 430 { 431 isNotNull = status; 432 } 433 434 439 public String getNotNullString() 440 { 441 return getTable().getDatabase().getPlatform() 442 .getNullString(this.isNotNull()); 443 } 444 445 448 public boolean isProtected() 449 { 450 return isProtected; 451 } 452 453 456 public void setProtected(boolean prot) 457 { 458 isProtected = prot; 459 } 460 461 464 public void setPrimaryKey(boolean pk) 465 { 466 isPrimaryKey = pk; 467 } 468 469 472 public boolean isPrimaryKey() 473 { 474 return isPrimaryKey; 475 } 476 477 480 public void setUnique (boolean u) 481 { 482 isUnique = u; 483 } 484 485 488 public boolean isUnique() 489 { 490 return isUnique; 491 } 492 493 496 public boolean requiresTransactionInPostgres() 497 { 498 return needsTransactionInPostgres; 499 } 500 501 504 public boolean isForeignKey() 505 { 506 return (getForeignKey() != null); 507 } 508 509 513 public boolean isMultipleFK() 514 { 515 ForeignKey fk = getForeignKey(); 516 if (fk != null) 517 { 518 Iterator fks = parentTable.getForeignKeys().iterator(); 519 while (fks.hasNext()) 520 { 521 ForeignKey key = (ForeignKey) fks.next(); 522 if (key.getForeignTableName().equals(fk.getForeignTableName()) 523 && !key.getLocalColumns().contains(this.name)) 524 { 525 return true; 526 } 527 } 528 } 529 530 return false; 532 } 533 534 538 public ForeignKey getForeignKey() 539 { 540 return parentTable.getForeignKey(this.name); 541 } 542 543 547 public String getRelatedTableName() 548 { 549 ForeignKey fk = getForeignKey(); 550 return (fk == null ? null : fk.getForeignTableName()); 551 } 552 553 557 public String getRelatedColumnName() 558 { 559 ForeignKey fk = getForeignKey(); 560 if (fk == null) 561 { 562 return null; 563 } 564 else 565 { 566 return fk.getLocalForeignMapping().get(this.name).toString(); 567 } 568 } 569 570 573 public void addReferrer(ForeignKey fk) 574 { 575 if (referrers == null) 576 { 577 referrers = new ArrayList (5); 578 } 579 referrers.add(fk); 580 } 581 582 585 public List getReferrers() 586 { 587 if (referrers == null) 588 { 589 referrers = new ArrayList (5); 590 } 591 return referrers; 592 } 593 594 597 public void setType(String torqueType) 598 { 599 SchemaType type = SchemaType.getEnum(torqueType); 600 if (type == null) 601 { 602 log.warn("SchemaType " + torqueType + " does not exist"); 603 type = Column.DEFAULT_TYPE; 604 } 605 setType(type); 606 } 607 608 611 public void setType(SchemaType torqueType) 612 { 613 domain = new Domain(getPlatform().getDomainForSchemaType(torqueType)); 614 if (torqueType.equals(SchemaType.VARBINARY) 615 || torqueType.equals(SchemaType.BLOB)) 616 { 617 needsTransactionInPostgres = true; 618 } 619 } 620 621 627 public Object getType() 628 { 629 return TypeMap.getJdbcType(domain.getType()).getName(); 630 } 631 632 635 public Object getTorqueType() 636 { 637 return domain.getType().getName(); 638 } 639 640 645 public boolean isString() 646 { 647 return (domain.getType().getName().indexOf ("CHAR") != -1); 648 } 649 650 654 public boolean needEscapedValue() 655 { 656 String torqueType = domain.getType().getName(); 657 return (torqueType != null) && (torqueType.equals("VARCHAR") 658 || torqueType.equals("LONGVARCHAR") 659 || torqueType.equals("DATE") 660 || torqueType.equals("DATETIME") 661 || torqueType.equals("TIMESTAMP") 662 || torqueType.equals("TIME") 663 || torqueType.equals("CHAR") 664 || torqueType.equals("CLOB")); 665 } 666 667 672 public String toString() 673 { 674 StringBuffer result = new StringBuffer (); 675 result.append(" <column name=\"").append(name).append('"'); 676 677 if (javaName != null) 678 { 679 result.append(" javaName=\"").append(javaName).append('"'); 680 } 681 682 if (isPrimaryKey) 683 { 684 result.append(" primaryKey=\"").append(isPrimaryKey).append('"'); 685 } 686 687 if (isNotNull) 688 { 689 result.append(" required=\"true\""); 690 } 691 else 692 { 693 result.append(" required=\"false\""); 694 } 695 696 result.append(" type=\"").append(domain.getType().getName()).append('"'); 697 698 if (domain.getSize() != null) 699 { 700 result.append(" size=\"").append(domain.getSize()).append('"'); 701 } 702 703 if (domain.getScale() != null) 704 { 705 result.append(" scale=\"").append(domain.getScale()).append('"'); 706 } 707 708 if (domain.getDefaultValue() != null) 709 { 710 result.append(" default=\"").append(domain.getDefaultValue()).append('"'); 711 } 712 713 if (isInheritance()) 714 { 715 result.append(" inheritance=\"").append(inheritanceType) 716 .append('"'); 717 } 718 719 result.append(" />\n"); 721 722 return result.toString(); 723 } 724 725 728 public String getSize() 729 { 730 return domain.getSize(); 731 } 732 733 736 public void setSize(String newSize) 737 { 738 domain.setSize(newSize); 739 } 740 741 744 public String getScale() 745 { 746 return domain.getScale(); 747 } 748 749 752 public void setScale(String newScale) 753 { 754 domain.setScale(newScale); 755 } 756 757 763 public String printSize() 764 { 765 return domain.printSize(); 766 } 767 768 772 public String getDefaultSetting() 773 { 774 return domain.getDefaultSetting(); 775 } 776 777 780 public void setDefaultValue(String def) 781 { 782 domain.setDefaultValue(def); 783 } 784 785 788 public String getDefaultValue() 789 { 790 return domain.getDefaultValue(); 791 } 792 793 796 public String getInputValidator() 797 { 798 return this.inputValidator; 799 } 800 801 805 public boolean isAutoIncrement() 806 { 807 return isAutoIncrement; 808 } 809 810 814 public void setAutoIncrement(boolean value) 815 { 816 isAutoIncrement = value; 817 } 818 819 public String getAutoIncrementString() 820 { 821 if (isAutoIncrement() 822 && IDMethod.NATIVE.equals(getTable().getIdMethod())) 823 { 824 return getPlatform().getAutoIncrement(); 825 } 826 return ""; 827 } 828 829 833 public void setTypeFromString(String typeName, String size) 834 { 835 String tn = typeName.toUpperCase(); 836 setType(tn); 837 838 if (size != null) 839 { 840 domain.setSize(size); 841 } 842 843 if (tn.indexOf("CHAR") != -1) 844 { 845 domain.setType(SchemaType.VARCHAR); 846 } 847 else if (tn.indexOf("INT") != -1) 848 { 849 domain.setType(SchemaType.INTEGER); 850 } 851 else if (tn.indexOf("FLOAT") != -1) 852 { 853 domain.setType(SchemaType.FLOAT); 854 } 855 else if (tn.indexOf("DATE") != -1) 856 { 857 domain.setType(SchemaType.DATE); 858 } 859 else if (tn.indexOf("TIME") != -1) 860 { 861 domain.setType(SchemaType.TIMESTAMP); 862 } 863 else if (tn.indexOf("BINARY") != -1) 864 { 865 domain.setType(SchemaType.LONGVARBINARY); 866 } 867 else 868 { 869 domain.setType(SchemaType.VARCHAR); 870 } 871 } 872 873 879 public String getJavaObject() 880 { 881 return TypeMap.getJavaObject(domain.getType()); 882 } 883 884 890 public String getJavaPrimitive() 891 { 892 return TypeMap.getJavaNative(domain.getType()); 893 } 894 895 903 public String getJavaNative() 904 { 905 String jtype = TypeMap.getJavaNativeObject(domain.getType()); 906 if (isUsePrimitive()) 907 { 908 jtype = TypeMap.getJavaNative(domain.getType()); 909 } 910 911 return jtype; 912 } 913 914 918 public String getVillageMethod() 919 { 920 String vmethod = TypeMap.getVillageObjectMethod(domain.getType()); 921 if (isUsePrimitive()) 922 { 923 vmethod = TypeMap.getVillageMethod(domain.getType()); 924 } 925 926 return vmethod; 927 } 928 929 933 public String getParameterParserMethod() 934 { 935 return TypeMap.getPPMethod(domain.getType()); 936 } 937 938 942 public boolean isBooleanInt() 943 { 944 return TypeMap.isBooleanInt(domain.getType()); 945 } 946 947 951 public boolean isBooleanChar() 952 { 953 return TypeMap.isBooleanChar(domain.getType()); 954 } 955 956 960 public boolean isBit() 961 { 962 return TypeMap.isBit(domain.getType()); 963 } 964 965 969 public boolean isPrimitive() 970 { 971 String t = getJavaNative(); 972 return "boolean".equals(t) 973 || "byte".equals(t) 974 || "short".equals(t) 975 || "int".equals(t) 976 || "long".equals(t) 977 || "float".equals(t) 978 || "double".equals(t) 979 || "char".equals(t); 980 } 981 982 public boolean isUsePrimitive() 983 { 984 String s = getJavaType(); 985 return (s != null && s.equals("primitive")) 986 || (s == null && !"object".equals( 987 getTable().getDatabase().getDefaultJavaType())); 988 } 989 990 993 public Domain getDomain() 994 { 995 return domain; 996 } 997 998 1001 public void setDomain(Domain domain) 1002 { 1003 this.domain = domain; 1004 } 1005 1006 private Platform getPlatform() 1007 { 1008 try 1009 { 1010 return getTable().getDatabase().getPlatform(); 1011 } 1012 catch (Exception ex) 1013 { 1014 log.warn("could not load platform implementation"); 1015 } 1016 return new PlatformDefaultImpl(); 1017 } 1018 1019 public String getSqlString() 1020 { 1021 StringBuffer sb = new StringBuffer (); 1022 sb.append(getName()); 1023 sb.append(' '); 1024 sb.append(getDomain().getSqlType()); 1025 if (getPlatform().hasSize(getDomain().getSqlType())) 1026 { 1027 sb.append(getDomain().printSize()); 1028 sb.append(' '); 1029 } 1030 if (getDomain().getDefaultValue() != null) 1031 { 1032 sb.append("default "); 1033 if (TypeMap.isTextType(getDomain().getType())) 1034 { 1035 sb.append('\'').append(getDefaultValue()).append('\''); 1037 } 1038 else 1039 { 1040 sb.append(getDefaultValue()); 1041 } 1042 sb.append(' '); 1043 } 1044 sb.append(getNotNullString()); 1045 sb.append(' '); 1046 sb.append(getAutoIncrementString()); 1047 return sb.toString(); 1048 } 1049 1050 1056 public boolean isCorrectGetters() 1057 { 1058 return correctGetters; 1059 } 1060 1061 1069 public void setCorrectGetters(boolean correctGetters) 1070 { 1071 this.correctGetters = correctGetters; 1072 } 1073} 1074 | Popular Tags |