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