Debian Codegraph @

github_test.go

raw symbols SCIP
1 /*
2 Copyright 2017 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreedto in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16 package sqlparser
17
18 // Additional tests to address the GitHub issues for this fork.
19
20 import (
21 "io"
22 "strings"
23 "testing"
24 )
25
26 func TestParsing(t *testing.T) {
27 tests := []struct {
28 id int // Github issue ID
29 sql string
30 skip string
31 }{
32 {id: 9, sql: "select 1 as 测试 from dual", skip: "Broken due to ReadByte()"},
33 {id: 12, sql: "SELECT * FROM AccessToken LIMIT 10 OFFSET 13"},
34 {id: 14, sql: "SELECT DATE_SUB(NOW(), INTERVAL 1 MONTH)"},
35 {id: 15, sql: "select STRAIGHT_JOIN t1.* FROM t1 INNER JOIN t2 ON t1.CommonID = t2.CommonID WHERE t1.FilterID = 1"},
36 {id: 16, sql: "SELECT a FROM t WHERE FUNC(a) = 1"}, // Doesn't seem broken, need better example
37 {id: 21, sql: `CREATE TABLE t (UpdateDatetime TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP)`},
38 {id: 21, sql: `CREATE TABLE t (UpdateDatetime TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间')`},
39 {id: 24, sql: `select * from t1 join t2 using(id)`},
40 }
41
42 for _, test := range tests {
43 if test.skip != "" {
44 continue
45 }
46
47 if _, err := Parse(test.sql); err != nil {
48 t.Errorf("https://github.com/xwb1989/sqlparser/issues/%d:\nParse(%q) err = %s, want nil", test.id, test.sql, err)
49 }
50 }
51 }
52
53 // ExampleParse is the first example from the README.md.
54 func ExampleParse() {
55 sql := "SELECT * FROM table WHERE a = 'abc'"
56 stmt, err := Parse(sql)
57 if err != nil {
58 // Do something with the err
59 }
60
61 // Otherwise do something with stmt
62 switch stmt := stmt.(type) {
63 case *Select:
64 _ = stmt
65 case *Insert:
66 }
67 }
68
69 // ExampleParseNext is the second example from the README.md.
70 func ExampleParseNext() {
71 r := strings.NewReader("INSERT INTO table1 VALUES (1, 'a'); INSERT INTO table2 VALUES (3, 4);")
72
73 tokens := NewTokenizer(r)
74 for {
75 stmt, err := ParseNext(tokens)
76 if err == io.EOF {
77 break
78 }
79 // Do something with stmt or err.
80 _ = stmt
81 }
82 }