-
Notifications
You must be signed in to change notification settings - Fork 14
/
stmt_test.go
307 lines (268 loc) · 9.19 KB
/
stmt_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
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
package sqlf_test
import (
"fmt"
"testing"
"time"
"github.com/leporo/sqlf"
"github.com/stretchr/testify/require"
)
func TestNewBuilder(t *testing.T) {
sqlf.SetDialect(sqlf.NoDialect)
q := sqlf.New("SELECT *").From("table")
defer q.Close()
sql := q.String()
args := q.Args()
require.Equal(t, "SELECT * FROM table", sql)
require.Empty(t, args)
}
func TestBasicSelect(t *testing.T) {
q := sqlf.From("table").Select("id").Where("id > ?", 42).Where("id < ?", 1000)
defer q.Close()
sql, args := q.String(), q.Args()
require.Equal(t, "SELECT id FROM table WHERE id > ? AND id < ?", sql)
require.Equal(t, []interface{}{42, 1000}, args)
}
func TestMixedOrder(t *testing.T) {
q := sqlf.Select("id").Where("id > ?", 42).From("table").Where("id < ?", 1000)
defer q.Close()
sql, args := q.String(), q.Args()
require.Equal(t, "SELECT id FROM table WHERE id > ? AND id < ?", sql)
require.Equal(t, []interface{}{42, 1000}, args)
}
func TestClause(t *testing.T) {
q := sqlf.Select("id").From("table").Where("id > ?", 42).Clause("FETCH NEXT").Clause("FOR UPDATE")
defer q.Close()
sql, args := q.String(), q.Args()
require.Equal(t, "SELECT id FROM table WHERE id > ? FETCH NEXT FOR UPDATE", sql)
require.Equal(t, []interface{}{42}, args)
}
func TestExpr(t *testing.T) {
q := sqlf.From("table").
Select("id").
Expr("(select 1 from related where table_id = table.id limit 1) AS has_related").
Where("id > ?", 42)
require.Equal(t, "SELECT id, (select 1 from related where table_id = table.id limit 1) AS has_related FROM table WHERE id > ?", q.String())
require.Equal(t, []interface{}{42}, q.Args())
q.Close()
}
func TestManyFields(t *testing.T) {
q := sqlf.Select("id").From("table").Where("id = ?", 42)
defer q.Close()
for i := 1; i <= 3; i++ {
q.Select(fmt.Sprintf("(id + ?) as id_%d", i), i*10)
}
for _, field := range []string{"uno", "dos", "tres"} {
q.Select(field)
}
sql, args := q.String(), q.Args()
require.Equal(t, "SELECT id, (id + ?) as id_1, (id + ?) as id_2, (id + ?) as id_3, uno, dos, tres FROM table WHERE id = ?", sql)
require.Equal(t, []interface{}{10, 20, 30, 42}, args)
}
func TestEvenMoreFields(t *testing.T) {
q := sqlf.Select("id").From("table")
defer q.Close()
for n := 1; n <= 50; n++ {
q.Select(fmt.Sprintf("field_%d", n))
}
sql, args := q.String(), q.Args()
require.Equal(t, 0, len(args))
for n := 1; n <= 50; n++ {
field := fmt.Sprintf(", field_%d", n)
require.Contains(t, sql, field)
}
}
func TestPgPlaceholders(t *testing.T) {
q := sqlf.PostgreSQL.From("series").
Select("id").
Where("time > ?", time.Now().Add(time.Hour*-24*14)).
Where("(time < ?)", time.Now().Add(time.Hour*-24*7))
defer q.Close()
sql, _ := q.String(), q.Args()
require.Equal(t, "SELECT id FROM series WHERE time > $1 AND (time < $2)", sql)
}
func TestPgPlaceholderEscape(t *testing.T) {
q := sqlf.PostgreSQL.From("series").
Select("id").
Where("time \\?> ? + 1", time.Now().Add(time.Hour*-24*14)).
Where("time < ?", time.Now().Add(time.Hour*-24*7))
defer q.Close()
sql, _ := q.String(), q.Args()
require.Equal(t, "SELECT id FROM series WHERE time ?> $1 + 1 AND time < $2", sql)
}
func TestTo(t *testing.T) {
var (
field1 int
field2 string
)
q := sqlf.From("table").
Select("field1").To(&field1).
Select("field2").To(&field2)
defer q.Close()
dest := q.Dest()
require.Equal(t, []interface{}{&field1, &field2}, dest)
}
func TestManyClauses(t *testing.T) {
q := sqlf.From("table").
Select("field").
Where("id > ?", 2).
Clause("UNO").
Clause("DOS").
Clause("TRES").
Clause("QUATRO").
Offset(10).
Limit(5).
Clause("NO LOCK")
defer q.Close()
sql, args := q.String(), q.Args()
require.Equal(t, "SELECT field FROM table WHERE id > ? UNO DOS TRES QUATRO LIMIT ? OFFSET ? NO LOCK", sql)
require.Equal(t, []interface{}{2, 5, 10}, args)
}
func TestWith(t *testing.T) {
var row struct {
ID int64 `db:"id"`
Quantity int64 `db:"quantity"`
}
q := sqlf.With("t",
sqlf.From("orders").
Select("id, quantity").
Where("ts < ?", time.Now())).
From("t").
Bind(&row)
defer q.Close()
require.Equal(t, "WITH t AS (SELECT id, quantity FROM orders WHERE ts < ?) SELECT id, quantity FROM t", q.String())
}
func TestWithRecursive(t *testing.T) {
q := sqlf.From("orders").
With("RECURSIVE regional_sales", sqlf.From("orders").Select("region, SUM(amount) AS total_sales").GroupBy("region")).
With("top_regions", sqlf.From("regional_sales").Select("region").OrderBy("total_sales DESC").Limit(5)).
Select("region").
Select("product").
Select("SUM(quantity) AS product_units").
Select("SUM(amount) AS product_sales").
Where("region IN (SELECT region FROM top_regions)").
GroupBy("region, product")
defer q.Close()
require.Equal(t, "WITH RECURSIVE regional_sales AS (SELECT region, SUM(amount) AS total_sales FROM orders GROUP BY region), top_regions AS (SELECT region FROM regional_sales ORDER BY total_sales DESC LIMIT ?) SELECT region, product, SUM(quantity) AS product_units, SUM(amount) AS product_sales FROM orders WHERE region IN (SELECT region FROM top_regions) GROUP BY region, product", q.String())
}
func TestSubQueryDialect(t *testing.T) {
q := sqlf.PostgreSQL.From("users u").
Select("email").
Where("registered > ?", "2019-01-01").
SubQuery("EXISTS (", ")",
sqlf.PostgreSQL.From("orders").
Select("id").
Where("user_id = u.id").
Where("amount > ?", 100))
defer q.Close()
// Parameter placeholder numbering should match the arguments
require.Equal(t, "SELECT email FROM users u WHERE registered > $1 AND EXISTS (SELECT id FROM orders WHERE user_id = u.id AND amount > $2)", q.String())
require.Equal(t, []interface{}{"2019-01-01", 100}, q.Args())
}
func TestClone(t *testing.T) {
var (
value string
value2 string
)
q := sqlf.From("table").Select("field").To(&value).Where("id = ?", 42)
defer q.Close()
require.Equal(t, "SELECT field FROM table WHERE id = ?", q.String())
q2 := q.Clone()
defer q2.Close()
require.Equal(t, q.Args(), q2.Args())
require.Equal(t, q.Dest(), q2.Dest())
require.Equal(t, q.String(), q2.String())
q2.Where("time < ?", time.Now())
require.Equal(t, q.Dest(), q2.Dest())
require.NotEqual(t, q.Args(), q2.Args())
require.NotEqual(t, q.String(), q2.String())
q2.Select("field2").To(&value2)
require.NotEqual(t, q.Dest(), q2.Dest())
require.NotEqual(t, q.Args(), q2.Args())
require.NotEqual(t, q.String(), q2.String())
// Add more clauses to original statement to re-allocate chunks array
q.With("top_users", sqlf.From("users").OrderBy("rating DESCT").Limit(10)).
GroupBy("id").
Having("field > ?", 10).
Paginate(1, 20).
Clause("FETCH ROWS ONLY").
Clause("FOR UPDATE")
q3 := q.Clone()
require.Equal(t, q.Args(), q3.Args())
require.Equal(t, q.Dest(), q3.Dest())
require.Equal(t, q.String(), q3.String())
require.NotEqual(t, q.Dest(), q2.Dest())
require.NotEqual(t, q.Args(), q2.Args())
require.NotEqual(t, q.String(), q2.String())
}
func TestJoin(t *testing.T) {
q := sqlf.From("orders o").Select("id").Join("users u", "u.id = o.user_id")
defer q.Close()
require.Equal(t, "SELECT id FROM orders o JOIN users u ON (u.id = o.user_id)", q.String())
}
func TestLeftJoin(t *testing.T) {
q := sqlf.From("orders o").Select("id").LeftJoin("users u", "u.id = o.user_id")
defer q.Close()
require.Equal(t, "SELECT id FROM orders o LEFT JOIN users u ON (u.id = o.user_id)", q.String())
}
func TestRightJoin(t *testing.T) {
q := sqlf.From("orders o").Select("id").RightJoin("users u", "u.id = o.user_id")
defer q.Close()
require.Equal(t, "SELECT id FROM orders o RIGHT JOIN users u ON (u.id = o.user_id)", q.String())
}
func TestFullJoin(t *testing.T) {
q := sqlf.From("orders o").Select("id").FullJoin("users u", "u.id = o.user_id")
defer q.Close()
require.Equal(t, "SELECT id FROM orders o FULL JOIN users u ON (u.id = o.user_id)", q.String())
}
func TestUnion(t *testing.T) {
q := sqlf.From("tasks").
Select("id, status").
Where("status = ?", "new").
Union(false, sqlf.PostgreSQL.From("tasks").
Select("id, status").
Where("status = ?", "wip"))
defer q.Close()
require.Equal(t, "SELECT id, status FROM tasks WHERE status = ? UNION SELECT id, status FROM tasks WHERE status = ?", q.String())
}
func TestLimit(t *testing.T) {
q := sqlf.From("items").
Select("id").
Where("id > ?", 42).
Limit(10).
Limit(11).
Limit(20)
defer q.Close()
require.Equal(t, "SELECT id FROM items WHERE id > ? LIMIT ?", q.String())
require.Equal(t, []interface{}{42, 20}, q.Args())
}
func TestBindStruct(t *testing.T) {
type Parent struct {
ID int64 `db:"id"`
Date time.Time `db:"date"`
Skipped string
}
var u struct {
Parent
ChildTime time.Time `db:"child_time"`
Name string `db:"name"`
Extra int64
}
q := sqlf.From("users").
Bind(&u).
Where("id = ?", 2)
defer q.Close()
require.Equal(t, "SELECT id, date, child_time, name FROM users WHERE id = ?", q.String())
require.Equal(t, []interface{}{2}, q.Args())
require.EqualValues(t, []interface{}{&u.ID, &u.Date, &u.ChildTime, &u.Name}, q.Dest())
}
func TestBulkInsert(t *testing.T) {
q := sqlf.InsertInto("vars")
defer q.Close()
for i := 1; i <= 5; i++ {
q.NewRow().
Set("no", i).
Set("val", i)
}
require.Equal(t, "INSERT INTO vars ( no, val ) VALUES ( ?, ? ), ( ?, ? ), ( ?, ? ), ( ?, ? ), ( ?, ? )", q.String())
require.Len(t, q.Args(), 10)
}