Search in sources :

Example 11 with UpdateStatement

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

the class InsertOrUpdateGenerator method getUpdateStatement.

/**
     * 
     * @param insertOrUpdateStatement
     * @param database
     * @param whereClause
     * @param sqlGeneratorChain
     * @return the update statement, if there is nothing to update return null
     */
protected String getUpdateStatement(InsertOrUpdateStatement insertOrUpdateStatement, Database database, String whereClause, SqlGeneratorChain sqlGeneratorChain) throws LiquibaseException {
    StringBuffer updateSqlString = new StringBuffer();
    UpdateGenerator update = new UpdateGenerator();
    UpdateStatement updateStatement = new UpdateStatement(insertOrUpdateStatement.getCatalogName(), insertOrUpdateStatement.getSchemaName(), insertOrUpdateStatement.getTableName());
    if (!(database instanceof OracleDatabase && insertOrUpdateStatement.getOnlyUpdate() != null && insertOrUpdateStatement.getOnlyUpdate())) {
        whereClause += ";\n";
    }
    updateStatement.setWhereClause(whereClause);
    String[] pkFields = insertOrUpdateStatement.getPrimaryKey().split(",");
    HashSet<String> hashPkFields = new HashSet<String>(Arrays.asList(pkFields));
    for (String columnKey : insertOrUpdateStatement.getColumnValues().keySet()) {
        if (!hashPkFields.contains(columnKey)) {
            updateStatement.addNewColumnValue(columnKey, insertOrUpdateStatement.getColumnValue(columnKey));
        }
    }
    // this isn't very elegant but the code fails above without any columns to update
    if (updateStatement.getNewColumnValues().isEmpty()) {
        throw new LiquibaseException("No fields to update in set clause");
    }
    Sql[] updateSql = update.generateSql(updateStatement, database, sqlGeneratorChain);
    for (Sql s : updateSql) {
        updateSqlString.append(s.toSql());
        updateSqlString.append(";");
    }
    updateSqlString.deleteCharAt(updateSqlString.lastIndexOf(";"));
    updateSqlString.append("\n");
    return updateSqlString.toString();
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) InsertOrUpdateStatement(liquibase.statement.core.InsertOrUpdateStatement) UpdateStatement(liquibase.statement.core.UpdateStatement) LiquibaseException(liquibase.exception.LiquibaseException) HashSet(java.util.HashSet) Sql(liquibase.sql.Sql) UnparsedSql(liquibase.sql.UnparsedSql)

Example 12 with UpdateStatement

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

the class UpdateGeneratorTest method testGenerateSqlWithNameValuePlaceholderPairs.

@Test
public void testGenerateSqlWithNameValuePlaceholderPairs() {
    // given
    Database database = new MSSQLDatabase();
    UpdateStatement statement = new UpdateStatement(null, null, "DATABASECHANGELOG").addNewColumnValue("MD5SUM", "7:e27bf9c0c2313160ef960a15d44ced47").setWhereClause(":name = :value AND :name = :value AND :name = :value").addWhereColumnName("ID").addWhereColumnName("AUTHOR").addWhereColumnName("FILENAME").addWhereParameters("SYPA: AUTO_START tüüp INT -> TEXT, vaartus 0 00 17 * * ?", "martin", "db/changelog.xml");
    UpdateGenerator generator = new UpdateGenerator();
    // when
    Sql[] sqls = generator.generateSql(statement, database, null);
    // then
    assertEquals("UPDATE [DATABASECHANGELOG] " + "SET [MD5SUM] = '7:e27bf9c0c2313160ef960a15d44ced47' " + "WHERE [ID] = N'SYPA: AUTO_START tüüp INT -> TEXT, vaartus 0 00 17 * * ?' " + "AND [AUTHOR] = 'martin' " + "AND [FILENAME] = 'db/changelog.xml'", sqls[0].toSql());
}
Also used : UpdateStatement(liquibase.statement.core.UpdateStatement) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Database(liquibase.database.Database) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Sql(liquibase.sql.Sql) Test(org.junit.Test)

Example 13 with UpdateStatement

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

the class Liquibase method clearCheckSums.

/**
     * Sets checksums to null so they will be repopulated next run
     */
public void clearCheckSums() throws LiquibaseException {
    log.info("Clearing database change log checksums");
    LockService lockService = LockServiceFactory.getInstance().getLockService(database);
    lockService.waitForLock();
    try {
        checkLiquibaseTables(false, null, new Contexts(), new LabelExpression());
        UpdateStatement updateStatement = new UpdateStatement(getDatabase().getLiquibaseCatalogName(), getDatabase().getLiquibaseSchemaName(), getDatabase().getDatabaseChangeLogTableName());
        updateStatement.addNewColumnValue("MD5SUM", null);
        ExecutorService.getInstance().getExecutor(database).execute(updateStatement);
        getDatabase().commit();
    } finally {
        try {
            lockService.releaseLock();
        } catch (LockException e) {
            log.severe("Could not release lock", e);
        }
    }
    resetServices();
}
Also used : UpdateStatement(liquibase.statement.core.UpdateStatement) LockService(liquibase.lockservice.LockService) LockException(liquibase.exception.LockException)

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