001: package liquibase.change;
002:
003: import liquibase.database.Database;
004: import liquibase.database.sql.SqlStatement;
005: import liquibase.database.sql.UpdateStatement;
006: import liquibase.database.structure.DatabaseObject;
007: import liquibase.database.structure.Table;
008: import liquibase.exception.UnsupportedChangeException;
009: import liquibase.util.StringUtils;
010: import org.w3c.dom.Document;
011: import org.w3c.dom.Element;
012:
013: import java.util.*;
014:
015: public class UpdateDataChange extends AbstractChange {
016:
017: private String schemaName;
018: private String tableName;
019: private List<ColumnConfig> columns;
020: private String whereClause;
021:
022: public UpdateDataChange() {
023: super ("update", "Update Data");
024: columns = new ArrayList<ColumnConfig>();
025: }
026:
027: public String getSchemaName() {
028: return schemaName;
029: }
030:
031: public void setSchemaName(String schemaName) {
032: this .schemaName = schemaName;
033: }
034:
035: public String getTableName() {
036: return tableName;
037: }
038:
039: public void setTableName(String tableName) {
040: this .tableName = tableName;
041: }
042:
043: public List<ColumnConfig> getColumns() {
044: return columns;
045: }
046:
047: public void setColumns(List<ColumnConfig> columns) {
048: this .columns = columns;
049: }
050:
051: public void addColumn(ColumnConfig column) {
052: columns.add(column);
053: }
054:
055: public void removeColumn(ColumnConfig column) {
056: columns.remove(column);
057: }
058:
059: public String getWhereClause() {
060: return whereClause;
061: }
062:
063: public void setWhereClause(String whereClause) {
064: this .whereClause = whereClause;
065: }
066:
067: public SqlStatement[] generateStatements(Database database)
068: throws UnsupportedChangeException {
069:
070: UpdateStatement statement = new UpdateStatement(
071: getSchemaName() == null ? database
072: .getDefaultSchemaName() : getSchemaName(),
073: getTableName());
074:
075: for (ColumnConfig column : columns) {
076: statement.addNewColumnValue(column.getName(), column
077: .getValueObject());
078: }
079:
080: statement.setWhereClause(whereClause);
081:
082: return new SqlStatement[] { statement };
083: }
084:
085: public String getConfirmationMessage() {
086: return "Data updated in " + getTableName();
087: }
088:
089: public Element createNode(Document currentChangeLogFileDOM) {
090: Element node = currentChangeLogFileDOM.createElement("update");
091: if (getSchemaName() != null) {
092: node.setAttribute("schemaName", getSchemaName());
093: }
094:
095: node.setAttribute("tableName", getTableName());
096:
097: for (ColumnConfig col : getColumns()) {
098: Element subNode = col.createNode(currentChangeLogFileDOM);
099: node.appendChild(subNode);
100: }
101:
102: if (StringUtils.trimToNull(getWhereClause()) != null) {
103: Element whereClause = currentChangeLogFileDOM
104: .createElement("where");
105: whereClause.appendChild(currentChangeLogFileDOM
106: .createTextNode(getWhereClause()));
107: node.appendChild(whereClause);
108: }
109: return node;
110: }
111:
112: public Set<DatabaseObject> getAffectedDatabaseObjects() {
113: Table dbObject = new Table(getTableName());
114:
115: return new HashSet<DatabaseObject>(Arrays.asList(dbObject));
116: }
117:
118: }
|