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.parser.SQLParserUtils;
17 
18 import hunt.sql.ast.statement.SQLSelectQueryBlock;
19 // import hunt.sql.dialect.db2.ast.stmt.DB2SelectQueryBlock;
20 // import hunt.sql.dialect.db2.parser.DB2ExprParser;
21 // import hunt.sql.dialect.db2.parser.DB2Lexer;
22 // import hunt.sql.dialect.db2.parser.DB2StatementParser;
23 // import hunt.sql.dialect.h2.parser.H2StatementParser;
24 // import hunt.sql.dialect.hive.parser.HiveStatementParser;
25 import hunt.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
26 import hunt.sql.dialect.mysql.parser.MySqlExprParser;
27 import hunt.sql.dialect.mysql.parser.MySqlLexer;
28 import hunt.sql.dialect.mysql.parser.MySqlStatementParser;
29 // import hunt.sql.dialect.odps.parser.OdpsExprParser;
30 // import hunt.sql.dialect.odps.parser.OdpsLexer;
31 // import hunt.sql.dialect.odps.parser.OdpsStatementParser;
32 // import hunt.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock;
33 // import hunt.sql.dialect.oracle.parser.OracleExprParser;
34 // import hunt.sql.dialect.oracle.parser.OracleLexer;
35 // import hunt.sql.dialect.oracle.parser.OracleStatementParser;
36 // import hunt.sql.dialect.phoenix.parser.PhoenixExprParser;
37 // import hunt.sql.dialect.phoenix.parser.PhoenixLexer;
38 // import hunt.sql.dialect.phoenix.parser.PhoenixStatementParser;
39 import hunt.sql.dialect.postgresql.parser.PGExprParser;
40 import hunt.sql.dialect.postgresql.parser.PGLexer;
41 import hunt.sql.dialect.postgresql.parser.PGSQLStatementParser;
42 // import hunt.sql.dialect.sqlserver.parser.SQLServerExprParser;
43 // import hunt.sql.dialect.sqlserver.parser.SQLServerLexer;
44 // import hunt.sql.dialect.sqlserver.parser.SQLServerStatementParser;
45 import hunt.sql.util.DBType;
46 import hunt.sql.parser.SQLStatementParser;
47 import hunt.sql.parser.SQLExprParser;
48 import hunt.sql.parser.SQLParserFeature;
49 import hunt.sql.parser.Lexer;
50 
51 
52 public class SQLParserUtils {
53 
54     public static SQLStatementParser createSQLStatementParser(string sql, string dbType) {
55         SQLParserFeature[] features;
56         if (DBType.ODPS.opEquals(dbType) || DBType.MYSQL.opEquals(dbType)) {
57             // features = new SQLParserFeature[]; 
58             features ~= SQLParserFeature.KeepComments;
59         } else {
60             // features = new SQLParserFeature[];
61         }
62         return createSQLStatementParser(sql, dbType, features);
63     }
64 
65     public static SQLStatementParser createSQLStatementParser(string sql, string dbType, bool keepComments) {
66         SQLParserFeature[] features;
67         if (keepComments) {
68             // features = new SQLParserFeature[];
69             features ~= SQLParserFeature.KeepComments;
70         } else {
71             // features = new SQLParserFeature[] ;
72         }
73 
74         return createSQLStatementParser(sql, dbType, features);
75     }
76 
77     public static SQLStatementParser createSQLStatementParser(string sql, string dbType, SQLParserFeature[] features...) {
78         // if (DBType.ORACLE.opEquals(dbType) || DBType.ALI_ORACLE.opEquals(dbType)) {
79         //     return new OracleStatementParser(sql);
80         // }
81 
82         if (DBType.MYSQL.opEquals(dbType) /* || DBType.ALIYUN_DRDS.opEquals(dbType) */) {
83             return new MySqlStatementParser(sql, features);
84         }
85 
86         if (DBType.MARIADB.opEquals(dbType)) {
87             return new MySqlStatementParser(sql, features);
88         }
89 
90         if (DBType.POSTGRESQL.opEquals(dbType)
91                 /* || DBType.ENTERPRISEDB.opEquals(dbType) */) {
92             return new PGSQLStatementParser(sql);
93         }
94 
95         // if (DBType.SQL_SERVER.opEquals(dbType) || DBType.JTDS.opEquals(dbType)) {
96         //     return new SQLServerStatementParser(sql);
97         // }
98 
99         // if (DBType.H2.opEquals(dbType)) {
100         //     return new H2StatementParser(sql);
101         // }
102         
103         // if (DBType.DB2.opEquals(dbType)) {
104         //     return new DB2StatementParser(sql);
105         // }
106         
107         // if (DBType.ODPS.opEquals(dbType)) {
108         //     return new OdpsStatementParser(sql);
109         // }
110 
111         // if (DBType.PHOENIX.opEquals(dbType)) {
112         //     return new PhoenixStatementParser(sql);
113         // }
114 
115         // if (DBType.HIVE.opEquals(dbType)) {
116         //     return new HiveStatementParser(sql);
117         // }
118 
119         if (DBType.ELASTIC_SEARCH.opEquals(dbType)) {
120             return new MySqlStatementParser(sql);
121         }
122 
123         return new SQLStatementParser(sql, dbType);
124     }
125 
126     public static SQLExprParser createExprParser(string sql, string dbType) {
127         // if (DBType.ORACLE.opEquals(dbType) || DBType.ALI_ORACLE.opEquals(dbType)) {
128         //     return new OracleExprParser(sql);
129         // }
130 
131         if (DBType.MYSQL.opEquals(dbType) || //
132             DBType.MARIADB.opEquals(dbType) /* || //
133             DBType.H2.opEquals(dbType) */) {
134             return new MySqlExprParser(sql);
135         }
136 
137         if (DBType.POSTGRESQL.opEquals(dbType)
138                 /* || DBType.ENTERPRISEDB.opEquals(dbType) */) {
139             return new PGExprParser(sql);
140         }
141 
142         // if (DBType.SQL_SERVER.opEquals(dbType) || DBType.JTDS.opEquals(dbType)) {
143         //     return new SQLServerExprParser(sql);
144         // }
145         
146         // if (DBType.DB2.opEquals(dbType)) {
147         //     return new DB2ExprParser(sql);
148         // }
149         
150         // if (DBType.ODPS.opEquals(dbType)) {
151         //     return new OdpsExprParser(sql);
152         // }
153 
154         // if (DBType.PHOENIX.opEquals(dbType)) {
155         //     return new PhoenixExprParser(sql);
156         // }
157 
158         return new SQLExprParser(sql);
159     }
160 
161     public static Lexer createLexer(string sql, string dbType) {
162         // if (DBType.ORACLE.opEquals(dbType) || DBType.ALI_ORACLE.opEquals(dbType)) {
163         //     return new OracleLexer(sql);
164         // }
165 
166         if (DBType.MYSQL.opEquals(dbType) || //
167                 DBType.MARIADB.opEquals(dbType) /* || //
168                 DBType.H2.opEquals(dbType) */) {
169             return new MySqlLexer(sql);
170         }
171 
172         if (DBType.POSTGRESQL.opEquals(dbType)
173                 || DBType.ENTERPRISEDB.opEquals(dbType)) {
174             return new PGLexer(sql);
175         }
176 
177         // if (DBType.SQL_SERVER.opEquals(dbType) || DBType.JTDS.opEquals(dbType)) {
178         //     return new SQLServerLexer(sql);
179         // }
180 
181         // if (DBType.DB2.opEquals(dbType)) {
182         //     return new DB2Lexer(sql);
183         // }
184 
185         // if (DBType.ODPS.opEquals(dbType)) {
186         //     return new OdpsLexer(sql);
187         // }
188 
189         // if (DBType.PHOENIX.opEquals(dbType)) {
190         //     return new PhoenixLexer(sql);
191         // }
192 
193         return new Lexer(sql);
194     }
195 
196     public static SQLSelectQueryBlock createSelectQueryBlock(string dbType) {
197         if (DBType.MYSQL.opEquals(dbType)) {
198             return new MySqlSelectQueryBlock();
199         }
200 
201         // if (DBType.ORACLE.opEquals(dbType)) {
202         //     return new OracleSelectQueryBlock();
203         // }
204 
205         // if (DBType.DB2.opEquals(dbType)) {
206         //     return new DB2SelectQueryBlock();
207         // }
208 
209         // if (DBType.POSTGRESQL.opEquals(dbType)) {
210         //     return new DB2SelectQueryBlock();
211         // }
212 
213         // if (DBType.ODPS.opEquals(dbType)) {
214         //     return new DB2SelectQueryBlock();
215         // }
216 
217         // if (DBType.SQL_SERVER.opEquals(dbType)) {
218         //     return new DB2SelectQueryBlock();
219         // }
220 
221         return new SQLSelectQueryBlock();
222      }
223 }