package liquibase.change.core;

import liquibase.change.*;
import liquibase.database.Database;
import liquibase.snapshot.SnapshotGeneratorFactory;
import liquibase.statement.SqlStatement;
import liquibase.statement.core.DropDefaultValueStatement;
import liquibase.structure.core.Column;
import liquibase.structure.core.Table;

/**
 * Removes the default value from an existing column.
 */
@DatabaseChange(name="dropDefaultValue", description="Removes the database default value for a column", priority = ChangeMetaData.PRIORITY_DEFAULT, appliesTo = "column")
public class DropDefaultValueChange extends AbstractChange {

    private String catalogName;
    private String schemaName;
    private String tableName;
    private String columnName;
    private String columnDataType;

    @DatabaseChangeProperty(mustEqualExisting ="column.relation.catalog", since = "3.0")
    public String getCatalogName() {
        return catalogName;
    }

    public void setCatalogName(String catalogName) {
        this.catalogName = catalogName;
    }

    @DatabaseChangeProperty(mustEqualExisting ="column.relation.schema")
    public String getSchemaName() {
        return schemaName;
    }

    public void setSchemaName(String schemaName) {
        this.schemaName = schemaName;
    }

    @DatabaseChangeProperty(mustEqualExisting = "column.relation", description = "Name of the table to containing the column")
    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    @DatabaseChangeProperty(mustEqualExisting = "column", description = "Name of column to drop the default value from")
    public String getColumnName() {
        return columnName;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

    @DatabaseChangeProperty()
    public String getColumnDataType() {
		return columnDataType;
	}
    
    public void setColumnDataType(String columnDataType) {
		this.columnDataType = columnDataType;
	}

    @Override
    public SqlStatement[] generateStatements(Database database) {

//todo    	if (database instanceof SQLiteDatabase) {
//    		// return special statements for SQLite databases
//    		return generateStatementsForSQLiteDatabase(database);
//        }

        return new SqlStatement[]{
                new DropDefaultValueStatement(getCatalogName(), getSchemaName(), getTableName(), getColumnName(), getColumnDataType()),
        };
    }

    @Override
    public ChangeStatus checkStatus(Database database) {
        try {
            Column snapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(new Column(Table.class, getCatalogName(), getSchemaName(), getTableName(), getColumnName()), database);
            return new ChangeStatus().assertComplete(snapshot.getDefaultValue() == null, "Column has a default value");
        } catch (Exception e) {
            return new ChangeStatus().unknown(e);
        }

    }
    
//    private SqlStatement[] generateStatementsForSQLiteDatabase(Database database) {
//
//    	// SQLite does not support this ALTER TABLE operation until now.
//		// For more information see: http://www.sqlite.org/omitted.html.
//		// This is a small work around...
//
//    	List<SqlStatement> statements = new ArrayList<SqlStatement>();
//
//		// define alter table logic
//		AlterTableVisitor rename_alter_visitor = new AlterTableVisitor() {
//			public ColumnConfig[] getColumnsToAdd() {
//				return new ColumnConfig[0];
//			}
//			public boolean copyThisColumn(ColumnConfig column) {
//				return true;
//			}
//			public boolean createThisColumn(ColumnConfig column) {
//				if (column.getName().equals(getColumnName())) {
//					column.setDefaultValue(null);
//					column.setDefaultValueBoolean(null);
//					column.setDefaultValueDate((Date)null);
//					column.setDefaultValueNumeric((Number)null);
//				}
//				return true;
//			}
//			public boolean createThisIndex(Index index) {
//				return true;
//			}
//		};
//
//    	try {
//    		// alter table
//			statements.addAll(SQLiteDatabase.getAlterTableStatements(
//					rename_alter_visitor,
//					database,getCatalogName(), getSchemaName(),getTableName()));
//    	} catch (Exception e) {
//			e.printStackTrace();
//		}
//
//    	return statements.toArray(new SqlStatement[statements.size()]);
//    }

    @Override
    public String getConfirmationMessage() {
        return "Default value dropped from "+getTableName()+"."+getColumnName();
    }

    @Override
    public String getSerializedObjectNamespace() {
        return STANDARD_CHANGELOG_NAMESPACE;
    }
}

@@ -3005,1388 +3005,8 @@
    %7D
-%0A    %0A//    private SqlStatement%5B%5D generateStatementsForSQLiteDatabase(Database database) %7B%0A//%0A//    %09// SQLite does not support this ALTER TABLE operation until now.%0A//%09%09// For more information see: http://www.sqlite.org/omitted.html.%0A//%09%09// This is a small work around...%0A//%0A//    %09List%3CSqlStatement%3E statements = new ArrayList%3CSqlStatement%3E();%0A//%0A//%09%09// define alter table logic%0A//%09%09AlterTableVisitor rename_alter_visitor = new AlterTableVisitor() %7B%0A//%09%09%09public ColumnConfig%5B%5D getColumnsToAdd() %7B%0A//%09%09%09%09return new ColumnConfig%5B0%5D;%0A//%09%09%09%7D%0A//%09%09%09public boolean copyThisColumn(ColumnConfig column) %7B%0A//%09%09%09%09return true;%0A//%09%09%09%7D%0A//%09%09%09public boolean createThisColumn(ColumnConfig column) %7B%0A//%09%09%09%09if (column.getName().equals(getColumnName())) %7B%0A//%09%09%09%09%09column.setDefaultValue(null);%0A//%09%09%09%09%09column.setDefaultValueBoolean(null);%0A//%09%09%09%09%09column.setDefaultValueDate((Date)null);%0A//%09%09%09%09%09column.setDefaultValueNumeric((Number)null);%0A//%09%09%09%09%7D%0A//%09%09%09%09return true;%0A//%09%09%09%7D%0A//%09%09%09public boolean createThisIndex(Index index) %7B%0A//%09%09%09%09return true;%0A//%09%09%09%7D%0A//%09%09%7D;%0A//%0A//    %09try %7B%0A//    %09%09// alter table%0A//%09%09%09statements.addAll(SQLiteDatabase.getAlterTableStatements(%0A//%09%09%09%09%09rename_alter_visitor,%0A//%09%09%09%09%09database,getCatalogName(), getSchemaName(),getTableName()));%0A//    %09%7D catch (Exception e) %7B%0A//%09%09%09e.printStackTrace();%0A//%09%09%7D%0A//%0A//    %09return statements.toArray(new SqlStatement%5Bstatements.size()%5D);%0A//    %7D
 %0A%0A  

Remove dead (commented out) Java code.