protected Column getAffectedColumn(AddColumnStatement statement) { return new Column() .setRelation( new Table() .setName(statement.getTableName()) .setSchema(new Schema(statement.getCatalogName(), statement.getSchemaName()))) .setName(statement.getColumnName()); }
protected String generateSingleColumnSQL(AddColumnStatement statement, Database database) { DatabaseDataType columnType = DataTypeFactory.getInstance() .fromDescription( statement.getColumnType() + (statement.isAutoIncrement() ? "{autoIncrement:true}" : ""), database) .toDatabaseDataType(database); String alterTable = " ADD " + database.escapeColumnName( statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), statement.getColumnName()) + " " + columnType; if (statement.isAutoIncrement() && database.supportsAutoIncrement()) { AutoIncrementConstraint autoIncrementConstraint = statement.getAutoIncrementConstraint(); alterTable += " " + database.getAutoIncrementClause( autoIncrementConstraint.getStartWith(), autoIncrementConstraint.getIncrementBy()); } alterTable += getDefaultClause(statement, database); if (!statement.isNullable()) { alterTable += " NOT NULL"; } else { if (database instanceof SybaseDatabase || database instanceof SybaseASADatabase || database instanceof MySQLDatabase || (database instanceof MSSQLDatabase && columnType.toString().equalsIgnoreCase("timestamp"))) { alterTable += " NULL"; } } if (statement.isPrimaryKey()) { alterTable += " PRIMARY KEY"; } if (database instanceof MySQLDatabase && statement.getRemarks() != null) { alterTable += " COMMENT '" + statement.getRemarks() + "' "; } if (statement.getAddAfterColumn() != null && !statement.getAddAfterColumn().isEmpty()) { alterTable += " AFTER `" + statement.getAddAfterColumn() + "` "; } return alterTable; }
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))); } }
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))); } } }
protected String generateSingleColumBaseSQL(AddColumnStatement statement, Database database) { return "ALTER TABLE " + database.escapeTableName( statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()); }