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.ExportParameterVisitorUtils;
17 
18 
19 import hunt.collection;
20 
21 import hunt.sql.ast.SQLExpr;
22 import hunt.sql.ast.SQLObject;
23 import hunt.sql.ast.expr;
24 import hunt.sql.visitor.ExportParameterizedOutputVisitor;
25 import hunt.sql.dialect.mysql.visitor.MySqlExportParameterVisitor;
26 // import hunt.sql.dialect.oracle.visitor.OracleExportParameterVisitor;
27 import hunt.sql.dialect.postgresql.visitor.PGExportParameterVisitor;
28 // import hunt.sql.dialect.sqlserver.visitor.MSSQLServerExportParameterVisitor;
29 // import hunt.sql.util.DBType;
30 import hunt.sql.visitor.ExportParameterVisitor;
31 import hunt.sql.util.DBType;
32 import hunt.String;
33 import hunt.util.Appendable;
34 import hunt.util.Common;
35 
36 public  class ExportParameterVisitorUtils {
37     
38     //private for util class not need new instance
39     private this() {
40         // super();
41     }
42 
43     public static ExportParameterVisitor createExportParameterVisitor(  Appendable out_p , string dbType) {
44         
45         if (DBType.MYSQL.name == (dbType)) {
46             return new MySqlExportParameterVisitor(out_p);
47         }
48         // if (DBType.ORACLE.name == (dbType) || DBType.ALI_ORACLE.name == (dbType)) {
49         //     return new OracleExportParameterVisitor(out_p);
50         // }
51         // if (DBType.DB2.name == (dbType)) {
52         //     return new DB2ExportParameterVisitor(out_p);
53         // }
54         
55         if (DBType.MARIADB.name == (dbType)) {
56             return new MySqlExportParameterVisitor(out_p);
57         }
58         
59         // if (DBType.H2.name == (dbType)) {
60         //     return new MySqlExportParameterVisitor(out_p);
61         // }
62 
63         if (DBType.POSTGRESQL.name == (dbType)
64                 || DBType.ENTERPRISEDB.name == (dbType)) {
65             return new PGExportParameterVisitor(out_p);
66         }
67 
68         // if (DBType.SQL_SERVER.name == (dbType) || DBType.JTDS.name == (dbType)) {
69         //     return new MSSQLServerExportParameterVisitor(out_p);
70         // }
71        return new ExportParameterizedOutputVisitor(out_p);
72     }
73 
74     
75 
76     public static bool exportParamterAndAccept( List!(Object) parameters, List!(SQLExpr) list) {
77         for (int i = 0, size = list.size(); i < size; ++i) {
78             SQLExpr param = list.get(i);
79 
80             SQLExpr result = exportParameter(parameters, param);
81             if (result != param) {
82                 list.set(i, result);
83             }
84         }
85 
86         return false;
87     }
88 
89     public static SQLExpr exportParameter( List!(Object) parameters,  SQLExpr param) {
90         Object value = null;
91         bool replace = false;
92 
93         if (cast(SQLCharExpr)(param) !is null) {
94             value = (cast(SQLCharExpr) param).getText();
95             replace = true;
96         } else if (cast(SQLBooleanExpr)(param) !is null) {
97             value = (cast(SQLBooleanExpr) param).getBooleanValue();
98             replace = true;
99         } else if (cast(SQLNumericLiteralExpr)(param) !is null) {
100             value = cast(Object)(cast(SQLNumericLiteralExpr) param).getNumber();
101             replace = true;
102         } else if (cast(SQLHexExpr)(param) !is null) {
103             value = (cast(SQLHexExpr) param)/* .toBytes() */;
104             replace = true;
105         } else if (cast(SQLTimestampExpr)(param) !is null || cast(SQLDateExpr)(param) !is null) {
106             value = (cast(SQLTimestampExpr) param).getValue();
107             replace = true;
108         } else if (cast(SQLListExpr)(param) !is null) {
109             SQLListExpr list = (cast(SQLListExpr) param);
110 
111             List!(Object) listValues = new ArrayList!(Object)();
112             for (int i = 0; i < list.getItems().size(); i++) {
113                 SQLExpr listItem = list.getItems().get(i);
114 
115                 if (cast(SQLCharExpr)(listItem) !is null) {
116                     Object listValue = (cast(SQLCharExpr) listItem).getText();
117                     listValues.add(listValue);
118                 } else if (cast(SQLBooleanExpr)(listItem) !is null) {
119                     Object listValue = (cast(SQLBooleanExpr) listItem).getBooleanValue();
120                     listValues.add(listValue);
121                 } else if (cast(SQLNumericLiteralExpr)(listItem) !is null) {
122                     Object listValue = cast(Object)(cast(SQLNumericLiteralExpr) listItem).getNumber();
123                     listValues.add(listValue);
124                 } else if (cast(SQLHexExpr)(param) !is null) {
125                     Object listValue = (cast(SQLHexExpr) listItem)/* .toBytes() */;//@gxc
126                     listValues.add(listValue);
127                 }
128             }
129 
130             if (listValues.size() == list.getItems().size()) {
131                 value = cast(Object)listValues;
132                 replace = true;
133             }
134         }
135 
136         if (replace) {
137             SQLObject parent = param.getParent();
138             if (parent !is null) {
139                 List!(SQLObject) mergedList = null;
140                 if (cast(SQLBinaryOpExpr)(parent) !is null) {
141                     mergedList = (cast(SQLBinaryOpExpr) parent).getMergedList();
142                 }
143                 if (mergedList !is null) {
144                     List!(Object) mergedListParams = new ArrayList!(Object)(mergedList.size() + 1);
145                     for (int i = 0; i < mergedList.size(); ++i) {
146                         SQLObject item = mergedList.get(i);
147                         if (cast(SQLBinaryOpExpr)(item) !is null) {
148                             SQLBinaryOpExpr binaryOpItem = cast(SQLBinaryOpExpr) item;
149                             exportParameter(mergedListParams, binaryOpItem.getRight());
150                         }
151                     }
152                     if (mergedListParams.size() > 0) {
153                         mergedListParams.add(0, value);
154                         value = cast(Object)mergedListParams;
155                     }
156                 }
157             }
158 
159             parameters.add(value);
160 
161             return new SQLVariantRefExpr("?");
162         }
163 
164         return param;
165     }
166 
167     public static void exportParameter( List!(Object) parameters, SQLBinaryOpExpr x) {
168         if (cast(SQLLiteralExpr)x.getLeft() !is null
169                 && cast(SQLLiteralExpr)x.getRight() !is null
170                 && x.getOperator().isRelational()) {
171             return;
172         }
173 
174         {
175             SQLExpr leftResult = ExportParameterVisitorUtils.exportParameter(parameters, x.getLeft());
176             if (leftResult != x.getLeft()) {
177                 x.setLeft(leftResult);
178             }
179         }
180 
181         {
182             SQLExpr rightResult = exportParameter(parameters, x.getRight());
183             if (rightResult != x.getRight()) {
184                 x.setRight(rightResult);
185             }
186         }
187     }
188 
189     public static void exportParameter( List!(Object) parameters, SQLBetweenExpr x) {
190         {
191             SQLExpr result = exportParameter(parameters, x.getBeginExpr());
192             if (result != x.getBeginExpr()) {
193                 x.setBeginExpr(result);
194             }
195         }
196 
197         {
198             SQLExpr result = exportParameter(parameters, x.getEndExpr());
199             if (result != x.getBeginExpr()) {
200                 x.setEndExpr(result);
201             }
202         }
203 
204     }
205 }