This repository has been archived by the owner on Nov 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
/
parser_test.go
197 lines (174 loc) · 4.41 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
// Copyright 2014 The ql Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ql
import (
"testing"
)
func TestParser0(t *testing.T) {
table := []struct {
src string
ok bool
}{
{"", true},
{";", true},
{"CREATE", false},
{"CREATE TABLE", false},
{"CREATE TABLE foo (", false},
// 5
{"CREATE TABLE foo ()", false},
{"CREATE TABLE foo ();", false},
{"CREATE TABLE foo (a byte)", true},
{"CREATE TABLE foo (a uint8);", true},
{"CREATE TABLE foo (a uint16, b uint32)", true},
// 10
{"CREATE TABLE foo (a uint64, b bool);", true},
{"CREATE TABLE foo (a int8, b int16) CREATE TABLE bar (x int32, y int64)", false},
{"CREATE TABLE foo (a int, b float32); CREATE TABLE bar (x float64, y float)", true},
{"INSERT INTO foo VALUES (1234)", true},
{"INSERT INTO foo VALUES (1234, 5678)", true},
// 15
{"INSERT INTO foo VALUES (1 || 2)", false},
{"INSERT INTO foo VALUES (1 | 2)", true},
{"INSERT INTO foo VALUES (false || true)", true},
{"INSERT INTO foo VALUES (id())", true},
{"INSERT INTO foo VALUES (bar(5678))", false},
// 20
{"INSERT INTO foo VALUES ()", false},
{"CREATE TABLE foo (a.b, b);", false},
{"CREATE TABLE foo (a, b.c);", false},
{"SELECT * FROM t", true},
{"SELECT * FROM t AS u", true},
// 25
{"SELECT * FROM t, v", true},
{"SELECT * FROM t AS u, v", true},
{"SELECT * FROM t, v AS w", true},
{"SELECT * FROM t AS u, v AS w", true},
{"SELECT * FROM foo, bar, foo", true},
// 30
{"CREATE TABLE foo (a bytes)", false},
{"SELECT DISTINCTS * FROM t", false},
{"SELECT DISTINCT * FROM t", true},
{"INSERT INTO foo (a) VALUES (42)", true},
{"INSERT INTO foo (a,) VALUES (42,)", true},
// 35
{"INSERT INTO foo (a,b) VALUES (42,314)", true},
{"INSERT INTO foo (a,b,) VALUES (42,314)", true},
{"INSERT INTO foo (a,b,) VALUES (42,314,)", true},
{"CREATE TABLE foo (a uint16, b uint32,)", true},
{"CREATE TABLE foo (a uint16, b uint32,) -- foo", true},
// 40
{"CREATE TABLE foo (a uint16, b uint32,) // foo", true},
{"CREATE TABLE foo (a uint16, b uint32,) /* foo */", true},
{"CREATE TABLE foo /* foo */ (a uint16, b uint32,) /* foo */", true},
{`-- Examples
ALTER TABLE Stock ADD Qty int;
ALTER TABLE Income DROP COLUMN Taxes;
CREATE TABLE department
(
DepartmentID int,
DepartmentName string, // optional comma
);
CREATE TABLE employee
(
LastName string,
DepartmentID int // optional comma
);
DROP TABLE Inventory;
INSERT INTO department (DepartmentID) VALUES (42);
INSERT INTO department (
DepartmentName,
DepartmentID,
)
VALUES (
"R&D",
42,
);
INSERT INTO department VALUES (
42,
"R&D",
);
SELECT * FROM Stock;
SELECT DepartmentID
FROM department
WHERE DepartmentID == 42
ORDER BY DepartmentName;
SELECT employee.LastName
FROM department, employee
WHERE department.DepartmentID == employee.DepartmentID
ORDER BY DepartmentID;
SELECT a.b, c.d
FROM
x AS a,
(
SELECT * FROM y; // optional semicolon
) AS c
WHERE a.e > c.e;
SELECT a.b, c.d
FROM
x AS a,
(
SELECT * FROM y // no semicolon
) AS c
WHERE a.e > c.e;
TRUNCATE TABLE department;
SELECT DepartmentID
FROM department
WHERE DepartmentID == ?1
ORDER BY DepartmentName;
SELECT employee.LastName
FROM department, employee
WHERE department.DepartmentID == $1 && employee.LastName > $2
ORDER BY DepartmentID;
`, true},
{"BEGIN TRANSACTION", true},
// 45
{"COMMIT", true},
{"ROLLBACK", true},
{`
BEGIN TRANSACTION;
INSERT INTO foo VALUES (42, 3.14);
INSERT INTO foo VALUES (-1, 2.78);
COMMIT;`, true},
{`
BEGIN TRANSACTION;
INSERT INTO AccountA (Amount) VALUES ($1);
INSERT INTO AccountB (Amount) VALUES (-$1);
COMMIT;`, true},
{` // A
BEGIN TRANSACTION;
INSERT INTO tmp SELECT * from bar;
SELECT * from tmp;
// B
ROLLBACK;`, true},
// 50
{`-- 6
ALTER TABLE none DROP COLUMN c1;
`, true},
}
for i, test := range table {
//dbg("%d ----\n%q\n----\n", i, test.src)
l, err := newLexer(test.src)
if err != nil {
t.Fatal(err)
}
ok := yyParse(l) == 0
if g, e := ok, test.ok; g != e {
if !ok {
t.Log(l.errs[0])
}
t.Error(i, test.src, g, e)
return
}
switch ok {
case true:
if len(l.errs) != 0 {
t.Fatal(l.errs)
}
case false:
if len(l.errs) == 0 {
t.Fatal(l.errs)
}
}
}
}