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.Unhex; 17 18 // import hunt.sql.visitor.SQLEvalVisitor.EVAL_EXPR; 19 // import hunt.sql.visitor.SQLEvalVisitor.EVAL_VALUE; 20 21 22 import hunt.sql.ast.SQLExpr; 23 import hunt.sql.ast.expr.SQLMethodInvokeExpr; 24 import hunt.sql.visitor.SQLEvalVisitor; 25 import hunt.sql.util.HexBin; 26 import hunt.sql.visitor.functions.Function; 27 //import hunt.lang; 28 import hunt.String; 29 import hunt.String; 30 import hunt.collection; 31 import std.conv; 32 import std.uni; 33 import hunt.text; 34 35 import std.concurrency : initOnce; 36 37 public class Unhex : Function { 38 39 static Unhex instance() { 40 __gshared Unhex inst; 41 return initOnce!inst(new Unhex()); 42 } 43 44 // public static Unhex instance; 45 46 // static this() 47 // { 48 // instance = new Unhex(); 49 // } 50 51 public Object eval(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) { 52 if (x.getParameters().size() != 1) { 53 return cast(Object)(SQLEvalVisitor.EVAL_ERROR); 54 } 55 56 SQLExpr param0 = x.getParameters().get(0); 57 58 if (cast(SQLMethodInvokeExpr)(param0) !is null) { 59 SQLMethodInvokeExpr paramMethodExpr = cast(SQLMethodInvokeExpr) param0; 60 if (paramMethodExpr.getMethodName().equalsIgnoreCase("hex")) { 61 SQLExpr subParamExpr = paramMethodExpr.getParameters().get(0); 62 subParamExpr.accept(visitor); 63 64 Object param0Value = subParamExpr.getAttributes().get(SQLEvalVisitor.EVAL_VALUE); 65 if (param0Value is null) { 66 x.putAttribute(SQLEvalVisitor.EVAL_EXPR,cast(Object)subParamExpr); 67 return cast(Object)(SQLEvalVisitor.EVAL_ERROR); 68 } 69 70 return param0Value; 71 } 72 } 73 74 param0.accept(visitor); 75 76 Object param0Value = param0.getAttributes().get(SQLEvalVisitor.EVAL_VALUE); 77 if (param0Value is null) { 78 return cast(Object)(SQLEvalVisitor.EVAL_ERROR); 79 } 80 81 if (cast(String)(param0Value) !is null) { 82 byte[] bytes = HexBin.decode((cast(String) param0Value).value()); 83 if (bytes is null) { 84 return cast(Object)(SQLEvalVisitor.EVAL_VALUE_NULL); 85 } 86 87 string result; 88 try { 89 result = cast(string)(bytes); 90 } catch (Exception e) { 91 throw new Exception(e.msg, e); 92 } 93 return new String(result); 94 } 95 96 return cast(Object)(SQLEvalVisitor.EVAL_ERROR); 97 } 98 }