Search in sources :

Example 1 with ColumnConstraint

use of liquibase.statement.ColumnConstraint in project liquibase by liquibase.

the class AddColumnGenerator method addForeignKeyStatements.

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)));
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) ColumnConstraint(liquibase.statement.ColumnConstraint) ForeignKeyConstraint(liquibase.statement.ForeignKeyConstraint) AddForeignKeyConstraintStatement(liquibase.statement.core.AddForeignKeyConstraintStatement) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 2 with ColumnConstraint

use of liquibase.statement.ColumnConstraint in project liquibase by liquibase.

the class AddUniqueConstraintExecutorTest method setupStatements.

@Override
protected List<? extends SqlStatement> setupStatements(Database database) {
    List<CreateTableStatement> statements = new ArrayList<CreateTableStatement>();
    CreateTableStatement table = new CreateTableStatement(null, null, TABLE_NAME);
    table.addColumn("id", DataTypeFactory.getInstance().fromDescription("int", database), null, new ColumnConstraint[] { new NotNullConstraint() }).addColumn(COLUMN_NAME, DataTypeFactory.getInstance().fromDescription("int", database), null, new ColumnConstraint[] { new NotNullConstraint() });
    statements.add(table);
    if (database.supportsSchemas()) {
        table = new CreateTableStatement(DatabaseTestContext.ALT_CATALOG, DatabaseTestContext.ALT_SCHEMA, TABLE_NAME);
        table.addColumn("id", DataTypeFactory.getInstance().fromDescription("int", database), null, new ColumnConstraint[] { new NotNullConstraint() }).addColumn(COLUMN_NAME, DataTypeFactory.getInstance().fromDescription("int", database), null, new ColumnConstraint[] { new NotNullConstraint() });
        statements.add(table);
    }
    return statements;
}
Also used : CreateTableStatement(liquibase.statement.core.CreateTableStatement) ColumnConstraint(liquibase.statement.ColumnConstraint) ArrayList(java.util.ArrayList) NotNullConstraint(liquibase.statement.NotNullConstraint)

Aggregations

ColumnConstraint (liquibase.statement.ColumnConstraint)2 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)1 ForeignKeyConstraint (liquibase.statement.ForeignKeyConstraint)1 NotNullConstraint (liquibase.statement.NotNullConstraint)1 AddForeignKeyConstraintStatement (liquibase.statement.core.AddForeignKeyConstraintStatement)1 CreateTableStatement (liquibase.statement.core.CreateTableStatement)1