01: package liquibase.change;
02:
03: import liquibase.database.Database;
04: import liquibase.database.sql.DeleteStatement;
05: import liquibase.database.sql.SqlStatement;
06: import liquibase.database.structure.DatabaseObject;
07: import liquibase.database.structure.Table;
08: import liquibase.exception.UnsupportedChangeException;
09: import liquibase.util.StringUtils;
10: import org.w3c.dom.Document;
11: import org.w3c.dom.Element;
12:
13: import java.util.Arrays;
14: import java.util.HashSet;
15: import java.util.Set;
16:
17: public class DeleteDataChange extends AbstractChange {
18:
19: private String schemaName;
20: private String tableName;
21: private String whereClause;
22:
23: public DeleteDataChange() {
24: super ("delete", "Delete Data");
25: }
26:
27: public String getSchemaName() {
28: return schemaName;
29: }
30:
31: public void setSchemaName(String schemaName) {
32: this .schemaName = schemaName;
33: }
34:
35: public String getTableName() {
36: return tableName;
37: }
38:
39: public void setTableName(String tableName) {
40: this .tableName = tableName;
41: }
42:
43: public String getWhereClause() {
44: return whereClause;
45: }
46:
47: public void setWhereClause(String whereClause) {
48: this .whereClause = whereClause;
49: }
50:
51: public SqlStatement[] generateStatements(Database database)
52: throws UnsupportedChangeException {
53:
54: DeleteStatement statement = new DeleteStatement(
55: getSchemaName() == null ? database
56: .getDefaultSchemaName() : getSchemaName(),
57: getTableName());
58:
59: statement.setWhereClause(whereClause);
60:
61: return new SqlStatement[] { statement };
62: }
63:
64: public String getConfirmationMessage() {
65: return "Data deleted from " + getTableName();
66: }
67:
68: public Element createNode(Document currentChangeLogFileDOM) {
69: Element node = currentChangeLogFileDOM.createElement("delete");
70: if (getSchemaName() != null) {
71: node.setAttribute("schemaName", getSchemaName());
72: }
73:
74: node.setAttribute("tableName", getTableName());
75:
76: if (StringUtils.trimToNull(getWhereClause()) != null) {
77: Element whereClause = currentChangeLogFileDOM
78: .createElement("where");
79: whereClause.appendChild(currentChangeLogFileDOM
80: .createTextNode(getWhereClause()));
81: node.appendChild(whereClause);
82: }
83: return node;
84: }
85:
86: public Set<DatabaseObject> getAffectedDatabaseObjects() {
87: Table dbObject = new Table(getTableName());
88:
89: return new HashSet<DatabaseObject>(Arrays.asList(dbObject));
90: }
91:
92: }
|