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.SQLHexExpr;
17 
18 import hunt.sql.ast.SQLExprImpl;
19 import hunt.sql.visitor.SQLASTVisitor;
20 import hunt.sql.util.HexBin;
21 import hunt.sql.ast.expr.SQLValuableExpr;
22 import hunt.sql.ast.expr.SQLLiteralExpr;
23 import hunt.collection;
24 import hunt.sql.ast.SQLObject;
25 import hunt.Byte;
26 import hunt.Nullable;
27 import hunt.util.StringBuilder;
28 
29 public class SQLHexExpr : SQLExprImpl , SQLLiteralExpr, SQLValuableExpr {
30 
31     private  string hex;
32 
33     public this(string hex){
34         this.hex = hex;
35     }
36 
37     public string getHex() {
38         return hex;
39     }
40 
41     override public void output(StringBuilder buf) {
42         buf.append("0x");
43         buf.append(this.hex);
44 
45         string charset = " not implement @gxc ";//cast(string) getAttribute("USING");
46         if (charset !is null) {
47             buf.append(" USING ");
48             buf.append(charset);
49         }
50     }
51 
52     override  protected void accept0(SQLASTVisitor visitor) {
53         visitor.visit(this);
54         visitor.endVisit(this);
55     }
56 
57    override
58     public size_t toHash() @trusted nothrow {
59          int prime = 31;
60         size_t result = 1;
61         result = prime * result + ((hex.length == 0) ? 0 : hashOf(hex));
62         return result;
63     }
64 
65    override
66     public bool opEquals(Object obj) {
67         if (this is obj) {
68             return true;
69         }
70         if (obj is null) {
71             return false;
72         }
73         if (typeid(this) != typeid(obj)) {
74             return false;
75         }
76         SQLHexExpr other = cast(SQLHexExpr) obj;
77         if (hex is null) {
78             if (other.hex !is null) {
79                 return false;
80             }
81         } else if (!(hex == other.hex)) {
82             return false;
83         }
84         return true;
85     }
86 
87     public byte[] toBytes() {
88         return HexBin.decode(this.hex);
89     }
90 
91     override public SQLHexExpr clone () {
92         return new SQLHexExpr(hex);
93     }
94 
95     override public Object getValue() {
96         return new Nullable!(byte[])(toBytes());
97     }
98 
99    override
100     public List!SQLObject getChildren() {
101         return Collections.emptyList!(SQLObject)();
102     }
103 }