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.SQLIfStatement;
17 
18 
19 import hunt.collection;
20 
21 import hunt.sql.ast.SQLExpr;
22 import hunt.sql.ast.SQLObjectImpl;
23 import hunt.sql.ast.SQLStatement;
24 import hunt.sql.ast.SQLStatementImpl;
25 import hunt.sql.visitor.SQLASTVisitor;
26 import hunt.sql.ast.SQLObject;
27 
28 public class SQLIfStatement : SQLStatementImpl {
29 
30     private SQLExpr            condition;
31     private List!SQLStatement statements;
32     private List!ElseIf       elseIfList;
33     private Else               elseItem;
34 
35     this()
36     {
37         statements = new ArrayList!SQLStatement();
38         elseIfList = new ArrayList!ElseIf();
39     }
40 
41     override public SQLIfStatement clone() {
42         SQLIfStatement x = new SQLIfStatement();
43 
44         foreach (SQLStatement stmt ; statements) {
45             SQLStatement stmt2 = stmt.clone();
46             stmt2.setParent(x);
47             x.statements.add(stmt2);
48         }
49         foreach (ElseIf o ; elseIfList) {
50             ElseIf o2 = o.clone();
51             o2.setParent(x);
52             x.elseIfList.add(o2);
53         }
54         if (elseItem !is null) {
55             x.setElseItem(elseItem.clone());
56         }
57 
58         return x;
59     }
60 
61     override
62     public void accept0(SQLASTVisitor visitor) {
63         if (visitor.visit(this)) {
64             acceptChild(visitor, condition);
65             acceptChild!SQLStatement(visitor, statements);
66             acceptChild!(SQLIfStatement.ElseIf)(visitor, elseIfList);
67             acceptChild(visitor, elseItem);
68         }
69         visitor.endVisit(this);
70     }
71 
72     public SQLExpr getCondition() {
73         return condition;
74     }
75 
76     public void setCondition(SQLExpr condition) {
77         if (condition !is null) {
78             condition.setParent(this);
79         }
80         this.condition = condition;
81     }
82 
83     public List!SQLStatement getStatements() {
84         return statements;
85     }
86 
87     public void addStatement(SQLStatement statement) {
88         if (statement !is null) {
89             statement.setParent(this);
90         }
91         this.statements.add(statement);
92     }
93 
94     public List!ElseIf getElseIfList() {
95         return elseIfList;
96     }
97 
98     public Else getElseItem() {
99         return elseItem;
100     }
101 
102     public void setElseItem(Else elseItem) {
103         if (elseItem !is null) {
104             elseItem.setParent(this);
105         }
106         this.elseItem = elseItem;
107     }
108 
109     public static class ElseIf : SQLObjectImpl {
110 
111         private SQLExpr            condition;
112         private List!SQLStatement statements;
113         this()
114         {
115             statements = new ArrayList!SQLStatement();
116         }
117 
118         override
119         public void accept0(SQLASTVisitor visitor) {
120             if (visitor.visit(this)) {
121                 acceptChild(visitor, condition);
122                 acceptChild!SQLStatement(visitor, statements);
123             }
124             visitor.endVisit(this);
125         }
126 
127         public List!SQLStatement getStatements() {
128             return statements;
129         }
130 
131         public void setStatements(List!SQLStatement statements) {
132             this.statements = statements;
133         }
134 
135         public SQLExpr getCondition() {
136             return condition;
137         }
138 
139         public void setCondition(SQLExpr condition) {
140             if (condition !is null) {
141                 condition.setParent(this);
142             }
143             this.condition = condition;
144         }
145 
146         override public ElseIf clone() {
147             ElseIf x = new ElseIf();
148 
149             if (condition !is null) {
150                 x.setCondition(condition.clone());
151             }
152             foreach (SQLStatement stmt ; statements) {
153                 SQLStatement stmt2 = stmt.clone();
154                 stmt2.setParent(x);
155                 x.statements.add(stmt2);
156             }
157 
158             return x;
159         }
160     }
161 
162     public static class Else : SQLObjectImpl {
163 
164         private List!SQLStatement statements;
165 
166         this()
167         {
168             statements = new ArrayList!SQLStatement();
169         }
170 
171         override
172         public void accept0(SQLASTVisitor visitor) {
173             if (visitor.visit(this)) {
174                 acceptChild!SQLStatement(visitor, statements);
175             }
176             visitor.endVisit(this);
177         }
178 
179         public List!SQLStatement getStatements() {
180             return statements;
181         }
182 
183         public void setStatements(List!SQLStatement statements) {
184             this.statements = statements;
185         }
186 
187         override public Else clone() {
188             Else x = new Else();
189             foreach (SQLStatement stmt ; statements) {
190                 SQLStatement stmt2 = stmt.clone();
191                 stmt2.setParent(x);
192                 x.statements.add(stmt2);
193             }
194             return x;
195         }
196     }
197 }