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.SQLCreateViewStatement;
17
18
19 import hunt.collection;
20
21 import hunt.sql.SQLUtils;
22 import hunt.sql.ast;
23 import hunt.sql.ast.expr.SQLCharExpr;
24 import hunt.sql.ast.expr.SQLIdentifierExpr;
25 import hunt.sql.ast.expr.SQLLiteralExpr;
26 import hunt.sql.ast.expr.SQLPropertyExpr;
27 import hunt.sql.visitor.SQLASTVisitor;
28 import hunt.sql.ast.statement.SQLTableElement;
29 import hunt.sql.ast.statement.SQLExprTableSource;
30 import hunt.sql.ast.statement.SQLSelect;
31 import hunt.sql.ast.statement.SQLCreateStatement;
32
33 public class SQLCreateViewStatement : SQLStatementImpl , SQLCreateStatement {
34
35 private bool orReplace = false;
36 private bool force = false;
37 // protected SQLName name;
38 protected SQLSelect subQuery;
39 protected bool ifNotExists = false;
40
41 protected string algorithm;
42 protected SQLName definer;
43 protected string sqlSecurity;
44
45 protected SQLExprTableSource tableSource;
46
47 protected List!SQLTableElement columns;
48
49 private bool withCheckOption;
50 private bool withCascaded;
51 private bool withLocal;
52 private bool withReadOnly;
53
54 private SQLLiteralExpr comment;
55
56 public this(){
57 columns = new ArrayList!SQLTableElement();
58 }
59
60 public this(string dbType){
61 columns = new ArrayList!SQLTableElement();
62 super(dbType);
63 }
64
65 public string computeName() {
66 if (tableSource is null) {
67 return null;
68 }
69
70 SQLExpr expr = tableSource.getExpr();
71 if (cast(SQLName)(expr) !is null ) {
72 string name = (cast(SQLName) expr).getSimpleName();
73 return SQLUtils.normalize(name);
74 }
75
76 return null;
77 }
78
79 public string getSchema() {
80 SQLName name = getName();
81 if (name is null) {
82 return null;
83 }
84
85 if (cast(SQLPropertyExpr)(name) !is null ) {
86 return (cast(SQLPropertyExpr) name).getOwnernName();
87 }
88
89 return null;
90 }
91
92 public bool isOrReplace() {
93 return orReplace;
94 }
95
96 public void setOrReplace(bool orReplace) {
97 this.orReplace = orReplace;
98 }
99
100 public SQLName getName() {
101 if (tableSource is null) {
102 return null;
103 }
104
105 return cast(SQLName) tableSource.getExpr();
106 }
107
108 public void setName(SQLName name) {
109 this.setTableSource(new SQLExprTableSource(name));
110 }
111
112 public void setName(string name) {
113 this.setName(new SQLIdentifierExpr(name));
114 }
115
116 public SQLExprTableSource getTableSource() {
117 return tableSource;
118 }
119
120 public void setTableSource(SQLExprTableSource tableSource) {
121 if (tableSource !is null) {
122 tableSource.setParent(this);
123 }
124 this.tableSource = tableSource;
125 }
126
127 public bool isWithCheckOption() {
128 return withCheckOption;
129 }
130
131 public void setWithCheckOption(bool withCheckOption) {
132 this.withCheckOption = withCheckOption;
133 }
134
135 public bool isWithCascaded() {
136 return withCascaded;
137 }
138
139 public void setWithCascaded(bool withCascaded) {
140 this.withCascaded = withCascaded;
141 }
142
143 public bool isWithLocal() {
144 return withLocal;
145 }
146
147 public void setWithLocal(bool withLocal) {
148 this.withLocal = withLocal;
149 }
150
151 public bool isWithReadOnly() {
152 return withReadOnly;
153 }
154
155 public void setWithReadOnly(bool withReadOnly) {
156 this.withReadOnly = withReadOnly;
157 }
158
159 public SQLSelect getSubQuery() {
160 return subQuery;
161 }
162
163 public void setSubQuery(SQLSelect subQuery) {
164 if (subQuery !is null) {
165 subQuery.setParent(this);
166 }
167 this.subQuery = subQuery;
168 }
169
170 public List!SQLTableElement getColumns() {
171 return columns;
172 }
173
174 public void addColumn(SQLTableElement column) {
175 if (column !is null) {
176 column.setParent(this);
177 }
178 this.columns.add(column);
179 }
180
181 public bool isIfNotExists() {
182 return ifNotExists;
183 }
184
185 public void setIfNotExists(bool ifNotExists) {
186 this.ifNotExists = ifNotExists;
187 }
188
189 public SQLLiteralExpr getComment() {
190 return comment;
191 }
192
193 public void setComment(SQLLiteralExpr comment) {
194 if (comment !is null) {
195 comment.setParent(this);
196 }
197 this.comment = comment;
198 }
199
200 public string getAlgorithm() {
201 return algorithm;
202 }
203
204 public void setAlgorithm(string algorithm) {
205 this.algorithm = algorithm;
206 }
207
208 public SQLName getDefiner() {
209 return definer;
210 }
211
212 public void setDefiner(SQLName definer) {
213 if (definer !is null) {
214 definer.setParent(this);
215 }
216 this.definer = definer;
217 }
218
219 public string getSqlSecurity() {
220 return sqlSecurity;
221 }
222
223 public void setSqlSecurity(string sqlSecurity) {
224 this.sqlSecurity = sqlSecurity;
225 }
226
227 public bool isForce() {
228 return force;
229 }
230
231 public void setForce(bool force) {
232 this.force = force;
233 }
234
235
236 override protected void accept0(SQLASTVisitor visitor) {
237 if (visitor.visit(this)) {
238 acceptChild(visitor, this.tableSource);
239 acceptChild!SQLTableElement(visitor, this.columns);
240 acceptChild(visitor, this.comment);
241 acceptChild(visitor, this.subQuery);
242 }
243 visitor.endVisit(this);
244 }
245
246 override public List!SQLObject getChildren() {
247 List!SQLObject children = new ArrayList!SQLObject();
248 if (tableSource !is null) {
249 children.add(tableSource);
250 }
251 children.addAll(cast(List!SQLObject)(this.columns));
252 if (comment !is null) {
253 children.add(comment);
254 }
255 if (subQuery !is null) {
256 children.add(subQuery);
257 }
258 return children;
259 }
260
261 public static enum Level {
262 CASCADED, LOCAL
263 }
264
265 public static class Column : SQLObjectImpl {
266
267 private SQLExpr expr;
268 private SQLCharExpr comment;
269
270 public SQLExpr getExpr() {
271 return expr;
272 }
273
274 public void setExpr(SQLExpr expr) {
275 if (expr !is null) {
276 expr.setParent(this);
277 }
278 this.expr = expr;
279 }
280
281 public SQLCharExpr getComment() {
282 return comment;
283 }
284
285 public void setComment(SQLCharExpr comment) {
286 if (comment !is null) {
287 comment.setParent(this);
288 }
289 this.comment = comment;
290 }
291
292
293 override protected void accept0(SQLASTVisitor visitor) {
294 if (visitor.visit(this)) {
295 acceptChild(visitor, expr);
296 acceptChild(visitor, comment);
297 }
298 }
299 }
300
301
302 override public SQLCreateViewStatement clone() {
303 SQLCreateViewStatement x = new SQLCreateViewStatement();
304
305 x.orReplace = orReplace;
306 x.force = force;
307 if (subQuery !is null) {
308 x.setSubQuery(subQuery.clone());
309 }
310 x.ifNotExists = ifNotExists;
311
312 x.algorithm = algorithm;
313 if (definer !is null) {
314 x.setDefiner(definer.clone());
315 }
316 x.sqlSecurity = sqlSecurity;
317 if (tableSource !is null) {
318 x.setTableSource(tableSource.clone());
319 }
320 foreach (SQLTableElement column ; columns) {
321 SQLTableElement column2 = column.clone();
322 column2.setParent(x);
323 x.columns.add(column2);
324 }
325 x.withCheckOption = withCheckOption;
326 x.withCascaded = withCascaded;
327 x.withLocal = withLocal;
328 x.withReadOnly = withReadOnly;
329
330 if (comment !is null) {
331 x.setComment(comment.clone());
332 }
333
334 return x;
335 }
336 }