1 module hunt.sql.ast.expr.SQLSomeExpr;
2 import hunt.sql.ast.statement.SQLSelectItem;
3 import hunt.sql.ast.statement.SQLSelect;
4 import hunt.sql.ast.statement.SQLSelectQueryBlock;
5 import hunt.sql.ast.SQLExprImpl;
6 import hunt.collection;
7 import hunt.sql.visitor.SQLASTVisitor;
8 import hunt.sql.ast.SQLDataType;
9 import hunt.sql.ast.SQLObject;
10 import hunt.util.StringBuilder;
11 
12 public  class SQLSomeExpr : SQLExprImpl {
13 
14     public SQLSelect subQuery;
15 
16     public this(){
17 
18     }
19 
20     public this(SQLSelect select){
21         this.setSubQuery(select);
22     }
23 
24     override public SQLSomeExpr clone() {
25         SQLSomeExpr x = new SQLSomeExpr();
26         if (subQuery !is null) {
27             x.setSubQuery(subQuery.clone());
28         }
29         return x;
30     }
31 
32     public SQLSelect getSubQuery() {
33         return this.subQuery;
34     }
35 
36     public void setSubQuery(SQLSelect subQuery) {
37         if (subQuery !is null) {
38             subQuery.setParent(this);
39         }
40         this.subQuery = subQuery;
41     }
42 
43     override public void output(StringBuilder buf) {
44         this.subQuery.output(buf);
45     }
46 
47     protected override void accept0(SQLASTVisitor visitor) {
48         if (visitor.visit(this)) {
49             acceptChild(visitor, this.subQuery);
50         }
51 
52         visitor.endVisit(this);
53     }
54 
55     public override size_t toHash() @trusted nothrow {
56          int prime = 31;
57         size_t result = 1;
58         result = prime * result + ((subQuery is null) ? 0 : (cast(Object)subQuery).toHash());
59         return result;
60     }
61 
62     public override bool opEquals(Object obj) {
63         if (this is obj) {
64             return true;
65         }
66         if (obj is null) {
67             return false;
68         }
69         if (typeid(SQLSomeExpr) != typeid(obj)) {
70             return false;
71         }
72         SQLSomeExpr other = cast(SQLSomeExpr) obj;
73         if (subQuery is null) {
74             if (other.subQuery !is null) {
75                 return false;
76             }
77         } else if (!(cast(Object)(subQuery)).opEquals(cast(Object)(other.subQuery))) {
78             return false;
79         }
80         return true;
81     }
82 
83     override public SQLDataType computeDataType() {
84         if (subQuery is null) {
85             return null;
86         }
87 
88         SQLSelectQueryBlock queryBlock = subQuery.getFirstQueryBlock();
89         if (queryBlock is null) {
90             return null;
91         }
92 
93         List!SQLSelectItem selectList = queryBlock.getSelectList();
94         if (selectList.size() == 1) {
95             return selectList.get(0).computeDataType();
96         }
97 
98         return null;
99     }
100 
101     public override List!SQLObject getChildren() {
102         return Collections.singletonList!SQLObject(this.subQuery);
103     }
104 }