DbOrm.java :  » Development » jodd » jodd » db » orm » Java Open Source

Java Open Source » Development » jodd 
jodd » jodd » db » orm » DbOrm.java
// Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.

package jodd.db.orm;

import java.util.HashMap;
import java.util.Map;

/**
 * Global DB-ORM mapping definitions, prefixes and cache.
 * 
 * <p>
 * Mapping definitions are used <b>only</b> by a result set mapper (such as {@link jodd.db.orm.mapper.ResultSetMapper}
 * to lookup for an entity from table name. Table names are read from result-set meta data, for example.
 * Moreover, it is not needed to use mappings at all: in that case just provide entity types during result set to
 * objects conversion.

 */
public class DbOrm {

  /**
   * Singleton.
   */
  protected DbOrm() {
  }

  // ---------------------------------------------------------------- singleton

  protected static DbOrm orm = new DbOrm();

  /**
   * Returns current DB-ORM instance.
   */
  public static DbOrm getInstance() {
    return orm;
  }

  /**
   * Sets new default instance for DB-ORM mapper.
   * If parameter is <code>null</code> current instance will be replaced
   * with new instance of this class.
   */
  public static void setInstance(DbOrm orm) {
    if (orm == null) {
      orm = new DbOrm();
    }
    DbOrm.orm = orm;
  }


  // ---------------------------------------------------------------- prefix

  protected String tablePrefix;

  /**
   * Sets default table prefix for all tables. This prefix affect default
   * conversions from bean name to table name and vice-versa when no annotations
   * are used.
   */
  public DbOrm setTablePrefix(String prefix) {
    this.tablePrefix = prefix;
    return this;
  }

  /**
   * Returns current table prefix.
   */
  public String getTablePrefix() {
    return tablePrefix;
  }


  protected String packagePrefix;

  /**
   * Sets package prefix for all DB entities. Used by {@link jodd.db.orm.sqlgen.DbSqlGenerator}
   * during creating sql queries.
   */
  public DbOrm setPackagePrefix(String prefix) {
    if ((packagePrefix != null) && (prefix.endsWith("."))) {
      prefix = prefix.substring(0, prefix.length() - 1);
    }
    this.packagePrefix = prefix;
    return this;
  }

  /**
   * Returns current package prefix.
   */
  public String getPackagePrefix() {
    return packagePrefix;
  }

  // ---------------------------------------------------------------- cache

  protected Map<Class, DbEntityDescriptor> descriptors = new HashMap<Class, DbEntityDescriptor>();

  /**
   * Lookups for {@link DbEntityDescriptor}. If it doesn't exist, new one will be created.
   * Returns <code>null</code> for core classes from <code>java</code> runtime packages.
   */
  public DbEntityDescriptor lookup(Class type) {
    String typeName = type.getName();
    if (typeName.indexOf("java.") == 0) {
      return null;
    }
    DbEntityDescriptor ded = descriptors.get(type);
    if (ded == null) {
      ded = new DbEntityDescriptor(type, this);
      descriptors.put(type, ded);
    }
    return ded;
  }

  /**
   * Clears descriptors cache.
   */
  public void clearDescriptorsCache() {
    descriptors.clear();
  }


  // ---------------------------------------------------------------- table separators

  protected String columnAliasSeparator = "$";

  /**
   * Returns value for separator for column aliases that divides table reference and column name.
   */
  public String getColumnAliasSeparator() {
    return columnAliasSeparator;
  }

  /**
   * Specifies separator for column aliases that divides table reference and column name.
   * Separator should contains of characters that are not used in table names, such as:
   * '$' or '__'.
   */
  public void setColumnAliasSeparator(String separator) {
    this.columnAliasSeparator = separator;
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.