-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
QlParser.java
233 lines (189 loc) · 7.31 KB
/
QlParser.java
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
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.repository.query;
import static java.util.Arrays.*;
import lombok.Value;
import lombok.experimental.UtilityClass;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* A parser that parses SQL, JQL and HQL queries in order to extract information needed in order to manipulate the
* queries.
*
* @author Jens Schauder
* @since 2.2.0
*/
@UtilityClass
class QlParser {
private static final Set<String> KEYWORDS = new HashSet<>(asList("order", "where", "join", "from", "case"));
// Used Regex/Unicode categories (see http://www.unicode.org/reports/tr18/#General_Category_Property):
// Z Separator
// Cc Control
// Cf Format
// P Punctuation
private static final Pattern IDENTIFIER = Pattern.compile("[._[\\P{Z}&&\\P{Cc}&&\\P{Cf}&&\\P{P}]]+");
/**
* Parses any String enclosed in single quotes. Result does contain the quotes.
*/
static final Parser<String, String> quotedLiteral = StringParsers.regex("'.*?'");
/**
* Parses white space to {@code null}.
*/
private static final Parser<String, String> ws = StringParsers.regex("\\s+").map(w -> null);
/**
* Parses an inline comment and maps it to {@code null}.
*/
private static final Parser<String, String> inlineComment = StringParsers.regex("--.*").map(c -> null);
/**
* Parses anything enclosed with double quotes.
*/
static final Parser<String, String> doubleQuotedIdentifier = StringParsers.regex("\".*?\"");
/**
* Parses a block comment enclosed by /* and * / (without the space).
*/
static final Parser<String, String> blockComment = StringParsers.regex("/\\*.*?\\*/").map(c -> null);
private static final Parser<String, String> openParen = StringParsers.keyword("(");
private static final Parser<String, String> closingParen = StringParsers.keyword(")");
private static final Parser<String, String> comma = StringParsers.keyword(",");
/**
* This is intended to parse any part of a SQL string that isn't recognised by other parsers.
*/
private static final Parser<String, String> word = StringParsers.regex("[%&*+\\-\\./\\\\:;<?\\[\\]\\^_|{}\\w]+");
/**
* Parses all tokens not enclosed in parenthesis.
*/
private static final Parser<String, String> noParensToken = Parsers.either(ws, blockComment, inlineComment,
quotedLiteral, doubleQuotedIdentifier, comma, word);
/**
* Parses all tokens including those included in parenthesis.
*/
private static final Parser<String, Object> token = Parsers.either(noParensToken, balancedParens());
private static final Parser<List<Object>, TableAlias> tableAlias = Parsers.concat( //
() -> keyword("from"), //
QlParser::any, // table expression
() -> Parsers.opt(keyword("as")), //
QlParser::identifier //
).map(l -> new TableAlias((String) l.get(3)));
private static final Parser<List<Object>, JoinAlias> joinAlias = Parsers.concat( //
() -> keyword("join"), //
() -> Parsers.opt(keyword("fetch")), //
QlParser::identifier, //
() -> Parsers.opt(keyword("as")), //
QlParser::identifier).map(l -> new JoinAlias((String) l.get(4)));
private static final Parser<List<Object>, FunctionAlias> functionAlias = Parsers.concat( //
QlParser::identifier, //
QlParser::parameterList, //
() -> Parsers.opt(keyword("as")), //
QlParser::identifier //
).map(l -> new FunctionAlias((String) l.get(3)));
private static final Parser<List<Object>, OrderBy> orderBy = Parsers.concat( //
() -> keyword("order"), //
() -> keyword("by")).map(l -> OrderBy.INSTANCE);
private Parser<List<Object>, SqlInfo> sqlInfoParser = Parsers.many(Parsers.either( //
joinAlias, //
functionAlias, //
tableAlias, //
orderBy, //
any()) // ignore all other elements
).map(QlParser::sqlElementsToSqlInfo);
/**
* Converts a list of {@link JoinAlias}, {@link TableAlias}, {@link OrderBy}, and {@link FunctionAlias} into a single
* SqlInfo instance;
*
* @param list list of objects to parse. Must not be {@code null}.
* @return a {@link SqlInfo} instance with the condensed information from the list. Guaranteed to be not {@code null}.
*/
private static SqlInfo sqlElementsToSqlInfo(List<Object> list) {
SqlInfo sqlInfo = new SqlInfo();
list.forEach(e -> {
if (e instanceof JoinAlias) {
sqlInfo.joinAliases.add(((JoinAlias) e).alias);
} else if (e instanceof TableAlias && sqlInfo.tableAlias == null) {
sqlInfo.tableAlias = ((TableAlias) e).alias;
} else if (e instanceof FunctionAlias) {
sqlInfo.functionAliases.add(((FunctionAlias) e).alias);
} else if (e instanceof OrderBy) {
sqlInfo.orderBy = true;
}
});
return sqlInfo;
}
private static Parser<List<Object>, List> parameterList() {
return ListParsers.element( //
List.class, //
l -> l.size() >= 2 //
&& l.get(0).equals("(") //
&& l.get(l.size() - 1).equals(")") //
);
}
/**
* Parses a SQL, JQL or HQL query and returns the essential information about the query.
*
* @param query the query to parse. Must not be {@code null}.
* @return a {@link SqlInfo} instance. Guaranteed to be not {@code null}.
*/
static SqlInfo parseSql(String query) {
Parser.ParseResult<String, List<Object>> parseResult = tokenizer.parse(query);
if (parseResult instanceof Parser.Success) {
Parser.ParseResult<List<Object>, SqlInfo> sqlInfoParseResult = sqlInfoParser
.parse(((Parser.Success<String, List<Object>>) parseResult).getResult());
if (sqlInfoParseResult instanceof Parser.Success) {
return ((Parser.Success<List<Object>, SqlInfo>) sqlInfoParseResult).getResult();
}
}
return new SqlInfo();
}
@Value
private static class FunctionAlias {
String alias;
}
private static Parser<List<Object>, String> identifier() {
return ListParsers.element(String.class,
s -> !KEYWORDS.contains(s.toLowerCase()) && IDENTIFIER.matcher(s).matches());
}
private static Parser<List<Object>, Object> any() {
return ListParsers.element(Object.class, s -> true);
}
private static Parser<List<Object>, String> keyword(String keyword) {
return ListParsers.element(String.class, s -> s.equalsIgnoreCase(keyword));
}
static final Parser<String, List<Object>> tokenizer = Parsers.many(token)
.map(l -> l.stream().filter(Objects::nonNull).collect(Collectors.toList()));
private static Parser<String, List<Object>> balancedParens() {
return Parsers.concat(() -> openParen, () -> tokenizer, () -> closingParen);
}
enum OrderBy {
INSTANCE
}
static class SqlInfo {
final Set<String> joinAliases = new HashSet<>();
final Set<String> functionAliases = new HashSet<>();
String tableAlias;
boolean orderBy;
}
@Value
private static class TableAlias {
String alias;
}
@Value
private static class JoinAlias {
String alias;
}
}