Coverage Report - liquibase.sqlgenerator.core.AddColumnGeneratorDefaultClauseBeforeNotNull
 
Classes in this File Line Coverage Branch Coverage Complexity
AddColumnGeneratorDefaultClauseBeforeNotNull
21%
7/33
47%
22/46
4.5
 
 1  
 package liquibase.sqlgenerator.core;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.List;
 5  
 
 6  
 import liquibase.database.Database;
 7  
 import liquibase.database.core.DB2Database;
 8  
 import liquibase.database.core.DerbyDatabase;
 9  
 import liquibase.database.core.FirebirdDatabase;
 10  
 import liquibase.database.core.H2Database;
 11  
 import liquibase.database.core.HsqlDatabase;
 12  
 import liquibase.database.core.InformixDatabase;
 13  
 import liquibase.database.core.OracleDatabase;
 14  
 import liquibase.database.core.SybaseASADatabase;
 15  
 import liquibase.database.core.SybaseDatabase;
 16  
 import liquibase.database.structure.Column;
 17  
 import liquibase.database.structure.Table;
 18  
 import liquibase.database.typeconversion.TypeConverterFactory;
 19  
 import liquibase.exception.ValidationErrors;
 20  
 import liquibase.sql.Sql;
 21  
 import liquibase.sql.UnparsedSql;
 22  
 import liquibase.sqlgenerator.SqlGeneratorChain;
 23  
 import liquibase.statement.core.AddColumnStatement;
 24  
 
 25  48
 public class AddColumnGeneratorDefaultClauseBeforeNotNull extends AddColumnGenerator {
 26  
     @Override
 27  
     public int getPriority() {
 28  2
         return PRIORITY_DATABASE;
 29  
     }
 30  
 
 31  
     @Override
 32  
     public boolean supports(AddColumnStatement statement, Database database) {
 33  16
         return database instanceof OracleDatabase || database instanceof HsqlDatabase || database instanceof H2Database
 34  
                 || database instanceof DerbyDatabase || database instanceof DB2Database
 35  
                 || database instanceof FirebirdDatabase || database instanceof SybaseDatabase
 36  
                 || database instanceof SybaseASADatabase || database instanceof InformixDatabase;
 37  
     }
 38  
 
 39  
     @Override
 40  
     public ValidationErrors validate(AddColumnStatement statement, Database database,
 41  
             SqlGeneratorChain sqlGeneratorChain) {
 42  18
         ValidationErrors validationErrors = super.validate(statement, database, sqlGeneratorChain);
 43  18
         if (database instanceof DerbyDatabase && statement.isAutoIncrement()) {
 44  1
             validationErrors.addError("Cannot add an identity column to a database");
 45  
         }
 46  18
         return validationErrors;
 47  
     }
 48  
 
 49  
     @Override
 50  
     public Sql[] generateSql(AddColumnStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
 51  0
         String alterTable = "ALTER TABLE "
 52  
                 + database.escapeTableName(statement.getSchemaName(), statement.getTableName())
 53  
                 + " ADD "
 54  
                 + database.escapeColumnName(statement.getSchemaName(), statement.getTableName(),
 55  
                         statement.getColumnName())
 56  
                 + " "
 57  
                 + TypeConverterFactory.getInstance().findTypeConverter(database)
 58  
                         .getDataType(statement.getColumnType(), statement.isAutoIncrement());
 59  
 
 60  0
         alterTable += getDefaultClause(statement, database);
 61  
 
 62  0
         if (primaryKeyBeforeNotNull(database)) {
 63  0
             if (statement.isPrimaryKey()) {
 64  0
                 alterTable += " PRIMARY KEY";
 65  
             }
 66  
         }
 67  
 
 68  0
         if (statement.isAutoIncrement()) {
 69  0
             alterTable += " " + database.getAutoIncrementClause();
 70  
         }
 71  
 
 72  0
         if (!statement.isNullable()) {
 73  0
             alterTable += " NOT NULL";
 74  0
         } else if (database instanceof SybaseDatabase || database instanceof SybaseASADatabase) {
 75  0
             alterTable += " NULL";
 76  
         }
 77  
 
 78  0
         if (!primaryKeyBeforeNotNull(database)) {
 79  0
             if (statement.isPrimaryKey()) {
 80  0
                 alterTable += " PRIMARY KEY";
 81  
             }
 82  
         }
 83  0
         if (statement.isUnique()) {
 84  0
             alterTable += " UNIQUE ";
 85  
         }
 86  
 
 87  0
         List<Sql> returnSql = new ArrayList<Sql>();
 88  0
         returnSql.add(new UnparsedSql(alterTable, new Column().setTable(
 89  
                 new Table(statement.getTableName()).setSchema(statement.getSchemaName())).setName(
 90  
                 statement.getColumnName())));
 91  
 
 92  0
         addForeignKeyStatements(statement, database, returnSql);
 93  
 
 94  0
         return returnSql.toArray(new Sql[returnSql.size()]);
 95  
     }
 96  
 
 97  
     private String getDefaultClause(AddColumnStatement statement, Database database) {
 98  0
         String clause = "";
 99  0
         Object defaultValue = statement.getDefaultValue();
 100  0
         if (defaultValue != null) {
 101  0
             clause += " DEFAULT "
 102  
                     + TypeConverterFactory.getInstance().findTypeConverter(database).getDataType(defaultValue)
 103  
                             .convertObjectToString(defaultValue, database);
 104  
         }
 105  0
         return clause;
 106  
     }
 107  
 
 108  
     private boolean primaryKeyBeforeNotNull(Database database) {
 109  0
         return !(database instanceof HsqlDatabase || database instanceof H2Database);
 110  
     }
 111  
 
 112  
 }