1 module hunt.sql.ast.expr.SQLUnaryExpr;
2 
3 import hunt.sql.ast.expr.SQLUnaryOperator;
4 import hunt.sql.ast.SQLExpr;
5 import hunt.sql.visitor.SQLASTVisitor;
6 import hunt.sql.ast.SQLExprImpl;
7 import hunt.collection;
8 import hunt.sql.ast.SQLObject;
9 
10 public class SQLUnaryExpr : SQLExprImpl // Serializable 
11 {
12 
13     private SQLExpr           expr;
14     private SQLUnaryOperator  operator;
15 
16     public this(){
17 
18     }
19 
20     public this(SQLUnaryOperator operator, SQLExpr expr){
21         this.operator = operator;
22         this.setExpr(expr);
23     }
24 
25     override public SQLUnaryExpr clone() {
26         SQLUnaryExpr x = new SQLUnaryExpr();
27         if (expr !is null) {
28             x.setExpr(expr.clone());
29         }
30         x.operator = operator;
31         return x;
32     }
33 
34     public SQLUnaryOperator getOperator() {
35         return operator;
36     }
37 
38     public void setOperator(SQLUnaryOperator operator) {
39         this.operator = operator;
40     }
41 
42     public SQLExpr getExpr() {
43         return this.expr;
44     }
45 
46     public void setExpr(SQLExpr expr) {
47         if (expr !is null) {
48             expr.setParent(this);
49         }
50         this.expr = expr;
51     }
52 
53     override  protected void accept0(SQLASTVisitor visitor) {
54         if (visitor.visit(this)) {
55             acceptChild(visitor, this.expr);
56         }
57 
58         visitor.endVisit(this);
59     }
60 
61     public override List!SQLObject getChildren() {
62         return Collections.singletonList!SQLObject(this.expr);
63     }
64 
65     public override size_t toHash() @trusted nothrow {
66          int prime = 31;
67         size_t result = 1;
68         result = prime * result + ((expr is null) ? 0 : (cast(Object)expr).toHash());
69         result = prime * result + hashOf(operator);
70         return result;
71     }
72 
73     public override bool opEquals(Object obj) {
74         if (this is obj) {
75             return true;
76         }
77         if (obj is null) {
78             return false;
79         }
80         if (typeid(SQLUnaryExpr) != typeid(obj)) {
81             return false;
82         }
83         SQLUnaryExpr other = cast(SQLUnaryExpr) obj;
84         if (expr is null) {
85             if (other.expr !is null) {
86                 return false;
87             }
88         } else if (!(cast(Object)(expr)).opEquals(cast(Object)(other.expr))) {
89             return false;
90         }
91         if (operator != other.operator) {
92             return false;
93         }
94         return true;
95     }
96 }