forked from JSQLParser/JSqlParser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParserKeywordsUtils.java
More file actions
437 lines (387 loc) · 17.7 KB
/
ParserKeywordsUtils.java
File metadata and controls
437 lines (387 loc) · 17.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2021 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.parser;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ParserKeywordsUtils {
public final static CharsetEncoder CHARSET_ENCODER = StandardCharsets.US_ASCII.newEncoder();
public final static int RESTRICTED_FUNCTION = 1;
public final static int RESTRICTED_SCHEMA = 2;
public final static int RESTRICTED_TABLE = 4;
public final static int RESTRICTED_COLUMN = 8;
public final static int RESTRICTED_EXPRESSION = 16;
public final static int RESTRICTED_ALIAS = 32;
public final static int RESTRICTED_SQL2016 = 64;
public final static int RESTRICTED_JSQLPARSER = 128
| RESTRICTED_FUNCTION
| RESTRICTED_SCHEMA
| RESTRICTED_TABLE
| RESTRICTED_COLUMN
| RESTRICTED_EXPRESSION
| RESTRICTED_ALIAS
| RESTRICTED_SQL2016;
// Classification follows http://www.h2database.com/html/advanced.html#keywords
public final static Object[][] ALL_RESERVED_KEYWORDS = {
{"ABSENT", RESTRICTED_JSQLPARSER},
{"ALL", RESTRICTED_SQL2016},
{"AND", RESTRICTED_SQL2016},
{"ANY", RESTRICTED_JSQLPARSER},
{"AS", RESTRICTED_SQL2016},
{"BETWEEN", RESTRICTED_SQL2016},
{"BOTH", RESTRICTED_SQL2016},
{"CASEWHEN", RESTRICTED_ALIAS},
{"CHECK", RESTRICTED_SQL2016},
{"CONNECT", RESTRICTED_ALIAS},
{"CONNECT_BY_ROOT", RESTRICTED_JSQLPARSER},
{"CSV", RESTRICTED_JSQLPARSER},
{"PRIOR", RESTRICTED_JSQLPARSER},
{"CONSTRAINT", RESTRICTED_SQL2016},
{"CREATE", RESTRICTED_ALIAS},
{"CROSS", RESTRICTED_SQL2016},
{"CURRENT", RESTRICTED_JSQLPARSER},
{"DEFAULT", RESTRICTED_ALIAS},
{"DISTINCT", RESTRICTED_SQL2016},
{"DISTINCTROW", RESTRICTED_SQL2016},
{"DOUBLE", RESTRICTED_ALIAS},
{"ELSE", RESTRICTED_JSQLPARSER},
{"ERRORS", RESTRICTED_JSQLPARSER},
{"EXCEPT", RESTRICTED_SQL2016},
{"EXCLUDES", RESTRICTED_JSQLPARSER},
{"EXISTS", RESTRICTED_SQL2016},
{"EXTEND", RESTRICTED_JSQLPARSER},
{"FALSE", RESTRICTED_SQL2016},
{"FBV", RESTRICTED_JSQLPARSER},
{"FETCH", RESTRICTED_SQL2016},
{"FILE", RESTRICTED_JSQLPARSER},
{"FINAL", RESTRICTED_JSQLPARSER},
{"FOR", RESTRICTED_SQL2016},
{"FORCE", RESTRICTED_SQL2016},
{"FOREIGN", RESTRICTED_SQL2016},
{"FROM", RESTRICTED_SQL2016},
{"FULL", RESTRICTED_SQL2016},
{"GLOBAL", RESTRICTED_ALIAS},
{"GROUP", RESTRICTED_SQL2016},
{"GROUPING", RESTRICTED_ALIAS},
{"QUALIFY", RESTRICTED_ALIAS},
{"HAVING", RESTRICTED_SQL2016},
{"IF", RESTRICTED_SQL2016},
{"IIF", RESTRICTED_ALIAS},
{"IGNORE", RESTRICTED_ALIAS},
{"ILIKE", RESTRICTED_SQL2016},
{"IMPORT", RESTRICTED_JSQLPARSER},
{"IN", RESTRICTED_SQL2016},
{"INCLUDES", RESTRICTED_JSQLPARSER},
{"INNER", RESTRICTED_SQL2016},
{"INTERSECT", RESTRICTED_SQL2016},
{"INTERVAL", RESTRICTED_SQL2016},
{"INTO", RESTRICTED_JSQLPARSER},
{"IS", RESTRICTED_SQL2016},
{"JOIN", RESTRICTED_JSQLPARSER},
{"LATERAL", RESTRICTED_SQL2016},
{"LEFT", RESTRICTED_SQL2016},
{"LIKE", RESTRICTED_SQL2016},
{"LIMIT", RESTRICTED_SQL2016},
{"MINUS", RESTRICTED_SQL2016},
{"NATURAL", RESTRICTED_SQL2016},
{"NOCYCLE", RESTRICTED_JSQLPARSER},
{"NOT", RESTRICTED_SQL2016},
{"NULL", RESTRICTED_SQL2016},
{"OFFSET", RESTRICTED_SQL2016},
{"ON", RESTRICTED_SQL2016},
{"ONLY", RESTRICTED_JSQLPARSER},
{"OPTIMIZE", RESTRICTED_ALIAS},
{"OR", RESTRICTED_SQL2016},
{"ORDER", RESTRICTED_SQL2016},
{"OUTER", RESTRICTED_JSQLPARSER},
{"OUTPUT", RESTRICTED_JSQLPARSER},
{"OPTIMIZE ", RESTRICTED_JSQLPARSER},
{"OVERWRITE ", RESTRICTED_JSQLPARSER},
{"PIVOT", RESTRICTED_JSQLPARSER},
{"PREFERRING", RESTRICTED_JSQLPARSER},
{"PREWHERE", RESTRICTED_JSQLPARSER},
{"PRIOR", RESTRICTED_ALIAS},
{"PROCEDURE", RESTRICTED_ALIAS},
{"PUBLIC", RESTRICTED_ALIAS},
{"RETURNS", RESTRICTED_JSQLPARSER},
{"RETURNING", RESTRICTED_JSQLPARSER},
{"RIGHT", RESTRICTED_SQL2016},
{"SAMPLE", RESTRICTED_ALIAS},
{"SCRIPT", RESTRICTED_JSQLPARSER},
{"SEL", RESTRICTED_ALIAS},
{"SELECT", RESTRICTED_ALIAS},
{"SEMI", RESTRICTED_JSQLPARSER},
{"SET", RESTRICTED_JSQLPARSER},
{"SETTINGS", RESTRICTED_JSQLPARSER},
{"SOME", RESTRICTED_JSQLPARSER},
{"START", RESTRICTED_JSQLPARSER},
{"STATEMENT", RESTRICTED_JSQLPARSER},
{"TABLES", RESTRICTED_ALIAS},
{"TOP", RESTRICTED_SQL2016},
{"TRAILING", RESTRICTED_SQL2016},
{"TRUE", RESTRICTED_SQL2016},
{"UNBOUNDED", RESTRICTED_JSQLPARSER},
{"UNION", RESTRICTED_SQL2016},
{"UNIQUE", RESTRICTED_SQL2016},
{"UNKNOWN", RESTRICTED_SQL2016},
{"UNPIVOT", RESTRICTED_JSQLPARSER},
{"USE", RESTRICTED_JSQLPARSER},
{"USING", RESTRICTED_SQL2016},
{"SQL_CACHE", RESTRICTED_JSQLPARSER},
{"SQL_CALC_FOUND_ROWS", RESTRICTED_JSQLPARSER},
{"SQL_NO_CACHE", RESTRICTED_JSQLPARSER},
{"STRAIGHT_JOIN", RESTRICTED_JSQLPARSER},
{"TABLESAMPLE", RESTRICTED_ALIAS},
{"VALUE", RESTRICTED_JSQLPARSER},
{"VALUES", RESTRICTED_SQL2016},
{"VARYING", RESTRICTED_JSQLPARSER},
{"VERIFY", RESTRICTED_JSQLPARSER},
{"WHEN", RESTRICTED_SQL2016},
{"WHERE", RESTRICTED_SQL2016},
{"WINDOW", RESTRICTED_SQL2016},
{"WITH", RESTRICTED_SQL2016},
{"XOR", RESTRICTED_JSQLPARSER},
{"XMLSERIALIZE", RESTRICTED_JSQLPARSER},
// add keywords from the composite token definitions:
// tk=<K_DATE_LITERAL> | tk=<K_DATETIMELITERAL> | tk=<K_STRING_FUNCTION_NAME>
// we will use the composite tokens instead, which are always hit first before the
// simple keywords
// @todo: figure out a way to remove these composite tokens, as they do more harm than
// good
{"SEL", RESTRICTED_JSQLPARSER},
{"SELECT", RESTRICTED_JSQLPARSER},
{"DATE", RESTRICTED_JSQLPARSER},
{"TIME", RESTRICTED_JSQLPARSER},
{"TIMESTAMP", RESTRICTED_JSQLPARSER},
{"YEAR", RESTRICTED_JSQLPARSER},
{"MONTH", RESTRICTED_JSQLPARSER},
{"DAY", RESTRICTED_JSQLPARSER},
{"HOUR", RESTRICTED_JSQLPARSER},
{"MINUTE", RESTRICTED_JSQLPARSER},
{"SECOND", RESTRICTED_JSQLPARSER},
{"SUBSTR", RESTRICTED_JSQLPARSER},
{"SUBSTRING", RESTRICTED_JSQLPARSER},
{"TRIM", RESTRICTED_JSQLPARSER},
{"POSITION", RESTRICTED_JSQLPARSER},
{"OVERLAY", RESTRICTED_JSQLPARSER},
{"NEXTVAL", RESTRICTED_COLUMN},
// @todo: Object Names should not start with Hex-Prefix, we shall not find that Token
{"0x", RESTRICTED_JSQLPARSER}
};
@SuppressWarnings({"PMD.ExcessiveMethodLength"})
public static List<String> getReservedKeywords(int restriction) {
ArrayList<String> keywords = new ArrayList<>();
for (Object[] data : ALL_RESERVED_KEYWORDS) {
int value = (int) data[1];
// test if bit is not set
if ((value & restriction) == restriction || (restriction & value) == value) {
keywords.add((String) data[0]);
}
}
return keywords;
}
/**
* @param args with: Grammar File, Keyword Documentation File
* @throws Exception
*/
public static void main(String[] args) throws Exception {
if (args.length < 2) {
throw new IllegalArgumentException("No filename provided aS context ARGS[0]");
}
File grammarFile = new File(args[0]);
if (grammarFile.exists() && grammarFile.canRead() && grammarFile.canWrite()) {
buildGrammarForRelObjectName(grammarFile);
buildGrammarForRelObjectNameWithoutValue(grammarFile);
} else {
throw new FileNotFoundException("Can't read file " + args[0]);
}
File keywordDocumentationFile = new File(args[1]);
keywordDocumentationFile.createNewFile();
if (keywordDocumentationFile.canWrite()) {
writeKeywordsDocumentationFile(keywordDocumentationFile);
} else {
throw new FileNotFoundException("Can't read file " + args[1]);
}
}
public static TreeSet<String> getAllKeywordsUsingRegex(File file) throws IOException {
Pattern tokenBlockPattern = Pattern.compile(
"TOKEN\\s*:\\s*/\\*.*\\*/*(?:\\r?\\n|\\r)\\{(?:[^}{]+|\\{(?:[^}{]+|\\{[^}{]*})*})*}",
Pattern.MULTILINE);
Pattern tokenStringValuePattern = Pattern.compile("\"(\\w{2,})\"", Pattern.MULTILINE);
TreeSet<String> allKeywords = new TreeSet<>();
Path path = file.toPath();
Charset charset = Charset.defaultCharset();
String content = new String(Files.readAllBytes(path), charset);
Matcher tokenBlockmatcher = tokenBlockPattern.matcher(content);
while (tokenBlockmatcher.find()) {
String tokenBlock = tokenBlockmatcher.group(0);
// remove single and multiline comments
tokenBlock = tokenBlock.replaceAll("(?sm)((\\/\\*.*?\\*\\/)|(\\/\\/.*?$))", "");
for (String tokenDefinition : getTokenDefinitions(tokenBlock)) {
// check if token definition is private
if (tokenDefinition.matches("(?sm)^<\\s*[^#].*")) {
Matcher tokenStringValueMatcher =
tokenStringValuePattern.matcher(tokenDefinition);
while (tokenStringValueMatcher.find()) {
String tokenValue = tokenStringValueMatcher.group(1);
// test if pure US-ASCII
if (CHARSET_ENCODER.canEncode(tokenValue) && tokenValue.matches("\\w+")) {
allKeywords.add(tokenValue);
}
}
}
}
}
return allKeywords;
}
@SuppressWarnings({"PMD.EmptyWhileStmt"})
private static List<String> getTokenDefinitions(String tokenBlock) {
List<String> tokenDefinitions = new ArrayList<>();
int level = 0;
char openChar = '<';
char closeChar = '>';
char[] tokenBlockChars = tokenBlock.toCharArray();
int tokenDefinitionStart = -1;
for (int i = 0; i < tokenBlockChars.length; ++i) {
if (isQuotationMark(i, tokenBlockChars)) {
// skip everything inside quotation marks
while (!isQuotationMark(++i, tokenBlockChars)) {
// skip until quotation ends
}
}
char character = tokenBlockChars[i];
if (character == openChar) {
if (level == 0) {
tokenDefinitionStart = i;
}
++level;
} else if (character == closeChar) {
--level;
if (level == 0 && tokenDefinitionStart >= 0) {
tokenDefinitions.add(tokenBlock.substring(tokenDefinitionStart, i + 1));
tokenDefinitionStart = -1;
}
}
}
return tokenDefinitions;
}
private static boolean isQuotationMark(int index, char[] str) {
if (str[index] == '\"') {
// check if quotation is escaped
if (index > 0 && str[index - 1] == '\\') {
return index > 1 && str[index - 2] == '\\';
}
return true;
}
return false;
}
public static void buildGrammarForRelObjectNameWithoutValue(File file) throws Exception {
Pattern methodBlockPattern = Pattern.compile(
"String\\W*RelObjectNameWithoutValue\\W*\\(\\W*\\)\\W*:\\s*\\{(?:[^}{]+|\\{(?:[^}{]+|\\{[^}{]*})*})*}\\s*\\{(?:[^}{]+|\\{(?:[^}{]+|\\{[^}{]*})*})*}",
Pattern.MULTILINE);
TreeSet<String> allKeywords = getAllKeywords(file);
for (String reserved : getReservedKeywords(RESTRICTED_JSQLPARSER)) {
allKeywords.remove(reserved);
}
StringBuilder builder = new StringBuilder();
builder.append("String RelObjectNameWithoutValue() :\n"
+ "{ Token tk = null; }\n"
+ "{\n"
// @todo: find a way to avoid those hardcoded compound tokens
+ " ( tk=<DATA_TYPE> | tk=<S_IDENTIFIER> | tk=<S_QUOTED_IDENTIFIER> | tk=<K_DATE_LITERAL> | tk=<K_DATETIMELITERAL> | tk=<K_STRING_FUNCTION_NAME> | tk=<K_ISOLATION> | tk=<K_TIME_KEY_EXPR> | tk=<K_TEXT_LITERAL> \n"
+ " ");
for (String keyword : allKeywords) {
builder.append(" | tk=\"").append(keyword).append("\"");
}
builder.append(" )\n" + " { return tk.image; }\n" + "}");
replaceInFile(file, methodBlockPattern, builder.toString());
}
public static void buildGrammarForRelObjectName(File file) throws Exception {
// Pattern pattern =
// Pattern.compile("String\\W*RelObjectName\\W*\\(\\W*\\)\\W*:\\s*\\{(?:[^}{]+|\\{(?:[^}{]+|\\{[^}{]*})*})*}\\s*\\{(?:[^}{]+|\\{(?:[^}{]+|\\{[^}{]*})*})*}",
// Pattern.MULTILINE);
TreeSet<String> allKeywords = new TreeSet<>();
for (String reserved : getReservedKeywords(RESTRICTED_ALIAS)) {
allKeywords.add(reserved);
}
for (String reserved : getReservedKeywords(RESTRICTED_JSQLPARSER & ~RESTRICTED_ALIAS)) {
allKeywords.remove(reserved);
}
StringBuilder builder = new StringBuilder();
builder.append("String RelObjectName() :\n"
+ "{ Token tk = null; String result = null; }\n"
+ "{\n"
+ " (result = RelObjectNameWithoutValue()\n"
+ " ");
for (String keyword : allKeywords) {
builder.append(" | tk=\"").append(keyword).append("\"");
}
builder.append(" )\n" + " { return tk!=null ? tk.image : result; }\n" + "}");
// @todo: Needs fine-tuning, we are not replacing this part yet
// replaceInFile(file, pattern, builder.toString());
}
public static TreeSet<String> getAllKeywords(File file) throws Exception {
return getAllKeywordsUsingRegex(file);
}
private static void replaceInFile(File file, Pattern pattern, String replacement)
throws IOException {
Path path = file.toPath();
Charset charset = Charset.defaultCharset();
String content = new String(Files.readAllBytes(path), charset);
content = pattern.matcher(content).replaceAll(replacement);
Files.write(file.toPath(), content.getBytes(charset));
}
public static String rightPadding(String input, char ch, int length) {
return String.format("%" + (-length) + "s", input).replace(' ', ch);
}
public static void writeKeywordsDocumentationFile(File file) throws IOException {
StringBuilder builder = new StringBuilder();
builder.append("***********************\n");
builder.append("Restricted Keywords\n");
builder.append("***********************\n");
builder.append("\n");
builder.append(
"The following Keywords are **restricted** in JSQLParser-|JSQLPARSER_VERSION| and must not be used for **Naming Objects**: \n");
builder.append("\n");
builder.append("+----------------------+-------------+-----------+\n");
builder.append("| **Keyword** | JSQL Parser | SQL:2016 |\n");
builder.append("+----------------------+-------------+-----------+\n");
for (Object[] keywordDefinition : ALL_RESERVED_KEYWORDS) {
builder.append("| ").append(rightPadding(keywordDefinition[0].toString(), ' ', 20))
.append(" | ");
int value = (int) keywordDefinition[1];
int restriction = RESTRICTED_JSQLPARSER;
String s = (value & restriction) == restriction || (restriction & value) == value
? "Yes"
: "";
builder.append(rightPadding(s, ' ', 11)).append(" | ");
restriction = RESTRICTED_SQL2016;
s = (value & restriction) == restriction || (restriction & value) == value
? "Yes"
: "";
builder.append(rightPadding(s, ' ', 9)).append(" | ");
builder.append("\n");
builder.append("+----------------------+-------------+-----------+\n");
}
try (FileWriter fileWriter = new FileWriter(file)) {
fileWriter.append(builder);
fileWriter.flush();
}
}
}