001package io.ebeaninternal.dbmigration.ddlgeneration.platform;
002
003import io.ebean.config.DbConstraintNaming;
004import io.ebeaninternal.dbmigration.ddlgeneration.platform.util.VowelRemover;
005
006/**
007 * Default implementation used to truncate or shorten db constraint names as required.
008 */
009public class DefaultConstraintMaxLength implements DbConstraintNaming.MaxLength {
010
011  private final int maxConstraintNameLength;
012
013  public DefaultConstraintMaxLength(int maxConstraintNameLength) {
014    this.maxConstraintNameLength = maxConstraintNameLength;
015  }
016
017  /**
018   * Apply a maximum length to the constraint name.
019   * <p>
020   * This implementation should work well apart from perhaps DB2 where the limit is 18.
021   */
022  @Override
023  public String maxLength(String constraintName, int count) {
024    if (constraintName.length() < maxConstraintNameLength) {
025      return constraintName;
026    }
027    if (maxConstraintNameLength < 60) {
028      // trim out vowels for Oracle / DB2 with short max lengths
029      constraintName = VowelRemover.trim(constraintName, 4);
030      if (constraintName.length() < maxConstraintNameLength) {
031        return constraintName;
032      }
033    }
034    // add the count to ensure the constraint name is unique
035    // (relying on the prefix having the table name to be globally unique)
036    return constraintName.substring(0, maxConstraintNameLength - 3) + "_" + count;
037  }
038}