-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathparser.go
293 lines (221 loc) · 7.35 KB
/
parser.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
package parser
/*
#cgo CFLAGS: -Iinclude -Iinclude/postgres -g -fstack-protector -std=gnu99 -Wno-unknown-warning-option
#cgo windows CFLAGS: -Iinclude/postgres/port/win32
#cgo LDFLAGS:
#include "pg_query.h"
#include "xxhash.h"
#include <stdlib.h>
// Avoid complexities dealing with C structs in Go
PgQueryDeparseResult pg_query_deparse_protobuf_direct_args(void* data, unsigned int len) {
PgQueryProtobuf p;
p.data = (char *) data;
p.len = len;
return pg_query_deparse_protobuf(p);
}
// Avoid inconsistent type behaviour in xxhash library
uint64_t pg_query_hash_xxh3_64(void *data, size_t len, size_t seed) {
return XXH3_64bits_withSeed(data, len, seed);
}
*/
import "C"
import (
"strings"
"unsafe"
)
func init() {
C.pg_query_init()
}
type Error struct {
Message string // exception message
Funcname string // source function of exception (e.g. SearchSysCache)
Filename string // source of exception (e.g. parse.l)
Lineno int // source of exception (e.g. 104)
Cursorpos int // char in query at which exception occurred
Context string // additional context (optional, can be NULL)
}
func (e *Error) Error() string {
return e.Message
}
func newPgQueryError(errC *C.PgQueryError) *Error {
err := &Error{
Message: C.GoString(errC.message),
Lineno: int(errC.lineno),
Cursorpos: int(errC.cursorpos),
}
if errC.funcname != nil {
err.Funcname = C.GoString(errC.funcname)
}
if errC.filename != nil {
err.Filename = C.GoString(errC.filename)
}
if errC.context != nil {
err.Context = C.GoString(errC.context)
}
return err
}
// ParseToJSON - Parses the given SQL statement into a parse tree (JSON format)
func ParseToJSON(input string) (result string, err error) {
inputC := C.CString(input)
defer C.free(unsafe.Pointer(inputC))
resultC := C.pg_query_parse(inputC)
defer C.pg_query_free_parse_result(resultC)
if resultC.error != nil {
err = newPgQueryError(resultC.error)
return
}
result = C.GoString(resultC.parse_tree)
return
}
// Scans the given SQL statement into a protobuf ScanResult
func ScanToProtobuf(input string) (result []byte, err error) {
inputC := C.CString(input)
defer C.free(unsafe.Pointer(inputC))
resultC := C.pg_query_scan(inputC)
defer C.pg_query_free_scan_result(resultC)
if resultC.error != nil {
err = newPgQueryError(resultC.error)
return
}
result = []byte(C.GoStringN(resultC.pbuf.data, C.int(resultC.pbuf.len)))
return
}
// ParseToProtobuf - Parses the given SQL statement into a parse tree (Protobuf format)
func ParseToProtobuf(input string) (result []byte, err error) {
inputC := C.CString(input)
defer C.free(unsafe.Pointer(inputC))
resultC := C.pg_query_parse_protobuf(inputC)
defer C.pg_query_free_protobuf_parse_result(resultC)
if resultC.error != nil {
err = newPgQueryError(resultC.error)
return
}
result = []byte(C.GoStringN(resultC.parse_tree.data, C.int(resultC.parse_tree.len)))
return
}
// DeparseFromProtobuf - Deparses the given Protobuf format parse tree into a SQL statement
func DeparseFromProtobuf(input []byte) (result string, err error) {
inputC := C.CBytes(input)
defer C.free(inputC)
resultC := C.pg_query_deparse_protobuf_direct_args(inputC, C.uint(len(input)))
defer C.pg_query_free_deparse_result(resultC)
if resultC.error != nil {
err = newPgQueryError(resultC.error)
return
}
result = C.GoString(resultC.query)
return
}
// ParsePlPgSqlToJSON - Parses the given PL/pgSQL function statement into a parse tree (JSON format)
func ParsePlPgSqlToJSON(input string) (result string, err error) {
inputC := C.CString(input)
defer C.free(unsafe.Pointer(inputC))
resultC := C.pg_query_parse_plpgsql(inputC)
defer C.pg_query_free_plpgsql_parse_result(resultC)
if resultC.error != nil {
err = newPgQueryError(resultC.error)
return
}
result = C.GoString(resultC.plpgsql_funcs)
return
}
// Normalize the passed SQL statement to replace constant values with ? characters
func Normalize(input string) (result string, err error) {
inputC := C.CString(input)
defer C.free(unsafe.Pointer(inputC))
resultC := C.pg_query_normalize(inputC)
defer C.pg_query_free_normalize_result(resultC)
if resultC.error != nil {
err = newPgQueryError(resultC.error)
return
}
result = C.GoString(resultC.normalized_query)
return
}
// Normalize the passed utility statement to replace constant values with ? characters
func NormalizeUtility(input string) (result string, err error) {
inputC := C.CString(input)
defer C.free(unsafe.Pointer(inputC))
resultC := C.pg_query_normalize_utility(inputC)
defer C.pg_query_free_normalize_result(resultC)
if resultC.error != nil {
err = newPgQueryError(resultC.error)
return
}
result = C.GoString(resultC.normalized_query)
return
}
func SplitWithScanner(input string, trimSpace bool) (result []string, err error) {
inputC := C.CString(input)
defer C.free(unsafe.Pointer(inputC))
resultC := C.pg_query_split_with_scanner(inputC)
defer C.pg_query_free_split_result(resultC)
if resultC.error != nil {
err = newPgQueryError(resultC.error)
return
}
result = handleSplitResult(input, trimSpace, resultC)
return
}
func SplitWithParser(input string, trimSpace bool) (result []string, err error) {
inputC := C.CString(input)
defer C.free(unsafe.Pointer(inputC))
resultC := C.pg_query_split_with_parser(inputC)
defer C.pg_query_free_split_result(resultC)
if resultC.error != nil {
err = newPgQueryError(resultC.error)
return
}
result = handleSplitResult(input, trimSpace, resultC)
return
}
func handleSplitResult(input string, trimSpace bool, resultC C.PgQuerySplitResult) (result []string) {
stmts := (**C.PgQuerySplitStmt)(unsafe.Pointer(resultC.stmts))
for i := 0; i < int(resultC.n_stmts); i++ {
stmtptr := (**C.PgQuerySplitStmt)(unsafe.Pointer(uintptr(unsafe.Pointer(stmts)) + uintptr(i)*unsafe.Sizeof(*stmts)))
stmt := **stmtptr
end := stmt.stmt_location + stmt.stmt_len
stmtStr := input[stmt.stmt_location:end]
if trimSpace {
stmtStr = strings.TrimSpace(stmtStr)
}
result = append(result, stmtStr)
}
return
}
// FingerprintToUInt64 - Fingerprint the passed SQL statement using the C extension and returns result as uint64
func FingerprintToUInt64(input string) (result uint64, err error) {
inputC := C.CString(input)
defer C.free(unsafe.Pointer(inputC))
resultC := C.pg_query_fingerprint(inputC)
defer C.pg_query_free_fingerprint_result(resultC)
if resultC.error != nil {
err = newPgQueryError(resultC.error)
return
}
// https://github.com/golang/go/issues/29878
result = *(*uint64)(unsafe.Pointer(&resultC.fingerprint))
return
}
// FingerprintToHexStr - Fingerprint the passed SQL statement using the C extension and returns result as hex string
func FingerprintToHexStr(input string) (result string, err error) {
inputC := C.CString(input)
defer C.free(unsafe.Pointer(inputC))
resultC := C.pg_query_fingerprint(inputC)
defer C.pg_query_free_fingerprint_result(resultC)
if resultC.error != nil {
err = newPgQueryError(resultC.error)
return
}
result = C.GoString(resultC.fingerprint_str)
return
}
// HashXXH3_64 - Helper method to run XXH3 hash function (64-bit variant) on the given bytes, with the specified seed
func HashXXH3_64(input []byte, seed uint64) (result uint64) {
inputC := C.CBytes(input)
defer C.free(inputC)
res := C.pg_query_hash_xxh3_64(inputC, C.size_t(len(input)), C.size_t(seed))
// https://github.com/golang/go/issues/29878
result = *(*uint64)(unsafe.Pointer(&res))
return
}