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 Booleanuage governing permissions and
14  * limitations under the License.
15  */
16 module hunt.sql.visitor.functions.Isnull;
17 
18 // import hunt.sql.visitor.SQLEvalVisitor.EVAL_ERROR;
19 // import hunt.sql.visitor.SQLEvalVisitor.EVAL_VALUE;
20 // import hunt.sql.visitor.SQLEvalVisitor.EVAL_VALUE_NULL;
21 
22 import hunt.collection;
23 
24 import hunt.sql.ast.SQLExpr;
25 import hunt.sql.ast.expr.SQLMethodInvokeExpr;
26 import hunt.sql.visitor.SQLEvalVisitor;
27 import hunt.sql.visitor.functions.Function;
28 import hunt.Boolean;
29 import hunt.String;
30 import hunt.String;
31 import hunt.collection;
32 import std.conv;
33 import std.uni;
34 
35 import std.concurrency : initOnce;
36 
37 public class Isnull : Function {
38 
39     static Isnull instance() {
40         __gshared Isnull inst;
41         return initOnce!inst(new Isnull());
42     }    
43 
44     // public static Isnull instance ;
45 
46     // static this()
47     // {
48     //     instance = new Isnull();
49     // }
50 
51     public Object eval(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) {
52          List!(SQLExpr) parameters = x.getParameters();
53         if (parameters.size() == 0) {
54             return cast(Object)(SQLEvalVisitor.EVAL_ERROR);
55         }
56 
57         SQLExpr condition = parameters.get(0);
58         condition.accept(visitor);
59         Object itemValue = condition.getAttributes().get(SQLEvalVisitor.EVAL_VALUE);
60         if (itemValue == SQLEvalVisitor.EVAL_VALUE_NULL) {
61             return Boolean.TRUE;
62         } else if (itemValue is null) {
63             return null;
64         } else {
65             return Boolean.FALSE;
66         }
67     }
68 }