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.visitor.functions.Hex; 17 18 // import hunt.sql.visitor.SQLEvalVisitor.EVAL_VALUE; 19 20 import hunt.sql.ast.SQLExpr; 21 import hunt.sql.ast.expr.SQLMethodInvokeExpr; 22 import hunt.sql.parser.ParserException; 23 import hunt.sql.visitor.SQLEvalVisitor; 24 import hunt.sql.util.HexBin; 25 import hunt.sql.visitor.functions.Function; 26 import hunt.Number; 27 import hunt.Long; 28 import hunt.String; 29 import hunt.String; 30 import hunt.collection; 31 import std.conv; 32 import std.uni; 33 34 import std.concurrency : initOnce; 35 36 public class Hex : Function { 37 38 static Hex instance() { 39 __gshared Hex inst; 40 return initOnce!inst(new Hex()); 41 } 42 43 // public static Hex instance; 44 45 // static this() 46 // { 47 // instance = new Hex(); 48 // } 49 50 public Object eval(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) { 51 if (x.getParameters().size() != 1) { 52 throw new ParserException("argument's != 1, " ~ x.getParameters().size().to!string); 53 } 54 55 SQLExpr param0 = x.getParameters().get(0); 56 param0.accept(visitor); 57 58 Object param0Value = param0.getAttributes().get(SQLEvalVisitor.EVAL_VALUE); 59 if (param0Value is null) { 60 return cast(Object)(SQLEvalVisitor.EVAL_ERROR); 61 } 62 63 if (cast(String)(param0Value) !is null) { 64 byte[] bytes = (cast(String) param0Value).getBytes(); 65 string result = HexBin.encode(bytes); 66 return new String(result); 67 } 68 69 if (cast(Number)(param0Value) !is null) { 70 long value = (cast(Number) param0Value).longValue(); 71 string result = toUpper(Long.toHexString(value)); 72 return new String(result); 73 } 74 75 return cast(Object)(SQLEvalVisitor.EVAL_ERROR); 76 } 77 }