Search in sources :

Example 11 with SqlStatement

use of liquibase.statement.SqlStatement in project liquibase by liquibase.

the class RenameTableChange method generateStatements.

@Override
public SqlStatement[] generateStatements(Database database) {
    List<SqlStatement> statements = new ArrayList<SqlStatement>();
    statements.add(new RenameTableStatement(getCatalogName(), getSchemaName(), getOldTableName(), getNewTableName()));
    if (database instanceof DB2Database) {
        statements.add(new ReorganizeTableStatement(getCatalogName(), getSchemaName(), getNewTableName()));
    }
    return statements.toArray(new SqlStatement[statements.size()]);
}
Also used : DB2Database(liquibase.database.core.DB2Database) SqlStatement(liquibase.statement.SqlStatement) ReorganizeTableStatement(liquibase.statement.core.ReorganizeTableStatement) ArrayList(java.util.ArrayList) RenameTableStatement(liquibase.statement.core.RenameTableStatement)

Example 12 with SqlStatement

use of liquibase.statement.SqlStatement in project liquibase by liquibase.

the class AddAutoIncrementChange method generateStatements.

@Override
public SqlStatement[] generateStatements(Database database) {
    if (database instanceof PostgresDatabase) {
        String sequenceName = (getTableName() + "_" + getColumnName() + "_seq");
        String escapedTableName = database.escapeObjectName(getTableName(), Table.class);
        String escapedColumnName = database.escapeObjectName(getColumnName(), Table.class);
        if (escapedTableName != null && escapedColumnName != null && !escapedTableName.startsWith("\"") && !escapedColumnName.startsWith("\"")) {
            sequenceName = sequenceName.toLowerCase();
        }
        String schemaPrefix;
        if (this.schemaName == null) {
            schemaPrefix = database.getDefaultSchemaName();
        } else {
            schemaPrefix = this.schemaName;
        }
        if (schemaPrefix == null) {
            schemaPrefix = "";
        } else {
            schemaPrefix = schemaPrefix + ".";
        }
        return new SqlStatement[] { new CreateSequenceStatement(catalogName, this.schemaName, sequenceName), new SetNullableStatement(catalogName, this.schemaName, getTableName(), getColumnName(), null, false), new AddDefaultValueStatement(catalogName, this.schemaName, getTableName(), getColumnName(), getColumnDataType(), new SequenceNextValueFunction(schemaPrefix + sequenceName)) };
    }
    return new SqlStatement[] { new AddAutoIncrementStatement(getCatalogName(), getSchemaName(), getTableName(), getColumnName(), getColumnDataType(), getStartWith(), getIncrementBy()) };
}
Also used : PostgresDatabase(liquibase.database.core.PostgresDatabase) SqlStatement(liquibase.statement.SqlStatement) SetNullableStatement(liquibase.statement.core.SetNullableStatement) CreateSequenceStatement(liquibase.statement.core.CreateSequenceStatement) SequenceNextValueFunction(liquibase.statement.SequenceNextValueFunction) AddAutoIncrementStatement(liquibase.statement.core.AddAutoIncrementStatement) AddDefaultValueStatement(liquibase.statement.core.AddDefaultValueStatement)

Example 13 with SqlStatement

use of liquibase.statement.SqlStatement in project liquibase by liquibase.

the class AddNotNullConstraintChange method generateStatementsForSQLiteDatabase.

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>();
    if (defaultNullValue != null) {
        statements.add(new UpdateStatement(getCatalogName(), getSchemaName(), getTableName()).addNewColumnValue(getColumnName(), getDefaultNullValue()).setWhereClause(getColumnName() + " IS NULL"));
    }
    //		// ... test if column contains NULL values
    //		if (defaultNullValue == null) {
    //			List<Map> null_rows = null;
    //			try {
    //				null_rows = database.getExecutor().
    //					queryForList(new RawSqlStatement(
    //						"SELECT * FROM `"+
    //						database.escapeTableName(getSchemaName(), getTableName())+
    //						"` WHERE `"+getColumnName()+"` IS NULL;"));
    //			} catch (DatabaseException e) {
    //				e.printStackTrace();
    //			}
    //    		if (null_rows.size()>0) {
    //    			throw new UnsupportedChangeException(
    //    					"Failed to add a Not-Null-Constraint because " +
    //    					"some values are null. Use the " +
    //    					"defaultNullValue attribute to define default " +
    //    					"values for the existing null values.");
    //    		}
    //    	}
    // define alter table logic
    AlterTableVisitor rename_alter_visitor = new AlterTableVisitor() {

        @Override
        public ColumnConfig[] getColumnsToAdd() {
            return new ColumnConfig[0];
        }

        @Override
        public boolean copyThisColumn(ColumnConfig column) {
            return true;
        }

        @Override
        public boolean createThisColumn(ColumnConfig column) {
            if (column.getName().equals(getColumnName())) {
                column.getConstraints().setNullable(false);
            }
            return true;
        }

        @Override
        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()]);
}
Also used : SqlStatement(liquibase.statement.SqlStatement) UpdateStatement(liquibase.statement.core.UpdateStatement) AlterTableVisitor(liquibase.database.core.SQLiteDatabase.AlterTableVisitor) ArrayList(java.util.ArrayList) Index(liquibase.structure.core.Index)

Example 14 with SqlStatement

use of liquibase.statement.SqlStatement in project liquibase by liquibase.

the class AddNotNullConstraintChange method generateStatements.

@Override
public SqlStatement[] generateStatements(Database database) {
    ////        if (database instanceof SQLiteDatabase) {
    //    		// return special statements for SQLite databases
    //    		return generateStatementsForSQLiteDatabase(database);
    //        }
    List<SqlStatement> statements = new ArrayList<SqlStatement>();
    if (defaultNullValue != null) {
        statements.add(new UpdateStatement(getCatalogName(), getSchemaName(), getTableName()).addNewColumnValue(getColumnName(), defaultNullValue).setWhereClause(database.escapeObjectName(getColumnName(), Column.class) + " IS NULL"));
    }
    statements.add(new SetNullableStatement(getCatalogName(), getSchemaName(), getTableName(), getColumnName(), getColumnDataType(), false, getConstraintName()));
    if (database instanceof DB2Database) {
        statements.add(new ReorganizeTableStatement(getCatalogName(), getSchemaName(), getTableName()));
    }
    return statements.toArray(new SqlStatement[statements.size()]);
}
Also used : DB2Database(liquibase.database.core.DB2Database) SqlStatement(liquibase.statement.SqlStatement) UpdateStatement(liquibase.statement.core.UpdateStatement) SetNullableStatement(liquibase.statement.core.SetNullableStatement) Column(liquibase.structure.core.Column) ReorganizeTableStatement(liquibase.statement.core.ReorganizeTableStatement) ArrayList(java.util.ArrayList)

Example 15 with SqlStatement

use of liquibase.statement.SqlStatement in project liquibase by liquibase.

the class AddUniqueConstraintChange method generateStatements.

@Override
public SqlStatement[] generateStatements(Database database) {
    //todo    	if (database instanceof SQLiteDatabase) {
    //    		// return special statements for SQLite databases
    //    		return generateStatementsForSQLiteDatabase(database);
    //        }
    boolean deferrable = false;
    if (getDeferrable() != null) {
        deferrable = getDeferrable();
    }
    boolean initiallyDeferred = false;
    if (getInitiallyDeferred() != null) {
        initiallyDeferred = getInitiallyDeferred();
    }
    boolean disabled = false;
    if (getDisabled() != null) {
        disabled = getDisabled();
    }
    boolean clustered = false;
    if (getClustered() != null) {
        clustered = getClustered();
    }
    AddUniqueConstraintStatement statement = new AddUniqueConstraintStatement(getCatalogName(), getSchemaName(), getTableName(), ColumnConfig.arrayFromNames(getColumnNames()), getConstraintName());
    statement.setTablespace(getTablespace()).setDeferrable(deferrable).setInitiallyDeferred(initiallyDeferred).setDisabled(disabled).setClustered(clustered);
    statement.setForIndexName(getForIndexName());
    statement.setForIndexSchemaName(getForIndexSchemaName());
    statement.setForIndexCatalogName(getForIndexCatalogName());
    return new SqlStatement[] { statement };
}
Also used : SqlStatement(liquibase.statement.SqlStatement) AddUniqueConstraintStatement(liquibase.statement.core.AddUniqueConstraintStatement)

Aggregations

SqlStatement (liquibase.statement.SqlStatement)49 ArrayList (java.util.ArrayList)17 Test (org.junit.Test)9 Column (liquibase.structure.core.Column)8 DB2Database (liquibase.database.core.DB2Database)7 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)7 ReorganizeTableStatement (liquibase.statement.core.ReorganizeTableStatement)7 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)6 DatabaseException (liquibase.exception.DatabaseException)6 Database (liquibase.database.Database)5 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)5 Sql (liquibase.sql.Sql)5 RawSqlStatement (liquibase.statement.core.RawSqlStatement)5 UpdateStatement (liquibase.statement.core.UpdateStatement)5 Index (liquibase.structure.core.Index)5 Change (liquibase.change.Change)4 ColumnConfig (liquibase.change.ColumnConfig)4 AddColumnChange (liquibase.change.core.AddColumnChange)3 PostgresDatabase (liquibase.database.core.PostgresDatabase)3 AlterTableVisitor (liquibase.database.core.SQLiteDatabase.AlterTableVisitor)3