001: // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
002:
003: package jodd.db.orm;
004:
005: import java.util.HashMap;
006: import java.util.Map;
007:
008: /**
009: * Global DB-ORM mapping definitions, prefixes and cache.
010: *
011: * <p>
012: * Mapping definitions are used <b>only</b> by a result set mapper (such as {@link jodd.db.orm.mapper.ResultSetMapper}
013: * to lookup for an entity from table name. Table names are read from result-set meta data, for example.
014: * Moreover, it is not needed to use mappings at all: in that case just provide entity types during result set to
015: * objects conversion.
016:
017: */
018: public class DbOrm {
019:
020: /**
021: * Singleton.
022: */
023: protected DbOrm() {
024: }
025:
026: // ---------------------------------------------------------------- singleton
027:
028: protected static DbOrm orm = new DbOrm();
029:
030: /**
031: * Returns current DB-ORM instance.
032: */
033: public static DbOrm getInstance() {
034: return orm;
035: }
036:
037: /**
038: * Sets new default instance for DB-ORM mapper.
039: * If parameter is <code>null</code> current instance will be replaced
040: * with new instance of this class.
041: */
042: public static void setInstance(DbOrm orm) {
043: if (orm == null) {
044: orm = new DbOrm();
045: }
046: DbOrm.orm = orm;
047: }
048:
049: // ---------------------------------------------------------------- prefix
050:
051: protected String tablePrefix;
052:
053: /**
054: * Sets default table prefix for all tables. This prefix affect default
055: * conversions from bean name to table name and vice-versa when no annotations
056: * are used.
057: */
058: public DbOrm setTablePrefix(String prefix) {
059: this .tablePrefix = prefix;
060: return this ;
061: }
062:
063: /**
064: * Returns current table prefix.
065: */
066: public String getTablePrefix() {
067: return tablePrefix;
068: }
069:
070: protected String packagePrefix;
071:
072: /**
073: * Sets package prefix for all DB entities. Used by {@link jodd.db.orm.sqlgen.DbSqlGenerator}
074: * during creating sql queries.
075: */
076: public DbOrm setPackagePrefix(String prefix) {
077: if ((packagePrefix != null) && (prefix.endsWith("."))) {
078: prefix = prefix.substring(0, prefix.length() - 1);
079: }
080: this .packagePrefix = prefix;
081: return this ;
082: }
083:
084: /**
085: * Returns current package prefix.
086: */
087: public String getPackagePrefix() {
088: return packagePrefix;
089: }
090:
091: // ---------------------------------------------------------------- cache
092:
093: protected Map<Class, DbEntityDescriptor> descriptors = new HashMap<Class, DbEntityDescriptor>();
094:
095: /**
096: * Lookups for {@link DbEntityDescriptor}. If it doesn't exist, new one will be created.
097: * Returns <code>null</code> for core classes from <code>java</code> runtime packages.
098: */
099: public DbEntityDescriptor lookup(Class type) {
100: String typeName = type.getName();
101: if (typeName.indexOf("java.") == 0) {
102: return null;
103: }
104: DbEntityDescriptor ded = descriptors.get(type);
105: if (ded == null) {
106: ded = new DbEntityDescriptor(type, this );
107: descriptors.put(type, ded);
108: }
109: return ded;
110: }
111:
112: /**
113: * Clears descriptors cache.
114: */
115: public void clearDescriptorsCache() {
116: descriptors.clear();
117: }
118:
119: // ---------------------------------------------------------------- table separators
120:
121: protected String columnAliasSeparator = "$";
122:
123: /**
124: * Returns value for separator for column aliases that divides table reference and column name.
125: */
126: public String getColumnAliasSeparator() {
127: return columnAliasSeparator;
128: }
129:
130: /**
131: * Specifies separator for column aliases that divides table reference and column name.
132: * Separator should contains of characters that are not used in table names, such as:
133: * '$' or '__'.
134: */
135: public void setColumnAliasSeparator(String separator) {
136: this.columnAliasSeparator = separator;
137: }
138:
139: }
|