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.SQLEvalVisitorImpl;
17 
18 
19 
20 import hunt.collection;
21 
22 import hunt.sql.visitor.SQLEvalVisitorUtils;
23 import hunt.sql.ast.expr.SQLBinaryExpr;
24 import hunt.sql.ast.expr.SQLBinaryOpExpr;
25 import hunt.sql.ast.expr.SQLBooleanExpr;
26 import hunt.sql.ast.expr.SQLCaseExpr;
27 import hunt.sql.ast.expr.SQLCharExpr;
28 import hunt.sql.ast.expr.SQLHexExpr;
29 import hunt.sql.ast.expr.SQLIdentifierExpr;
30 import hunt.sql.ast.expr.SQLInListExpr;
31 import hunt.sql.ast.expr.SQLIntegerExpr;
32 import hunt.sql.ast.expr.SQLMethodInvokeExpr;
33 import hunt.sql.ast.expr.SQLNullExpr;
34 import hunt.sql.ast.expr.SQLNumberExpr;
35 import hunt.sql.ast.expr.SQLQueryExpr;
36 import hunt.sql.ast.expr.SQLVariantRefExpr;
37 import hunt.sql.visitor.functions.Function;
38 import hunt.sql.visitor.SQLASTVisitorAdapter;
39 import hunt.sql.visitor.SQLEvalVisitor;
40 
41 public class SQLEvalVisitorImpl : SQLASTVisitorAdapter , SQLEvalVisitor {
42 
43     alias visit = SQLASTVisitorAdapter.visit;
44     alias endVisit = SQLASTVisitorAdapter.endVisit;
45 
46     private List!(Object)        parameters;
47 
48     private Map!(string, Function) functions;
49 
50     private int                 variantIndex     = -1;
51 
52     private bool             markVariantIndex = true;
53 
54     public this(){
55         this(new ArrayList!(Object)(1));
56     }
57 
58     public this(List!(Object) parameters){
59         parameters       = new ArrayList!(Object)();
60         functions        = new HashMap!(string, Function)();
61         this.parameters = parameters;
62     }
63 
64     public List!(Object) getParameters() {
65         return parameters;
66     }
67 
68     public void setParameters(List!(Object) parameters) {
69         this.parameters = parameters;
70     }
71 
72     override public bool visit(SQLCharExpr x) {
73         return SQLEvalVisitorUtils.visit(this, x);
74     }
75 
76     public int incrementAndGetVariantIndex() {
77         return ++variantIndex;
78     }
79 
80     public int getVariantIndex() {
81         return variantIndex;
82     }
83 
84     override public bool visit(SQLVariantRefExpr x) {
85         return SQLEvalVisitorUtils.visit(this, x);
86     }
87 
88     override public bool visit(SQLBinaryOpExpr x) {
89         return SQLEvalVisitorUtils.visit(this, x);
90     }
91 
92     override public bool visit(SQLIntegerExpr x) {
93         return SQLEvalVisitorUtils.visit(this, x);
94     }
95 
96     override public bool visit(SQLNumberExpr x) {
97         return SQLEvalVisitorUtils.visit(this, x);
98     }
99     
100     override public bool visit(SQLHexExpr x) {
101         return SQLEvalVisitorUtils.visit(this, x);
102     }
103 
104     override
105     public bool visit(SQLCaseExpr x) {
106         return SQLEvalVisitorUtils.visit(this, x);
107     }
108 
109     override
110     public bool visit(SQLInListExpr x) {
111         return SQLEvalVisitorUtils.visit(this, x);
112     }
113 
114     override
115     public bool visit(SQLNullExpr x) {
116         return SQLEvalVisitorUtils.visit(this, x);
117     }
118 
119     override
120     public bool visit(SQLMethodInvokeExpr x) {
121         return SQLEvalVisitorUtils.visit(this, x);
122     }
123 
124     override
125     public bool visit(SQLQueryExpr x) {
126         return SQLEvalVisitorUtils.visit(this, x);
127     }
128 
129     public bool isMarkVariantIndex() {
130         return markVariantIndex;
131     }
132 
133     public void setMarkVariantIndex(bool markVariantIndex) {
134         this.markVariantIndex = markVariantIndex;
135     }
136 
137     override
138     public Function getFunction(string funcName) {
139         return functions.get(funcName);
140     }
141 
142     override
143     public void registerFunction(string funcName, Function function_p) {
144         functions.put(funcName, function_p);
145     }
146     
147     override public bool visit(SQLIdentifierExpr x) {
148         return SQLEvalVisitorUtils.visit(this, x);
149     }
150 
151     override
152     public void unregisterFunction(string funcName) {
153         functions.remove(funcName);
154     }
155     
156     override
157     public bool visit(SQLBooleanExpr x) {
158         x.getAttributes().put(SQLEvalVisitor.EVAL_VALUE, x.getBooleanValue());
159         return false;
160     }
161 
162     override
163     public bool visit(SQLBinaryExpr x) {
164         return SQLEvalVisitorUtils.visit(this, x);
165     }
166 }