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