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.Char; 17 18 // import hunt.sql.visitor.SQLEvalVisitor.EVAL_VALUE; 19 20 21 import hunt.sql.ast.SQLExpr; 22 import hunt.sql.ast.expr.SQLMethodInvokeExpr; 23 import hunt.sql.visitor.SQLEvalVisitor; 24 import hunt.sql.visitor.functions.Function; 25 import hunt.Number; 26 import hunt.String; 27 import hunt.String; 28 import hunt.collection; 29 import hunt.math; 30 import hunt.text; 31 import std.concurrency : initOnce; 32 33 public class Char : Function { 34 35 static Char instance() { 36 __gshared Char inst; 37 return initOnce!inst(new Char()); 38 } 39 // public static Char instance; 40 41 // static this() 42 // { 43 // instance = new Char(); 44 // } 45 46 public Object eval(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) { 47 if (x.getParameters().size() == 0) { 48 return cast(Object)(SQLEvalVisitor.EVAL_ERROR); 49 } 50 51 StringBuilder buf = new StringBuilder(x.getParameters().size()); 52 foreach(SQLExpr param ; x.getParameters()) { 53 param.accept(visitor); 54 55 Object paramValue = param.getAttributes().get(SQLEvalVisitor.EVAL_VALUE); 56 57 if (cast(Number)(paramValue) !is null) { 58 int charCode = (cast(Number) paramValue).intValue(); 59 buf.append(cast(char) charCode); 60 } else if (cast(String)(paramValue) !is null) { 61 try { 62 int charCode = new BigDecimal((cast(String) paramValue).value()).intValue(); 63 buf.append(cast(char) charCode); 64 } catch (Exception e) { 65 } 66 } else { 67 return cast(Object)(SQLEvalVisitor.EVAL_ERROR); 68 } 69 } 70 71 return new String(buf.toString()); 72 } 73 }