001: package liquibase.change;
002:
003: import liquibase.database.Database;
004: import liquibase.database.sql.CreateViewStatement;
005: import liquibase.database.sql.SqlStatement;
006: import liquibase.database.structure.DatabaseObject;
007: import liquibase.database.structure.View;
008: import liquibase.exception.UnsupportedChangeException;
009: import org.w3c.dom.Document;
010: import org.w3c.dom.Element;
011:
012: import java.util.Arrays;
013: import java.util.HashSet;
014: import java.util.Set;
015:
016: /**
017: * Creats a new view.
018: */
019: public class CreateViewChange extends AbstractChange {
020:
021: private String schemaName;
022: private String viewName;
023: private String selectQuery;
024: private Boolean replaceIfExists;
025:
026: public CreateViewChange() {
027: super ("createView", "Create View");
028: }
029:
030: public String getSchemaName() {
031: return schemaName;
032: }
033:
034: public void setSchemaName(String schemaName) {
035: this .schemaName = schemaName;
036: }
037:
038: public String getViewName() {
039: return viewName;
040: }
041:
042: public void setViewName(String viewName) {
043: this .viewName = viewName;
044: }
045:
046: public String getSelectQuery() {
047: return selectQuery;
048: }
049:
050: public void setSelectQuery(String selectQuery) {
051: this .selectQuery = selectQuery;
052: }
053:
054: public Boolean getReplaceIfExists() {
055: return replaceIfExists;
056: }
057:
058: public void setReplaceIfExists(Boolean replaceIfExists) {
059: this .replaceIfExists = replaceIfExists;
060: }
061:
062: public SqlStatement[] generateStatements(Database database)
063: throws UnsupportedChangeException {
064: boolean replaceIfExists = false;
065: if (getReplaceIfExists() != null && getReplaceIfExists()) {
066: replaceIfExists = true;
067: }
068: return new SqlStatement[] { new CreateViewStatement(
069: getSchemaName() == null ? database
070: .getDefaultSchemaName() : getSchemaName(),
071: getViewName(), getSelectQuery(), replaceIfExists), };
072: }
073:
074: public String getConfirmationMessage() {
075: return "View " + getViewName() + " created";
076: }
077:
078: public Element createNode(Document currentChangeLogFileDOM) {
079: Element element = currentChangeLogFileDOM
080: .createElement("createView");
081:
082: if (getSchemaName() != null) {
083: element.setAttribute("schemaName", getSchemaName());
084: }
085:
086: element.setAttribute("viewName", getViewName());
087: element.appendChild(currentChangeLogFileDOM
088: .createTextNode(getSelectQuery()));
089:
090: return element;
091: }
092:
093: protected Change[] createInverses() {
094: DropViewChange inverse = new DropViewChange();
095: inverse.setViewName(getViewName());
096:
097: return new Change[] { inverse };
098: }
099:
100: public Set<DatabaseObject> getAffectedDatabaseObjects() {
101: View dbObject = new View();
102: dbObject.setName(viewName);
103:
104: return new HashSet<DatabaseObject>(Arrays.asList(dbObject));
105: }
106:
107: }
|