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 SqlServerSchemaGenerator extends SchemaGenerator 40 { 41 public SqlServerSchemaGenerator(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 += " [numeric](" + prop.getLength() + ", " 61 + prop.getDecimals() + ") "; 62 else if (prop.getTypeCode() == DjDomain.BYTE_TYPE) result += " [image] "; 63 else if (prop.getTypeCode() == DjDomain.DATE_TYPE) result += " [datetime] "; 64 else if (prop.getTypeCode() == DjDomain.INT_TYPE) result += " [bigint] "; 65 else if (prop.getTypeCode() == DjDomain.LONG_TYPE) result += " [bigint] "; 66 else if (prop.getTypeCode() == DjDomain.STRING_TYPE) result += " [varchar](" + prop.getLength() + ") "; 67 else throw new Exception ("Unknown property type: " + prop.getTypeCode()); 68 69 if (prop.isRequired()) result += " not null"; 70 if (!isLast) result += ","; 71 result += "\n"; 72 73 return result; 74 } 75 76 public String getTableCreateEnd(DjExtent extent) throws Exception 77 { 78 return ")\nGO\n\n"; 79 } 80 81 public String getTablePk(DjExtent extent, int idx) throws Exception 82 { 83 String result = ""; 84 if (idx == 0) result += "\n/* Primary keys */\n"; 85 86 result += "alter table [" + getTableName(extent) + "] with nocheck add constraint [" + getTableName(extent) 87 + "_pk] primary key clustered([" + extent.getIdProperty().getName() + "]) on [primary]\n" + "GO\n\n"; 88 return result; 89 } 90 91 public String getRelation(DjRelation relation, int idx, int idxInTable) throws Exception 92 { 93 String result = ""; 94 if (idx == 0) result += "\n/* Foreign keys */\n"; 95 96 DjExtent extent = relation.getDetailExtent(); 97 result += "alter table [" + getTableName(extent) + "] add constraint [" + getTableName(extent) + "_fk" + idxInTable 98 + "] foreign key([" + relation.getDetailProperty().getName() + "]) references [" 99 + getTableName(relation.getMasterExtent()) + "]([" 100 + relation.getMasterExtent().getIdProperty().getName() + "])\n" + "GO\n\n"; 101 return result; 102 } 103 104 public String getIndex(DjRelation relation, int idx, int idxInTable) throws Exception 105 { 106 String result = ""; 107 if (idx == 0) result += "\n/* Indexes on foreign key columns */\n"; 108 109 DjExtent extent = relation.getDetailExtent(); 110 result += "create index [" + getTableName(extent) + "_" + idxInTable + "] on [" + getTableName(extent) + "]([" 111 + relation.getDetailProperty().getName() + "])\n" + "GO\n\n"; 112 return result; 113 } 114 } | Popular Tags |