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