-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqueryParser.go
339 lines (302 loc) · 8.15 KB
/
queryParser.go
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
package main
/*
* This file contains a parser for a really small subset of the Postgres SQL
* dialect. The objective is to only support LISTEN, UNLISTEN and trivial
* "ping"-type SELECT statements. Many queries accepted by Postgres proper are
* rejected, but that's fine for our purposes -- in fact, this parser probably
* tries to support way too many corner cases already.
*/
import (
"errors"
"fmt"
"strings"
"unicode/utf8"
)
/* possible token types returned by nextToken() */
type queryParserTokenType int
const (
tokEOF queryParserTokenType = iota
tokIdentifier
tokDigit
tokSemicolon
tokStar
)
const (
flagAllowEOF uint32 = 1
flagAllowQuotedIdentifiers = 2
)
func (t queryParserTokenType) String() string {
switch t {
case tokEOF:
return "EOF"
case tokIdentifier:
return "identifier"
case tokDigit:
return "digit"
case tokSemicolon:
return "semicolon"
case tokStar:
return "asterisk"
default:
panic(fmt.Sprintf("unrecognized token type %d", t))
}
}
type queryParserToken struct {
typ queryParserTokenType
payload string
}
// maximum query length, in characters
const maxQuerySize = 512
var (
errQueryParserInputNotUtf8 = errors.New("invalid input syntax for encoding UTF-8")
errQueryParserUnexpectedEOF = errors.New("unexpected EOF")
errQueryTooLong = errors.New("query length exceeds maximum allowed size")
)
// These should match src/backend/parser/scan.l
const whiteSpaceCharacters string = " \t\n\r\f"
func ParseQuery(rawinput string) (q FrontendQuery, err error) {
var token queryParserToken
if !utf8.ValidString(rawinput) {
return nil, errQueryParserInputNotUtf8
}
if len(rawinput) >= maxQuerySize {
return nil, errQueryTooLong
}
// Hack for JDBC. It would be better if we knew how to parse SET commands
// properly and had a configuration file setting for those we can safely
// ignore, but this'll have to do for now.
switch rawinput {
case "SET extra_float_digits = 3":
return NewNopSetCommand(), nil
case "SET application_name = 'PostgreSQL JDBC Driver'":
return NewNopSetCommand(), nil
}
input := []rune(rawinput)
input, err = nextToken(input, &token, flagAllowEOF)
if err != nil {
return nil, err
} else if token.typ == tokEOF {
return NewEmptyQuery(), nil
} else if token.typ == tokSemicolon {
return NewEmptyQuery(), semicolonOrEOF(input)
} else if token.typ != tokIdentifier {
return nil, fmt.Errorf("unexpected token type %q", token.typ)
}
switch token.payload {
case "select":
return parseSelect(input)
case "listen":
return parseListen(input)
case "unlisten":
return parseUnlisten(input)
default:
return nil, fmt.Errorf("parse error at or near %q", token.payload)
}
}
func parseSelect(input []rune) (q FrontendQuery, err error) {
var token queryParserToken
input, err = nextToken(input, &token, 0)
if err != nil {
return nil, err
} else if token.typ != tokDigit {
return nil, fmt.Errorf("unexpected token type %q", token.typ)
}
/* must be at EOF */
return NewTrivialSelect(), semicolonOrEOF(input)
}
func semicolonOrEOF(input []rune) error {
var token queryParserToken
input, err := nextToken(input, &token, flagAllowEOF)
if err != nil {
return err
} else if token.typ == tokEOF {
return nil
} else if token.typ != tokSemicolon {
return fmt.Errorf("unexpected data after query string")
}
input, err = nextToken(input, &token, flagAllowEOF)
if err != nil {
return err
} else if token.typ == tokEOF {
return nil
} else {
return fmt.Errorf("garbage after semicolon")
}
}
func unexpectedToken(token queryParserToken) error {
return fmt.Errorf("parse error: unexpected token %q", token.typ.String())
}
func parseListen(input []rune) (q FrontendQuery, err error) {
var token queryParserToken
input, err = nextToken(input, &token, flagAllowQuotedIdentifiers)
if err != nil {
return nil, err
} else if token.typ == tokIdentifier {
return NewListenRequest(token.payload), semicolonOrEOF(input)
} else {
return nil, unexpectedToken(token)
}
}
func parseUnlisten(input []rune) (q FrontendQuery, err error) {
var token queryParserToken
input, err = nextToken(input, &token, flagAllowQuotedIdentifiers)
if err != nil {
return nil, err
} else if token.typ == tokStar {
return NewWildcardUnlistenRequest(), semicolonOrEOF(input)
} else if token.typ == tokIdentifier {
return NewUnlistenRequest(token.payload), semicolonOrEOF(input)
} else {
return nil, unexpectedToken(token)
}
}
func nextToken(input []rune, token *queryParserToken, flags uint32) (rest []rune, err error) {
foundComment:
input = stripLeadingWhitespace(input)
if len(input) == 0 {
if flags&flagAllowEOF > 0 {
token.typ = tokEOF
return nil, nil
} else {
return nil, errQueryParserUnexpectedEOF
}
}
r := input[0]
if flags&flagAllowQuotedIdentifiers > 0 && r == '"' {
return readQuotedIdentifier(input[1:], token)
} else if isIdentifierStart(r) {
return readIdentifier(input, token)
} else if r == '1' {
return readDigit(input, token)
} else if r == '-' || r == '/' {
input, err = readCommentOrError(input)
if err != nil {
return nil, err
}
goto foundComment
} else if r == ';' {
token.typ = tokSemicolon
return input[1:], nil
} else if r == '*' {
token.typ = tokStar
return input[1:], nil
} else {
return nil, errors.New("parse error")
}
}
func readCommentOrError(input []rune) (rest []rune, err error) {
if len(input) < 2 {
return nil, errQueryParserUnexpectedEOF
}
if input[0] == '-' && input[1] == '-' {
input = input[2:]
for len(input) > 0 && input[0] != '\r' && input[0] != '\n' {
input = input[1:]
}
return input, err
} else if input[0] == '/' && input[1] == '*' {
input = input[2:]
for {
if len(input) < 2 {
return nil, errQueryParserUnexpectedEOF
}
if input[0] == '*' && input[1] == '/' {
return input[2:], nil
} else if input[0] == '/' && input[1] == '*' {
/* C-style comments nest; recurse */
input, err = readCommentOrError(input)
if err != nil {
return nil, err
}
} else {
input = input[1:]
}
}
} else {
return nil, fmt.Errorf("parse error at or near %q", string(input[:2]))
}
}
func readDigit(input []rune, token *queryParserToken) (rest []rune, err error) {
if len(input) == 1 {
token.typ = tokDigit
return nil, nil
}
/*
* We're not at the end yet; we allow anything *except* another digit. In
* reality we only support whitespace and/or comments at the end, but this
* is not the place to enforce that.
*/
r := input[1]
if r >= '0' && r <= '9' {
return nil, errors.New("unexpected integer")
} else {
token.typ = tokDigit
return input[1:], nil
}
}
func isIdentifierStart(r rune) bool {
if r == '_' {
return true
} else if r >= 'A' && r <= 'Z' {
return true
} else if r >= 'a' && r <= 'z' {
return true
} else if r >= '\200' && r <= '\377' {
return true
}
return false
}
func isIdentifierContinuation(r rune) bool {
if isIdentifierStart(r) {
return true
} else if r >= '0' && r <= '9' {
return true
} else if r == '$' {
return true
}
return false
}
func readQuotedIdentifier(input []rune, token *queryParserToken) (rest []rune, err error) {
var identifier string
for {
if len(input) < 1 {
return nil, errQueryParserUnexpectedEOF
} else if input[0] == '"' {
input = input[1:]
break
} else if input[0] == '\\' {
if len(input) < 2 {
return nil, errQueryParserUnexpectedEOF
}
if input[1] != '\\' && input[1] != '"' {
return nil, fmt.Errorf("unexpected escape character '%c'", input[1])
}
identifier += string(input[1])
input = input[2:]
} else {
identifier += string(input[0])
input = input[1:]
}
}
token.payload = identifier
token.typ = tokIdentifier
return input, nil
}
func readIdentifier(input []rune, token *queryParserToken) (rest []rune, err error) {
i := input
input = input[1:]
identifierLen := 1
for len(input) > 0 && isIdentifierContinuation(input[0]) {
identifierLen++
input = input[1:]
}
token.payload = strings.ToLower(string(i[:identifierLen]))
token.typ = tokIdentifier
return input, nil
}
func stripLeadingWhitespace(input []rune) []rune {
for len(input) > 0 && strings.IndexRune(whiteSpaceCharacters, input[0]) != -1 {
input = input[1:]
}
return input
}