001package io.ebean.config;
002
003import io.ebean.util.CamelCaseHelper;
004
005/**
006 * Converts between Camel Case and Underscore based names for both table and
007 * column names (and is the default naming convention in Ebean).
008 *
009 * @author emcgreal
010 * @author rbygrave
011 */
012public class UnderscoreNamingConvention extends AbstractNamingConvention {
013
014  /**
015   * Force toUnderscore to return in upper case.
016   */
017  private boolean forceUpperCase = false;
018
019  /**
020   * The digits compressed.
021   */
022  private boolean digitsCompressed = true;
023
024  /**
025   * Create with a given sequence format.
026   *
027   * @param sequenceFormat the sequence format
028   */
029  public UnderscoreNamingConvention(String sequenceFormat) {
030    super(sequenceFormat);
031  }
032
033  /**
034   * Create with a sequence format of "{table}_seq".
035   */
036  public UnderscoreNamingConvention() {
037    super();
038  }
039
040  /**
041   * Returns the last part of the class name.
042   *
043   * @param beanClass the bean class
044   * @return the table name from class
045   */
046  @Override
047  public TableName getTableNameByConvention(Class<?> beanClass) {
048
049    return new TableName(getCatalog(), getSchema(), toUnderscoreFromCamel(beanClass.getSimpleName()));
050  }
051
052  /**
053   * Converts Camel case property name to underscore based column name.
054   *
055   * @return the column from property
056   */
057  @Override
058  public String getColumnFromProperty(Class<?> beanClass, String propertyName) {
059
060    return toUnderscoreFromCamel(propertyName);
061  }
062
063  /**
064   * Return true if the result will be upper case.
065   * <p>
066   * False if it will be lower case.
067   * </p>
068   */
069  public boolean isForceUpperCase() {
070    return forceUpperCase;
071  }
072
073  /**
074   * Set to true to make the result upper case.
075   */
076  public void setForceUpperCase(boolean forceUpperCase) {
077    this.forceUpperCase = forceUpperCase;
078  }
079
080  /**
081   * Returns true if digits are compressed.
082   */
083  public boolean isDigitsCompressed() {
084    return digitsCompressed;
085  }
086
087  /**
088   * Sets to true for digits to be compressed (without a leading underscore).
089   */
090  public void setDigitsCompressed(boolean digitsCompressed) {
091    this.digitsCompressed = digitsCompressed;
092  }
093
094  @Override
095  public String getForeignKey(String prefix, String fkProperty) {
096    return prefix + "_" + toUnderscoreFromCamel(fkProperty);
097  }
098
099  /**
100   * Convert and return the string to underscore from camel case.
101   */
102  protected String toUnderscoreFromCamel(String camelCase) {
103    return CamelCaseHelper.toUnderscoreFromCamel(camelCase, digitsCompressed, forceUpperCase);
104  }
105
106  /**
107   * Convert and return the from string from underscore to camel case.
108   */
109  protected String toCamelFromUnderscore(String underscore) {
110    return CamelCaseHelper.toCamelFromUnderscore(underscore);
111  }
112}