-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
sql_formatter.go
36 lines (33 loc) · 1.05 KB
/
sql_formatter.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
package zetasql
import "C"
import (
"unsafe"
internal "github.com/goccy/go-zetasql/internal/ccall/go-zetasql"
"github.com/goccy/go-zetasql/internal/helper"
)
// FormatSQL formats ZetaSQL statements.
// Multiple statements separated by semi-colons are supported.
//
// On return, the first return value is always populated with equivalent SQL.
// The returned error contains the concatenation of any errors that
// occurred while parsing the statements.
//
// Any statements that fail to parse as valid ZetaSQL are returned unchanged.
// All valid statements will be reformatted.
//
// CAVEATS:
// 1. This can only reformat SQL statements that can be parsed successfully.
// Statements that cannot be parsed are returned unchanged.
// 2. Comments are stripped in the formatted output.
func FormatSQL(sql string) (string, error) {
var (
out unsafe.Pointer
status unsafe.Pointer
)
internal.FormatSql(unsafe.Pointer(C.CString(sql)), &out, &status)
st := helper.NewStatus(status)
if !st.OK() {
return "", st.Error()
}
return C.GoString((*C.char)(out)), nil
}