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.Insert; 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.visitor.SQLEvalVisitor; 23 import hunt.sql.visitor.functions.Function; 24 import hunt.Number; 25 import hunt.String; 26 import hunt.String; 27 import hunt.collection; 28 import std.conv; 29 import std.uni; 30 import hunt.text; 31 32 import std.concurrency : initOnce; 33 34 public class Insert : Function { 35 36 static Insert instance() { 37 __gshared Insert inst; 38 return initOnce!inst(new Insert()); 39 } 40 41 // public static Insert instance; 42 // static this() 43 // { 44 // instance = new Insert(); 45 // } 46 47 public Object eval(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) { 48 if (x.getParameters().size() != 4) { 49 return cast(Object)(SQLEvalVisitor.EVAL_ERROR); 50 } 51 52 SQLExpr param0 = x.getParameters().get(0); 53 SQLExpr param1 = x.getParameters().get(1); 54 SQLExpr param2 = x.getParameters().get(2); 55 SQLExpr param3 = x.getParameters().get(3); 56 param0.accept(visitor); 57 param1.accept(visitor); 58 param2.accept(visitor); 59 param3.accept(visitor); 60 61 Object param0Value = param0.getAttributes().get(SQLEvalVisitor.EVAL_VALUE); 62 Object param1Value = param1.getAttributes().get(SQLEvalVisitor.EVAL_VALUE); 63 Object param2Value = param2.getAttributes().get(SQLEvalVisitor.EVAL_VALUE); 64 Object param3Value = param3.getAttributes().get(SQLEvalVisitor.EVAL_VALUE); 65 66 if (!(cast(String)(param0Value) !is null)) { 67 return cast(Object)(SQLEvalVisitor.EVAL_ERROR); 68 } 69 if (!(cast(Number)(param1Value) !is null)) { 70 return cast(Object)(SQLEvalVisitor.EVAL_ERROR); 71 } 72 if (!(cast(Number)(param2Value) !is null)) { 73 return cast(Object)(SQLEvalVisitor.EVAL_ERROR); 74 } 75 if (!(cast(String)(param3Value) !is null)) { 76 return cast(Object)(SQLEvalVisitor.EVAL_ERROR); 77 } 78 79 string str = (cast(String) param0Value).value(); 80 int pos = (cast(Number) param1Value).intValue(); 81 int len = (cast(Number) param2Value).intValue(); 82 string newstr = (cast(String) param3Value).value(); 83 84 if (pos <= 0) { 85 return new String(str); 86 } 87 88 if (pos == 1) { 89 if (len > str.length) { 90 return new String(newstr); 91 } 92 return new String(newstr ~ str.substring(len)); 93 } 94 95 string first = str.substring(0, pos - 1); 96 if (pos + len - 1 > str.length) { 97 return new String(first ~ newstr); 98 } 99 100 return new String(first ~ newstr ~ str.substring(pos + len - 1)); 101 } 102 }