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