コード例 #1
0
  protected void addForeignKeyStatements(
      AddColumnStatement statement, Database database, List<Sql> returnSql) {
    for (ColumnConstraint constraint : statement.getConstraints()) {
      if (constraint instanceof ForeignKeyConstraint) {
        ForeignKeyConstraint fkConstraint = (ForeignKeyConstraint) constraint;
        String refSchemaName = null;
        String refTableName;
        String refColName;
        if (fkConstraint.getReferences() != null) {
          Matcher referencesMatcher =
              Pattern.compile("([\\w\\._]+)\\(([\\w_]+)\\)").matcher(fkConstraint.getReferences());
          if (!referencesMatcher.matches()) {
            throw new UnexpectedLiquibaseException(
                "Don't know how to find table and column names from "
                    + fkConstraint.getReferences());
          }
          refTableName = referencesMatcher.group(1);
          refColName = referencesMatcher.group(2);
        } else {
          refTableName = ((ForeignKeyConstraint) constraint).getReferencedTableName();
          refColName = ((ForeignKeyConstraint) constraint).getReferencedColumnNames();
        }

        if (refTableName.indexOf(".") > 0) {
          refSchemaName = refTableName.split("\\.")[0];
          refTableName = refTableName.split("\\.")[1];
        }

        AddForeignKeyConstraintStatement addForeignKeyConstraintStatement =
            new AddForeignKeyConstraintStatement(
                fkConstraint.getForeignKeyName(),
                statement.getCatalogName(),
                statement.getSchemaName(),
                statement.getTableName(),
                ColumnConfig.arrayFromNames(statement.getColumnName()),
                null,
                refSchemaName,
                refTableName,
                ColumnConfig.arrayFromNames(refColName));
        returnSql.addAll(
            Arrays.asList(
                SqlGeneratorFactory.getInstance()
                    .generateSql(addForeignKeyConstraintStatement, database)));
      }
    }
  }
コード例 #2
0
 protected void addUniqueConstrantStatements(
     AddColumnStatement statement, Database database, List<Sql> returnSql) {
   if (statement.isUnique()) {
     AddUniqueConstraintStatement addConstraintStmt =
         new AddUniqueConstraintStatement(
             statement.getCatalogName(),
             statement.getSchemaName(),
             statement.getTableName(),
             ColumnConfig.arrayFromNames(statement.getColumnName()),
             statement.getUniqueStatementName());
     returnSql.addAll(
         Arrays.asList(
             SqlGeneratorFactory.getInstance().generateSql(addConstraintStmt, database)));
   }
 }