FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
sqlparser/ast_test.go at master · CovenantSQL/sqlparser · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
CovenantSQL
/
sqlparser
Public
forked from
xwb1989/sqlparser
Notifications
You must be signed in to change notification settings
Fork
2
Star
18
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
sqlparser
/
ast_test.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
597 lines (565 loc) · 14.6 KB
Breadcrumbs
sqlparser
/
ast_test.go
Copy path
File metadata and controls
597 lines (565 loc) · 14.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*
Copyright 2017 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
sqlparser
import
(
"bytes"
"encoding/json"
"reflect"
"strings"
"testing"
"unsafe"
"github.com/CovenantSQL/sqlparser/dependency/sqltypes"
)
func
TestAppend
(
t
*
testing.
T
) {
query
:=
"select * from t where a = 1"
tree
,
err
:=
Parse
(
query
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
var
b
bytes.
Buffer
Append
(
&
b
,
tree
)
got
:=
b
.
String
()
want
:=
query
if
got
!=
want
{
t
.
Errorf
(
"Append: %s, want %s"
,
got
,
want
)
}
Append
(
&
b
,
tree
)
got
=
b
.
String
()
want
=
query
+
query
if
got
!=
want
{
t
.
Errorf
(
"Append: %s, want %s"
,
got
,
want
)
}
}
func
TestSelect
(
t
*
testing.
T
) {
tree
,
err
:=
Parse
(
"select * from t where a = 1"
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
expr
:=
tree
.(
*
Select
).
Where
.
Expr
sel
:=
&
Select
{}
sel
.
AddWhere
(
expr
)
buf
:=
NewTrackedBuffer
(
nil
)
sel
.
Where
.
Format
(
buf
)
want
:=
" where a = 1"
if
buf
.
String
()
!=
want
{
t
.
Errorf
(
"where: %q, want %s"
,
buf
.
String
(),
want
)
}
sel
.
AddWhere
(
expr
)
buf
=
NewTrackedBuffer
(
nil
)
sel
.
Where
.
Format
(
buf
)
want
=
" where a = 1 and a = 1"
if
buf
.
String
()
!=
want
{
t
.
Errorf
(
"where: %q, want %s"
,
buf
.
String
(),
want
)
}
sel
=
&
Select
{}
sel
.
AddHaving
(
expr
)
buf
=
NewTrackedBuffer
(
nil
)
sel
.
Having
.
Format
(
buf
)
want
=
" having a = 1"
if
buf
.
String
()
!=
want
{
t
.
Errorf
(
"having: %q, want %s"
,
buf
.
String
(),
want
)
}
sel
.
AddHaving
(
expr
)
buf
=
NewTrackedBuffer
(
nil
)
sel
.
Having
.
Format
(
buf
)
want
=
" having a = 1 and a = 1"
if
buf
.
String
()
!=
want
{
t
.
Errorf
(
"having: %q, want %s"
,
buf
.
String
(),
want
)
}
// OR clauses must be parenthesized.
tree
,
err
=
Parse
(
"select * from t where a = 1 or b = 1"
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
expr
=
tree
.(
*
Select
).
Where
.
Expr
sel
=
&
Select
{}
sel
.
AddWhere
(
expr
)
buf
=
NewTrackedBuffer
(
nil
)
sel
.
Where
.
Format
(
buf
)
want
=
" where (a = 1 or b = 1)"
if
buf
.
String
()
!=
want
{
t
.
Errorf
(
"where: %q, want %s"
,
buf
.
String
(),
want
)
}
sel
=
&
Select
{}
sel
.
AddHaving
(
expr
)
buf
=
NewTrackedBuffer
(
nil
)
sel
.
Having
.
Format
(
buf
)
want
=
" having (a = 1 or b = 1)"
if
buf
.
String
()
!=
want
{
t
.
Errorf
(
"having: %q, want %s"
,
buf
.
String
(),
want
)
}
}
func
TestAddOrder
(
t
*
testing.
T
) {
src
,
err
:=
Parse
(
"select foo, bar from baz order by foo"
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
order
:=
src
.(
*
Select
).
OrderBy
[
0
]
dst
,
err
:=
Parse
(
"select * from t"
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
dst
.(
*
Select
).
AddOrder
(
order
)
buf
:=
NewTrackedBuffer
(
nil
)
dst
.
Format
(
buf
)
want
:=
"select * from t order by foo asc"
if
buf
.
String
()
!=
want
{
t
.
Errorf
(
"order: %q, want %s"
,
buf
.
String
(),
want
)
}
dst
,
err
=
Parse
(
"select * from t union select * from s"
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
dst
.(
*
Union
).
AddOrder
(
order
)
buf
=
NewTrackedBuffer
(
nil
)
dst
.
Format
(
buf
)
want
=
"select * from t union select * from s order by foo asc"
if
buf
.
String
()
!=
want
{
t
.
Errorf
(
"order: %q, want %s"
,
buf
.
String
(),
want
)
}
}
func
TestSetLimit
(
t
*
testing.
T
) {
src
,
err
:=
Parse
(
"select foo, bar from baz limit 4"
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
limit
:=
src
.(
*
Select
).
Limit
dst
,
err
:=
Parse
(
"select * from t"
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
dst
.(
*
Select
).
SetLimit
(
limit
)
buf
:=
NewTrackedBuffer
(
nil
)
dst
.
Format
(
buf
)
want
:=
"select * from t limit 4"
if
buf
.
String
()
!=
want
{
t
.
Errorf
(
"limit: %q, want %s"
,
buf
.
String
(),
want
)
}
dst
,
err
=
Parse
(
"select * from t union select * from s"
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
dst
.(
*
Union
).
SetLimit
(
limit
)
buf
=
NewTrackedBuffer
(
nil
)
dst
.
Format
(
buf
)
want
=
"select * from t union select * from s limit 4"
if
buf
.
String
()
!=
want
{
t
.
Errorf
(
"order: %q, want %s"
,
buf
.
String
(),
want
)
}
}
func
TestWhere
(
t
*
testing.
T
) {
var
w
*
Where
buf
:=
NewTrackedBuffer
(
nil
)
w
.
Format
(
buf
)
if
buf
.
String
()
!=
""
{
t
.
Errorf
(
"w.Format(nil): %q, want
\"
\"
"
,
buf
.
String
())
}
w
=
NewWhere
(
WhereStr
,
nil
)
buf
=
NewTrackedBuffer
(
nil
)
w
.
Format
(
buf
)
if
buf
.
String
()
!=
""
{
t
.
Errorf
(
"w.Format(&Where{nil}: %q, want
\"
\"
"
,
buf
.
String
())
}
}
func
TestIsAggregate
(
t
*
testing.
T
) {
f
:=
FuncExpr
{
Name
:
NewColIdent
(
"avg"
)}
if
!
f
.
IsAggregate
() {
t
.
Error
(
"IsAggregate: false, want true"
)
}
f
=
FuncExpr
{
Name
:
NewColIdent
(
"Avg"
)}
if
!
f
.
IsAggregate
() {
t
.
Error
(
"IsAggregate: false, want true"
)
}
f
=
FuncExpr
{
Name
:
NewColIdent
(
"foo"
)}
if
f
.
IsAggregate
() {
t
.
Error
(
"IsAggregate: true, want false"
)
}
}
func
TestReplaceExpr
(
t
*
testing.
T
) {
tcases
:=
[]
struct
{
in
,
out
string
}{{
in
:
"select * from t where (select a from b)"
,
out
:
":a"
,
}, {
in
:
"select * from t where (select a from b) and b"
,
out
:
":a and b"
,
}, {
in
:
"select * from t where a and (select a from b)"
,
out
:
"a and :a"
,
}, {
in
:
"select * from t where (select a from b) or b"
,
out
:
":a or b"
,
}, {
in
:
"select * from t where a or (select a from b)"
,
out
:
"a or :a"
,
}, {
in
:
"select * from t where not (select a from b)"
,
out
:
"not :a"
,
}, {
in
:
"select * from t where ((select a from b))"
,
out
:
"(:a)"
,
}, {
in
:
"select * from t where (select a from b) = 1"
,
out
:
":a = 1"
,
}, {
in
:
"select * from t where a = (select a from b)"
,
out
:
"a = :a"
,
}, {
in
:
"select * from t where a like b escape (select a from b)"
,
out
:
"a like b escape :a"
,
}, {
in
:
"select * from t where (select a from b) between a and b"
,
out
:
":a between a and b"
,
}, {
in
:
"select * from t where a between (select a from b) and b"
,
out
:
"a between :a and b"
,
}, {
in
:
"select * from t where a between b and (select a from b)"
,
out
:
"a between b and :a"
,
}, {
in
:
"select * from t where (select a from b) is null"
,
out
:
":a is null"
,
}, {
// exists should not replace.
in
:
"select * from t where exists (select a from b)"
,
out
:
"exists (select a from b)"
,
}, {
in
:
"select * from t where a in ((select a from b), 1)"
,
out
:
"a in (:a, 1)"
,
}, {
in
:
"select * from t where a in (0, (select a from b), 1)"
,
out
:
"a in (0, :a, 1)"
,
}, {
in
:
"select * from t where (select a from b) + 1"
,
out
:
":a + 1"
,
}, {
in
:
"select * from t where 1+(select a from b)"
,
out
:
"1 + :a"
,
}, {
in
:
"select * from t where -(select a from b)"
,
out
:
"-:a"
,
}, {
in
:
"select * from t where interval (select a from b) aa"
,
out
:
"interval :a aa"
,
}, {
in
:
"select * from t where func((select a from b), 1)"
,
out
:
"func(:a, 1)"
,
}, {
in
:
"select * from t where func(1, (select a from b), 1)"
,
out
:
"func(1, :a, 1)"
,
}, {
in
:
"select * from t where group_concat((select a from b), 1 order by a)"
,
out
:
"group_concat(:a, 1 order by a asc)"
,
}, {
in
:
"select * from t where group_concat(1 order by (select a from b), a)"
,
out
:
"group_concat(1 order by :a asc, a asc)"
,
}, {
in
:
"select * from t where group_concat(1 order by a, (select a from b))"
,
out
:
"group_concat(1 order by a asc, :a asc)"
,
}, {
in
:
"select * from t where substr(a, (select a from b), b)"
,
out
:
"substr(a, :a, b)"
,
}, {
in
:
"select * from t where substr(a, b, (select a from b))"
,
out
:
"substr(a, b, :a)"
,
}, {
in
:
"select * from t where case (select a from b) when a then b when b then c else d end"
,
out
:
"case :a when a then b when b then c else d end"
,
}, {
in
:
"select * from t where case a when (select a from b) then b when b then c else d end"
,
out
:
"case a when :a then b when b then c else d end"
,
}, {
in
:
"select * from t where case a when b then (select a from b) when b then c else d end"
,
out
:
"case a when b then :a when b then c else d end"
,
}, {
in
:
"select * from t where case a when b then c when (select a from b) then c else d end"
,
out
:
"case a when b then c when :a then c else d end"
,
}, {
in
:
"select * from t where case a when b then c when d then c else (select a from b) end"
,
out
:
"case a when b then c when d then c else :a end"
,
}}
to
:=
NewValArg
([]
byte
(
":a"
))
for
_
,
tcase
:=
range
tcases
{
tree
,
err
:=
Parse
(
tcase
.
in
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
var
from
*
Subquery
_
=
Walk
(
func
(
node
SQLNode
) (
kontinue
bool
,
err
error
) {
if
sq
,
ok
:=
node
.(
*
Subquery
);
ok
{
from
=
sq
return
false
,
nil
}
return
true
,
nil
},
tree
)
if
from
==
nil
{
t
.
Fatalf
(
"from is nil for %s"
,
tcase
.
in
)
}
expr
:=
ReplaceExpr
(
tree
.(
*
Select
).
Where
.
Expr
,
from
,
to
)
got
:=
String
(
expr
)
if
tcase
.
out
!=
got
{
t
.
Errorf
(
"ReplaceExpr(%s): %s, want %s"
,
tcase
.
in
,
got
,
tcase
.
out
)
}
}
}
func
TestExprFromValue
(
t
*
testing.
T
) {
tcases
:=
[]
struct
{
in
sqltypes.
Value
out
SQLNode
err
string
}{{
in
:
sqltypes
.
NULL
,
out
:
&
NullVal
{},
}, {
in
:
sqltypes
.
NewInt64
(
1
),
out
:
NewIntVal
([]
byte
(
"1"
)),
}, {
in
:
sqltypes
.
NewFloat64
(
1.1
),
out
:
NewFloatVal
([]
byte
(
"1.1"
)),
}, {
in
:
sqltypes
.
MakeTrusted
(
sqltypes
.
Decimal
, []
byte
(
"1.1"
)),
out
:
NewFloatVal
([]
byte
(
"1.1"
)),
}, {
in
:
sqltypes
.
NewVarChar
(
"aa"
),
out
:
NewStrVal
([]
byte
(
"aa"
)),
}, {
in
:
sqltypes
.
MakeTrusted
(
sqltypes
.
Expression
, []
byte
(
"rand()"
)),
err
:
"cannot convert value EXPRESSION(rand()) to AST"
,
}}
for
_
,
tcase
:=
range
tcases
{
got
,
err
:=
ExprFromValue
(
tcase
.
in
)
if
tcase
.
err
!=
""
{
if
err
==
nil
||
err
.
Error
()
!=
tcase
.
err
{
t
.
Errorf
(
"ExprFromValue(%v) err: %v, want %s"
,
tcase
.
in
,
err
,
tcase
.
err
)
}
continue
}
if
err
!=
nil
{
t
.
Error
(
err
)
}
if
got
,
want
:=
got
,
tcase
.
out
;
!
reflect
.
DeepEqual
(
got
,
want
) {
t
.
Errorf
(
"ExprFromValue(%v): %v, want %s"
,
tcase
.
in
,
got
,
want
)
}
}
}
func
TestColNameEqual
(
t
*
testing.
T
) {
var
c1
,
c2
*
ColName
if
c1
.
Equal
(
c2
) {
t
.
Error
(
"nil columns equal, want unequal"
)
}
c1
=
&
ColName
{
Name
:
NewColIdent
(
"aa"
),
}
c2
=
&
ColName
{
Name
:
NewColIdent
(
"bb"
),
}
if
c1
.
Equal
(
c2
) {
t
.
Error
(
"columns equal, want unequal"
)
}
c2
.
Name
=
NewColIdent
(
"aa"
)
if
!
c1
.
Equal
(
c2
) {
t
.
Error
(
"columns unequal, want equal"
)
}
}
func
TestColIdent
(
t
*
testing.
T
) {
str
:=
NewColIdent
(
"Ab"
)
if
str
.
String
()
!=
"Ab"
{
t
.
Errorf
(
"String=%s, want Ab"
,
str
.
String
())
}
if
str
.
String
()
!=
"Ab"
{
t
.
Errorf
(
"Val=%s, want Ab"
,
str
.
String
())
}
if
str
.
Lowered
()
!=
"ab"
{
t
.
Errorf
(
"Val=%s, want ab"
,
str
.
Lowered
())
}
if
!
str
.
Equal
(
NewColIdent
(
"aB"
)) {
t
.
Error
(
"str.Equal(NewColIdent(aB))=false, want true"
)
}
if
!
str
.
EqualString
(
"ab"
) {
t
.
Error
(
"str.EqualString(ab)=false, want true"
)
}
str
=
NewColIdent
(
""
)
if
str
.
Lowered
()
!=
""
{
t
.
Errorf
(
"Val=%s, want
\"
\"
"
,
str
.
Lowered
())
}
}
func
TestColIdentMarshal
(
t
*
testing.
T
) {
str
:=
NewColIdent
(
"Ab"
)
b
,
err
:=
json
.
Marshal
(
str
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
got
:=
string
(
b
)
want
:=
`"Ab"`
if
got
!=
want
{
t
.
Errorf
(
"json.Marshal()= %s, want %s"
,
got
,
want
)
}
var
out
ColIdent
if
err
:=
json
.
Unmarshal
(
b
,
&
out
);
err
!=
nil
{
t
.
Errorf
(
"Unmarshal err: %v, want nil"
,
err
)
}
if
!
reflect
.
DeepEqual
(
out
,
str
) {
t
.
Errorf
(
"Unmarshal: %v, want %v"
,
out
,
str
)
}
}
func
TestColIdentSize
(
t
*
testing.
T
) {
size
:=
unsafe
.
Sizeof
(
NewColIdent
(
""
))
want
:=
2
*
unsafe
.
Sizeof
(
""
)
if
size
!=
want
{
t
.
Errorf
(
"Size of ColIdent: %d, want 32"
,
want
)
}
}
func
TestTableIdentMarshal
(
t
*
testing.
T
) {
str
:=
NewTableIdent
(
"Ab"
)
b
,
err
:=
json
.
Marshal
(
str
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
got
:=
string
(
b
)
want
:=
`"Ab"`
if
got
!=
want
{
t
.
Errorf
(
"json.Marshal()= %s, want %s"
,
got
,
want
)
}
var
out
TableIdent
if
err
:=
json
.
Unmarshal
(
b
,
&
out
);
err
!=
nil
{
t
.
Errorf
(
"Unmarshal err: %v, want nil"
,
err
)
}
if
!
reflect
.
DeepEqual
(
out
,
str
) {
t
.
Errorf
(
"Unmarshal: %v, want %v"
,
out
,
str
)
}
}
func
TestHexDecode
(
t
*
testing.
T
) {
testcase
:=
[]
struct
{
in
,
out
string
}{{
in
:
"313233"
,
out
:
"123"
,
}, {
in
:
"ag"
,
out
:
"encoding/hex: invalid byte: U+0067 'g'"
,
}, {
in
:
"777"
,
out
:
"encoding/hex: odd length hex string"
,
}}
for
_
,
tc
:=
range
testcase
{
out
,
err
:=
newHexVal
(
tc
.
in
).
HexDecode
()
if
err
!=
nil
{
if
err
.
Error
()
!=
tc
.
out
{
t
.
Errorf
(
"Decode(%q): %v, want %s"
,
tc
.
in
,
err
,
tc
.
out
)
}
continue
}
if
!
bytes
.
Equal
(
out
, []
byte
(
tc
.
out
)) {
t
.
Errorf
(
"Decode(%q): %s, want %s"
,
tc
.
in
,
out
,
tc
.
out
)
}
}
}
func
TestCompliantName
(
t
*
testing.
T
) {
testcases
:=
[]
struct
{
in
,
out
string
}{{
in
:
"aa"
,
out
:
"aa"
,
}, {
in
:
"1a"
,
out
:
"_a"
,
}, {
in
:
"a1"
,
out
:
"a1"
,
}, {
in
:
"a.b"
,
out
:
"a_b"
,
}, {
in
:
".ab"
,
out
:
"_ab"
,
}}
for
_
,
tc
:=
range
testcases
{
out
:=
NewColIdent
(
tc
.
in
).
CompliantName
()
if
out
!=
tc
.
out
{
t
.
Errorf
(
"ColIdent(%s).CompliantNamt: %s, want %s"
,
tc
.
in
,
out
,
tc
.
out
)
}
out
=
NewTableIdent
(
tc
.
in
).
CompliantName
()
if
out
!=
tc
.
out
{
t
.
Errorf
(
"TableIdent(%s).CompliantNamt: %s, want %s"
,
tc
.
in
,
out
,
tc
.
out
)
}
}
}
func
TestColumns_FindColumn
(
t
*
testing.
T
) {
cols
:=
Columns
{
NewColIdent
(
"a"
),
NewColIdent
(
"c"
),
NewColIdent
(
"b"
),
NewColIdent
(
"0"
)}
testcases
:=
[]
struct
{
in
string
out
int
}{{
in
:
"a"
,
out
:
0
,
}, {
in
:
"b"
,
out
:
2
,
},
{
in
:
"0"
,
out
:
3
,
},
{
in
:
"f"
,
out
:
-
1
,
}}
for
_
,
tc
:=
range
testcases
{
val
:=
cols
.
FindColumn
(
NewColIdent
(
tc
.
in
))
if
val
!=
tc
.
out
{
t
.
Errorf
(
"FindColumn(%s): %d, want %d"
,
tc
.
in
,
val
,
tc
.
out
)
}
}
}
func
TestSplitStatementToPieces
(
t
*
testing.
T
) {
testcases
:=
[]
struct
{
input
string
output
string
}{{
input
:
"select * from table"
,
}, {
input
:
"select * from table1; select * from table2;"
,
output
:
"select * from table1; select * from table2"
,
}, {
input
:
"select * from /* comment ; */ table;"
,
output
:
"select * from /* comment ; */ table"
,
}, {
input
:
"select * from table where semi = ';';"
,
output
:
"select * from table where semi = ';'"
,
}, {
input
:
"select * from table1;--comment;
\n
select * from table2;"
,
output
:
"select * from table1;--comment;
\n
select * from table2"
,
}, {
input
:
"CREATE TABLE `total_data` (`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id', "
+
"`region` varchar(32) NOT NULL COMMENT 'region name, like zh; th; kepler',"
+
"`data_size` bigint NOT NULL DEFAULT '0' COMMENT 'data size;',"
+
"`createtime` datetime NOT NULL DEFAULT NOW() COMMENT 'create time;',"
+
"`comment` varchar(100) NOT NULL DEFAULT '' COMMENT 'comment',"
+
"PRIMARY KEY (`id`))"
,
}}
for
_
,
tcase
:=
range
testcases
{
if
tcase
.
output
==
""
{
tcase
.
output
=
tcase
.
input
}
stmtPieces
,
err
:=
SplitStatementToPieces
(
tcase
.
input
)
if
err
!=
nil
{
t
.
Errorf
(
"input: %s, err: %v"
,
tcase
.
input
,
err
)
continue
}
out
:=
strings
.
Join
(
stmtPieces
,
";"
)
if
out
!=
tcase
.
output
{
t
.
Errorf
(
"out: %s, want %s"
,
out
,
tcase
.
output
)
}
}
}
Back
|
FazBrowse Home
|
New Git URL