1 /*
2  * Copyright 2015-2018 HuntLabs.cn
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 module hunt.sql.ast.statement.SQLAlterViewStatement;
17 
18 import hunt.sql.SQLUtils;
19 import hunt.sql.ast;
20 import hunt.sql.ast.expr.SQLIdentifierExpr;
21 import hunt.sql.ast.expr.SQLLiteralExpr;
22 import hunt.sql.ast.expr.SQLPropertyExpr;
23 import hunt.sql.visitor.SQLASTVisitor;
24 import hunt.sql.ast.statement.SQLTableElement;
25 import hunt.sql.ast.statement.SQLExprTableSource;
26 import hunt.sql.ast.statement.SQLSelect;
27 import hunt.sql.ast.statement.SQLCreateStatement;
28 
29 import hunt.collection;
30 
31 public class SQLAlterViewStatement : SQLStatementImpl , SQLCreateStatement {
32 
33     private bool     force       = false;
34     // protected SQLName   name;
35     protected SQLSelect subQuery;
36     protected bool   ifNotExists = false;
37 
38     protected string    algorithm;
39     protected SQLName   definer;
40     protected string    sqlSecurity;
41 
42     protected SQLExprTableSource tableSource;
43 
44     protected  List!SQLTableElement columns;
45 
46     private bool withCheckOption;
47     private bool withCascaded;
48     private bool withLocal;
49     private bool withReadOnly;
50 
51     private SQLLiteralExpr comment;
52 
53     public this(){
54         columns = new ArrayList!SQLTableElement();
55     }
56 
57     public this(string dbType){
58         columns = new ArrayList!SQLTableElement();
59         super(dbType);
60     }
61 
62     public string computeName() {
63         if (tableSource is null) {
64             return null;
65         }
66 
67         SQLExpr expr = tableSource.getExpr();
68         if ( cast(SQLName)expr !is null) {
69             string name = (cast(SQLName) expr).getSimpleName();
70             return SQLUtils.normalize(name);
71         }
72 
73         return null;
74     }
75 
76     public string getSchema() {
77         SQLName name = getName();
78         if (name is null) {
79             return null;
80         }
81 
82         if ( cast(SQLPropertyExpr)name !is null) {
83             return (cast(SQLPropertyExpr) name).getOwnernName();
84         }
85 
86         return null;
87     }
88 
89     public SQLName getName() {
90         if (tableSource is null) {
91             return null;
92         }
93 
94         return cast(SQLName) tableSource.getExpr();
95     }
96 
97     public void setName(SQLName name) {
98         this.setTableSource(new SQLExprTableSource(name));
99     }
100 
101     public void setName(string name) {
102         this.setName(new SQLIdentifierExpr(name));
103     }
104 
105     public SQLExprTableSource getTableSource() {
106         return tableSource;
107     }
108 
109     public void setTableSource(SQLExprTableSource tableSource) {
110         if (tableSource !is null) {
111             tableSource.setParent(this);
112         }
113         this.tableSource = tableSource;
114     }
115 
116     public bool isWithCheckOption() {
117         return withCheckOption;
118     }
119 
120     public void setWithCheckOption(bool withCheckOption) {
121         this.withCheckOption = withCheckOption;
122     }
123 
124     public bool isWithCascaded() {
125         return withCascaded;
126     }
127 
128     public void setWithCascaded(bool withCascaded) {
129         this.withCascaded = withCascaded;
130     }
131 
132     public bool isWithLocal() {
133         return withLocal;
134     }
135 
136     public void setWithLocal(bool withLocal) {
137         this.withLocal = withLocal;
138     }
139 
140     public bool isWithReadOnly() {
141         return withReadOnly;
142     }
143 
144     public void setWithReadOnly(bool withReadOnly) {
145         this.withReadOnly = withReadOnly;
146     }
147 
148     public SQLSelect getSubQuery() {
149         return subQuery;
150     }
151 
152     public void setSubQuery(SQLSelect subQuery) {
153         if (subQuery !is null) {
154             subQuery.setParent(this);
155         }
156         this.subQuery = subQuery;
157     }
158 
159     public List!SQLTableElement getColumns() {
160         return columns;
161     }
162     
163     public void addColumn(SQLTableElement column) {
164         if (column !is null) {
165             column.setParent(this);
166         }
167         this.columns.add(column);
168     }
169 
170     public bool isIfNotExists() {
171         return ifNotExists;
172     }
173 
174     public void setIfNotExists(bool ifNotExists) {
175         this.ifNotExists = ifNotExists;
176     }
177 
178     public SQLLiteralExpr getComment() {
179         return comment;
180     }
181 
182     public void setComment(SQLLiteralExpr comment) {
183         if (comment !is null) {
184             comment.setParent(this);
185         }
186         this.comment = comment;
187     }
188 
189     public string getAlgorithm() {
190         return algorithm;
191     }
192 
193     public void setAlgorithm(string algorithm) {
194         this.algorithm = algorithm;
195     }
196 
197     public SQLName getDefiner() {
198         return definer;
199     }
200 
201     public void setDefiner(SQLName definer) {
202         if (definer !is null) {
203             definer.setParent(this);
204         }
205         this.definer = definer;
206     }
207 
208     public string getSqlSecurity() {
209         return sqlSecurity;
210     }
211 
212     public void setSqlSecurity(string sqlSecurity) {
213         this.sqlSecurity = sqlSecurity;
214     }
215 
216     public bool isForce() {
217         return force;
218     }
219 
220     public void setForce(bool force) {
221         this.force = force;
222     }
223 
224     override  protected void accept0(SQLASTVisitor visitor) {
225         if (visitor.visit(this)) {
226             acceptChild(visitor, this.tableSource);
227             acceptChild!SQLTableElement(visitor, this.columns);
228             acceptChild(visitor, this.comment);
229             acceptChild(visitor, this.subQuery);
230         }
231         visitor.endVisit(this);
232     }
233 
234     override public List!SQLObject getChildren() {
235         List!SQLObject children = new ArrayList!SQLObject();
236         if (tableSource !is null) {
237             children.add(tableSource);
238         }
239         children.addAll(cast(List!SQLObject)(this.columns));
240         if (comment !is null) {
241             children.add(comment);
242         }
243         if (subQuery !is null) {
244             children.add(subQuery);
245         }
246         return children;
247     }
248 
249     override public SQLAlterViewStatement clone() {
250         SQLAlterViewStatement x = new SQLAlterViewStatement();
251 
252         x.force = force;
253         if (subQuery !is null) {
254             x.setSubQuery(subQuery.clone());
255         }
256         x.ifNotExists = ifNotExists;
257 
258         x.algorithm = algorithm;
259         if (definer !is null) {
260             x.setDefiner(definer.clone());
261         }
262         x.sqlSecurity = sqlSecurity;
263         if (tableSource !is null) {
264             x.setTableSource(tableSource.clone());
265         }
266         foreach (SQLTableElement column ; columns) {
267             SQLTableElement column2 = column.clone();
268             column2.setParent(x);
269             x.columns.add(column2);
270         }
271         x.withCheckOption = withCheckOption;
272         x.withCascaded = withCascaded;
273         x.withLocal = withLocal;
274         x.withReadOnly = withReadOnly;
275 
276         if (comment !is null) {
277             x.setComment(comment.clone());
278         }
279 
280         return x;
281     }
282 }