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.Least;
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.SQLEvalVisitorUtils;
24 import hunt.sql.visitor.functions.Function;
25 //import hunt.lang;
26 import hunt.String;
27 import hunt.String;
28 import hunt.collection;
29 import std.conv;
30 import std.uni;
31 
32 import std.concurrency : initOnce;
33 
34 public class Least : Function {
35     
36     static Least instance() {
37         __gshared Least inst;
38         return initOnce!inst(new Least());
39     }    
40 
41     // public  static Least instance;
42 
43     // static this()
44     // {
45     //     instance = new Least();
46     // }
47 
48     public Object eval(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) {
49         Object result = null;
50         foreach(SQLExpr item ; x.getParameters()) {
51             item.accept(visitor);
52 
53             Object itemValue = item.getAttributes().get(SQLEvalVisitor.EVAL_VALUE);
54             if (result is null) {
55                 result = itemValue;
56             } else {
57                 if (SQLEvalVisitorUtils.lt(itemValue, result)) {
58                     result = itemValue;
59                 }
60             }
61         }
62 
63         return result;
64     }
65 }