Repository navigation
Expand file tree
/
Copy pathAbstractJSqlParser.java
More file actions
283 lines (228 loc) · 10.2 KB
/
Copy pathAbstractJSqlParser.java
File metadata and controls
283 lines (228 loc) · 10.2 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
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2020 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.parser;
import net.sf.jsqlparser.parser.feature.Feature;
import net.sf.jsqlparser.parser.feature.FeatureConfiguration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
public abstract class AbstractJSqlParser<P> {
protected int jdbcParameterIndex = 0;
protected boolean errorRecovery = false;
protected List<ParseException> parseErrors = new ArrayList<>();
public enum AdjacentStringLiterals {
OFF, NEWLINE, WHITESPACE
}
public enum Dialect {
ANSI_SQL(AdjacentStringLiterals.NEWLINE), ORACLE, MYSQL(AdjacentStringLiterals.OFF,
Feature.allowBackslashEscapeCharacter,
Feature.allowHashLineComments,
Feature.allowDoubleQuotedStrings), MARIADB(AdjacentStringLiterals.OFF,
Feature.allowBackslashEscapeCharacter,
Feature.allowHashLineComments,
Feature.allowDoubleQuotedStrings), SQLSERVER(AdjacentStringLiterals.OFF,
Feature.allowSquareBracketQuotation), POSTGRESQL(
AdjacentStringLiterals.NEWLINE,
Feature.allowDollarQuotedStringTags,
Feature.allowDigitSeparators), H2, EXASOL, BIGQUERY(
AdjacentStringLiterals.WHITESPACE,
Feature.allowDoubleQuotedStrings,
Feature.allowHashLineComments,
Feature.allowBackslashEscapeCharacter), DATABRICKS(
AdjacentStringLiterals.WHITESPACE,
Feature.allowDoubleQuotedStrings,
Feature.allowBackslashEscapeCharacter), SNOWFLAKE(
Feature.allowBackslashEscapeCharacter), INFORMIX, SPANNER, DORIS, COCKROACHDB, TERADATA, SQLITE;
private final Set<Feature> lexerFeatures;
private final AdjacentStringLiterals adjacentStringLiterals;
Dialect(AdjacentStringLiterals adjacentStringLiterals, Feature... lexerFeatures) {
this.adjacentStringLiterals = adjacentStringLiterals;
this.lexerFeatures = lexerFeatures.length == 0 ? EnumSet.noneOf(Feature.class)
: EnumSet.copyOf(Arrays.asList(lexerFeatures));
}
Dialect(Feature... lexerFeatures) {
this(AdjacentStringLiterals.OFF, lexerFeatures);
}
/** Returns the immutable lexer defaults shared by this dialect preset. */
public Set<Feature> getLexerFeatures() {
return Collections.unmodifiableSet(lexerFeatures);
}
public AdjacentStringLiterals getAdjacentStringLiterals() {
return adjacentStringLiterals;
}
}
public P withSquareBracketQuotation() {
return withFeature(Feature.allowSquareBracketQuotation, true);
}
public P withSquareBracketQuotation(boolean allowSquareBracketQuotation) {
return withFeature(Feature.allowSquareBracketQuotation, allowSquareBracketQuotation);
}
public P withAllowComplexParsing() {
return withFeature(Feature.allowComplexParsing, true);
}
public P withAllowComplexParsing(boolean allowComplexParsing) {
return withFeature(Feature.allowComplexParsing, allowComplexParsing);
}
public P withComplexParsing() {
return withFeature(Feature.allowComplexParsing, true);
}
public P withComplexParsing(boolean allowComplexParsing) {
return withFeature(Feature.allowComplexParsing, allowComplexParsing);
}
public P withUnsupportedStatements() {
return withFeature(Feature.allowUnsupportedStatements, true);
}
public P withUnsupportedStatements(boolean allowUnsupportedStatements) {
return withFeature(Feature.allowUnsupportedStatements, allowUnsupportedStatements);
}
/** Enables GROUP BY ASC/DESC for MySQL versions before 8.0.13. Requires MYSQL dialect. */
public P withLegacyMySqlGroupBy(boolean enabled) {
return withFeature(Feature.allowLegacyMySqlGroupBy, enabled);
}
/**
* Resolves MySQL executable comments against this server version (for example 90702 for 9.7.2).
* Requires the MYSQL dialect. Rendered SQL reflects this version, without conditional wrappers.
* Without an explicit version, conditional executable comments raise a lexical error.
*
* @param version major * 10000 + minor * 100 + patch, from 0 through 999999
* @return this parser
*/
public P withMySqlServerVersion(int version) {
if (version < 0 || version > 999999) {
throw new IllegalArgumentException("MySQL server version must be between 0 and 999999");
}
return withFeature(Feature.mySqlServerVersion, (long) version);
}
public P withTimeOut(long timeOutMillSeconds) {
return withFeature(Feature.timeOut, timeOutMillSeconds);
}
/**
* Applies this dialect's enabled lexer defaults to the current configuration. Existing options
* remain set; apply explicit overrides after this call, or use a new parser for a fresh preset.
*/
public P withDialect(Dialect dialect) {
withFeature(Feature.dialect, dialect.name());
if (dialect.getAdjacentStringLiterals() != AdjacentStringLiterals.OFF) {
withAdjacentStringLiterals(dialect.getAdjacentStringLiterals());
}
for (Feature lexerFeature : dialect.getLexerFeatures()) {
withFeature(lexerFeature, true);
}
return me();
}
public P withAdjacentStringLiterals() {
return withAdjacentStringLiterals(AdjacentStringLiterals.NEWLINE);
}
public P withAdjacentStringLiterals(boolean adjacentStringLiterals) {
return withAdjacentStringLiterals(
adjacentStringLiterals ? AdjacentStringLiterals.NEWLINE
: AdjacentStringLiterals.OFF);
}
public P withAdjacentStringLiterals(AdjacentStringLiterals adjacentStringLiterals) {
getConfiguration().setValue(Feature.adjacentStringLiterals, adjacentStringLiterals);
return me();
}
public P withAllowedNestingDepth(int allowedNestingDepth) {
return withFeature(Feature.allowedNestingDepth, allowedNestingDepth);
}
public P withBackslashEscapeCharacter() {
return withFeature(Feature.allowBackslashEscapeCharacter, true);
}
public P withBackslashEscapeCharacter(boolean allowBackslashEscapeCharacter) {
return withFeature(Feature.allowBackslashEscapeCharacter, allowBackslashEscapeCharacter);
}
/**
* Controls tagged dollar quotes; disabled by default, enabled by the PostgreSQL dialect preset.
* False preserves dollar-containing identifier spellings.
*/
public P withDollarQuotedStringTags(boolean allowDollarQuotedStringTags) {
return withFeature(Feature.allowDollarQuotedStringTags, allowDollarQuotedStringTags);
}
public P withDoubleQuotedStrings() {
return withFeature(Feature.allowDoubleQuotedStrings, true);
}
public P withDoubleQuotedStrings(boolean allowDoubleQuotedStrings) {
return withFeature(Feature.allowDoubleQuotedStrings, allowDoubleQuotedStrings);
}
public P withHashLineComments() {
return withFeature(Feature.allowHashLineComments, true);
}
public P withHashLineComments(boolean allowHashLineComments) {
return withFeature(Feature.allowHashLineComments, allowHashLineComments);
}
public P withBlankLinesAsSeparator(boolean allowBlankLinesAsStatementSeparator) {
return withFeature(Feature.allowBlankLinesAsStatementSeparator,
allowBlankLinesAsStatementSeparator);
}
public P withDigitSeparators() {
return withFeature(Feature.allowDigitSeparators, true);
}
public P withDigitSeparators(boolean allowDigitSeparators) {
return withFeature(Feature.allowDigitSeparators, allowDigitSeparators);
}
public P withUnparenthesizedSubSelects() {
return withFeature(Feature.allowUnparenthesizedSubSelects, true);
}
public P withUnparenthesizedSubSelects(boolean allowUnparenthesizedSubSelects) {
return withFeature(Feature.allowUnparenthesizedSubSelects, allowUnparenthesizedSubSelects);
}
public P withFeature(Feature f, boolean enabled) {
getConfiguration().setValue(f, enabled);
return me();
}
public P withFeature(Feature f, long value) {
getConfiguration().setValue(f, value);
return me();
}
public P withFeature(Feature f, String value) {
getConfiguration().setValue(f, value);
return me();
}
public abstract FeatureConfiguration getConfiguration();
public FeatureConfiguration setValue(Feature feature, Object value) {
return getConfiguration().setValue(feature, value);
}
public Object getValue(Feature feature) {
return getConfiguration().getValue(feature);
}
public abstract P me();
public boolean getAsBoolean(Feature f) {
return getConfiguration().getAsBoolean(f);
}
public Long getAsLong(Feature f) {
return getConfiguration().getAsLong(f);
}
public int getAsInt(Feature f) {
return getConfiguration().getAsInt(f);
}
public Integer getAsInteger(Feature f) {
return getConfiguration().getAsInteger(f);
}
public String getAsString(Feature f) {
return getConfiguration().getAsString(f);
}
public void setErrorRecovery(boolean errorRecovery) {
this.errorRecovery = errorRecovery;
}
public P withErrorRecovery() {
this.errorRecovery = true;
return me();
}
public P withErrorRecovery(boolean errorRecovery) {
this.errorRecovery = errorRecovery;
return me();
}
public List<ParseException> getParseErrors() {
return parseErrors;
}
}