01: package liquibase.change;
02:
03: import liquibase.database.Database;
04: import liquibase.database.sql.RenameViewStatement;
05: import liquibase.database.sql.SqlStatement;
06: import liquibase.database.structure.DatabaseObject;
07: import liquibase.database.structure.View;
08: import liquibase.exception.UnsupportedChangeException;
09: import org.w3c.dom.Document;
10: import org.w3c.dom.Element;
11:
12: import java.util.Arrays;
13: import java.util.HashSet;
14: import java.util.Set;
15:
16: /**
17: * Renames an existing view.
18: */
19: public class RenameViewChange extends AbstractChange {
20: private String schemaName;
21: private String oldViewName;
22: private String newViewName;
23:
24: public RenameViewChange() {
25: super ("renameView", "Rename View");
26: }
27:
28: public String getSchemaName() {
29: return schemaName;
30: }
31:
32: public void setSchemaName(String schemaName) {
33: this .schemaName = schemaName;
34: }
35:
36: public String getOldViewName() {
37: return oldViewName;
38: }
39:
40: public void setOldViewName(String oldViewName) {
41: this .oldViewName = oldViewName;
42: }
43:
44: public String getNewViewName() {
45: return newViewName;
46: }
47:
48: public void setNewViewName(String newViewName) {
49: this .newViewName = newViewName;
50: }
51:
52: public SqlStatement[] generateStatements(Database database)
53: throws UnsupportedChangeException {
54: return new SqlStatement[] { new RenameViewStatement(
55: getSchemaName() == null ? database
56: .getDefaultSchemaName() : getSchemaName(),
57: getOldViewName(), getNewViewName()) };
58: }
59:
60: protected Change[] createInverses() {
61: RenameViewChange inverse = new RenameViewChange();
62: inverse.setOldViewName(getNewViewName());
63: inverse.setNewViewName(getOldViewName());
64:
65: return new Change[] { inverse };
66: }
67:
68: public String getConfirmationMessage() {
69: return "View " + oldViewName + " renamed to " + newViewName;
70: }
71:
72: public Element createNode(Document currentChangeLogFileDOM) {
73: Element element = currentChangeLogFileDOM
74: .createElement(getTagName());
75: if (getSchemaName() != null) {
76: element.setAttribute("schemaName", getSchemaName());
77: }
78: element.setAttribute("oldViewName", getOldViewName());
79: element.setAttribute("newViewName", getNewViewName());
80:
81: return element;
82: }
83:
84: public Set<DatabaseObject> getAffectedDatabaseObjects() {
85: View oldView = new View();
86: oldView.setName(oldViewName);
87:
88: View newView = new View();
89: newView.setName(newViewName);
90:
91: return new HashSet<DatabaseObject>(Arrays.asList(oldView,
92: newView));
93: }
94:
95: }
|