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.SQLBetweenExpr;
17 
18 import hunt.collection;
19 
20 import hunt.sql.ast;
21 import hunt.sql.visitor.SQLASTVisitor;
22 import hunt.sql.ast.expr.SQLBooleanExpr;
23 
24 public class SQLBetweenExpr : SQLExprImpl , SQLReplaceable //Serializable, 
25 {
26 
27     public SQLExpr            testExpr;
28     private bool           not;
29     public SQLExpr            beginExpr;
30     public SQLExpr            endExpr;
31 
32     public this(){
33 
34     }
35 
36     override public SQLBetweenExpr clone() {
37         SQLBetweenExpr x = new SQLBetweenExpr();
38         if (testExpr !is null) {
39             x.setTestExpr(testExpr.clone());
40         }
41         x.not = not;
42         if (beginExpr !is null) {
43             x.setBeginExpr(beginExpr.clone());
44         }
45         if (endExpr !is null) {
46             x.setEndExpr(endExpr.clone());
47         }
48         return x;
49     }
50 
51     public this(SQLExpr testExpr, SQLExpr beginExpr, SQLExpr endExpr){
52         setTestExpr(testExpr);
53         setBeginExpr(beginExpr);
54         setEndExpr(endExpr);
55     }
56 
57     public this(SQLExpr testExpr, bool not, SQLExpr beginExpr, SQLExpr endExpr){
58         this(testExpr, beginExpr, endExpr);
59         this.not = not;
60     }
61 
62     override  protected void accept0(SQLASTVisitor visitor) {
63         if (visitor.visit(this)) {
64             acceptChild(visitor, this.testExpr);
65             acceptChild(visitor, this.beginExpr);
66             acceptChild(visitor, this.endExpr);
67         }
68         visitor.endVisit(this);
69     }
70 
71     override public List!SQLObject getChildren() {
72         // return Arrays.!SQLObjectasList(this.testExpr, beginExpr, this.endExpr);
73         List!SQLObject ls = new ArrayList!SQLObject();
74         ls.add(this.testExpr);
75         ls.add(beginExpr);
76         ls.add(this.endExpr);
77         return ls;
78     }
79 
80     public SQLExpr getTestExpr() {
81         return this.testExpr;
82     }
83 
84     public void setTestExpr(SQLExpr testExpr) {
85         if (testExpr !is null) {
86             testExpr.setParent(this);
87         }
88         this.testExpr = testExpr;
89     }
90 
91     public bool isNot() {
92         return this.not;
93     }
94 
95     public void setNot(bool not) {
96         this.not = not;
97     }
98 
99     public SQLExpr getBeginExpr() {
100         return this.beginExpr;
101     }
102 
103     public void setBeginExpr(SQLExpr beginExpr) {
104         if (beginExpr !is null) {
105             beginExpr.setParent(this);
106         }
107         this.beginExpr = beginExpr;
108     }
109 
110     public SQLExpr getEndExpr() {
111         return this.endExpr;
112     }
113 
114     public void setEndExpr(SQLExpr endExpr) {
115         if (endExpr !is null) {
116             endExpr.setParent(this);
117         }
118         this.endExpr = endExpr;
119     }
120 
121    override
122     public size_t toHash() @trusted nothrow {
123          int prime = 31;
124         size_t result = 1;
125         result = prime * result + ((beginExpr is null) ? 0 : (cast(Object)beginExpr).toHash());
126         result = prime * result + ((endExpr is null) ? 0 : (cast(Object)endExpr).toHash());
127         result = prime * result + (not ? 1231 : 1237);
128         result = prime * result + ((testExpr is null) ? 0 : (cast(Object)testExpr).toHash());
129         return result;
130     }
131 
132    override
133     public bool opEquals(Object obj) {
134         if (this is obj) {
135             return true;
136         }
137         if (obj is null) {
138             return false;
139         }
140         if (typeid(this) != typeid(obj)) {
141             return false;
142         }
143         SQLBetweenExpr other = cast(SQLBetweenExpr) obj;
144         if (beginExpr is null) {
145             if (other.beginExpr !is null) {
146                 return false;
147             }
148         } else if (!(cast(Object)beginExpr).opEquals(cast(Object)(other.beginExpr))) {
149             return false;
150         }
151         if (endExpr is null) {
152             if (other.endExpr !is null) {
153                 return false;
154             }
155         } else if (!(cast(Object)endExpr).opEquals(cast(Object)(other.endExpr))) {
156             return false;
157         }
158         if (not != other.not) {
159             return false;
160         }
161         if (testExpr is null) {
162             if (other.testExpr !is null) {
163                 return false;
164             }
165         } else if (!(cast(Object)testExpr).opEquals(cast(Object)(other.testExpr))) {
166             return false;
167         }
168         return true;
169     }
170 
171     override public SQLDataType computeDataType() {
172         return SQLBooleanExpr.DEFAULT_DATA_TYPE;
173     }
174 
175    override
176     public bool replace(SQLExpr expr, SQLExpr target) {
177         if (expr == testExpr) {
178             setTestExpr(target);
179             return true;
180         }
181 
182         if (expr == beginExpr) {
183             setBeginExpr(target);
184             return true;
185         }
186 
187         if (expr == endExpr) {
188             setEndExpr(target);
189             return true;
190         }
191 
192         return false;
193     }
194 }