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.Instr;
17 
18 // import hunt.sql.visitor.SQLEvalVisitor.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 std.string;
25 import hunt.String;
26 import hunt.String;
27 import hunt.Integer;
28 
29 import std.concurrency : initOnce;
30 
31 public class Instr : Function {
32 
33     static Instr instance() {
34         __gshared Instr inst;
35         return initOnce!inst(new Instr());
36     }        
37     // public  static Instr instance;
38 
39     // static this()
40     // {
41     //     instance = new Instr();
42     // }
43     
44     public Object eval(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) {
45         if (x.getParameters().size() != 2) {
46             return cast(Object)(SQLEvalVisitor.EVAL_ERROR);
47         }
48 
49         SQLExpr param0 = x.getParameters().get(0);
50         SQLExpr param1 = x.getParameters().get(1);
51         param0.accept(visitor);
52         param1.accept(visitor);
53 
54         Object param0Value = param0.getAttributes().get(SQLEvalVisitor.EVAL_VALUE);
55         Object param1Value = param1.getAttributes().get(SQLEvalVisitor.EVAL_VALUE);
56         if (param0Value is null || param1Value is null) {
57             return cast(Object)(SQLEvalVisitor.EVAL_ERROR);
58         }
59 
60         string strValue0 = param0Value.toString();
61         string strValue1 = param1Value.toString();
62 
63         int result = cast(int)(strValue0.indexOf(strValue1) + 1);
64         return new Integer(result);
65     }
66 }