-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquery_string_test.go
151 lines (142 loc) · 3.18 KB
/
query_string_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
package sqlstr_test
import (
"testing"
"github.com/imantung/sqlstr"
"github.com/stretchr/testify/require"
)
func TestQueryString_After(t *testing.T) {
testcase := []struct {
query string
word string
atAfter string
}{
{
"SELECT * FROM table",
"FROM",
"table",
},
{
"SELECT * FROM table",
"FROM",
"table",
},
{
"select * from table;",
"FROM",
"table",
},
{
"SELECT * FROM table WHERE true;",
"FROM",
"table",
},
{
"SELECT * FROM table1 WHERE true;",
"FROM",
"table1",
},
{
"select * from table1, table2, table3;",
"FROM",
"table1",
},
}
for _, tt := range testcase {
queryString := sqlstr.NewQueryString(tt.query)
require.Equal(t, tt.atAfter, queryString.After(tt.word))
}
}
func TestQueryString_AfterAll(t *testing.T) {
testcase := []struct {
query string
word string
atAfters []string
}{
{
`SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;`,
"FROM",
[]string{"table1", "table2"},
},
}
for _, tt := range testcase {
queryString := sqlstr.NewQueryString(tt.query)
require.Equal(t, tt.atAfters, queryString.AfterAll(tt.word))
}
}
func TestQueryString_TableNames(t *testing.T) {
testcase := []struct {
query string
tableNames []string
}{
{
"SELECT * FROM `table1`, `table2`, `table3` WHERE true;",
[]string{"table1", "table2", "table3"},
},
{
"SELECT * FROM table1, table2, table3 WHERE true;",
[]string{"table1", "table2", "table3"},
},
{
`SELECT * FROM table1, table2, table3 WHERE true
UNION
SELECT * FROM table4, table5, table6 WHERE true`,
[]string{"table1", "table2", "table3", "table4", "table5", "table6"},
},
{
`SELECT * FROM table1, table2, table3;`,
[]string{"table1", "table2", "table3"},
},
{
`SELECT * FROM table1, table2, table3`,
[]string{"table1", "table2", "table3"},
},
{
`SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;`,
[]string{"table1", "table2"},
},
{
`SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;`,
[]string{"table1", "table2"},
},
{
`UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;`,
[]string{"Customers"},
},
{
`DELETE FROM Customers;`,
[]string{"Customers"},
},
{
`INSERT INTO Customers(CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');`,
[]string{"Customers"},
},
{
`SELECT * FROM table1
RIGHT JOIN table2 ON table1.id = table2.id
LEFT JOIN table3 ON table1.id = table3.id
INNER JOIN table4 ON table1.id = table4.id
OUTER JOIN table5 ON table1.id = table5.id
WHERE true`,
[]string{"table1", "table2", "table3", "table4", "table5"},
},
{
"SELECT * FROM table1 t1, table2 AS t2, table3 as t3 JOIN table4 as t4 on t1.id = t4.id;",
[]string{"table1", "table2", "table3", "table4"},
},
}
for _, tt := range testcase {
queryString := sqlstr.NewQueryString(tt.query)
require.Equal(t, tt.tableNames, queryString.TableNames())
}
}