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.Ltrim;
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.lang;
25 import hunt.String;
26 import hunt.String;
27 import hunt.collection;
28 import std.conv;
29 import std.uni;
30 import hunt.sql.parser.CharTypes;
31 import hunt.text;
32 
33 import std.concurrency : initOnce;
34 
35 public class Ltrim : Function {
36     
37     static Ltrim instance() {
38         __gshared Ltrim inst;
39         return initOnce!inst(new Ltrim());
40     } 
41 
42     // public  static Ltrim instance;
43 
44     // static this()
45     // {
46     //     instance = new Ltrim();
47     // }
48 
49     public Object eval(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) {
50         if (x.getParameters().size() != 1) {
51             return cast(Object)(SQLEvalVisitor.EVAL_ERROR);
52         }
53 
54         SQLExpr param0 = x.getParameters().get(0);
55         param0.accept(visitor);
56 
57         Object param0Value = param0.getAttributes().get(SQLEvalVisitor.EVAL_VALUE);
58         if (param0Value is null) {
59             return cast(Object)(SQLEvalVisitor.EVAL_ERROR);
60         }
61 
62         string strValue = param0Value.toString();
63         
64         int index = -1;
65         for (int i = 0; i < strValue.length; ++i) {
66             if (!CharTypes.isWhitespace(charAt(strValue, i))) {
67                 index = i;
68                 break;
69             }
70         }
71         
72         if (index <= 0) {
73             return new String(strValue);
74         } else {
75             return new String(strValue.substring(index));
76         }
77     }
78 }