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.SQLDateExpr;
17 
18 import hunt.sql.ast.SQLDataType;
19 import hunt.sql.ast.SQLExpr;
20 import hunt.sql.ast.SQLExprImpl;
21 import hunt.sql.ast.SQLObject;
22 import hunt.sql.ast.statement.SQLCharacterDataType;
23 // import hunt.sql.dialect.oracle.ast.expr.OracleExpr;
24 // import hunt.sql.dialect.oracle.visitor.OracleASTVisitor;
25 import hunt.sql.visitor.SQLASTVisitor;
26 import hunt.sql.ast.expr.SQLValuableExpr;
27 import hunt.sql.ast.expr.SQLLiteralExpr;
28 import hunt.collection;
29 import hunt.sql.ast.expr.SQLCharExpr;
30 import hunt.String;
31 
32 import std.concurrency : initOnce;
33 
34 public class SQLDateExpr : SQLExprImpl , SQLLiteralExpr, SQLValuableExpr {
35     
36     static SQLDataType DEFAULT_DATA_TYPE() {
37         __gshared SQLDataType inst;
38         return initOnce!inst(new SQLCharacterDataType("date"));
39     }
40     
41     private SQLExpr literal;
42 
43     public this(){
44 
45     }
46 
47     public this(String literal) {
48         this.setLiteral(literal);
49     }
50 
51     public this(string literal) {
52         this.setLiteral(new String(literal));
53     }
54 
55     public SQLExpr getLiteral() {
56         return literal;
57     }
58 
59     public void setLiteral(String literal) {
60         setLiteral(new SQLCharExpr(literal));
61     }
62 
63     public void setLiteral(SQLExpr x) {
64         if (x !is null) {
65             x.setParent(this);
66         }
67         this.literal = x;
68     }
69 
70     public String getValue() {
71         if (cast(SQLCharExpr) literal !is null) {
72             return (cast(SQLCharExpr) literal).getText();
73         }
74         return null;
75     }
76 
77     override  protected void accept0(SQLASTVisitor visitor) {
78         visitor.visit(this);
79         visitor.endVisit(this);
80     }
81 
82    override
83     public size_t toHash() @trusted nothrow {
84          int prime = 31;
85         size_t result = 1;
86         result = prime * result + ((literal is null) ? 0 : (cast(Object)literal).toHash());
87         return result;
88     }
89 
90    override
91     public bool opEquals(Object obj) {
92         if (this is obj) {
93             return true;
94         }
95         if (obj is null) {
96             return false;
97         }
98         if (typeid(this) != typeid(obj)) {
99             return false;
100         }
101         SQLDateExpr other = cast(SQLDateExpr) obj;
102         if (literal is null) {
103             if (other.literal !is null) {
104                 return false;
105             }
106         } else if (!(cast(Object)(literal)).opEquals(cast(Object)(other.literal))) {
107             return false;
108         }
109         return true;
110     }
111 
112     override public SQLDateExpr clone() {
113         SQLDateExpr x = new SQLDateExpr();
114 
115         if (this.literal !is null) {
116             x.setLiteral(literal.clone());
117         }
118 
119         return x;
120     }
121 
122    override
123     public List!SQLObject getChildren() {
124         return Collections.emptyList!(SQLObject)();
125     }
126 }