Java Code Examples for liquibase.database.Database#escapeConstraintName()

The following examples show how to use liquibase.database.Database#escapeConstraintName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PerconaAddColumnChange.java    From liquibase-percona with Apache License 2.0 5 votes vote down vote up
private String addForeignKeyConstraint(AddColumnConfig column, Database database) {
    String result = "";
    ConstraintsConfig constraintsConfig = column.getConstraints();
    if (constraintsConfig != null && (StringUtil.isNotEmpty(constraintsConfig.getReferences()) || StringUtil.isNotEmpty(constraintsConfig.getReferencedTableName()) )) {
        result += ", ADD ";
        if (StringUtil.isNotEmpty(constraintsConfig.getForeignKeyName())) {
            result += "CONSTRAINT " + database.escapeConstraintName(constraintsConfig.getForeignKeyName()) + " ";
        }
        result +=  "FOREIGN KEY ("
                + database.escapeColumnName(null, null, null, column.getName()) + ") REFERENCES ";

        String referencedTable;
        String referencedColumn;

        if (StringUtil.isNotEmpty(constraintsConfig.getReferences())) {
            Matcher references = Pattern.compile("([\\w\\._]+)\\(([\\w_]+)\\)").matcher(constraintsConfig.getReferences());
            if (!references.matches()) {
                throw new UnexpectedLiquibaseException("Unable to get table name and column name from " + constraintsConfig.getReferences());
            }
            referencedTable = references.group(1);
            referencedColumn = references.group(2);
        } else {
            referencedTable = constraintsConfig.getReferencedTableName();
            referencedColumn = constraintsConfig.getReferencedColumnNames();
        }

        referencedTable = PerconaChangeUtil.resolveReferencedPerconaTableName(getTableName(), referencedTable);

        result += database.escapeTableName(null, null, referencedTable) + "(";
        result += database.escapeColumnName(null, null, null, referencedColumn);
        result += ")";
    }
    return result;
}
 
Example 2
Source File: PerconaAddColumnChange.java    From liquibase-percona with Apache License 2.0 5 votes vote down vote up
private String addUniqueKeyConstraint(AddColumnConfig column, Database database) {
    String result = "";
    ConstraintsConfig constraintsConfig = column.getConstraints();
    if (constraintsConfig != null && constraintsConfig.isUnique() != null && constraintsConfig.isUnique()) {
        result += ", ADD ";
        if (StringUtil.isNotEmpty(constraintsConfig.getUniqueConstraintName())) {
            result += "CONSTRAINT " + database.escapeConstraintName(constraintsConfig.getUniqueConstraintName()) + " ";
        }
        result +=  "UNIQUE (" + database.escapeColumnName(null, null, null, column.getName()) + ")";
    }
    return result;
}
 
Example 3
Source File: AddPrimaryKeyGeneratorMSSQL.java    From liquibase-mssql with Apache License 2.0 5 votes vote down vote up
/**
 * The extension's implementation is essentially a copy/paste of the default implementation, with the following changes:
 *
 * 1) Removed other database platform specific logic other than MSSQL (purely to simplify)
 *
 * 2) Added support for setting fillFactor
 *
 * @param statement
 * @param database
 * @param sqlGeneratorChain
 * @return
 */
private Sql[] generateMSSQLSql(AddPrimaryKeyStatementMSSQL statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
  String sql;
  if (statement.getConstraintName() == null) {
    sql = "ALTER TABLE " + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()) + " ADD PRIMARY KEY (" + database.escapeColumnNameList(statement.getColumnNames()) + ")";
  } else {
    sql = "ALTER TABLE " + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()) + " ADD CONSTRAINT " + database.escapeConstraintName(statement.getConstraintName())+" PRIMARY KEY";
    if (!statement.isClustered()) {
      sql += " NONCLUSTERED";
    }
    sql += " (" + database.escapeColumnNameList(statement.getColumnNames()) + ")";
  }

  // the only new feature being added is support for fillFactor
  sql += " WITH (FILLFACTOR = " + statement.getFillFactor() + ")";

  if (StringUtils.trimToNull(statement.getTablespace()) != null && database.supportsTablespaces()) {
    sql += " ON "+statement.getTablespace();
  }

  if (statement.getForIndexName() != null) {
    sql += " USING INDEX "+database.escapeObjectName(statement.getForIndexCatalogName(), statement.getForIndexSchemaName(), statement.getForIndexName(), Index.class);
  }

  return new Sql[] {
      new UnparsedSql(sql, getAffectedPrimaryKey(statement))
  };
}