001package io.ebean.config;
002
003/**
004 * Naming convention used for constraint names.
005 * <p>
006 * Note that these constraint names are trimmed in the PlatformDdl which can be overridden
007 * but provides a decent default implementation.
008 * </p>
009 */
010public class DbConstraintNaming {
011
012  /**
013   * Defines how constraint names are shortened if required based on platform limitations.
014   */
015  public interface MaxLength {
016
017    /**
018     * Truncate or shorten the constraint name to support DB platform limitations.
019     * <p>
020     * There is a default implementation of this which is used if an implementation is
021     * not specified.
022     * </p>
023     */
024    String maxLength(String constraintName, int count);
025  }
026
027  protected String pkPrefix = "pk_";
028  protected String pkSuffix = "";
029
030  protected String fkPrefix = "fk_";
031  protected String fkMiddle = "_";
032  protected String fkSuffix = "";
033
034  protected String fkIndexPrefix = "ix_";
035  protected String fkIndexMiddle = "_";
036  protected String fkIndexSuffix = "";
037
038  protected String indexPrefix = "ix_";
039  protected String indexMiddle = "_";
040  protected String indexSuffix = "";
041
042  protected String uqPrefix = "uq_";
043  protected String uqSuffix = "";
044
045  protected String ckPrefix = "ck_";
046  protected String ckSuffix = "";
047
048  protected MaxLength maxLength;
049
050  protected DbConstraintNormalise normalise;
051
052  /**
053   * Construct using default of lower case for both table and column names.
054   */
055  public DbConstraintNaming() {
056    this(true, true);
057  }
058
059  /**
060   * Construct specifying if lower case should be used (for both table and column names).
061   */
062  public DbConstraintNaming(boolean lowerCase) {
063    this(lowerCase, lowerCase);
064  }
065
066  /**
067   * Construct specifying if lower case should be used for both table and column names.
068   */
069  public DbConstraintNaming(boolean lowerCaseTableNames, boolean lowerCaseColumnNames) {
070    this.normalise = new DbConstraintNormalise(lowerCaseTableNames, lowerCaseColumnNames);
071  }
072
073  /**
074   * Return the MaxLength implementation used to truncate/shorten db constraint names as necessary.
075   */
076  public MaxLength getMaxLength() {
077    return maxLength;
078  }
079
080  /**
081   * Set the MaxLength implementation used to truncate/shorten db constraint names as necessary.
082   */
083  public void setMaxLength(MaxLength maxLength) {
084    this.maxLength = maxLength;
085  }
086
087  /**
088   * Return the primary key constraint name.
089   */
090  public String primaryKeyName(String tableName) {
091    return pkPrefix + normaliseTable(tableName) + pkSuffix;
092  }
093
094  /**
095   * Return the foreign key constraint name given a single column foreign key.
096   */
097  public String foreignKeyConstraintName(String tableName, String columnName) {
098    return fkPrefix + normaliseTable(tableName) + fkMiddle + normaliseColumn(columnName) + fkSuffix;
099  }
100
101  /**
102   * Return the index name associated with a foreign key constraint given multiple columns.
103   */
104  public String foreignKeyIndexName(String tableName, String[] columns) {
105    String colPart = joinColumnNames(columns);
106    return fkIndexPrefix + normaliseTable(tableName) + fkIndexMiddle + colPart + fkIndexSuffix;
107  }
108
109  /**
110   * Return the index name associated with a foreign key constraint given a single column foreign key.
111   */
112  public String foreignKeyIndexName(String tableName, String column) {
113    String colPart = normaliseTable(column);
114    return fkIndexPrefix + normaliseTable(tableName) + fkIndexMiddle + colPart + fkIndexSuffix;
115  }
116
117  /**
118   * Return the index name for a general index (not associated with a foreign key).
119   */
120  public String indexName(String tableName, String column) {
121    String colPart = normaliseTable(column);
122    return indexPrefix + normaliseTable(tableName) + indexMiddle + colPart + indexSuffix;
123  }
124
125  /**
126   * Return the index name for a general index (not associated with a foreign key).
127   */
128  public String indexName(String tableName, String[] columns) {
129    String colPart = joinColumnNames(columns);
130    return indexPrefix + normaliseTable(tableName) + indexMiddle + colPart + indexSuffix;
131  }
132
133  /**
134   * Join the column names together with underscores.
135   */
136  protected String joinColumnNames(String[] columns) {
137    if (columns.length == 1) {
138      return normaliseColumn(columns[0]);
139    }
140    StringBuilder sb = new StringBuilder();
141    for (int i = 0; i < columns.length; i++) {
142      if (i > 0) {
143        sb.append("_");
144      }
145      sb.append(normaliseColumn(columns[i]));
146    }
147    return sb.toString();
148  }
149
150  /**
151   * Return the unique constraint name.
152   */
153  public String uniqueConstraintName(String tableName, String columnName) {
154    return uqPrefix + normaliseTable(tableName) + "_" + normaliseColumn(columnName) + uqSuffix;
155  }
156
157  /**
158   * Return the unique constraint name.
159   */
160  public String uniqueConstraintName(String tableName, String[] columns) {
161    String colPart = joinColumnNames(columns);
162    return uqPrefix + normaliseTable(tableName) + "_" + colPart + uqSuffix;
163  }
164
165  /**
166   * Return the check constraint name.
167   */
168  public String checkConstraintName(String tableName, String columnName) {
169    return ckPrefix + normaliseTable(tableName) + "_" + normaliseColumn(columnName) + ckSuffix;
170  }
171
172  /**
173   * Normalise the table name by trimming catalog and schema and removing any
174   * quoted identifier characters (",',[,] etc).
175   */
176  public String normaliseTable(String tableName) {
177    return normalise.normaliseTable(tableName);
178  }
179
180  /**
181   * Normalise the column name by removing any quoted identifier characters (",',[,] etc).
182   */
183  public String normaliseColumn(String tableName) {
184    return normalise.normaliseColumn(tableName);
185  }
186
187  /**
188   * Lower case the table name checking for quoted identifiers.
189   */
190  public String lowerTableName(String tableName) {
191    return normalise.lowerTableName(tableName);
192  }
193
194  /**
195   * Lower case the column name checking for quoted identifiers.
196   */
197  public String lowerColumnName(String name) {
198    return normalise.lowerColumnName(name);
199  }
200}