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