1 3 package jodd.db.orm; 4 5 import java.util.HashMap ; 6 import java.util.Map ; 7 8 18 public class DbOrm { 19 20 23 protected DbOrm() { 24 25 } 26 27 29 protected static DbOrm orm = new DbOrm(); 30 31 34 public static DbOrm getInstance() { 35 return orm; 36 } 37 38 43 public static void setInstance(DbOrm orm) { 44 if (orm == null) { 45 orm = new DbOrm(); 46 } 47 DbOrm.orm = orm; 48 } 49 50 51 53 protected String tablePrefix = null; 54 55 60 public DbOrm setTablePrefix(String prefix) { 61 this.tablePrefix = prefix; 62 return this; 63 } 64 65 68 public String getTablePrefix() { 69 return tablePrefix; 70 } 71 72 73 protected String packagePrefix = null; 74 75 79 public DbOrm setPackagePrefix(String prefix) { 80 if ((packagePrefix != null) && (prefix.endsWith("."))) { 81 prefix = prefix.substring(0, prefix.length() - 1); 82 } 83 this.packagePrefix = prefix; 84 return this; 85 } 86 87 90 public String getPackagePrefix() { 91 return packagePrefix; 92 } 93 94 96 protected Map <String , Class > tableMaps = new HashMap <String , Class >(); 97 98 106 public DbOrm mapTable(Class type) { 107 DbEntityDescriptor ded = lookup(type); 108 if (ded == null) { 109 throw new DbOrmException("Mapping to a core java class has no sence: '" + type + "'."); 110 } 111 String tableName = ded.getTableName(); 112 Class previousType = tableMaps.put(tableName, type); 113 if (previousType != null) { 114 throw new DbOrmException("Table '" + tableName + "' already registered with type '" + previousType + "'."); 115 } 116 return this; 117 } 118 119 122 public void clearAllMappings() { 123 tableMaps.clear(); 124 } 125 126 129 public Class getMappedEntityType(String tableName) { 130 return tableMaps.get(tableName); 131 } 132 133 134 136 protected Map <Class , DbEntityDescriptor> descriptors = new HashMap <Class , DbEntityDescriptor>(); 137 138 142 public DbEntityDescriptor lookup(Class type) { 143 String typeName = type.getName(); 144 if (typeName.indexOf("java.") == 0) { 145 return null; 146 } 147 DbEntityDescriptor ded = descriptors.get(type); 148 if (ded == null) { 149 ded = new DbEntityDescriptor(type, this); 150 descriptors.put(type, ded); 151 } 152 return ded; 153 } 154 155 158 public void clearDescriptorsCache() { 159 descriptors.clear(); 160 } 161 162 163 165 protected String columnAliasSeparator = "$"; 166 167 170 public String getColumnAliasSeparator() { 171 return columnAliasSeparator; 172 } 173 174 179 public void setColumnAliasSeparator(String separator) { 180 this.columnAliasSeparator = separator; 181 } 182 183 } 184 | Popular Tags |