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