-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patholiphant.go
More file actions
114 lines (97 loc) · 3.94 KB
/
Copy patholiphant.go
File metadata and controls
114 lines (97 loc) · 3.94 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
// Package oliphant is a pure Go (no cgo, no wasm) drop-in replacement for
// github.com/pganalyze/pg_query_go/v6: same exported functions, same
// generated protobuf types, same JSON output, same error type, same
// fingerprints.
//
// Consumers alias the import, exactly as pg_query_go users already do:
//
// import pg_query "github.com/sqlc-dev/oliphant"
//
// This file mirrors pg_query_go's pg_query.go function-for-function; the
// bodies delegate to the parser subpackage just as upstream's do.
package oliphant
import (
"google.golang.org/protobuf/proto"
"github.com/sqlc-dev/oliphant/parser"
)
func Scan(input string) (result *ScanResult, err error) {
protobufScan, err := parser.ScanToProtobuf(input)
if err != nil {
return
}
result = &ScanResult{}
err = proto.Unmarshal(protobufScan, result)
return
}
// ParseToJSON - Parses the given SQL statement into a parse tree (JSON format)
func ParseToJSON(input string) (result string, err error) {
return parser.ParseToJSON(input)
}
// Parse the given SQL statement into a parse tree (Go struct format)
//
// Upstream this decodes the protobuf the C parser returns across the cgo
// boundary; here the parser is Go and hands back the tree itself, so
// parser.ParseToTree skips the encode/decode. Behavior is unchanged — see
// its comment for the UTF-8 fallback that keeps it that way.
func Parse(input string) (tree *ParseResult, err error) {
return parser.ParseToTree(input)
}
// Deparse - Deparses a given Go parse tree into a SQL statement
func Deparse(tree *ParseResult) (output string, err error) {
protobufTree, err := proto.Marshal(tree)
if err != nil {
return
}
output, err = parser.DeparseFromProtobuf(protobufTree)
return
}
// ParsePlPgSqlToJSON - Parses the given PL/pgSQL function statement into a parse tree (JSON format)
func ParsePlPgSqlToJSON(input string) (result string, err error) {
return parser.ParsePlPgSqlToJSON(input)
}
// Normalize the passed SQL statement to replace constant values with $n parameter references
func Normalize(input string) (result string, err error) {
return parser.Normalize(input)
}
// NormalizeUtility - Normalize the passed utility statement to replace constant values with $n parameter references
func NormalizeUtility(input string) (result string, err error) {
return parser.NormalizeUtility(input)
}
// Fingerprint - Fingerprint the passed SQL statement to a hex string
func Fingerprint(input string) (result string, err error) {
return parser.FingerprintToHexStr(input)
}
// FingerprintToUInt64 - Fingerprint the passed SQL statement to a uint64
func FingerprintToUInt64(input string) (result uint64, err error) {
return parser.FingerprintToUInt64(input)
}
// 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) {
return parser.HashXXH3_64(input, seed)
}
func SplitWithScanner(input string, trimSpace bool) (result []string, err error) {
return parser.SplitWithScanner(input, trimSpace)
}
func SplitWithParser(input string, trimSpace bool) (result []string, err error) {
return parser.SplitWithParser(input, trimSpace)
}
// IsUtilityStmt - Determines whether each statement in the query is a utility statement
//
// Returns a slice of booleans, one for each statement in the query.
// true = utility statement / DDL, false = SELECT / INSERT / UPDATE / DELETE / MERGE
func IsUtilityStmt(input string) (result []bool, err error) {
return parser.IsUtilityStmt(input)
}
// Summary - Extracts summary information from SQL statement
//
// Optionally, you can pass a positive numbered truncateLimit to return a
// "smart" truncated version of the input statement that is at most limit length.
func Summary(input string, truncateLimit int) (result *SummaryResult, err error) {
protobufSummary, err := parser.SummaryToProtobuf(input, truncateLimit)
if err != nil {
return
}
result = &SummaryResult{}
err = proto.Unmarshal(protobufSummary, result)
return
}