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.dialect.mysql.ast.expr.MySqlExtractExpr;
17 
18 import hunt.sql.ast.SQLExpr;
19 import hunt.sql.ast.SQLExprImpl;
20 import hunt.sql.ast.expr.SQLIntervalUnit;
21 import hunt.sql.dialect.mysql.visitor.MySqlASTVisitor;
22 import hunt.sql.visitor.SQLASTVisitor;
23 import hunt.sql.dialect.mysql.ast.expr.MySqlExpr;
24 import hunt.sql.ast.SQLObject;
25 
26 
27 import hunt.collection;
28 
29 public class MySqlExtractExpr : SQLExprImpl , MySqlExpr {
30 
31     private SQLExpr           value;
32     private SQLIntervalUnit unit;
33 
34     public this(){
35     }
36 
37     override public MySqlExtractExpr clone() {
38         MySqlExtractExpr x = new MySqlExtractExpr();
39         if (value !is null) {
40             x.setValue(value.clone());
41         }
42         x.unit = unit;
43         return x;
44     }
45 
46     public SQLExpr getValue() {
47         return value;
48     }
49 
50     public void setValue(SQLExpr value) {
51         if (value !is null) {
52             value.setParent(this);
53         }
54         this.value = value;
55     }
56 
57     public SQLIntervalUnit getUnit() {
58         return unit;
59     }
60 
61     public void setUnit(SQLIntervalUnit unit) {
62         this.unit = unit;
63     }
64 
65     override  protected void accept0(SQLASTVisitor visitor) {
66         MySqlASTVisitor mysqlVisitor = cast(MySqlASTVisitor) visitor;
67         if (mysqlVisitor.visit(this)) {
68             acceptChild(visitor, value);
69         }
70         mysqlVisitor.endVisit(this);
71     }
72     override
73     public List!SQLObject getChildren() {
74         return Collections.singletonList!SQLObject(value);
75     }
76 
77     override
78     public size_t toHash() @trusted nothrow {
79          int prime = 31;
80         size_t result = 1;
81         result = prime * result + hashOf(unit);
82         result = prime * result + ((value is null) ? 0 : (cast(Object)value).toHash());
83         return result;
84     }
85 
86     override
87     public bool opEquals(Object obj) {
88         if (this == obj) {
89             return true;
90         }
91         if (obj is null) {
92             return false;
93         }
94         if (!(cast(MySqlExtractExpr)(obj) !is null)) {
95             return false;
96         }
97         MySqlExtractExpr other = cast(MySqlExtractExpr) obj;
98         if (unit != other.unit) {
99             return false;
100         }
101         if (value is null) {
102             if (other.value !is null) {
103                 return false;
104             }
105         } else if (!(cast(Object)(value)).opEquals(cast(Object)(other.value))) {
106             return false;
107         }
108         return true;
109     }
110 
111 }