-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathCCJSqlParserUtil.java
More file actions
599 lines (542 loc) · 21.5 KB
/
Copy pathCCJSqlParserUtil.java
File metadata and controls
599 lines (542 loc) · 21.5 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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2019 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.parser;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Stack;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.parser.feature.Feature;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.Statements;
import net.sf.jsqlparser.statement.create.table.ColDataType;
/**
* Toolfunctions to start and use JSqlParser.
*
* @author toben
*/
@SuppressWarnings("PMD.CyclomaticComplexity")
public final class CCJSqlParserUtil {
public final static Logger LOGGER = Logger.getLogger(CCJSqlParserUtil.class.getName());
static {
LOGGER.setLevel(Level.OFF);
}
private CCJSqlParserUtil() {}
public static Statement parse(Reader statementReader) throws JSQLParserException {
ExecutorService executorService = Executors.newSingleThreadExecutor();
Statement statement;
CCJSqlParser parser = new CCJSqlParser(new StreamProvider(statementReader));
try {
statement = parseStatement(parser, executorService);
} finally {
executorService.shutdown();
}
return statement;
}
public static Statement parse(String sql) throws JSQLParserException {
return parse(sql, null);
}
/**
* Parses an sql statement while allowing via consumer to configure the used parser before.
* <p>
* For instance to activate SQLServer bracket quotation on could use:
* <p>
* {@code
* CCJSqlParserUtil.parse("select * from [mytable]", parser -> parser.withSquareBracketQuotation(true));
* }
*
* @param sql
* @param consumer
* @return
* @throws JSQLParserException
*/
public static Statement parse(String sql, Consumer<CCJSqlParser> consumer)
throws JSQLParserException {
if (sql == null || sql.isEmpty()) {
return null;
}
ExecutorService executorService = Executors.newSingleThreadExecutor();
Statement statement;
try {
statement = parse(sql, executorService, consumer);
} catch (JSQLParserException ex) {
throw new JSQLParserException(sql, ex);
} finally {
executorService.shutdown();
}
return statement;
}
public static Statement parse(String sql, ExecutorService executorService,
Consumer<CCJSqlParser> consumer)
throws JSQLParserException {
if (sql == null || sql.isEmpty()) {
return null;
}
Statement statement;
// first, try to parse fast and simple
CCJSqlParser parser = newParser(sql);
if (consumer != null) {
consumer.accept(parser);
}
boolean allowComplex = parser.getAsBoolean(Feature.allowComplexParsing);
int allowedNestingDepth = parser.getAsInt(Feature.allowedNestingDepth);
LOGGER.info("Allowed Complex Parsing: " + allowComplex);
try {
LOGGER.info("Trying SIMPLE parsing " + (allowComplex ? "first" : "only"));
statement = parseStatement(parser.withAllowComplexParsing(false), executorService);
} catch (JSQLParserException ex) {
LOGGER.info("Nesting Depth" + getNestingDepth(sql));
if (allowComplex
&& (allowedNestingDepth < 0 || getNestingDepth(sql) <= allowedNestingDepth)) {
LOGGER.info("Trying COMPLEX parsing when SIMPLE parsing failed");
// beware: the parser must not be reused, but needs to be re-initiated
parser = newParser(sql);
if (consumer != null) {
consumer.accept(parser);
}
statement = parseStatement(parser.withAllowComplexParsing(true), executorService);
} else {
throw ex;
}
}
return statement;
}
public static CCJSqlParser newParser(String sql) {
if (sql == null || sql.isEmpty()) {
return null;
}
return new CCJSqlParser(new StringProvider(sql));
}
public static CCJSqlParser newParser(InputStream is) throws IOException {
return new CCJSqlParser(new StreamProvider(is));
}
public static CCJSqlParser newParser(InputStream is, String encoding) throws IOException {
return new CCJSqlParser(new StreamProvider(is, encoding));
}
public static Node parseAST(String sql) throws JSQLParserException {
if (sql == null || sql.isEmpty()) {
return null;
}
CCJSqlParser parser = newParser(sql);
try {
parser.Statement();
return parser.jjtree.rootNode();
} catch (Exception ex) {
throw new JSQLParserException(ex);
}
}
public static Statement parse(InputStream is) throws JSQLParserException {
try {
CCJSqlParser parser = newParser(is);
return parser.Statement();
} catch (Exception ex) {
throw new JSQLParserException(ex);
}
}
public static Statement parse(InputStream is, String encoding) throws JSQLParserException {
try {
CCJSqlParser parser = newParser(is, encoding);
return parser.Statement();
} catch (Exception ex) {
throw new JSQLParserException(ex);
}
}
public static Expression parseExpression(String expression) throws JSQLParserException {
if (expression == null || expression.isEmpty()) {
return null;
}
return parseExpression(expression, true);
}
public static Expression parseExpression(String expression, boolean allowPartialParse)
throws JSQLParserException {
if (expression == null || expression.isEmpty()) {
return null;
}
return parseExpression(expression, allowPartialParse, p -> {
});
}
/**
* Parses a column data type fragment. The complete input must represent the data type; trailing
* tokens are rejected.
*
* @param columnDataType the column data type fragment to parse
* @return the parsed column data type, or {@code null} for a null or empty input
* @throws JSQLParserException when the input cannot be parsed completely
* @see #parseColDataType(String, Consumer)
*/
public static ColDataType parseColDataType(String columnDataType) throws JSQLParserException {
return parseColDataType(columnDataType, null);
}
/**
* Parses a column data type fragment while allowing the parser to be configured. The complete
* input must represent the data type; trailing tokens are rejected.
*
* @param columnDataType the column data type fragment to parse
* @param consumer parser configuration callback, or {@code null}
* @return the parsed column data type, or {@code null} for a null or empty input
* @throws JSQLParserException when the input cannot be parsed completely
*/
public static ColDataType parseColDataType(String columnDataType,
Consumer<CCJSqlParser> consumer) throws JSQLParserException {
if (columnDataType == null || columnDataType.isEmpty()) {
return null;
}
try {
return parseColDataType(columnDataType, false, consumer);
} catch (JSQLParserException ex) {
return parseColDataType(columnDataType, true, consumer);
}
}
private static ColDataType parseColDataType(String columnDataType, boolean allowComplexParsing,
Consumer<CCJSqlParser> consumer) throws JSQLParserException {
CCJSqlParser parser = newParser(columnDataType)
.withAllowComplexParsing(allowComplexParsing);
if (consumer != null) {
consumer.accept(parser);
}
try {
ColDataType result = parser.ColDataType();
if (parser.getNextToken().kind != CCJSqlParserTokenManager.EOF) {
throw new JSQLParserException(
"could only parse partial column data type " + result);
}
return result;
} catch (ParseException ex) {
throw new JSQLParserException(columnDataType, ex);
}
}
@SuppressWarnings("PMD.CyclomaticComplexity")
public static Expression parseExpression(String expressionStr, boolean allowPartialParse,
Consumer<CCJSqlParser> consumer) throws JSQLParserException {
if (expressionStr == null || expressionStr.isEmpty()) {
return null;
}
Expression expression = null;
// first, try to parse fast and simple
try {
CCJSqlParser parser = newParser(expressionStr).withAllowComplexParsing(false);
if (consumer != null) {
consumer.accept(parser);
}
try {
expression = parser.Expression();
if (parser.getNextToken().kind != CCJSqlParserTokenManager.EOF) {
throw new JSQLParserException(
"could only parse partial expression " + expression.toString());
}
} catch (ParseException ex) {
throw new JSQLParserException(expressionStr, ex);
}
} catch (JSQLParserException ex1) {
// when fast simple parsing fails, try complex parsing but only if it has a chance to
// succeed
CCJSqlParser parser = newParser(expressionStr).withAllowComplexParsing(true);
if (consumer != null) {
consumer.accept(parser);
}
try {
expression = parser.Expression();
if (!allowPartialParse
&& parser.getNextToken().kind != CCJSqlParserTokenManager.EOF) {
throw new JSQLParserException(
"could only parse partial expression " + expression.toString());
}
} catch (JSQLParserException ex) {
throw ex;
} catch (ParseException ex) {
throw new JSQLParserException(ex);
}
}
return expression;
}
/**
* Parse an conditional expression. This is the expression after a where clause. Partial parsing
* is enabled.
*
* @param condExpr
* @return the expression parsed
* @see #parseCondExpression(String, boolean)
*/
public static Expression parseCondExpression(String condExpr) throws JSQLParserException {
if (condExpr == null || condExpr.isEmpty()) {
return null;
}
return parseCondExpression(condExpr, true);
}
/**
* Parse an conditional expression. This is the expression after a where clause.
*
* @param condExpr
* @param allowPartialParse false: needs the whole string to be processed.
* @return the expression parsed
* @see #parseCondExpression(String)
*/
public static Expression parseCondExpression(String condExpr, boolean allowPartialParse)
throws JSQLParserException {
if (condExpr == null || condExpr.isEmpty()) {
return null;
}
return parseCondExpression(condExpr, allowPartialParse, p -> {
});
}
@SuppressWarnings("PMD.CyclomaticComplexity")
public static Expression parseCondExpression(String conditionalExpressionStr,
boolean allowPartialParse, Consumer<CCJSqlParser> consumer) throws JSQLParserException {
if (conditionalExpressionStr == null || conditionalExpressionStr.isEmpty()) {
return null;
}
Expression expression = null;
// first, try to parse fast and simple
try {
CCJSqlParser parser =
newParser(conditionalExpressionStr).withAllowComplexParsing(false);
if (consumer != null) {
consumer.accept(parser);
}
try {
expression = parser.Expression();
if (parser.getNextToken().kind != CCJSqlParserTokenManager.EOF) {
throw new JSQLParserException(
"could only parse partial expression " + expression.toString());
}
} catch (ParseException ex) {
throw new JSQLParserException(ex);
}
} catch (JSQLParserException ex1) {
CCJSqlParser parser =
newParser(conditionalExpressionStr).withAllowComplexParsing(true);
if (consumer != null) {
consumer.accept(parser);
}
try {
expression = parser.Expression();
if (!allowPartialParse
&& parser.getNextToken().kind != CCJSqlParserTokenManager.EOF) {
throw new JSQLParserException(
"could only parse partial expression " + expression.toString());
}
} catch (JSQLParserException ex) {
throw ex;
} catch (ParseException ex) {
throw new JSQLParserException(ex);
}
}
return expression;
}
/**
* @param parser the Parser armed with a Statement text
* @param executorService the Executor Service for parsing within a Thread
* @return the parsed Statement
* @throws JSQLParserException when either the Statement can't be parsed or the configured
* timeout is reached
*/
public static Statement parseStatement(CCJSqlParser parser, ExecutorService executorService)
throws JSQLParserException {
Statement statement;
Future<Statement> future = executorService.submit(new Callable<Statement>() {
@Override
public Statement call() throws ParseException {
return parser.Statement();
}
});
try {
statement = future.get(parser.getAsLong(Feature.timeOut),
TimeUnit.MILLISECONDS);
} catch (TimeoutException ex) {
parser.interrupted = true;
future.cancel(true);
throw new JSQLParserException("Time out occurred.", ex);
} catch (Exception ex) {
throw new JSQLParserException(ex);
}
return statement;
}
/**
* Parse a statement list.
*
* @return the statements parsed
*/
public static Statements parseStatements(String sqls) throws JSQLParserException {
if (sqls == null || sqls.isEmpty()) {
return null;
}
return parseStatements(sqls, null);
}
public static Statements parseStatements(String sqls, Consumer<CCJSqlParser> consumer)
throws JSQLParserException {
if (sqls == null || sqls.isEmpty()) {
return null;
}
ExecutorService executorService = Executors.newSingleThreadExecutor();
final Statements statements = parseStatements(sqls, executorService, consumer);
executorService.shutdown();
return statements;
}
/**
* Parse a statement list.
*
* @return the statements parsed
*/
public static Statements parseStatements(String sqls, ExecutorService executorService,
Consumer<CCJSqlParser> consumer)
throws JSQLParserException {
if (sqls == null || sqls.isEmpty()) {
return null;
}
Statements statements = null;
CCJSqlParser parser = newParser(sqls);
if (consumer != null) {
consumer.accept(parser);
}
boolean allowComplex = parser.getAsBoolean(Feature.allowComplexParsing);
int allowedNestingDepth = parser.getAsInt(Feature.allowedNestingDepth);
// first, try to parse fast and simple
try {
statements = parseStatements(parser.withAllowComplexParsing(false), executorService);
} catch (JSQLParserException ex) {
// when fast simple parsing fails, try complex parsing but only if it has a chance to
// succeed
if (allowComplex
&& (allowedNestingDepth < 0 || getNestingDepth(sqls) <= allowedNestingDepth)) {
// beware: parser must not be re-used but needs to be re-initiated
parser = newParser(sqls);
if (consumer != null) {
consumer.accept(parser);
}
statements = parseStatements(parser.withAllowComplexParsing(true), executorService);
}
}
return statements;
}
/**
* @param parser the Parser armed with a Statement text
* @param executorService the Executor Service for parsing within a Thread
* @return the Statements (representing a List of single statements)
* @throws JSQLParserException when either the Statement can't be parsed or the configured
* timeout is reached
*/
public static Statements parseStatements(CCJSqlParser parser, ExecutorService executorService)
throws JSQLParserException {
Statements statements = null;
Future<Statements> future = executorService.submit(new Callable<Statements>() {
@Override
public Statements call() throws ParseException {
return parser.Statements();
}
});
try {
statements = future.get(parser.getAsLong(Feature.timeOut),
TimeUnit.MILLISECONDS);
} catch (TimeoutException ex) {
parser.interrupted = true;
future.cancel(true);
throw new JSQLParserException("Time out occurred.", ex);
} catch (Exception ex) {
throw new JSQLParserException(ex);
}
return statements;
}
public static void streamStatements(StatementListener listener, InputStream is, String encoding)
throws JSQLParserException {
try {
CCJSqlParser parser = newParser(is, encoding);
do {
Statement stmt = parser.SingleStatement();
listener.accept(stmt);
if (parser.getToken(1).kind == CCJSqlParserTokenManager.ST_SEMICOLON) {
parser.getNextToken();
}
} while (parser.getToken(1).kind != CCJSqlParserTokenManager.EOF);
} catch (Exception ex) {
throw new JSQLParserException(ex);
}
}
public static int getNestingDepth(String sql) {
int maxlevel = 0;
int level = 0;
char[] chars = sql.toCharArray();
for (char c : chars) {
switch (c) {
case '(':
level++;
break;
case ')':
if (maxlevel < level) {
maxlevel = level;
}
level--;
break;
default:
// Codazy/PMD insists in a Default statement
}
}
return maxlevel;
}
public static int getUnbalancedPosition(String text) {
Stack<Character> stack = new Stack<>();
boolean insideQuote = false;
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c == '"' || c == '\'') {
if (!insideQuote) {
stack.push(c); // Add quote to stack
} else if (stack.peek() == c) {
stack.pop(); // Matching quote found, remove from stack
}
insideQuote = !insideQuote; // Toggle insideQuote flag
} else if (!insideQuote && (c == '(' || c == '[' || c == '{')) {
stack.push(c); // Add opening bracket to stack
} else if (!insideQuote && (c == ')' || c == ']' || c == '}')) {
if (stack.isEmpty()) {
return i; // Return position of unbalanced closing bracket
}
char top = stack.pop();
if (c == ')' && top != '(' || c == ']' && top != '[' || c == '}' && top != '{') {
return i; // Return position of unbalanced closing bracket
}
}
}
if (!stack.isEmpty()) {
char unbalanced = stack.peek();
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == unbalanced) {
return i; // Return position of unbalanced opening bracket or quote
}
}
}
return -1; // Return -1 if all brackets and quotes are balanced
}
public static String sanitizeSingleSql(String sqlStr) {
final Pattern SQL_DELIMITER_SPLIT =
Pattern.compile("((?:'[^']*+'|[^\\n])*+)");
final StringBuilder builder = new StringBuilder();
final Matcher matcher = SQL_DELIMITER_SPLIT.matcher(sqlStr);
while (matcher.find()) {
for (int i = 1; i <= matcher.groupCount(); i++) {
if (!matcher.group(i).isEmpty()) {
builder.append("\n").append(matcher.group(i));
}
}
}
return builder.toString();
}
}