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