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.dialect.mysql.visitor.MySqlEvalVisitorImpl;
17 
18 
19 
20 import hunt.collection;
21 
22 
23 import hunt.sql.ast.expr.SQLBinaryExpr;
24 import hunt.sql.ast.expr.SQLBetweenExpr;
25 import hunt.sql.ast.expr.SQLBinaryOpExpr;
26 import hunt.sql.ast.expr.SQLBooleanExpr;
27 import hunt.sql.ast.expr.SQLCaseExpr;
28 import hunt.sql.ast.expr.SQLCharExpr;
29 import hunt.sql.ast.expr.SQLHexExpr;
30 import hunt.sql.ast.expr.SQLIdentifierExpr;
31 import hunt.sql.ast.expr.SQLInListExpr;
32 import hunt.sql.ast.expr.SQLIntegerExpr;
33 import hunt.sql.ast.expr.SQLMethodInvokeExpr;
34 import hunt.sql.ast.expr.SQLNullExpr;
35 import hunt.sql.ast.expr.SQLNumberExpr;
36 import hunt.sql.ast.expr.SQLQueryExpr;
37 import hunt.sql.ast.expr.SQLUnaryExpr;
38 import hunt.sql.ast.expr.SQLVariantRefExpr;
39 import hunt.sql.visitor.SQLEvalVisitor;
40 import hunt.sql.visitor.SQLEvalVisitorUtils;
41 import hunt.sql.visitor.functions.Function;
42 import hunt.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter;
43 //import hunt.lang;
44 
45 public class MySqlEvalVisitorImpl : MySqlASTVisitorAdapter , SQLEvalVisitor {
46     alias visit = MySqlASTVisitorAdapter.visit;
47     private Map!(string, Function) functions;
48     private List!(Object)          parameters;
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         this.parameters = parameters;
60         functions        = new HashMap!(string, Function)();
61         parameters       = new ArrayList!(Object)();
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(SQLUnaryExpr x) {
93         return SQLEvalVisitorUtils.visit(this, x);
94     }
95 
96     override public bool visit(SQLIntegerExpr x) {
97         return SQLEvalVisitorUtils.visit(this, x);
98     }
99 
100     override public bool visit(SQLNumberExpr x) {
101         return SQLEvalVisitorUtils.visit(this, x);
102     }
103     
104     override public bool visit(SQLHexExpr x) {
105         return SQLEvalVisitorUtils.visit(this, x);
106     }
107     
108     override
109     public bool visit(SQLBinaryExpr x) {
110         return SQLEvalVisitorUtils.visit(this, x);
111     }
112 
113     override
114     public bool visit(SQLCaseExpr x) {
115         return SQLEvalVisitorUtils.visit(this, x);
116     }
117 
118     override
119     public bool visit(SQLBetweenExpr x) {
120         return SQLEvalVisitorUtils.visit(this, x);
121     }
122 
123     override
124     public bool visit(SQLInListExpr x) {
125         return SQLEvalVisitorUtils.visit(this, x);
126     }
127 
128     override
129     public bool visit(SQLNullExpr x) {
130         return SQLEvalVisitorUtils.visit(this, x);
131     }
132 
133     override
134     public bool visit(SQLMethodInvokeExpr x) {
135         return SQLEvalVisitorUtils.visit(this, x);
136     }
137 
138     override
139     public bool visit(SQLQueryExpr x) {
140         return SQLEvalVisitorUtils.visit(this, x);
141     }
142 
143     public bool isMarkVariantIndex() {
144         return markVariantIndex;
145     }
146 
147     public void setMarkVariantIndex(bool markVariantIndex) {
148         this.markVariantIndex = markVariantIndex;
149     }
150 
151     override
152     public Function getFunction(string funcName) {
153         return functions.get(funcName);
154     }
155 
156     override
157     public void registerFunction(string funcName, Function function_p) {
158         functions.put(funcName, function_p);
159     }
160 
161     override public bool visit(SQLIdentifierExpr x) {
162         return SQLEvalVisitorUtils.visit(this, x);
163     }
164 
165     override
166     public void unregisterFunction(string funcName) {
167         functions.remove(funcName);
168     }
169     
170     override
171     public bool visit(SQLBooleanExpr x) {
172         x.getAttributes().put(EVAL_VALUE, (x.getBooleanValue()));
173         return false;
174     }
175 }