1 30 package com.genimen.djeneric.tools.modeler.schemagenerator; 31 32 import com.genimen.djeneric.repository.DjDomain; 33 import com.genimen.djeneric.repository.DjExtent; 34 import com.genimen.djeneric.repository.DjPersistenceManager; 35 import com.genimen.djeneric.repository.DjProperty; 36 import com.genimen.djeneric.repository.DjRelation; 37 import com.genimen.djeneric.repository.rdbmsdirect.RdbmsDirectPersistenceManager; 38 39 public class DB2SchemaGenerator extends SchemaGenerator 40 { 41 public DB2SchemaGenerator(DjPersistenceManager mgr) 42 { 43 super(mgr); 44 } 45 46 public String getTableCreateStart(DjExtent extent) throws Exception 47 { 48 return "-- Objects of type " + extent.getObjectType() + "\n" + "create table " + getTableName(extent) + "(\n"; 49 } 50 51 public String getTypeColumnCreate(DjExtent extent) throws Exception 52 { 53 return " " + RdbmsDirectPersistenceManager.OBJECT_TYPE_COLUMN + " varchar(" 54 + RdbmsDirectPersistenceManager.MAX_INTERNAL_CODE_LENGTH + ") not null,\n"; 55 } 56 57 public String getColumnCreate(DjProperty prop, boolean isFirst, boolean isLast) throws Exception 58 { 59 String result = " " + prop.getName() + " "; 60 if (prop.getTypeCode() == DjDomain.BIGDECIMAL_TYPE) result += " decimal(" + prop.getLength() + ", " 61 + prop.getDecimals() + ") "; 62 else if (prop.getTypeCode() == DjDomain.BYTE_TYPE) result += " blob (" 63 + (prop.getLength() == 0 ? "1 M" : String.valueOf(prop 64 .getLength())) + ") "; 65 else if (prop.getTypeCode() == DjDomain.DATE_TYPE) result += " timestamp "; 66 else if (prop.getTypeCode() == DjDomain.INT_TYPE) result += " integer(" + prop.getLength() + ", 0) "; 67 else if (prop.getTypeCode() == DjDomain.LONG_TYPE) result += " decimal(" + prop.getLength() + ", 0) "; 68 else if (prop.getTypeCode() == DjDomain.STRING_TYPE) 69 { 70 result += " varchar(" + prop.getLength() + ") "; 71 } 72 else throw new Exception ("Unknown property type: " + prop.getTypeCode()); 73 74 if (prop.isRequired()) result += " not null"; 75 if (!isLast) result += ","; 76 result += "\n"; 77 78 return result; 79 } 80 81 public String getTableCreateEnd(DjExtent extent) throws Exception 82 { 83 return ");\n\n"; 84 } 85 86 public String getTablePk(DjExtent extent, int idx) throws Exception 87 { 88 String result = ""; 89 if (idx == 0) result += "\n-- Primary keys\n"; 90 91 result += "alter table " + getTableName(extent) + " add constraint " + getTableName(extent) + "_pk primary key(" 92 + extent.getIdProperty().getName() + ");\n"; 93 return result; 94 } 95 96 public String getRelation(DjRelation relation, int idx, int idxInTable) throws Exception 97 { 98 String result = ""; 99 if (idx == 0) result += "\n-- Foreign keys\n"; 100 101 DjExtent extent = relation.getDetailExtent(); 102 result += "alter table " + getTableName(extent) + " add constraint " + getTableName(extent) + "_fk" + idxInTable 103 + " foreign key(" + relation.getDetailProperty().getName() + ") references " 104 + getTableName(relation.getMasterExtent()) + "(" + relation.getMasterExtent().getIdProperty().getName() 105 + ");\n"; 106 return result; 107 } 108 109 public String getIndex(DjRelation relation, int idx, int idxInTable) throws Exception 110 { 111 String result = ""; 112 if (idx == 0) result += "\n-- Indexes on foreign key columns\n"; 113 114 DjExtent extent = relation.getDetailExtent(); 115 result += "create index " + getTableName(extent) + "_" + idxInTable + " on " + getTableName(extent) + "(" 116 + relation.getDetailProperty().getName() + ");\n"; 117 return result; 118 } 119 } | Popular Tags |