1 module hunt.sql.ast.expr.SQLNumberExpr;
2 import hunt.sql.ast.expr.SQLValuableExpr;
3 import hunt.sql.ast.expr.SQLNumericLiteralExpr;
4 import hunt.sql.ast.SQLDataType;
5 import hunt.Number;
6 import hunt.math;
7 import hunt.collection;
8 import hunt.String;
9 import hunt.text;
10 import hunt.sql.visitor.SQLASTVisitor;
11 import hunt.sql.ast.SQLDataTypeImpl;
12 import hunt.sql.util.Utils;
13 import hunt.util.StringBuilder;
14 
15 public class SQLNumberExpr : SQLNumericLiteralExpr, SQLValuableExpr
16 {
17 
18     public static SQLDataType defaultDataType;
19 
20     private Number number;
21 
22     private char[] chars;
23 
24     public this()
25     {
26         defaultDataType = new SQLDataTypeImpl("number");
27     }
28 
29     public this(Number number)
30     {
31         this.number = number;
32     }
33 
34     public this(char[] chars)
35     {
36         this.chars = chars;
37     }
38 
39     public override Number getNumber()
40     {
41         try
42         {
43             if (chars !is null && number is null)
44             {
45                 this.number = new BigDecimal(chars);
46             }
47         }
48         catch (Exception)
49         {
50         }
51 
52         return this.number;
53     }
54 
55     public Object getValue()
56     {
57         return cast(Object)getNumber();
58     }
59 
60     override public void setNumber(Number number)
61     {
62         this.number = number;
63         this.chars = null;
64     }
65 
66     override void output(StringBuilder buf)
67     {
68         if (chars !is null)
69         {
70             buf.append(chars);
71         }
72         else
73         {
74             buf.append((cast(Object)(this.number)).toString());
75         }
76     }
77 
78     override protected void accept0(SQLASTVisitor visitor)
79     {
80         visitor.visit(this);
81         visitor.endVisit(this);
82     }
83 
84     override public size_t toHash() @trusted nothrow
85     {
86         Number number;
87         try
88         {
89              number = getNumber();
90             if (number is null)
91             {
92                 return 0;
93             }
94         }
95         catch (Exception)
96         {
97         }
98 
99         return (cast(Object) number).toHash();
100     }
101 
102     override public bool opEquals(Object obj)
103     {
104         if (chars !is null && number is null)
105         {
106             this.number = new BigDecimal(chars);
107         }
108 
109         if (this is obj)
110         {
111             return true;
112         }
113         if (obj is null)
114         {
115             return false;
116         }
117         if (typeid(this) != typeid(obj))
118         {
119             return false;
120         }
121 
122         SQLNumberExpr other = cast(SQLNumberExpr) obj;
123         return Utils.equals(cast(Object)getNumber(), cast(Object)(other.getNumber()));
124     }
125 
126     override public SQLNumberExpr clone()
127     {
128         SQLNumberExpr x = new SQLNumberExpr();
129         x.chars = chars;
130         x.number = number;
131         return x;
132     }
133 
134     override public SQLDataType computeDataType()
135     {
136         return defaultDataType;
137     }
138 }