-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathparse_helpers_test.go
163 lines (129 loc) · 2.74 KB
/
parse_helpers_test.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
package memefish_test
import (
"fmt"
"github.com/MakeNowJust/heredoc/v2"
"github.com/cloudspannerecosystem/memefish"
)
func ExampleParseStatement() {
stmt, err := memefish.ParseStatement("path/to/file.sql", "SELECT * FROM foo")
if err != nil {
panic(err)
}
fmt.Println(stmt.SQL())
// Output:
// SELECT * FROM foo
}
func ExampleParseStatements() {
stmts, err := memefish.ParseStatements("path/to/file.sql", "SELECT 1; INSERT foo (x, y) VALUES (1, 2)")
if err != nil {
panic(err)
}
for _, stmt := range stmts {
fmt.Printf("%s;\n", stmt.SQL())
}
// Output:
// SELECT 1;
// INSERT INTO foo (x, y) VALUES (1, 2);
}
func ExampleParseQuery() {
stmt, err := memefish.ParseQuery("path/to/file.sql", "SELECT * FROM foo")
if err != nil {
panic(err)
}
fmt.Println(stmt.SQL())
// Output:
// SELECT * FROM foo
}
func ExampleParseExpr() {
stmt, err := memefish.ParseExpr("", "a * b")
if err != nil {
panic(err)
}
fmt.Println(stmt.SQL())
// Output:
// a * b
}
func ExampleParseType() {
stmt, err := memefish.ParseType("", "ARRAY<STRUCT<n INT64, s STRING>>")
if err != nil {
panic(err)
}
fmt.Println(stmt.SQL())
// Output:
// ARRAY<STRUCT<n INT64, s STRING>>
}
func ExampleParseDDL() {
sql := heredoc.Doc(`
CREATE TABLE foo (
x int64,
y int64,
) PRIMARY KEY (x)
`)
ddl, err := memefish.ParseDDL("path/to/file.sql", sql)
if err != nil {
panic(err)
}
fmt.Println(ddl.SQL())
// Output:
// CREATE TABLE foo (
// x INT64,
// y INT64
// ) PRIMARY KEY (x)
}
func ExampleParseDDLs() {
sql := heredoc.Doc(`
CREATE TABLE foo (x int64, y int64) PRIMARY KEY (x);
CREATE TABLE bar (
x int64, z int64,
)
PRIMARY KEY (x, z),
INTERLEAVE IN PARENT foo;
`)
ddls, err := memefish.ParseDDLs("path/to/file.sql", sql)
if err != nil {
panic(err)
}
for _, ddl := range ddls {
fmt.Printf("%s;\n", ddl.SQL())
}
// Output:
// CREATE TABLE foo (
// x INT64,
// y INT64
// ) PRIMARY KEY (x);
// CREATE TABLE bar (
// x INT64,
// z INT64
// ) PRIMARY KEY (x, z),
// INTERLEAVE IN PARENT foo;
}
func ExampleParseDML() {
sql := heredoc.Doc(`
INSERT INTO foo (x, y)
VALUES (1, 2),
(3, 4)
`)
dml, err := memefish.ParseDML("path/to/file.sql", sql)
if err != nil {
panic(err)
}
fmt.Println(dml.SQL())
// Output:
// INSERT INTO foo (x, y) VALUES (1, 2), (3, 4)
}
func ExampleParseDMLs() {
sql := heredoc.Doc(`
INSERT INTO foo (x, y) VALUES (1, 2), (3, 4);
DELETE FROM foo WHERE foo.x = 1 AND foo.y = 2;
`)
dmls, err := memefish.ParseDMLs("path/to/file.sql", sql)
if err != nil {
panic(err)
}
for _, dml := range dmls {
fmt.Printf("%s;\n", dml.SQL())
}
// Output:
// INSERT INTO foo (x, y) VALUES (1, 2), (3, 4);
// DELETE FROM foo WHERE foo.x = 1 AND foo.y = 2;
}