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