1 | |
package liquibase.sqlgenerator.core; |
2 | |
|
3 | |
import liquibase.database.Database; |
4 | |
import liquibase.database.typeconversion.TypeConverterFactory; |
5 | |
import liquibase.database.structure.Column; |
6 | |
import liquibase.database.structure.Table; |
7 | |
import liquibase.exception.ValidationErrors; |
8 | |
import liquibase.sql.Sql; |
9 | |
import liquibase.sql.UnparsedSql; |
10 | |
import liquibase.sqlgenerator.SqlGenerator; |
11 | |
import liquibase.sqlgenerator.SqlGeneratorChain; |
12 | |
import liquibase.statement.core.AddDefaultValueStatement; |
13 | |
|
14 | 115 | public class AddDefaultValueGenerator extends AbstractSqlGenerator<AddDefaultValueStatement> { |
15 | |
|
16 | |
public ValidationErrors validate(AddDefaultValueStatement addDefaultValueStatement, Database database, |
17 | |
SqlGeneratorChain sqlGeneratorChain) { |
18 | 16 | ValidationErrors validationErrors = new ValidationErrors(); |
19 | 16 | validationErrors.checkRequiredField("defaultValue", addDefaultValueStatement.getDefaultValue()); |
20 | 16 | validationErrors.checkRequiredField("columnName", addDefaultValueStatement.getColumnName()); |
21 | 16 | validationErrors.checkRequiredField("tableName", addDefaultValueStatement.getTableName()); |
22 | 16 | return validationErrors; |
23 | |
} |
24 | |
|
25 | |
public Sql[] generateSql(AddDefaultValueStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) { |
26 | 0 | Object defaultValue = statement.getDefaultValue(); |
27 | 0 | return new Sql[] { new UnparsedSql("ALTER TABLE " |
28 | |
+ database.escapeTableName(statement.getSchemaName(), statement.getTableName()) |
29 | |
+ " ALTER COLUMN " |
30 | |
+ database.escapeColumnName(statement.getSchemaName(), statement.getTableName(), |
31 | |
statement.getColumnName()) |
32 | |
+ " SET DEFAULT " |
33 | |
+ TypeConverterFactory.getInstance().findTypeConverter(database).getDataType(defaultValue) |
34 | |
.convertObjectToString(defaultValue, database), new Column().setTable( |
35 | |
new Table(statement.getTableName()).setSchema(statement.getSchemaName())).setName( |
36 | |
statement.getColumnName())) }; |
37 | |
} |
38 | |
} |