Coverage Report - liquibase.sqlgenerator.core.DropDefaultValueGenerator
 
Classes in this File Line Coverage Branch Coverage Complexity
DropDefaultValueGenerator
24%
8/33
13%
3/22
5
 
 1  
 package liquibase.sqlgenerator.core;
 2  
 
 3  
 import liquibase.database.Database;
 4  
 import liquibase.database.core.DerbyDatabase;
 5  
 import liquibase.database.core.InformixDatabase;
 6  
 import liquibase.database.core.MSSQLDatabase;
 7  
 import liquibase.database.core.MaxDBDatabase;
 8  
 import liquibase.database.core.MySQLDatabase;
 9  
 import liquibase.database.core.OracleDatabase;
 10  
 import liquibase.database.core.SQLiteDatabase;
 11  
 import liquibase.database.core.SybaseASADatabase;
 12  
 import liquibase.exception.DatabaseException;
 13  
 import liquibase.exception.UnexpectedLiquibaseException;
 14  
 import liquibase.exception.ValidationErrors;
 15  
 import liquibase.sql.Sql;
 16  
 import liquibase.sql.UnparsedSql;
 17  
 import liquibase.sqlgenerator.SqlGeneratorChain;
 18  
 import liquibase.statement.core.DropDefaultValueStatement;
 19  
 
 20  86
 public class DropDefaultValueGenerator extends AbstractSqlGenerator<DropDefaultValueStatement> {
 21  
 
 22  
     @Override
 23  
     public boolean supports(DropDefaultValueStatement statement, Database database) {
 24  61
         return !(database instanceof SQLiteDatabase);
 25  
     }
 26  
 
 27  
     @Override
 28  
     public ValidationErrors validate(DropDefaultValueStatement dropDefaultValueStatement, Database database,
 29  
             SqlGeneratorChain sqlGeneratorChain) {
 30  15
         ValidationErrors validationErrors = new ValidationErrors();
 31  15
         validationErrors.checkRequiredField("tableName", dropDefaultValueStatement.getTableName());
 32  15
         validationErrors.checkRequiredField("columnName", dropDefaultValueStatement.getColumnName());
 33  
 
 34  15
         if (database instanceof InformixDatabase) {
 35  1
             validationErrors.checkRequiredField("columnDataType", dropDefaultValueStatement.getColumnDataType());
 36  
         }
 37  
 
 38  15
         return validationErrors;
 39  
     }
 40  
 
 41  
     @Override
 42  
     public Sql[] generateSql(DropDefaultValueStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
 43  
         String sql;
 44  0
         if (database instanceof MSSQLDatabase) {
 45  0
             String productVersion = null;
 46  
             try {
 47  0
                 productVersion = database.getDatabaseProductVersion();
 48  0
             } catch (DatabaseException e) {
 49  0
                 throw new UnexpectedLiquibaseException(e);
 50  0
             }
 51  0
             if (productVersion.startsWith("9") || productVersion.startsWith("10")) { // SQL Server 2005/2008
 52  
                 // SQL Server 2005 does not often work with the simpler query shown below
 53  0
                 String query = "DECLARE @default sysname\n";
 54  0
                 query += "SELECT @default = object_name(default_object_id) FROM sys.columns WHERE object_id=object_id('"
 55  
                         + statement.getSchemaName()
 56  
                         + "."
 57  
                         + statement.getTableName()
 58  
                         + "') AND name='"
 59  
                         + statement.getColumnName() + "'\n";
 60  0
                 query += "EXEC ('ALTER TABLE "
 61  
                         + database.escapeTableName(statement.getSchemaName(), statement.getTableName())
 62  
                         + " DROP CONSTRAINT ' + @default)";
 63  
                 // System.out.println("DROP QUERY : " + query);
 64  0
                 sql = query;
 65  0
             } else {
 66  
                 // FIXME this syntax does not supported by MSSQL 2000
 67  0
                 sql = "ALTER TABLE "
 68  
                         + database.escapeTableName(statement.getSchemaName(), statement.getTableName())
 69  
                         + " DROP CONSTRAINT select d.name from syscolumns c,sysobjects d, sysobjects t where c.id=t.id AND d.parent_obj=t.id AND d.type='D' AND t.type='U' AND c.name='"
 70  
                         + statement.getColumnName() + "' AND t.name='" + statement.getTableName() + "'";
 71  
             }
 72  0
         } else if (database instanceof MySQLDatabase) {
 73  0
             sql = "ALTER TABLE "
 74  
                     + database.escapeTableName(statement.getSchemaName(), statement.getTableName())
 75  
                     + " ALTER "
 76  
                     + database.escapeColumnName(statement.getSchemaName(), statement.getTableName(),
 77  
                             statement.getColumnName()) + " DROP DEFAULT";
 78  0
         } else if (database instanceof OracleDatabase || database instanceof SybaseASADatabase) {
 79  0
             sql = "ALTER TABLE "
 80  
                     + database.escapeTableName(statement.getSchemaName(), statement.getTableName())
 81  
                     + " MODIFY "
 82  
                     + database.escapeColumnName(statement.getSchemaName(), statement.getTableName(),
 83  
                             statement.getColumnName()) + " DEFAULT NULL";
 84  0
         } else if (database instanceof DerbyDatabase) {
 85  0
             sql = "ALTER TABLE "
 86  
                     + database.escapeTableName(statement.getSchemaName(), statement.getTableName())
 87  
                     + " ALTER COLUMN  "
 88  
                     + database.escapeColumnName(statement.getSchemaName(), statement.getTableName(),
 89  
                             statement.getColumnName()) + " WITH DEFAULT NULL";
 90  0
         } else if (database instanceof MaxDBDatabase) {
 91  0
             sql = "ALTER TABLE "
 92  
                     + database.escapeTableName(statement.getSchemaName(), statement.getTableName())
 93  
                     + " COLUMN  "
 94  
                     + database.escapeColumnName(statement.getSchemaName(), statement.getTableName(),
 95  
                             statement.getColumnName()) + " DROP DEFAULT";
 96  0
         } else if (database instanceof InformixDatabase) {
 97  
             /*
 98  
              * TODO If dropped from a not null column the not null constraint will be dropped, too. If the column is
 99  
              * "NOT NULL" it has to be added behind the datatype.
 100  
              */
 101  0
             sql = "ALTER TABLE "
 102  
                     + database.escapeTableName(statement.getSchemaName(), statement.getTableName())
 103  
                     + " MODIFY ("
 104  
                     + database.escapeColumnName(statement.getSchemaName(), statement.getTableName(),
 105  
                             statement.getColumnName()) + " " + statement.getColumnDataType() + ")";
 106  
         } else {
 107  0
             sql = "ALTER TABLE "
 108  
                     + database.escapeTableName(statement.getSchemaName(), statement.getTableName())
 109  
                     + " ALTER COLUMN  "
 110  
                     + database.escapeColumnName(statement.getSchemaName(), statement.getTableName(),
 111  
                             statement.getColumnName()) + " SET DEFAULT NULL";
 112  
         }
 113  
 
 114  0
         return new Sql[] { new UnparsedSql(sql) };
 115  
     }
 116  
 }