-
Notifications
You must be signed in to change notification settings - Fork 5
/
parser_test.go
240 lines (231 loc) · 6.34 KB
/
parser_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
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
package gosqlparser_test
import (
"fmt"
"reflect"
"testing"
"encoding/json"
sql "github.com/krasun/gosqlparser"
)
func Example() {
query, err := sql.Parse("SELECT col1, col2 FROM table1 WHERE col1 == \"abc\" AND col3 == 5 LIMIT 10")
if err != nil {
fmt.Printf("unexpected error: %s", err)
return
}
json, err := json.Marshal(query)
if err != nil {
fmt.Printf("unexpected error: %s", err)
return
}
fmt.Println(string(json))
// Output:
// {"Table":"table1","Columns":["col1","col2"],"Where":{"Expr":{"Left":{"Left":{"Name":"col1"},"Operator":0,"Right":{"Value":"\"abc\""}},"Operator":1,"Right":{"Left":{"Name":"col3"},"Operator":0,"Right":{"Value":"5"}}}},"Limit":"10"}
}
func TestParser(t *testing.T) {
testCases := []struct {
name string
input string
expectedStatement sql.Statement
err error
}{
{
"broken statement",
"table1",
nil,
fmt.Errorf("expected SELECT, INSERT, UPDATE, DELETE, CREATE or DROP, but got identifier: table1"),
},
{
"broken statement",
"`",
nil,
fmt.Errorf("unexpected rune"),
},
{
"unfinished SELECT statement",
"SELECT table1",
nil,
fmt.Errorf("expected FROM, delimeter, but got end: \"\""),
},
{
"unfinished SELECT FROM statement",
"SELECT col1 FROM",
nil,
fmt.Errorf("expected identifier, but got end: \"\""),
},
{
"unfinished SELECT FROM WHERE statement",
"SELECT col FROM table1 WHERE",
nil,
fmt.Errorf("expected identifier, integer, string, but got end: \"\""),
},
{
"unfinished WHERE statement",
"SELECT col FROM table1 WHERE a ==",
nil,
fmt.Errorf("expected identifier, integer, string, but got end: \"\""),
},
{
"unfinished WHERE statement",
"SELECT col FROM table1 WHERE a",
nil,
fmt.Errorf("expected equals, but got end: \"\""),
},
{
"unfinished WHERE statement",
"SELECT col FROM table1 LIMIT",
nil,
fmt.Errorf("expected integer, but got end: \"\""),
},
{
"broken SELECT statement",
"SELECT col, FROM table1 LIMIT",
nil,
fmt.Errorf("expected identifier, but got FROM: \"FROM\""),
},
{
"unfinished DELETE FROM statement",
"DELETE FROM",
nil,
fmt.Errorf("expected identifier, but got end: \"\""),
},
{
"full CREATE TABLE query",
"CREATE TABLE table1 (col1 INTEGER, col2 STRING)",
&sql.CreateTable{"table1", []sql.ColumnDefinition{{"col1", sql.TypeInteger}, {"col2", sql.TypeString}}, sql.EngineDefault},
nil,
},
{
"full CREATE TABLE query with LSM engine",
"CREATE TABLE table1 (col1 INTEGER, col2 STRING) ENGINE=LSM",
&sql.CreateTable{"table1", []sql.ColumnDefinition{{"col1", sql.TypeInteger}, {"col2", sql.TypeString}}, sql.EngineLSM},
nil,
},
{
"full CREATE TABLE query with B+ tree engine",
"CREATE TABLE table1 (col1 INTEGER, col2 STRING) ENGINE=BPTREE",
&sql.CreateTable{"table1", []sql.ColumnDefinition{{"col1", sql.TypeInteger}, {"col2", sql.TypeString}}, sql.EngineBPTree},
nil,
},
{
"full DROP TABLE query",
"DROP TABLE table1",
&sql.DropTable{"table1"},
nil,
},
{
"broken DROP TABLE",
"DROP table1",
nil,
fmt.Errorf("expected TABLE, but got identifier: \"table1\""),
},
{
"simple SELECT FROM",
"SELECT col1, col2 FROM table1",
&sql.Select{"table1", []string{"col1", "col2"}, nil, ""},
nil,
},
{
"simple DELETE FROM",
"DELETE FROM table1",
&sql.Delete{"table1", nil},
nil,
},
{
"DELETE FROM WHERE",
"DELETE FROM table1 WHERE col1 == col2",
&sql.Delete{"table1", &sql.Where{sql.ExprOperation{sql.ExprIdentifier{"col1"}, sql.OperatorEquals, sql.ExprIdentifier{"col2"}}}},
nil,
},
{
"simple INSERT INTO",
"INSERT INTO table1 (col1, col2) VALUES (\"val1\", 25)",
&sql.Insert{"table1", []string{"col1", "col2"}, []string{"\"val1\"", "25"}},
nil,
},
{
"simple UPDATE",
"UPDATE table1 SET col1 = \"val1\", col2 = 2",
&sql.Update{"table1", []string{"col1", "col2"}, []string{"\"val1\"", "2"}, nil},
nil,
},
{
"simple UPDATE",
"UPDATE table1 SET col1 = \"val1\", col2 = 2 WHERE col1 == col2",
&sql.Update{"table1", []string{"col1", "col2"}, []string{"\"val1\"", "2"}, &sql.Where{sql.ExprOperation{sql.ExprIdentifier{"col1"}, sql.OperatorEquals, sql.ExprIdentifier{"col2"}}}},
nil,
},
{
"SELECT FROM with LIMIT",
"SELECT col1, col2 FROM table1 LIMIT 10",
&sql.Select{"table1", []string{"col1", "col2"}, nil, "10"},
nil,
},
{
"SELECT FROM with simple WHERE",
"SELECT col1, col2 FROM table1 WHERE col1 == col2",
&sql.Select{"table1", []string{"col1", "col2"}, &sql.Where{sql.ExprOperation{sql.ExprIdentifier{"col1"}, sql.OperatorEquals, sql.ExprIdentifier{"col2"}}}, ""},
nil,
},
{
"SELECT FROM with WHERE AND LIMIT",
"SELECT col1, col2 FROM table1 WHERE col1 == col2 AND col3 == col4 LIMIT 10",
&sql.Select{
"table1",
[]string{"col1", "col2"},
&sql.Where{
sql.ExprOperation{
sql.ExprOperation{
sql.ExprIdentifier{"col1"}, sql.OperatorEquals, sql.ExprIdentifier{"col2"},
},
sql.OperatorLogicalAnd,
sql.ExprOperation{
sql.ExprIdentifier{"col3"}, sql.OperatorEquals, sql.ExprIdentifier{"col4"},
},
},
},
"10",
},
nil,
},
{
"SELECT FROM with WHERE AND VALUES",
"SELECT col1, col2 FROM table1 WHERE col1 == \"abc\" AND col3 == 5 LIMIT 10",
&sql.Select{
"table1",
[]string{"col1", "col2"},
&sql.Where{
sql.ExprOperation{
sql.ExprOperation{
sql.ExprIdentifier{"col1"}, sql.OperatorEquals, sql.ExprValueString{"\"abc\""},
},
sql.OperatorLogicalAnd,
sql.ExprOperation{
sql.ExprIdentifier{"col3"}, sql.OperatorEquals, sql.ExprValueInteger{"5"},
},
},
},
"10",
},
nil,
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
statement, err := sql.Parse(testCase.input)
if testCase.err != nil {
if err == nil {
t.Errorf("expected error \"%s\", but got nil", testCase.err)
} else if testCase.err.Error() != err.Error() {
t.Errorf("expected error \"%s\", but got \"%s\"", testCase.err, err)
}
} else {
if err != nil {
t.Errorf("unexpected error \"%s\"", err)
}
}
if testCase.expectedStatement != nil && !reflect.DeepEqual(testCase.expectedStatement, statement) {
t.Errorf("expected %v, but got %v", testCase.expectedStatement, statement)
}
})
}
}