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.SQLCreateTriggerStatement;
17 
18 
19 import hunt.collection;
20 
21 import hunt.sql.ast;
22 import hunt.sql.visitor.SQLASTVisitor;
23 import hunt.sql.ast.statement.SQLExprTableSource;
24 import hunt.sql.ast.statement.SQLCreateStatement;
25 
26 public class SQLCreateTriggerStatement : SQLStatementImpl , SQLCreateStatement {
27 
28     private SQLName                  name;
29     private bool                  orReplace      = false;
30     private TriggerType              triggerType;
31 
32     private SQLName                  definer;
33 
34     private bool                  update;
35     private bool                  _delete;
36     private bool                  insert;
37 
38     private SQLExprTableSource       on;
39 
40     private bool                  forEachRow     = false;
41 
42     private List!SQLName            updateOfColumns;
43 
44     private SQLExpr                  when;
45     private SQLStatement             body;
46     
47     public this() {
48          updateOfColumns = new ArrayList!SQLName();
49     }
50     
51     public this(string dbType) {
52         updateOfColumns = new ArrayList!SQLName();
53         super (dbType);
54     }
55 
56     override  protected void accept0(SQLASTVisitor visitor) {
57         if (visitor.visit(this)) {
58             acceptChild(visitor, name);
59             acceptChild!SQLName(visitor, updateOfColumns);
60             acceptChild(visitor, on);
61             acceptChild(visitor, when);
62             acceptChild(visitor, body);
63         }
64         visitor.endVisit(this);
65     }
66 
67     override
68     public List!SQLObject getChildren() {
69         List!SQLObject children = new ArrayList!SQLObject();
70         if (name !is null) {
71             children.add(name);
72         }
73         children.addAll(cast(List!SQLObject)(updateOfColumns));
74         if (on !is null) {
75             children.add(on);
76         }
77         if (when !is null) {
78             children.add(when);
79         }
80         if (body !is null) {
81             children.add(body);
82         }
83         return children;
84     }
85 
86     public SQLExprTableSource getOn() {
87         return on;
88     }
89 
90     public void setOn(SQLName on) {
91         this.setOn(new SQLExprTableSource(on));
92     }
93 
94     public void setOn(SQLExprTableSource on) {
95         if (on !is null) {
96             on.setParent(this);
97         }
98         this.on = on;
99     }
100 
101     public SQLName getName() {
102         return name;
103     }
104 
105     public void setName(SQLName name) {
106         if (name !is null) {
107             name.setParent(this);
108         }
109         this.name = name;
110     }
111 
112     public SQLStatement getBody() {
113         return body;
114     }
115 
116     public void setBody(SQLStatement body) {
117         if (body !is null) {
118             body.setParent(this);
119         }
120         this.body = body;
121     }
122 
123     public bool isOrReplace() {
124         return orReplace;
125     }
126 
127     public void setOrReplace(bool orReplace) {
128         this.orReplace = orReplace;
129     }
130 
131     public TriggerType getTriggerType() {
132         return triggerType;
133     }
134 
135     public void setTriggerType(TriggerType triggerType) {
136         this.triggerType = triggerType;
137     }
138 
139     public List!TriggerEvent getTriggerEvents() {
140         return null;
141     }
142 
143     public bool isForEachRow() {
144         return forEachRow;
145     }
146 
147     public void setForEachRow(bool forEachRow) {
148         this.forEachRow = forEachRow;
149     }
150 
151     public List!SQLName getUpdateOfColumns() {
152         return updateOfColumns;
153     }
154 
155     public SQLExpr getWhen() {
156         return when;
157     }
158 
159     public void setWhen(SQLExpr x) {
160         if (x !is null) {
161             x.setParent(this);
162         }
163         this.when = x;
164     }
165 
166     public bool isUpdate() {
167         return update;
168     }
169 
170     public void setUpdate(bool update) {
171         this.update = update;
172     }
173 
174     public bool isDelete() {
175         return _delete;
176     }
177 
178     public void setDelete(bool _delete) {
179         this._delete = _delete;
180     }
181 
182     public bool isInsert() {
183         return insert;
184     }
185 
186     public void setInsert(bool insert) {
187         this.insert = insert;
188     }
189 
190     public SQLName getDefiner() {
191         return definer;
192     }
193 
194     public void setDefiner(SQLName x) {
195         if (x !is null) {
196             x.setParent(this);
197         }
198         this.definer = x;
199     }
200 
201     public static struct TriggerType {
202         enum TriggerType BEFORE = TriggerType("BEFORE");
203         enum TriggerType AFTER = TriggerType("AFTER");
204         enum TriggerType INSTEAD_OF = TriggerType("INSTEAD_OF");
205 
206         private string _name;
207 
208         this(string name)
209         {
210             _name = name;
211         }
212 
213         @property string name()
214         {
215             return _name;
216         }
217 
218         bool opEquals(const TriggerType h) nothrow {
219             return _name == h._name ;
220         } 
221 
222         bool opEquals(ref const TriggerType h) nothrow {
223             return _name == h._name ;
224         } 
225     }
226 
227     public static struct TriggerEvent {
228         enum TriggerEvent INSERT = TriggerEvent("INSERT");
229         enum TriggerEvent UPDATE = TriggerEvent("UPDATE");
230         enum TriggerEvent DELETE = TriggerEvent("DELETE");
231 
232         private string _name;
233 
234         this(string name)
235         {
236             _name = name;
237         }
238 
239         @property string name()
240         {
241             return _name;
242         }
243 
244         bool opEquals(const TriggerType h) nothrow {
245             return _name == h._name ;
246         } 
247 
248         bool opEquals(ref const TriggerType h) nothrow {
249             return _name == h._name ;
250         } 
251     }
252 }