-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgolyglot.go
More file actions
332 lines (284 loc) · 9.16 KB
/
Copy pathgolyglot.go
File metadata and controls
332 lines (284 loc) · 9.16 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
// Package golyglot provides Go bindings for the polyglot-sql library,
// a multi-dialect SQL parser, formatter, and transpiler.
//
// The library is loaded on first use from a path
// specified by the GOLYGLOT_LIBRARY_PATH environment variable.
// If not found, it is automatically downloaded from GitHub releases.
package golyglot
import (
"encoding/json"
"fmt"
"runtime"
"sync"
"unsafe"
)
const (
// Version of the polyglot-sql FFI library to download.
Version = "1.0.17"
// GitHub release URL pattern for downloading the FFI library.
releaseURLPattern = "https://github.com/slingdata-io/golyglot/releases/download/v%s/polyglot-sql-ffi-%s-%s.%s"
StatusSuccess = 0
StatusParseError = 1
StatusGenerateError = 2
StatusTranspileError = 3
StatusValidationError = 4
StatusInvalidArgument = 5
StatusSerializationError = 6
StatusInternalError = 99
)
var (
initOnce sync.Once
initErr error
libHandle uintptr
// FFI function pointers registered via purego.RegisterLibFunc.
// polyglot_result_t is {char* data, char* error, int32 status} = 20 bytes (padded to 24).
// On ARM64 (AAPCS), structs > 16 bytes are returned via x8 (indirect).
// purego.RegisterLibFunc handles this transparently when the Go func returns a struct.
ffiParse func(sql uintptr, dialect uintptr) ffiResult
ffiParseOne func(sql uintptr, dialect uintptr) ffiResult
ffiGenerate func(astJSON uintptr, dialect uintptr) ffiResult
ffiTranspile func(sql uintptr, fromDialect uintptr, toDialect uintptr) ffiResult
ffiFormat func(sql uintptr, dialect uintptr) ffiResult
ffiTokenize func(sql uintptr, dialect uintptr) ffiResult
ffiValidate func(sql uintptr, dialect uintptr) ffiValidationResult
ffiVersion func() uintptr
ffiDialectList func() uintptr
ffiDialectCount func() int32
ffiFreeString func(s uintptr)
ffiFreeResult func(result ffiResult)
ffiFreeValidation func(result ffiValidationResult)
)
// ffiResult mirrors the C polyglot_result_t struct.
type ffiResult struct {
Data uintptr // char* data
Error uintptr // char* error
Status int32 // status code
_ int32 // padding
}
// ffiValidationResult mirrors the C polyglot_validation_result_t struct.
type ffiValidationResult struct {
Valid int32 // 1 if valid
_ int32 // padding
ErrorsJSON uintptr // char* errors_json
Error uintptr // char* error
Status int32 // status code
_ int32 // padding
}
// ParseResult holds the result of parsing SQL.
type ParseResult struct {
AST json.RawMessage // JSON array of AST expression nodes
}
// TranspileResult holds the result of transpiling SQL.
type TranspileResult struct {
SQL string
}
// FormatResult holds the result of formatting SQL.
type FormatResult struct {
SQL string
}
// ValidationResult holds the result of validating SQL.
type ValidationResult struct {
Valid bool
Errors json.RawMessage // JSON array of validation errors
}
// TokenizeResult holds the result of tokenizing SQL.
type TokenizeResult struct {
Tokens json.RawMessage
}
// Init loads the polyglot-sql FFI library. It is called automatically on first use,
// but can be called explicitly to trigger download/loading early.
func Init() error {
initOnce.Do(func() {
initErr = initLib()
})
return initErr
}
// ptrArg converts a byte slice to uintptr for passing to C.
func ptrArg(b []byte) uintptr {
return uintptr(unsafe.Pointer(&b[0]))
}
// uintptrToString converts a uintptr C string to Go string.
func uintptrToString(ptr uintptr) string {
if ptr == 0 {
return ""
}
// Convert uintptr to *byte via double pointer indirection to satisfy go vet.
// #nosec G103 -- required for FFI interop
return goString((*byte)(*(*unsafe.Pointer)(unsafe.Pointer(&ptr))))
}
// Parse parses SQL into an AST JSON array of Expression nodes.
func Parse(sql, dialect string) (*ParseResult, error) {
if err := Init(); err != nil {
return nil, err
}
sqlBytes := cString(sql)
dialectBytes := cString(dialect)
result := ffiParse(ptrArg(sqlBytes), ptrArg(dialectBytes))
defer ffiFreeResult(result)
if result.Status != StatusSuccess {
return nil, fmt.Errorf("parse error: %s", uintptrToString(result.Error))
}
return &ParseResult{AST: json.RawMessage(uintptrToString(result.Data))}, nil
}
// ParseOne parses a single SQL statement into an AST JSON object.
func ParseOne(sql, dialect string) (*ParseResult, error) {
if err := Init(); err != nil {
return nil, err
}
sqlBytes := cString(sql)
dialectBytes := cString(dialect)
result := ffiParseOne(ptrArg(sqlBytes), ptrArg(dialectBytes))
defer ffiFreeResult(result)
if result.Status != StatusSuccess {
return nil, fmt.Errorf("parse error: %s", uintptrToString(result.Error))
}
return &ParseResult{AST: json.RawMessage(uintptrToString(result.Data))}, nil
}
// Generate converts AST JSON back to SQL string.
func Generate(astJSON, dialect string) (*TranspileResult, error) {
if err := Init(); err != nil {
return nil, err
}
astBytes := cString(astJSON)
dialectBytes := cString(dialect)
result := ffiGenerate(ptrArg(astBytes), ptrArg(dialectBytes))
defer ffiFreeResult(result)
if result.Status != StatusSuccess {
return nil, fmt.Errorf("generate error: %s", uintptrToString(result.Error))
}
data := uintptrToString(result.Data)
return &TranspileResult{SQL: extractSQLFromJSON(data)}, nil
}
// Transpile converts SQL from one dialect to another.
func Transpile(sql, fromDialect, toDialect string) (*TranspileResult, error) {
if err := Init(); err != nil {
return nil, err
}
sqlBytes := cString(sql)
fromBytes := cString(fromDialect)
toBytes := cString(toDialect)
result := ffiTranspile(ptrArg(sqlBytes), ptrArg(fromBytes), ptrArg(toBytes))
defer ffiFreeResult(result)
if result.Status != StatusSuccess {
return nil, fmt.Errorf("transpile error: %s", uintptrToString(result.Error))
}
data := uintptrToString(result.Data)
return &TranspileResult{SQL: extractSQLFromJSON(data)}, nil
}
// Format pretty-prints SQL for a dialect.
func Format(sql, dialect string) (*FormatResult, error) {
if err := Init(); err != nil {
return nil, err
}
sqlBytes := cString(sql)
dialectBytes := cString(dialect)
result := ffiFormat(ptrArg(sqlBytes), ptrArg(dialectBytes))
defer ffiFreeResult(result)
if result.Status != StatusSuccess {
return nil, fmt.Errorf("format error: %s", uintptrToString(result.Error))
}
data := uintptrToString(result.Data)
return &FormatResult{SQL: extractSQLFromJSON(data)}, nil
}
// Validate checks SQL syntax for a dialect.
func Validate(sql, dialect string) (*ValidationResult, error) {
if err := Init(); err != nil {
return nil, err
}
sqlBytes := cString(sql)
dialectBytes := cString(dialect)
result := ffiValidate(ptrArg(sqlBytes), ptrArg(dialectBytes))
if result.Status != StatusSuccess {
errMsg := uintptrToString(result.Error)
ffiFreeValidation(result)
return nil, fmt.Errorf("validation error: %s", errMsg)
}
vr := &ValidationResult{Valid: result.Valid == 1}
if result.ErrorsJSON != 0 {
vr.Errors = json.RawMessage(uintptrToString(result.ErrorsJSON))
}
ffiFreeValidation(result)
return vr, nil
}
// Tokenize splits SQL into tokens.
func Tokenize(sql, dialect string) (*TokenizeResult, error) {
if err := Init(); err != nil {
return nil, err
}
sqlBytes := cString(sql)
dialectBytes := cString(dialect)
result := ffiTokenize(ptrArg(sqlBytes), ptrArg(dialectBytes))
defer ffiFreeResult(result)
if result.Status != StatusSuccess {
return nil, fmt.Errorf("tokenize error: %s", uintptrToString(result.Error))
}
return &TokenizeResult{Tokens: json.RawMessage(uintptrToString(result.Data))}, nil
}
// LibVersion returns the version string of the loaded polyglot-sql library.
func LibVersion() (string, error) {
if err := Init(); err != nil {
return "", err
}
ptr := ffiVersion()
if ptr == 0 {
return "", fmt.Errorf("version returned nil")
}
// version pointer is statically allocated, do NOT free
return uintptrToString(ptr), nil
}
// DialectList returns the list of supported dialect names as JSON.
func DialectList() (string, error) {
if err := Init(); err != nil {
return "", err
}
ptr := ffiDialectList()
if ptr == 0 {
return "", fmt.Errorf("dialect_list returned nil")
}
s := uintptrToString(ptr)
ffiFreeString(ptr)
return s, nil
}
// DialectCount returns the number of supported dialects.
func DialectCount() (int, error) {
if err := Init(); err != nil {
return 0, err
}
return int(ffiDialectCount()), nil
}
// extractSQLFromJSON parses the JSON result from polyglot FFI functions
// that return SQL. The format is typically a JSON array of SQL strings: ["SQL"]
func extractSQLFromJSON(data string) string {
var arr []string
if err := json.Unmarshal([]byte(data), &arr); err == nil && len(arr) > 0 {
return arr[0]
}
return data
}
// releaseAssetInfo returns the platform-specific release asset details.
func releaseAssetInfo() (osName, archName, ext string) {
switch runtime.GOOS {
case "darwin":
osName = "macos"
case "linux":
osName = "linux"
case "windows":
osName = "windows"
default:
osName = runtime.GOOS
}
switch runtime.GOARCH {
case "amd64":
archName = "x86_64"
case "arm64":
archName = "aarch64"
default:
archName = runtime.GOARCH
}
if runtime.GOOS == "windows" {
ext = "zip"
} else {
ext = "tar.gz"
}
return
}