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.SQLIntegerExpr;
17 
18 import hunt.sql.ast.SQLDataType;
19 import hunt.sql.ast.SQLDataTypeImpl;
20 import hunt.sql.visitor.SQLASTVisitor;
21 import hunt.sql.ast.expr.SQLNumericLiteralExpr;
22 import hunt.sql.ast.expr.SQLValuesExpr;
23 import hunt.sql.ast.expr.SQLValuableExpr;
24 import hunt.Number;
25 // import hunt.Integer;
26 import hunt.Long;
27 import hunt.collection;
28 import hunt.util.StringBuilder;
29 
30 import std.concurrency : initOnce;
31 
32 public class SQLIntegerExpr : SQLNumericLiteralExpr , SQLValuableExpr {
33     
34     static SQLDataType DEFAULT_DATA_TYPE() {
35         __gshared SQLDataType inst;
36         return initOnce!inst(new SQLDataTypeImpl("bigint"));
37     }
38 
39     private Number number;
40 
41     public this(Number number){
42 
43         this.number = number;
44     }
45 
46     public this(long number){
47         this.number = new Long(number);
48     }
49 
50     public override Number getNumber() {
51         return this.number;
52     }
53 
54     override public void setNumber(Number number) {
55         this.number = number;
56     }
57 
58     public void setNumber(long number) {
59         this.number = new Long(number);
60     }
61 
62     override public void output(StringBuilder buf) {
63         buf.append(this.number.longValue);
64     }
65 
66     override  protected void accept0(SQLASTVisitor visitor) {
67         visitor.visit(this);
68 
69         visitor.endVisit(this);
70     }
71 
72    override
73     public size_t toHash() @trusted nothrow {
74          int prime = 31;
75         size_t result = 1;
76         result = prime * result + ((number is null) ? 0 : (cast(Object)number).toHash());
77         return result;
78     }
79 
80    override
81     public bool opEquals(Object obj) {
82         if (this is obj) {
83             return true;
84         }
85         if (obj is null) {
86             return false;
87         }
88         if (typeid(this) != typeid(obj)) {
89             return false;
90         }
91         SQLIntegerExpr other = cast(SQLIntegerExpr) obj;
92         if (number is null) {
93             if (other.number !is null) {
94                 return false;
95             }
96         } else if (!(cast(Object)(number)).opEquals(cast(Object)(other.number))) {
97             return false;
98         }
99         return true;
100     }
101 
102    override
103     public Object getValue() {
104         return cast(Object)this.number;
105     }
106 
107     override public SQLIntegerExpr clone() {
108         return new SQLIntegerExpr(this.number);
109     }
110 
111     override public SQLDataType computeDataType() {
112         return DEFAULT_DATA_TYPE;
113     }
114 
115 }