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.SQLBinaryExpr;
17 
18 import hunt.sql.ast.SQLExprImpl;
19 import hunt.sql.ast.SQLObject;
20 import hunt.sql.visitor.SQLASTVisitor;
21 import hunt.sql.util.Utils;
22 import hunt.sql.ast.expr.SQLValuableExpr;
23 import hunt.sql.ast.expr.SQLLiteralExpr;
24 import hunt.collection;
25 import hunt.Number;
26 import hunt.String;
27 import hunt.math;
28 import hunt.text;
29 import hunt.Long;
30 
31 public class SQLBinaryExpr : SQLExprImpl , SQLLiteralExpr, SQLValuableExpr {
32 
33     private string text;
34 
35     private  Number val;
36 
37     public this(){
38 
39     }
40 
41     public this(string value){
42         super();
43         this.text = value;
44     }
45 
46     public string getText() {
47         return text;
48     }
49 
50     public Object getValue() {
51         if (text is null) {
52             return null;
53         }
54 
55         if (val is null) {
56             long[] words = new long[text.length / 64 + 1];
57             for (int i = cast(int)(text.length) - 1; i >= 0; --i) {
58                 char ch = charAt(text, i);
59                 if (ch == '1') {
60                     int wordIndex = i >> 6;
61                     words[wordIndex] |= (1L << (text.length - 1 - i));
62                 }
63             }
64 
65             if (words.length == 1) {
66                 val = new Long(words[0]);
67             } else {
68                 byte[] bytes = new byte[words.length * 8];
69 
70                 for (int i = 0; i < words.length; ++i) {
71                     Utils.putLong(bytes, cast(int)(words.length - 1 - i) * 8, words[i]);
72                 }
73 
74                 val = new BigInteger(bytes);
75             }
76         }
77 
78         return cast(Object)val;
79     }
80 
81     public void setValue(string value) {
82         this.text = value;
83     }
84 
85     override public void accept0(SQLASTVisitor visitor) {
86         visitor.visit(this);
87 
88         visitor.endVisit(this);
89     }
90 
91     override public void output(StringBuilder buf) {
92         buf.append("b'");
93         buf.append(text);
94         buf.append('\'');
95     }
96 
97    override
98     public size_t toHash() @trusted nothrow {
99          int prime = 31;
100         size_t result = 1;
101         result = prime * result + ((text is null) ? 0 : hashOf(text));
102         return result;
103     }
104 
105     override public SQLBinaryExpr clone() {
106         return new SQLBinaryExpr(text);
107     }
108 
109    override
110     public List!SQLObject getChildren() {
111         return Collections.emptyList!(SQLObject)();
112     }
113 
114    override
115     public bool opEquals(Object obj) {
116         if (this is obj) {
117             return true;
118         }
119         if (obj is null) {
120             return false;
121         }
122         if (typeid(this) != typeid(obj)) {
123             return false;
124         }
125         SQLBinaryExpr other = cast(SQLBinaryExpr) obj;
126         if (text is null) {
127             if (other.text !is null) {
128                 return false;
129             }
130         } else if (!(text == other.text)) {
131             return false;
132         }
133         return true;
134     }
135 
136 }