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.SQLMergeStatement;
17 
18 
19 import hunt.collection;
20 
21 import hunt.sql.ast.SQLExpr;
22 import hunt.sql.ast.SQLHint;
23 import hunt.sql.ast.SQLName;
24 import hunt.sql.ast.SQLObjectImpl;
25 import hunt.sql.ast.SQLStatementImpl;
26 import hunt.sql.visitor.SQLASTVisitor;
27 import hunt.sql.ast.statement.SQLExprTableSource;
28 import hunt.sql.ast.statement.SQLTableSource;
29 import hunt.sql.ast.statement.SQLErrorLoggingClause;
30 import hunt.sql.ast.statement.SQLUpdateSetItem;
31 import hunt.sql.ast.SQLObject;
32 
33 public class SQLMergeStatement : SQLStatementImpl {
34 
35     private  List!SQLHint      hints;
36 
37     private SQLTableSource           into;
38     private string                   _alias;
39     private SQLTableSource           using;
40     private SQLExpr                  on;
41     private MergeUpdateClause        updateClause;
42     private MergeInsertClause        insertClause;
43     private SQLErrorLoggingClause errorLoggingClause;
44 
45     this()
46     {
47         hints = new ArrayList!SQLHint();
48     }
49 
50     override public void accept0(SQLASTVisitor visitor) {
51         if (visitor.visit(this)) {
52             acceptChild(visitor, into);
53             acceptChild(visitor, using);
54             acceptChild(visitor, on);
55             acceptChild(visitor, updateClause);
56             acceptChild(visitor, insertClause);
57             acceptChild(visitor, errorLoggingClause);
58         }
59         visitor.endVisit(this);
60     }
61 
62     public string getAlias() {
63         return into.getAlias();
64     }
65 
66     public SQLTableSource getInto() {
67         return into;
68     }
69     
70     public void setInto(SQLName into) {
71         this.setInto(new SQLExprTableSource(into));
72     }
73 
74     public void setInto(SQLTableSource into) {
75         if (into !is null) {
76             into.setParent(this);
77         }
78         this.into = into;
79     }
80 
81     public SQLTableSource getUsing() {
82         return using;
83     }
84 
85     public void setUsing(SQLTableSource using) {
86         this.using = using;
87     }
88 
89     public SQLExpr getOn() {
90         return on;
91     }
92 
93     public void setOn(SQLExpr on) {
94         this.on = on;
95     }
96 
97     public MergeUpdateClause getUpdateClause() {
98         return updateClause;
99     }
100 
101     public void setUpdateClause(MergeUpdateClause updateClause) {
102         this.updateClause = updateClause;
103     }
104 
105     public MergeInsertClause getInsertClause() {
106         return insertClause;
107     }
108 
109     public void setInsertClause(MergeInsertClause insertClause) {
110         this.insertClause = insertClause;
111     }
112 
113     public SQLErrorLoggingClause getErrorLoggingClause() {
114         return errorLoggingClause;
115     }
116 
117     public void setErrorLoggingClause(SQLErrorLoggingClause errorLoggingClause) {
118         this.errorLoggingClause = errorLoggingClause;
119     }
120 
121     public List!SQLHint getHints() {
122         return hints;
123     }
124 
125     public static class MergeUpdateClause : SQLObjectImpl {
126 
127         private List!SQLUpdateSetItem items;
128         private SQLExpr                where;
129         private SQLExpr                deleteWhere;
130 
131         this()
132         {
133             items = new ArrayList!SQLUpdateSetItem();
134         }
135 
136         public List!SQLUpdateSetItem getItems() {
137             return items;
138         }
139 
140         public void addItem(SQLUpdateSetItem item) {
141             if (item !is null) {
142                 item.setParent(this);
143             }
144             this.items.add(item);
145         }
146 
147         public SQLExpr getWhere() {
148             return where;
149         }
150 
151         public void setWhere(SQLExpr where) {
152             this.where = where;
153         }
154 
155         public SQLExpr getDeleteWhere() {
156             return deleteWhere;
157         }
158 
159         public void setDeleteWhere(SQLExpr deleteWhere) {
160             this.deleteWhere = deleteWhere;
161         }
162 
163         override
164         public void accept0(SQLASTVisitor visitor) {
165             if (visitor.visit(this)) {
166                 acceptChild!SQLUpdateSetItem(visitor, items);
167                 acceptChild(visitor, where);
168                 acceptChild(visitor, deleteWhere);
169             }
170             visitor.endVisit(this);
171         }
172 
173     }
174 
175     public static class MergeInsertClause : SQLObjectImpl {
176 
177         private List!SQLExpr columns;
178         private List!SQLExpr values;
179         private SQLExpr       where;
180 
181         this()
182         {
183             columns = new ArrayList!SQLExpr();
184             values  = new ArrayList!SQLExpr();
185         }
186 
187         override
188         public void accept0(SQLASTVisitor visitor) {
189             if (visitor.visit(this)) {
190                 acceptChild!SQLExpr(visitor, columns);
191                 acceptChild!SQLExpr(visitor, values);
192                 acceptChild(visitor, where);
193             }
194             visitor.endVisit(this);
195         }
196 
197         public List!SQLExpr getColumns() {
198             return columns;
199         }
200 
201         public void setColumns(List!SQLExpr columns) {
202             this.columns = columns;
203         }
204 
205         public List!SQLExpr getValues() {
206             return values;
207         }
208 
209         public void setValues(List!SQLExpr values) {
210             this.values = values;
211         }
212 
213         public SQLExpr getWhere() {
214             return where;
215         }
216 
217         public void setWhere(SQLExpr where) {
218             this.where = where;
219         }
220 
221     }
222 }
223 
224 // public  class MergeUpdateClause : SQLObjectImpl {
225 
226 //         private List!SQLUpdateSetItem items;
227 //         private SQLExpr                where;
228 //         private SQLExpr                deleteWhere;
229 
230 //         this()
231 //         {
232 //             items = new ArrayList!SQLUpdateSetItem();
233 //         }
234 
235 //         public List!SQLUpdateSetItem getItems() {
236 //             return items;
237 //         }
238 
239 //         public void addItem(SQLUpdateSetItem item) {
240 //             if (item !is null) {
241 //                 item.setParent(this);
242 //             }
243 //             this.items.add(item);
244 //         }
245 
246 //         public SQLExpr getWhere() {
247 //             return where;
248 //         }
249 
250 //         public void setWhere(SQLExpr where) {
251 //             this.where = where;
252 //         }
253 
254 //         public SQLExpr getDeleteWhere() {
255 //             return deleteWhere;
256 //         }
257 
258 //         public void setDeleteWhere(SQLExpr deleteWhere) {
259 //             this.deleteWhere = deleteWhere;
260 //         }
261 
262 //         override
263 //         public void accept0(SQLASTVisitor visitor) {
264 //             if (visitor.visit(this)) {
265 //                 acceptChild(visitor, cast(List!SQLObject)items);
266 //                 acceptChild(visitor, where);
267 //                 acceptChild(visitor, deleteWhere);
268 //             }
269 //             visitor.endVisit(this);
270 //         }
271 
272 //     }
273 
274 //     public  class MergeInsertClause : SQLObjectImpl {
275 
276 //         private List!SQLExpr columns;
277 //         private List!SQLExpr values;
278 //         private SQLExpr       where;
279 
280 //         this()
281 //         {
282 //             columns = new ArrayList!SQLExpr();
283 //             values  = new ArrayList!SQLExpr();
284 //         }
285 
286 //         override
287 //         public void accept0(SQLASTVisitor visitor) {
288 //             if (visitor.visit(this)) {
289 //                 acceptChild(visitor, cast(List!SQLObject)columns);
290 //                 acceptChild(visitor, cast(List!SQLObject)values);
291 //                 acceptChild(visitor, where);
292 //             }
293 //             visitor.endVisit(this);
294 //         }
295 
296 //         public List!SQLExpr getColumns() {
297 //             return columns;
298 //         }
299 
300 //         public void setColumns(List!SQLExpr columns) {
301 //             this.columns = columns;
302 //         }
303 
304 //         public List!SQLExpr getValues() {
305 //             return values;
306 //         }
307 
308 //         public void setValues(List!SQLExpr values) {
309 //             this.values = values;
310 //         }
311 
312 //         public SQLExpr getWhere() {
313 //             return where;
314 //         }
315 
316 //         public void setWhere(SQLExpr where) {
317 //             this.where = where;
318 //         }
319 
320 //     }