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();
}
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());
}
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();
}
Aggregations