1 package liquibase.change.core; 2 3 import liquibase.change.AbstractChange; 4 import liquibase.change.ChangeMetaData; 5 import liquibase.change.ChangeWithColumns; 6 import liquibase.change.ColumnConfig; 7 import liquibase.change.TextNode; 8 import liquibase.database.Database; 9 import liquibase.statement.SqlStatement; 10 import liquibase.statement.core.UpdateStatement; 11 import liquibase.util.StringUtils; 12 13 import java.util.ArrayList; 14 import java.util.List; 15 16 public class UpdateDataChange extends AbstractChange implements ChangeWithColumns { 17 18 private String schemaName; 19 private String tableName; 20 private List<ColumnConfig> columns; 21 22 @TextNode(nodeName = "where") 23 private String whereClause; 24 25 public UpdateDataChange() { 26 super("update", "Update Data", ChangeMetaData.PRIORITY_DEFAULT); 27 columns = new ArrayList<ColumnConfig>(); 28 } 29 30 public String getSchemaName() { 31 return schemaName; 32 } 33 34 public void setSchemaName(String schemaName) { 35 this.schemaName = StringUtils.trimToNull(schemaName); 36 } 37 38 public String getTableName() { 39 return tableName; 40 } 41 42 public void setTableName(String tableName) { 43 this.tableName = tableName; 44 } 45 46 public List<ColumnConfig> getColumns() { 47 return columns; 48 } 49 50 public void setColumns(List<ColumnConfig> columns) { 51 this.columns = columns; 52 } 53 54 public void addColumn(ColumnConfig column) { 55 columns.add(column); 56 } 57 58 public void removeColumn(ColumnConfig column) { 59 columns.remove(column); 60 } 61 62 public String getWhereClause() { 63 return whereClause; 64 } 65 66 public void setWhereClause(String whereClause) { 67 this.whereClause = whereClause; 68 } 69 70 public SqlStatement[] generateStatements(Database database) { 71 72 UpdateStatement statement = new UpdateStatement(getSchemaName() == null ? database.getDefaultSchemaName() 73 : getSchemaName(), getTableName()); 74 75 for (ColumnConfig column : columns) { 76 statement.addNewColumnValue(column.getName(), column.getValueObject()); 77 } 78 79 statement.setWhereClause(whereClause); 80 81 return new SqlStatement[] { statement }; 82 } 83 84 public String getConfirmationMessage() { 85 return "Data updated in " + getTableName(); 86 } 87 88 }