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.expr.SQLInListExpr;
17 
18 
19 import hunt.sql.ast.SQLDataType;
20 import hunt.sql.ast.SQLExpr;
21 import hunt.sql.ast.SQLExprImpl;
22 import hunt.sql.ast.SQLObject;
23 import hunt.sql.visitor.SQLASTVisitor;
24 import hunt.collection;
25 import hunt.sql.ast.expr.SQLBooleanExpr;
26 
27 public  class SQLInListExpr : SQLExprImpl //, Serializable 
28 {
29     private bool           not              = false;
30     private SQLExpr           expr;
31     private List!SQLExpr     targetList;
32 
33     public this(){
34         targetList       = new ArrayList!SQLExpr();
35     }
36 
37     public this(SQLExpr expr){
38         this();
39         this.setExpr(expr);
40     }
41 
42     public this(SQLExpr expr, bool not){
43         this();
44         this.setExpr(expr);
45         this.not = not;
46     }
47 
48     override public SQLInListExpr clone() {
49         SQLInListExpr x = new SQLInListExpr();
50         x.not = not;
51         if (expr !is null) {
52             x.setExpr(expr.clone());
53         }
54         foreach (SQLExpr e ; targetList) {
55             SQLExpr e2 = e.clone();
56             e2.setParent(x);
57             x.targetList.add(e2);
58         }
59         return x;
60     }
61 
62     public bool isNot() {
63         return this.not;
64     }
65 
66     public void setNot(bool not) {
67         this.not = not;
68     }
69 
70     public SQLExpr getExpr() {
71         return this.expr;
72     }
73 
74     public void setExpr(SQLExpr expr) {
75         if (expr !is null) {
76             expr.setParent(this);
77         }
78         
79         this.expr = expr;
80     }
81 
82     public List!SQLExpr getTargetList() {
83         return this.targetList;
84     }
85 
86     public void setTargetList(List!SQLExpr targetList) {
87         this.targetList = targetList;
88     }
89 
90     override  protected void accept0(SQLASTVisitor visitor) {
91         if (visitor.visit(this)) {
92             acceptChild(visitor, this.expr);
93             acceptChild!SQLExpr(visitor, this.targetList);
94         }
95 
96         visitor.endVisit(this);
97     }
98 
99     override public List!SQLObject getChildren() {
100         List!SQLObject children = new ArrayList!SQLObject();
101         if (this.expr !is null) {
102             children.add(this.expr);
103         }
104         children.addAll(cast(List!SQLObject)(this.targetList));
105         return children;
106     }
107 
108    override
109     public size_t toHash() @trusted nothrow {
110          int prime = 31;
111         size_t result = 1;
112         result = prime * result + ((expr is null) ? 0 : (cast(Object)expr).toHash());
113         result = prime * result + (not ? 1231 : 1237);
114         result = prime * result + ((targetList is null) ? 0 : (cast(Object)targetList).toHash());
115         return result;
116     }
117 
118    override
119     public bool opEquals(Object obj) {
120         if (this is obj) {
121             return true;
122         }
123         if (obj is null) {
124             return false;
125         }
126         if (typeid(this) != typeid(obj)) {
127             return false;
128         }
129         SQLInListExpr other = cast(SQLInListExpr) obj;
130         if (expr is null) {
131             if (other.expr !is null) {
132                 return false;
133             }
134         } else if (!(cast(Object)(expr)).opEquals(cast(Object)(other.expr))) {
135             return false;
136         }
137         if (not != other.not) {
138             return false;
139         }
140         if (targetList is null) {
141             if (other.targetList !is null) {
142                 return false;
143             }
144         } else if (!(cast(Object)(targetList)).opEquals(cast(Object)(other.targetList))) {
145             return false;
146         }
147         return true;
148     }
149 
150     override public SQLDataType computeDataType() {
151         return SQLBooleanExpr.DEFAULT_DATA_TYPE;
152     }
153 }