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 | 10 | super("update", "Update Data", ChangeMetaData.PRIORITY_DEFAULT); |
27 | 10 | columns = new ArrayList<ColumnConfig>(); |
28 | 10 | } |
29 | |
|
30 | |
public String getSchemaName() { |
31 | 0 | return schemaName; |
32 | |
} |
33 | |
|
34 | |
public void setSchemaName(String schemaName) { |
35 | 0 | this.schemaName = StringUtils.trimToNull(schemaName); |
36 | 0 | } |
37 | |
|
38 | |
public String getTableName() { |
39 | 0 | return tableName; |
40 | |
} |
41 | |
|
42 | |
public void setTableName(String tableName) { |
43 | 0 | this.tableName = tableName; |
44 | 0 | } |
45 | |
|
46 | |
public List<ColumnConfig> getColumns() { |
47 | 0 | return columns; |
48 | |
} |
49 | |
|
50 | |
public void setColumns(List<ColumnConfig> columns) { |
51 | 0 | this.columns = columns; |
52 | 0 | } |
53 | |
|
54 | |
public void addColumn(ColumnConfig column) { |
55 | 0 | columns.add(column); |
56 | 0 | } |
57 | |
|
58 | |
public void removeColumn(ColumnConfig column) { |
59 | 0 | columns.remove(column); |
60 | 0 | } |
61 | |
|
62 | |
public String getWhereClause() { |
63 | 0 | return whereClause; |
64 | |
} |
65 | |
|
66 | |
public void setWhereClause(String whereClause) { |
67 | 0 | this.whereClause = whereClause; |
68 | 0 | } |
69 | |
|
70 | |
public SqlStatement[] generateStatements(Database database) { |
71 | |
|
72 | 0 | UpdateStatement statement = new UpdateStatement(getSchemaName() == null ? database.getDefaultSchemaName() |
73 | |
: getSchemaName(), getTableName()); |
74 | |
|
75 | 0 | for (ColumnConfig column : columns) { |
76 | 0 | statement.addNewColumnValue(column.getName(), column.getValueObject()); |
77 | |
} |
78 | |
|
79 | 0 | statement.setWhereClause(whereClause); |
80 | |
|
81 | 0 | return new SqlStatement[] { statement }; |
82 | |
} |
83 | |
|
84 | |
public String getConfirmationMessage() { |
85 | 0 | return "Data updated in " + getTableName(); |
86 | |
} |
87 | |
|
88 | |
} |