Search in sources :

Example 1 with UpdateStatement

use of liquibase.statement.core.UpdateStatement in project liquibase by liquibase.

the class UpdateDataChange method generateStatements.

@Override
public SqlStatement[] generateStatements(Database database) {
    boolean needsPreparedStatement = false;
    for (ColumnConfig column : getColumns()) {
        if (column.getValueBlobFile() != null) {
            needsPreparedStatement = true;
        }
        if (column.getValueClobFile() != null) {
            needsPreparedStatement = true;
        }
        if (database instanceof OracleDatabase && column.getType() != null && column.getType().equalsIgnoreCase("CLOB") && column.getValue() != null && column.getValue().length() >= 4000) {
            needsPreparedStatement = true;
        }
    }
    if (needsPreparedStatement) {
        UpdateExecutablePreparedStatement statement = new UpdateExecutablePreparedStatement(database, catalogName, schemaName, tableName, columns, getChangeSet(), this.getResourceAccessor());
        statement.setWhereClause(where);
        for (ColumnConfig whereParam : whereParams) {
            if (whereParam.getName() != null) {
                statement.addWhereColumnName(whereParam.getName());
            }
            statement.addWhereParameter(whereParam.getValueObject());
        }
        return new SqlStatement[] { statement };
    }
    UpdateStatement statement = new UpdateStatement(getCatalogName(), getSchemaName(), getTableName());
    for (ColumnConfig column : getColumns()) {
        statement.addNewColumnValue(column.getName(), column.getValueObject());
    }
    statement.setWhereClause(where);
    for (ColumnConfig whereParam : whereParams) {
        if (whereParam.getName() != null) {
            statement.addWhereColumnName(whereParam.getName());
        }
        statement.addWhereParameter(whereParam.getValueObject());
    }
    return new SqlStatement[] { statement };
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) SqlStatement(liquibase.statement.SqlStatement) UpdateStatement(liquibase.statement.core.UpdateStatement) UpdateExecutablePreparedStatement(liquibase.statement.UpdateExecutablePreparedStatement)

Example 2 with UpdateStatement

use of liquibase.statement.core.UpdateStatement in project liquibase by liquibase.

the class AddColumnChange method generateStatements.

@Override
public SqlStatement[] generateStatements(Database database) {
    List<SqlStatement> sql = new ArrayList<SqlStatement>();
    List<AddColumnStatement> addColumnStatements = new ArrayList<AddColumnStatement>();
    List<UpdateStatement> addColumnUpdateStatements = new ArrayList<UpdateStatement>();
    if (getColumns().size() == 0) {
        return new SqlStatement[] { new AddColumnStatement(catalogName, schemaName, tableName, null, null, null) };
    }
    for (AddColumnConfig column : getColumns()) {
        Set<ColumnConstraint> constraints = new HashSet<ColumnConstraint>();
        ConstraintsConfig constraintsConfig = column.getConstraints();
        if (constraintsConfig != null) {
            if (constraintsConfig.isNullable() != null && !constraintsConfig.isNullable()) {
                constraints.add(new NotNullConstraint());
            }
            if (constraintsConfig.isUnique() != null && constraintsConfig.isUnique()) {
                constraints.add(new UniqueConstraint());
            }
            if (constraintsConfig.isPrimaryKey() != null && constraintsConfig.isPrimaryKey()) {
                constraints.add(new PrimaryKeyConstraint(constraintsConfig.getPrimaryKeyName()));
            }
            if (constraintsConfig.getReferences() != null || (constraintsConfig.getReferencedColumnNames() != null && constraintsConfig.getReferencedTableName() != null)) {
                constraints.add(new ForeignKeyConstraint(constraintsConfig.getForeignKeyName(), constraintsConfig.getReferences(), constraintsConfig.getReferencedTableName(), constraintsConfig.getReferencedColumnNames()));
            }
        }
        if (column.isAutoIncrement() != null && column.isAutoIncrement()) {
            constraints.add(new AutoIncrementConstraint(column.getName(), column.getStartWith(), column.getIncrementBy()));
        }
        AddColumnStatement addColumnStatement = new AddColumnStatement(getCatalogName(), getSchemaName(), getTableName(), column.getName(), column.getType(), column.getDefaultValueObject(), column.getRemarks(), constraints.toArray(new ColumnConstraint[constraints.size()]));
        addColumnStatement.setDefaultValueConstraintName(column.getDefaultValueConstraintName());
        if ((database instanceof MySQLDatabase) && (column.getAfterColumn() != null)) {
            addColumnStatement.setAddAfterColumn(column.getAfterColumn());
        } else if (((database instanceof HsqlDatabase) || (database instanceof H2Database)) && (column.getBeforeColumn() != null)) {
            addColumnStatement.setAddBeforeColumn(column.getBeforeColumn());
        } else if ((database instanceof FirebirdDatabase) && (column.getPosition() != null)) {
            addColumnStatement.setAddAtPosition(column.getPosition());
        }
        addColumnStatements.add(addColumnStatement);
        if (column.getValueObject() != null) {
            UpdateStatement updateStatement = new UpdateStatement(getCatalogName(), getSchemaName(), getTableName());
            updateStatement.addNewColumnValue(column.getName(), column.getValueObject());
            if (database instanceof DB2Database) {
                // Cannot update until table is reorganized in DB2
                addColumnUpdateStatements.add(updateStatement);
            } else {
                sql.add(updateStatement);
            }
        }
    }
    if (database instanceof DB2Database) {
        sql.add(new ReorganizeTableStatement(getCatalogName(), getSchemaName(), getTableName()));
        // Add all the update statements after the reorg table in DB2
        sql.addAll(addColumnUpdateStatements);
    }
    if (addColumnStatements.size() == 1) {
        sql.add(0, addColumnStatements.get(0));
    } else {
        sql.add(0, new AddColumnStatement(addColumnStatements));
    }
    for (ColumnConfig column : getColumns()) {
        String columnRemarks = StringUtils.trimToNull(column.getRemarks());
        if (columnRemarks != null) {
            SetColumnRemarksStatement remarksStatement = new SetColumnRemarksStatement(catalogName, schemaName, tableName, column.getName(), columnRemarks);
            if (SqlGeneratorFactory.getInstance().supports(remarksStatement, database)) {
                sql.add(remarksStatement);
            }
        }
    }
    return sql.toArray(new SqlStatement[sql.size()]);
}
Also used : ReorganizeTableStatement(liquibase.statement.core.ReorganizeTableStatement) ArrayList(java.util.ArrayList) SetColumnRemarksStatement(liquibase.statement.core.SetColumnRemarksStatement) HashSet(java.util.HashSet) UpdateStatement(liquibase.statement.core.UpdateStatement) AddColumnStatement(liquibase.statement.core.AddColumnStatement)

Example 3 with UpdateStatement

use of liquibase.statement.core.UpdateStatement 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 4 with UpdateStatement

use of liquibase.statement.core.UpdateStatement 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 5 with UpdateStatement

use of liquibase.statement.core.UpdateStatement in project liquibase by liquibase.

the class LockDatabaseChangeLogGenerator method generateSql.

@Override
public Sql[] generateSql(LockDatabaseChangeLogStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    String liquibaseSchema = database.getLiquibaseSchemaName();
    String liquibaseCatalog = database.getLiquibaseCatalogName();
    UpdateStatement updateStatement = new UpdateStatement(liquibaseCatalog, liquibaseSchema, database.getDatabaseChangeLogLockTableName());
    updateStatement.addNewColumnValue("LOCKED", true);
    updateStatement.addNewColumnValue("LOCKGRANTED", new Timestamp(new java.util.Date().getTime()));
    updateStatement.addNewColumnValue("LOCKEDBY", hostname + hostDescription + " (" + hostaddress + ")");
    updateStatement.setWhereClause(database.escapeColumnName(liquibaseCatalog, liquibaseSchema, database.getDatabaseChangeLogTableName(), "ID") + " = 1 AND " + database.escapeColumnName(liquibaseCatalog, liquibaseSchema, database.getDatabaseChangeLogTableName(), "LOCKED") + " = " + DataTypeFactory.getInstance().fromDescription("boolean", database).objectToSql(false, database));
    return SqlGeneratorFactory.getInstance().generateSql(updateStatement, database);
}
Also used : UpdateStatement(liquibase.statement.core.UpdateStatement) Timestamp(java.sql.Timestamp)

Aggregations

UpdateStatement (liquibase.statement.core.UpdateStatement)13 Sql (liquibase.sql.Sql)5 SqlStatement (liquibase.statement.SqlStatement)5 Column (liquibase.structure.core.Column)5 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)2 ChangeSet (liquibase.changelog.ChangeSet)2 Database (liquibase.database.Database)2 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)2 OracleDatabase (liquibase.database.core.OracleDatabase)2 LiquibaseException (liquibase.exception.LiquibaseException)2 UnparsedSql (liquibase.sql.UnparsedSql)2 ReorganizeTableStatement (liquibase.statement.core.ReorganizeTableStatement)2 Test (org.junit.Test)2 Timestamp (java.sql.Timestamp)1 Change (liquibase.change.Change)1 TagDatabaseChange (liquibase.change.core.TagDatabaseChange)1 DB2Database (liquibase.database.core.DB2Database)1 AlterTableVisitor (liquibase.database.core.SQLiteDatabase.AlterTableVisitor)1 DatabaseException (liquibase.exception.DatabaseException)1