public ValidationErrors validate( InsertOrUpdateStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) { ValidationErrors validationErrors = new ValidationErrors(); validationErrors.checkRequiredField("tableName", statement.getTableName()); validationErrors.checkRequiredField("columns", statement.getColumnValues()); validationErrors.checkRequiredField("primaryKey", statement.getPrimaryKey()); return validationErrors; }
@Override protected String getRecordCheck( InsertOrUpdateStatement insertOrUpdateStatement, Database database, String whereClause) { StringBuilder sql = new StringBuilder(); sql.append("MERGE INTO "); sql.append(insertOrUpdateStatement.getTableName()); sql.append(" USING (VALUES (1)) ON "); sql.append(whereClause); sql.append(" WHEN NOT MATCHED THEN "); return sql.toString(); }
protected String getWhereClause( InsertOrUpdateStatement insertOrUpdateStatement, Database database) { StringBuffer where = new StringBuffer(); String[] pkColumns = insertOrUpdateStatement.getPrimaryKey().split(","); for (String thisPkColumn : pkColumns) { where .append( database.escapeColumnName( insertOrUpdateStatement.getSchemaName(), insertOrUpdateStatement.getTableName(), thisPkColumn)) .append(" = "); Object newValue = insertOrUpdateStatement.getColumnValues().get(thisPkColumn); if (newValue == null || newValue.toString().equals("NULL")) { where.append("NULL"); } else if (newValue instanceof String && database.shouldQuoteValue(((String) newValue))) { where.append("'").append(database.escapeStringForDatabase((String) newValue)).append("'"); } else if (newValue instanceof Date) { where.append(database.getDateLiteral(((Date) newValue))); } else if (newValue instanceof Boolean) { if (((Boolean) newValue)) { where.append( TypeConverterFactory.getInstance() .findTypeConverter(database) .getBooleanType() .getTrueBooleanValue()); } else { where.append( TypeConverterFactory.getInstance() .findTypeConverter(database) .getBooleanType() .getFalseBooleanValue()); } } else { where.append(newValue); } where.append(" AND "); } where.delete(where.lastIndexOf(" AND "), where.lastIndexOf(" AND ") + " AND ".length()); return where.toString(); }
/** * @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.getSchemaName(), insertOrUpdateStatement.getTableName()); updateStatement.setWhereClause(whereClause + ";\n"); 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(); }