package liquibase.sqlgenerator.core;

import liquibase.database.Database;
import liquibase.database.core.*;
import liquibase.structure.core.Column;
import liquibase.structure.core.Schema;
import liquibase.structure.core.Table;
import liquibase.datatype.DataTypeFactory;
import liquibase.exception.ValidationErrors;
import liquibase.sql.Sql;
import liquibase.sql.UnparsedSql;
import liquibase.sqlgenerator.SqlGeneratorChain;
import liquibase.statement.core.AddAutoIncrementStatement;

public class AddAutoIncrementGenerator extends AbstractSqlGenerator<AddAutoIncrementStatement> {

    @Override
    public int getPriority() {
        return PRIORITY_DEFAULT;
    }

    @Override
    public boolean supports(AddAutoIncrementStatement statement, Database database) {
        return (database.supportsAutoIncrement()
                && !(database instanceof DerbyDatabase)
                && !(database instanceof MSSQLDatabase)
                && !(database instanceof HsqlDatabase)
                && !(database instanceof H2Database)
                && !(database instanceof OracleDatabase));
    }

    @Override
    public ValidationErrors validate(
    		AddAutoIncrementStatement statement,
    		Database database,
    		SqlGeneratorChain sqlGeneratorChain) {
        ValidationErrors validationErrors = new ValidationErrors();

        validationErrors.checkRequiredField("columnName", statement.getColumnName());
        validationErrors.checkRequiredField("tableName", statement.getTableName());
        validationErrors.checkRequiredField("columnDataType", statement.getColumnDataType());


        return validationErrors;
    }

    @Override
    public Sql[] generateSql(
    		AddAutoIncrementStatement statement,
    		Database database,
    		SqlGeneratorChain sqlGeneratorChain) {
        String sql = "ALTER TABLE "
            + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName())
            + " MODIFY "
            + database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), statement.getColumnName())
            + " "
            + DataTypeFactory.getInstance().fromDescription(statement.getColumnDataType() + "{autoIncrement:true}", database).toDatabaseDataType(database)
            + " " 
            + database.getAutoIncrementClause(statement.getStartWith(), statement.getIncrementBy());

        return new Sql[]{
            new UnparsedSql(sql, getAffectedColumn(statement))
        };
    }

    protected Column getAffectedColumn(AddAutoIncrementStatement statement) {
        return new Column()
            .setRelation(new Table().setName(statement.getTableName()).setSchema(new Schema(statement.getCatalogName(), statement.getSchemaName())))
            .setName(statement.getColumnName());
    }
}

@@ -1800,18 +1800,549 @@
     
-    String
+%09String sql;%0A    %09if (database instanceof SybaseASADatabase) %7B%0A%09        sql = %22ALTER TABLE %22%0A%09%09            + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName())%0A%09%09            + %22 ALTER %22%0A%09%09            + database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), statement.getColumnName())%0A%09%09            + %22 SET %22 %0A%09%09            + database.getAutoIncrementClause(statement.getStartWith(), statement.getIncrementBy());%0A    %09%7D else %7B%0A%09       
  sql
@@ -2359,16 +2359,17 @@
 TABLE %22%0A
+%09
         
@@ -2472,32 +2472,33 @@
 getTableName())%0A
+%09
             + %22 
@@ -2506,16 +2506,17 @@
 ODIFY %22%0A
+%09
         
@@ -2655,16 +2655,17 @@
 Name())%0A
+%09
         
@@ -2674,16 +2674,17 @@
   + %22 %22%0A
+%09
         
@@ -2822,32 +2822,33 @@
 aType(database)%0A
+%09
             + %22 
@@ -2850,16 +2850,17 @@
  + %22 %22 %0A
+%09
         
@@ -2952,16 +2952,22 @@
 tBy());%0A
+    %09%7D
 %0A       

correct AddAutoIncrement for SQLAnywhere