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.SQLAssignItem;
17 
18 import hunt.sql.ast.SQLExpr;
19 import hunt.sql.ast.SQLObjectImpl;
20 import hunt.sql.ast.SQLReplaceable;
21 import hunt.sql.visitor.SQLASTVisitor;
22 import hunt.collection;
23 import hunt.util.StringBuilder;
24 
25 public class SQLAssignItem : SQLObjectImpl , SQLReplaceable {
26 
27     private SQLExpr target;
28     private SQLExpr value;
29 
30     public this(){
31     }
32 
33     public this(SQLExpr target, SQLExpr value){
34         setTarget(target);
35         setValue(value);
36     }
37 
38     override public SQLAssignItem clone() {
39         SQLAssignItem x = new SQLAssignItem();
40         if (target !is null) {
41             x.setTarget(target.clone());
42         }
43         if (value !is null) {
44             x.setValue(value.clone());
45         }
46         return x;
47     }
48 
49     public SQLExpr getTarget() {
50         return target;
51     }
52 
53     public void setTarget(SQLExpr target) {
54         if (target !is null) {
55             target.setParent(this);
56         }
57         this.target = target;
58     }
59 
60     public SQLExpr getValue() {
61         return value;
62     }
63 
64     public void setValue(SQLExpr value) {
65         if (value !is null) {
66             value.setParent(this);
67         }
68         this.value = value;
69     }
70 
71     override public void output(StringBuilder buf) {
72         target.output(buf);
73         buf.append(" = ");
74         value.output(buf);
75     }
76 
77     
78     override  protected void accept0(SQLASTVisitor visitor) {
79         if (visitor.visit(this)) {
80             acceptChild(visitor, this.target);
81             acceptChild(visitor, this.value);
82         }
83         visitor.endVisit(this);
84     }
85 
86     override
87     public bool replace(SQLExpr expr, SQLExpr target) {
88         if (this.target == expr) {
89             setTarget(target);
90             return true;
91         }
92 
93         if (this.value == expr) {
94             setValue(target);
95             return true;
96         }
97         return false;
98     }
99 }