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 092 return pkPrefix + normaliseTable(tableName) + pkSuffix; 093 } 094 095 /** 096 * Return the foreign key constraint name given a single column foreign key. 097 */ 098 public String foreignKeyConstraintName(String tableName, String columnName) { 099 return fkPrefix + normaliseTable(tableName) + fkMiddle + normaliseColumn(columnName) + fkSuffix; 100 } 101 102 /** 103 * Return the index name associated with a foreign key constraint given multiple columns. 104 */ 105 public String foreignKeyIndexName(String tableName, String[] columns) { 106 107 String colPart = joinColumnNames(columns); 108 return fkIndexPrefix + normaliseTable(tableName) + fkIndexMiddle + colPart + fkIndexSuffix; 109 } 110 111 /** 112 * Return the index name associated with a foreign key constraint given a single column foreign key. 113 */ 114 public String foreignKeyIndexName(String tableName, String column) { 115 116 String colPart = normaliseTable(column); 117 return fkIndexPrefix + normaliseTable(tableName) + fkIndexMiddle + colPart + fkIndexSuffix; 118 } 119 120 /** 121 * Return the index name for a general index (not associated with a foreign key). 122 */ 123 public String indexName(String tableName, String column) { 124 125 String colPart = normaliseTable(column); 126 return indexPrefix + normaliseTable(tableName) + indexMiddle + colPart + indexSuffix; 127 } 128 129 /** 130 * Return the index name for a general index (not associated with a foreign key). 131 */ 132 public String indexName(String tableName, String[] columns) { 133 134 String colPart = joinColumnNames(columns); 135 return indexPrefix + normaliseTable(tableName) + indexMiddle + colPart + indexSuffix; 136 } 137 138 /** 139 * Join the column names together with underscores. 140 */ 141 protected String joinColumnNames(String[] columns) { 142 143 if (columns.length == 1) { 144 return normaliseColumn(columns[0]); 145 } 146 StringBuilder sb = new StringBuilder(); 147 for (int i = 0; i < columns.length; i++) { 148 if (i > 0) { 149 sb.append("_"); 150 } 151 sb.append(normaliseColumn(columns[i])); 152 } 153 return sb.toString(); 154 } 155 156 /** 157 * Return the unique constraint name. 158 */ 159 public String uniqueConstraintName(String tableName, String columnName) { 160 161 return uqPrefix + normaliseTable(tableName) + "_" + normaliseColumn(columnName) + uqSuffix; 162 } 163 164 /** 165 * Return the unique constraint name. 166 */ 167 public String uniqueConstraintName(String tableName, String[] columns) { 168 169 String colPart = joinColumnNames(columns); 170 return uqPrefix + normaliseTable(tableName) + "_" + colPart + uqSuffix; 171 } 172 173 /** 174 * Return the check constraint name. 175 */ 176 public String checkConstraintName(String tableName, String columnName) { 177 178 return ckPrefix + normaliseTable(tableName) + "_" + normaliseColumn(columnName) + ckSuffix; 179 } 180 181 /** 182 * Normalise the table name by trimming catalog and schema and removing any 183 * quoted identifier characters (",',[,] etc). 184 */ 185 public String normaliseTable(String tableName) { 186 return normalise.normaliseTable(tableName); 187 } 188 189 /** 190 * Normalise the column name by removing any quoted identifier characters (",',[,] etc). 191 */ 192 public String normaliseColumn(String tableName) { 193 return normalise.normaliseColumn(tableName); 194 } 195 196 /** 197 * Lower case the table name checking for quoted identifiers. 198 */ 199 public String lowerTableName(String tableName) { 200 return normalise.lowerTableName(tableName); 201 } 202 203 /** 204 * Lower case the column name checking for quoted identifiers. 205 */ 206 public String lowerColumnName(String name) { 207 return normalise.lowerColumnName(name); 208 } 209}