Search in sources :

Example 11 with Sql

use of liquibase.sql.Sql in project liquibase by liquibase.

the class AddColumnGeneratorSQLite method generateSql.

@Override
public Sql[] generateSql(final AddColumnStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    // 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<Sql> sql = new ArrayList<Sql>();
    // define alter table logic
    SQLiteDatabase.AlterTableVisitor rename_alter_visitor = new SQLiteDatabase.AlterTableVisitor() {

        public ColumnConfig[] getColumnsToAdd() {
            return new ColumnConfig[] { new ColumnConfig().setName(statement.getColumnName()).setType(statement.getColumnType()).setAutoIncrement(statement.isAutoIncrement()) };
        }

        public boolean copyThisColumn(ColumnConfig column) {
            return !column.getName().equals(statement.getColumnName());
        }

        public boolean createThisColumn(ColumnConfig column) {
            return true;
        }

        public boolean createThisIndex(Index index) {
            return true;
        }
    };
    try {
        // alter table
        List<SqlStatement> alterTableStatements = SQLiteDatabase.getAlterTableStatements(rename_alter_visitor, database, statement.getCatalogName(), statement.getSchemaName(), statement.getTableName());
        sql.addAll(Arrays.asList(SqlGeneratorFactory.getInstance().generateSql(alterTableStatements.toArray(new SqlStatement[alterTableStatements.size()]), database)));
    } catch (DatabaseException e) {
        System.err.println(e);
        e.printStackTrace();
    }
    return sql.toArray(new Sql[sql.size()]);
}
Also used : SqlStatement(liquibase.statement.SqlStatement) ColumnConfig(liquibase.change.ColumnConfig) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) ArrayList(java.util.ArrayList) Index(liquibase.structure.core.Index) DatabaseException(liquibase.exception.DatabaseException) Sql(liquibase.sql.Sql) UnparsedSql(liquibase.sql.UnparsedSql)

Example 12 with Sql

use of liquibase.sql.Sql in project liquibase by liquibase.

the class AddDefaultValueGeneratorPostgres method generateSql.

@Override
public Sql[] generateSql(final AddDefaultValueStatement statement, final Database database, final SqlGeneratorChain sqlGeneratorChain) {
    if (!(statement.getDefaultValue() instanceof SequenceNextValueFunction)) {
        return super.generateSql(statement, database, sqlGeneratorChain);
    }
    List<Sql> commands = new ArrayList<Sql>(Arrays.asList(super.generateSql(statement, database, sqlGeneratorChain)));
    // for postgres, we need to also set the sequence to be owned by this table for true serial like functionality.
    // this will allow a drop table cascade to remove the sequence as well.
    SequenceNextValueFunction sequenceFunction = (SequenceNextValueFunction) statement.getDefaultValue();
    String sequenceName = sequenceFunction.getValue();
    String sequenceSchemaName = sequenceFunction.getSequenceSchemaName();
    String sequence = database.escapeObjectName(null, sequenceSchemaName, sequenceName, Sequence.class);
    Sql alterSequenceOwner = new UnparsedSql("ALTER SEQUENCE " + sequence + " OWNED BY " + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()) + "." + database.escapeObjectName(statement.getColumnName(), Column.class), getAffectedColumn(statement), getAffectedSequence(sequenceFunction));
    commands.add(alterSequenceOwner);
    return commands.toArray(new Sql[commands.size()]);
}
Also used : UnparsedSql(liquibase.sql.UnparsedSql) ArrayList(java.util.ArrayList) SequenceNextValueFunction(liquibase.statement.SequenceNextValueFunction) Sql(liquibase.sql.Sql) UnparsedSql(liquibase.sql.UnparsedSql)

Example 13 with Sql

use of liquibase.sql.Sql in project liquibase by liquibase.

the class SqlGeneratorFactory method getAffectedDatabaseObjects.

public Set<DatabaseObject> getAffectedDatabaseObjects(SqlStatement statement, Database database) {
    Set<DatabaseObject> affectedObjects = new HashSet<DatabaseObject>();
    SqlGeneratorChain sqlGeneratorChain = createGeneratorChain(statement, database);
    if (sqlGeneratorChain != null) {
        //noinspection unchecked
        Sql[] sqls = sqlGeneratorChain.generateSql(statement, database);
        if (sqls != null) {
            for (Sql sql : sqls) {
                affectedObjects.addAll(sql.getAffectedDatabaseObjects());
            }
        }
    }
    return affectedObjects;
}
Also used : DatabaseObject(liquibase.structure.DatabaseObject) Sql(liquibase.sql.Sql)

Example 14 with Sql

use of liquibase.sql.Sql in project liquibase by liquibase.

the class SqlGeneratorFactory method generateSql.

public Sql[] generateSql(SqlStatement[] statements, Database database) {
    List<Sql> returnList = new ArrayList<Sql>();
    SqlGeneratorFactory factory = SqlGeneratorFactory.getInstance();
    for (SqlStatement statement : statements) {
        Sql[] sqlArray = factory.generateSql(statement, database);
        if (sqlArray != null && sqlArray.length > 0) {
            List<Sql> sqlList = Arrays.asList(sqlArray);
            returnList.addAll(sqlList);
        }
    }
    return returnList.toArray(new Sql[returnList.size()]);
}
Also used : SqlStatement(liquibase.statement.SqlStatement) Sql(liquibase.sql.Sql)

Example 15 with Sql

use of liquibase.sql.Sql in project liquibase by liquibase.

the class AddAutoIncrementGeneratorMySQL method generateSql.

@Override
public Sql[] generateSql(final AddAutoIncrementStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    Sql[] sql = super.generateSql(statement, database, sqlGeneratorChain);
    if (statement.getStartWith() != null) {
        MySQLDatabase mysqlDatabase = (MySQLDatabase) database;
        String alterTableSql = "ALTER TABLE " + mysqlDatabase.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()) + " " + mysqlDatabase.getTableOptionAutoIncrementStartWithClause(statement.getStartWith());
        sql = concact(sql, new UnparsedSql(alterTableSql, getAffectedTable(statement)));
    }
    return sql;
}
Also used : UnparsedSql(liquibase.sql.UnparsedSql) MySQLDatabase(liquibase.database.core.MySQLDatabase) UnparsedSql(liquibase.sql.UnparsedSql) Sql(liquibase.sql.Sql)

Aggregations

Sql (liquibase.sql.Sql)95 Test (org.junit.Test)55 Database (liquibase.database.Database)42 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)40 AbstractSqlGeneratorTest (liquibase.sqlgenerator.AbstractSqlGeneratorTest)37 OracleDatabase (liquibase.database.core.OracleDatabase)36 PostgresDatabase (liquibase.database.core.PostgresDatabase)36 MySQLDatabase (liquibase.database.core.MySQLDatabase)35 DB2Database (liquibase.database.core.DB2Database)33 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)33 SybaseDatabase (liquibase.database.core.SybaseDatabase)33 AutoIncrementConstraint (liquibase.statement.AutoIncrementConstraint)33 HsqlDatabase (liquibase.database.core.HsqlDatabase)32 SybaseASADatabase (liquibase.database.core.SybaseASADatabase)32 CreateTableStatement (liquibase.statement.core.CreateTableStatement)32 DerbyDatabase (liquibase.database.core.DerbyDatabase)31 H2Database (liquibase.database.core.H2Database)31 UnparsedSql (liquibase.sql.UnparsedSql)31 ArrayList (java.util.ArrayList)11 CatalogAndSchema (liquibase.CatalogAndSchema)8