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.SQLExistsExpr;
17 
18 import hunt.serialization.JsonSerializer;
19 
20 import hunt.collection;
21 
22 import hunt.sql.ast.SQLExprImpl;
23 import hunt.sql.ast.statement.SQLSelect;
24 import hunt.sql.visitor.SQLASTVisitor;
25 import hunt.sql.ast.SQLObject;
26 
27 
28 public  class SQLExistsExpr : SQLExprImpl //, Serializable 
29 {
30 
31     public bool            not              = false;
32     public SQLSelect          subQuery;
33 
34     public this(){
35 
36     }
37 
38     public this(SQLSelect subQuery){
39         this.setSubQuery(subQuery);
40     }
41 
42     public this(SQLSelect subQuery, bool not){
43         this.setSubQuery(subQuery);
44         this.not = not;
45     }
46 
47     public bool isNot() {
48         return this.not;
49     }
50 
51     public void setNot(bool not) {
52         this.not = not;
53     }
54 
55     public SQLSelect getSubQuery() {
56         return this.subQuery;
57     }
58 
59     public void setSubQuery(SQLSelect subQuery) {
60         if (subQuery !is null) {
61             subQuery.setParent(this);
62         }
63         this.subQuery = subQuery;
64     }
65 
66     override  protected void accept0(SQLASTVisitor visitor) {
67         if (visitor.visit(this)) {
68             acceptChild(visitor, this.subQuery);
69         }
70 
71         visitor.endVisit(this);
72     }
73 
74    override
75     public List!SQLObject getChildren() {
76         return Collections.singletonList!SQLObject(this.subQuery);
77     }
78 
79    override
80     public size_t toHash() @trusted nothrow {
81          int prime = 31;
82         size_t result = 1;
83         result = prime * result + (not ? 1231 : 1237);
84         result = prime * result + ((subQuery is null) ? 0 : (cast(Object)subQuery).toHash());
85         return result;
86     }
87 
88    override
89     public bool opEquals(Object obj) {
90         if (this is obj) {
91             return true;
92         }
93         if (obj is null) {
94             return false;
95         }
96         if (typeid(this) != typeid(obj)) {
97             return false;
98         }
99         SQLExistsExpr other = cast(SQLExistsExpr) obj;
100         if (not != other.not) {
101             return false;
102         }
103         if (subQuery is null) {
104             if (other.subQuery !is null) {
105                 return false;
106             }
107         } else if (!(cast(Object)(subQuery)).opEquals(cast(Object)(other.subQuery))) {
108             return false;
109         }
110         return true;
111     }
112 
113     override public SQLExistsExpr clone () {
114         SQLExistsExpr x = new SQLExistsExpr();
115 
116         x.not = not;
117         if (subQuery !is null) {
118             x.setSubQuery(subQuery.clone());
119         }
120 
121         return x;
122     }
123 }