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.SQLSetStatement;
17 
18 
19 import hunt.collection;
20 
21 import hunt.sql.ast;
22 import hunt.sql.ast.expr.SQLBinaryOpExpr;
23 import hunt.sql.ast.expr.SQLBinaryOperator;
24 import hunt.sql.ast.expr.SQLIntegerExpr;
25 import hunt.sql.visitor.SQLASTVisitor;
26 import hunt.sql.ast.statement.SQLAssignItem;
27 import hunt.util.StringBuilder;
28 
29 public class  SQLSetStatement: SQLStatementImpl {
30     private Option option;
31 
32     private List!SQLAssignItem items ;
33     
34     private List!SQLCommentHint hints;
35 
36     public this(){
37         items = new ArrayList!SQLAssignItem();
38     }
39     
40     public this(string dbType){
41         items = new ArrayList!SQLAssignItem();
42         super (dbType);
43     }
44     
45     public this(SQLExpr target, SQLExpr value){
46         this(target, value, null);
47     }
48 
49     public this(SQLExpr target, SQLExpr value, string dbType){
50         items = new ArrayList!SQLAssignItem();
51         super (dbType);
52         SQLAssignItem item = new SQLAssignItem(target, value);
53         item.setParent(this);
54         this.items.add(item);
55     }
56 
57     public static SQLSetStatement plus(SQLName target) {
58         SQLExpr value = new SQLBinaryOpExpr(target.clone(), SQLBinaryOperator.Add, new SQLIntegerExpr(1));
59         return new SQLSetStatement(target, value);
60     }
61 
62     public List!SQLAssignItem getItems() {
63         return items;
64     }
65 
66     public void setItems(List!SQLAssignItem items) {
67         this.items = items;
68     }
69 
70     public List!SQLCommentHint getHints() {
71         return hints;
72     }
73 
74     public void setHints(List!SQLCommentHint hints) {
75         this.hints = hints;
76     }
77 
78     public Option getOption() {
79         return option;
80     }
81 
82     public void setOption(Option option) {
83         this.option = option;
84     }
85 
86     public void set(SQLExpr target, SQLExpr value) {
87         SQLAssignItem assignItem = new SQLAssignItem(target, value);
88         assignItem.setParent(this);
89         this.items.add(assignItem);
90     }
91 
92     
93     override  protected void accept0(SQLASTVisitor visitor) {
94         if (visitor.visit(this)) {
95             acceptChild!SQLAssignItem(visitor, this.items);
96             acceptChild!SQLCommentHint(visitor, this.hints);
97         }
98         visitor.endVisit(this);
99     }
100 
101     override public void output(StringBuilder buf) {
102         buf.append("SET ");
103 
104         for (int i = 0; i < items.size(); ++i) {
105             if (i != 0) {
106                 buf.append(", ");
107             }
108 
109             SQLAssignItem item = items.get(i);
110             item.output(buf);
111         }
112     }
113 
114     override public SQLSetStatement clone() {
115         SQLSetStatement x = new SQLSetStatement();
116         foreach (SQLAssignItem item ; items) {
117             SQLAssignItem item2 = item.clone();
118             item2.setParent(x);
119             x.items.add(item2);
120         }
121         if (hints !is null) {
122             foreach (SQLCommentHint hint ; hints) {
123                 SQLCommentHint h2 = hint.clone();
124                 h2.setParent(x);
125                 x.hints.add(h2);
126             }
127         }
128         return x;
129     }
130 
131     override public List!SQLObject getChildren() {
132         return cast(List!SQLObject)(this.items);
133     }
134 
135     public static struct Option {
136         enum Option IDENTITY_INSERT = Option("IDENTITY_INSERT");
137         enum Option PASSWORD = Option("PASSWORD"); // mysql
138         enum Option GLOBAL = Option("GLOBAL");
139         enum Option SESSION = Option("SESSION");
140         enum Option LOCAL = Option("LOCAL");
141 
142         private string _name;
143 
144         @property string name()
145         {
146             return _name;
147         }
148 
149         this(string name)
150         {
151             _name = name;
152         }
153 
154         bool opEquals(const Option h) nothrow {
155             return _name == h._name ;
156         } 
157 
158         bool opEquals(ref const Option h) nothrow {
159             return _name == h._name ;
160         }
161     }
162 
163 }