-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathselect_test.go
More file actions
457 lines (410 loc) · 12.5 KB
/
select_test.go
File metadata and controls
457 lines (410 loc) · 12.5 KB
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
//go:build itest
package itests
import (
"testing"
"github.com/georgysavva/scany/v2/pgxscan"
sq "github.com/n-r-w/squirrel"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSelectBuilderComplexQuery(t *testing.T) {
t.Parallel()
pool, ctx := newTestPool(t)
setupSQL := `
CREATE TABLE users (
id bigserial PRIMARY KEY,
name text NOT NULL,
email text NOT NULL,
status text NOT NULL,
age integer NOT NULL
);
CREATE TABLE orders (
id bigserial PRIMARY KEY,
user_id bigint NOT NULL REFERENCES users(id),
amount double precision NOT NULL,
state text NOT NULL
);
INSERT INTO users (name, email, status, age) VALUES
('Alice', 'alice@example.com', 'active', 30),
('Bob', 'bob@example.com', 'inactive', 35),
('Cara', 'cara@example.com', 'active', 42),
('Dan', 'dan@example.com', 'active', 19);
INSERT INTO orders (user_id, amount, state) VALUES
(1, 120.5, 'paid'),
(1, 40, 'refunded'),
(2, 15, 'refunded'),
(3, 200, 'paid'),
(3, 50, 'paid'),
(4, 5, 'paid');
`
execSetup(t, pool, ctx, setupSQL)
orderStats := sq.Select("o.user_id").
Column(sq.Alias(sq.Sum(sq.Expr("o.amount")), "total_amount")).
Column(sq.Alias(sq.Count(sq.Expr("o.id")), "paid_count")).
From("orders o").
Where(sq.Eq{"o.state": "paid"}).
GroupBy("o.user_id")
refundedExists := sq.Exists(
sq.Select("1").
From("orders r").
Where(sq.And{
sq.Expr("r.user_id = u.id"),
sq.Eq{"r.state": "refunded"},
}),
)
statusCase := sq.Case().
When(sq.Expr("u.status = ?", "active"), "active").
Else("inactive")
selectQuery := sq.Select("u.id", "u.name").
Column(sq.Alias(sq.Coalesce(0.0, sq.Expr("os.total_amount")), "total_amount")).
Column(sq.Alias(sq.Coalesce(0, sq.Expr("os.paid_count")), "paid_count")).
Column(sq.Alias(statusCase, "status_label")).
Column(sq.Alias(refundedExists, "has_refunds")).
Column(sq.Alias(sq.Count(sq.Expr("o.id")), "orders_count")).
From("users u").
LeftJoin("order_stats os ON os.user_id = u.id").
LeftJoin("orders o ON o.user_id = u.id").
Where(sq.And{
sq.Range("u.age", 20, 45),
sq.Or{
sq.Eq{"u.status": "active"},
sq.Like{"u.email": "%example.com"},
},
}).
Search("example.com", "u.email").
GroupBy("u.id", "u.name", "os.total_amount", "os.paid_count", "u.status").
Having(sq.Expr("COUNT(o.id) >= ?", 1)).
OrderBy("total_amount DESC", "u.id").
Limit(3).
Offset(0)
query := sq.With("order_stats").As(orderStats).
Select(selectQuery).
PlaceholderFormat(sq.Dollar)
sql, args, err := query.ToSql()
require.NoError(t, err)
type selectResult struct {
ID int64 `db:"id"`
Name string `db:"name"`
TotalAmount float64 `db:"total_amount"`
PaidCount int64 `db:"paid_count"`
StatusLabel string `db:"status_label"`
HasRefunds bool `db:"has_refunds"`
OrdersCount int64 `db:"orders_count"`
}
var results []selectResult
err = pgxscan.Select(ctx, pool, &results, sql, args...)
require.NoError(t, err)
expected := []selectResult{
{
ID: 3,
Name: "Cara",
TotalAmount: 250,
PaidCount: 2,
StatusLabel: "active",
HasRefunds: false,
OrdersCount: 2,
},
{
ID: 1,
Name: "Alice",
TotalAmount: 120.5,
PaidCount: 1,
StatusLabel: "active",
HasRefunds: true,
OrdersCount: 2,
},
{
ID: 2,
Name: "Bob",
TotalAmount: 0,
PaidCount: 0,
StatusLabel: "inactive",
HasRefunds: true,
OrdersCount: 1,
},
}
assert.Equal(t, expected, results)
}
func TestSelectBuilderAllConstructs(t *testing.T) {
t.Parallel()
pool, ctx := newTestPool(t)
setupSQL := `
CREATE TABLE departments_all (
id bigserial PRIMARY KEY,
name text NOT NULL
);
CREATE TABLE users_all (
id bigserial PRIMARY KEY,
name text NOT NULL,
email text NOT NULL,
status text NOT NULL,
age integer NOT NULL,
department_id bigint NOT NULL REFERENCES departments_all(id)
);
CREATE TABLE emails_all (
id bigserial PRIMARY KEY,
user_id bigint NOT NULL REFERENCES users_all(id),
address text NOT NULL,
verified boolean NOT NULL
);
CREATE TABLE orders_all (
id bigserial PRIMARY KEY,
user_id bigint NOT NULL REFERENCES users_all(id),
amount double precision NOT NULL,
state text NOT NULL
);
CREATE TABLE groups_all (
id bigserial PRIMARY KEY,
name text NOT NULL
);
CREATE TABLE user_groups_all (
user_id bigint NOT NULL REFERENCES users_all(id),
group_id bigint NOT NULL REFERENCES groups_all(id)
);
INSERT INTO departments_all (id, name) VALUES
(1, 'Engineering'),
(2, 'Sales');
INSERT INTO users_all (id, name, email, status, age, department_id) VALUES
(1, 'Alice', 'alice@example.com', 'active', 30, 1),
(2, 'Bob', 'bob@example.com', 'inactive', 35, 1),
(3, 'Cara', 'cara@example.com', 'active', 42, 2),
(4, 'Dan', 'dan@example.com', 'pending', 25, 2);
INSERT INTO emails_all (user_id, address, verified) VALUES
(1, 'alice@work.com', true),
(3, 'cara@work.com', false);
INSERT INTO orders_all (user_id, amount, state) VALUES
(1, 100, 'paid'),
(1, 20, 'refunded'),
(2, 15, 'refunded'),
(3, 200, 'paid'),
(3, 50, 'paid'),
(4, 5, 'paid');
INSERT INTO groups_all (id, name) VALUES
(1, 'core'),
(2, 'banned');
INSERT INTO user_groups_all (user_id, group_id) VALUES
(1, 1),
(2, 1),
(3, 1),
(3, 2),
(4, 1);
`
execSetup(t, pool, ctx, setupSQL)
activeUsers := sq.Select("id", "name", "email", "status", "age", "department_id").
From("users_all").
Where(sq.Eq{"status": []string{"active", "pending", "inactive"}})
orderTotals := sq.Select("o.user_id").
Column(sq.Alias(sq.Sum(sq.Expr("o.amount")), "sum_amount")).
Column(sq.Alias(sq.Count(sq.Expr("o.id")), "orders_count")).
Column(sq.Alias(sq.Min(sq.Expr("o.amount")), "min_amount")).
Column(sq.Alias(sq.Max(sq.Expr("o.amount")), "max_amount")).
Column(sq.Alias(sq.Avg(sq.Expr("o.amount")), "avg_amount")).
From("orders_all o").
GroupBy("o.user_id")
groupSub := sq.Select("ug.user_id").
From("user_groups_all ug").
Where(sq.Eq{"ug.group_id": 1})
bannedSub := sq.Select("ug.user_id").
From("user_groups_all ug").
Where(sq.Eq{"ug.group_id": 2})
groupJoin := sq.Select("ug.user_id").
From("user_groups_all ug").
Where(sq.Eq{"ug.group_id": 1}).
Prefix("JOIN (").
Suffix(") ug_filter ON ug_filter.user_id = au.id")
statusCase := sq.Case("au.status").
When(sq.Expr("?", "active"), 1).
When(sq.Expr("?", "pending"), 2).
Else(0)
displayName := sq.ConcatExpr(
"CONCAT(",
sq.Expr("au.name"),
", ' <', ",
sq.Expr("au.email"),
", '>')",
)
refundedExists := sq.Exists(
sq.Select("1").
From("orders_all r").
Where(sq.Expr("r.user_id = au.id")).
Where(sq.Eq{"r.state": "refunded"}),
)
noChargebacks := sq.NotExists(
sq.Select("1").
From("orders_all cb").
Where(sq.Expr("cb.user_id = au.id")).
Where(sq.Eq{"cb.state": "chargeback"}),
)
deptSub := sq.Select("d.id").
From("departments_all d").
Where(sq.Expr("d.id = au.department_id"))
orderByColumns := map[int]string{
1: "au.status",
2: "au.name",
3: "au.id",
}
orderByConds := []sq.OrderCond{
{ColumnID: 1, Direction: sq.Asc},
{ColumnID: 2, Direction: sq.Asc},
{ColumnID: 3, Direction: sq.Desc},
}
selectQuery := sq.Select().
PrefixExpr(sq.Expr("/* select-all-constructs */")).
Options("DISTINCT ON (au.status)").
Alias("au", "pref").Columns("id", "name").
Column(sq.Alias(displayName, "display_name")).
Column(sq.Alias(sq.Coalesce("n/a", sq.Expr("e.address")), "email_label")).
Column(sq.Alias(statusCase, "status_rank")).
Column(sq.Alias(refundedExists, "has_refunds")).
Column(sq.Alias(noChargebacks, "no_chargebacks")).
Column(sq.Alias(sq.Coalesce(0.0, sq.Expr("ot.sum_amount")), "sum_amount")).
Column(sq.Alias(sq.Coalesce(0, sq.Expr("ot.orders_count")), "orders_count")).
Column(sq.Alias(sq.Expr("ot.min_amount"), "min_amount")).
Column(sq.Alias(sq.Expr("ot.max_amount"), "max_amount")).
Column(sq.Alias(sq.Expr("ot.avg_amount"), "avg_amount")).
From("active_users au").
Join("order_totals ot ON ot.user_id = au.id").
InnerJoin("orders_all o ON o.user_id = au.id").
LeftJoin("emails_all e ON e.user_id = au.id").
RightJoin("departments_all d ON d.id = au.department_id").
CrossJoin("groups_all g").
JoinClause(groupJoin).
Where(sq.And{
sq.Range("au.age", 20, 35),
sq.Or{
sq.EqNotEmpty{"au.status": "active", "au.name": ""},
sq.Eq{"au.status": "pending"},
},
sq.Not(sq.Expr("au.status = ?", "inactive")),
sq.Eq{"au.id": []int64{1, 2, 3, 4}},
sq.Eq{"g.id": 1},
sq.NotEq{"au.email": "blocked@example.com"},
sq.Eq{"au.department_id": deptSub},
sq.In("au.id", groupSub),
sq.NotIn("au.id", bannedSub),
sq.Like{"au.email": "%@example.com"},
sq.NotLike{"au.email": "%@spam.com"},
sq.ILike{"au.name": "%a%"},
sq.NotILike{"au.name": "%zzz%"},
sq.Equal(sq.Select("1"), 1),
sq.NotEqual(sq.Select("1"), 2),
sq.Greater(sq.Select("2"), 1),
sq.GreaterOrEqual(sq.Select("2"), 2),
sq.Less(sq.Select("1"), 2),
sq.LessOrEqual(sq.Select("1"), 1),
refundedExists,
noChargebacks,
}).
Search("example.com", "au.email", "au.name").
GroupBy(
"au.id",
"au.name",
"au.email",
"au.status",
"e.address",
"ot.sum_amount",
"ot.orders_count",
"ot.min_amount",
"ot.max_amount",
"ot.avg_amount",
).
Having(sq.Expr("COUNT(o.id) >= ?", 1)).
OrderByCond(
orderByColumns,
orderByConds,
sq.OrderByCondOption{ColumnID: 2, NullsType: sq.OrderNullsLast},
).
Limit(10).
Offset(0)
query := sq.With("active_users").As(activeUsers).
Cte("order_totals").As(orderTotals).
Select(selectQuery).
PlaceholderFormat(sq.Dollar)
sql, args, err := query.ToSql()
require.NoError(t, err)
type selectResult struct {
PrefID int64 `db:"pref_id"`
PrefName string `db:"pref_name"`
DisplayName string `db:"display_name"`
EmailLabel string `db:"email_label"`
StatusRank int `db:"status_rank"`
HasRefunds bool `db:"has_refunds"`
NoChargebacks bool `db:"no_chargebacks"`
SumAmount float64 `db:"sum_amount"`
OrdersCount int64 `db:"orders_count"`
MinAmount float64 `db:"min_amount"`
MaxAmount float64 `db:"max_amount"`
AvgAmount float64 `db:"avg_amount"`
}
var results []selectResult
err = pgxscan.Select(ctx, pool, &results, sql, args...)
require.NoError(t, err)
require.Len(t, results, 1)
got := results[0]
assert.Equal(t, int64(1), got.PrefID)
assert.Equal(t, "Alice", got.PrefName)
assert.Equal(t, "Alice <alice@example.com>", got.DisplayName)
assert.Equal(t, "alice@work.com", got.EmailLabel)
assert.Equal(t, 1, got.StatusRank)
assert.True(t, got.HasRefunds)
assert.True(t, got.NoChargebacks)
assert.InEpsilon(t, 120.0, got.SumAmount, 0.0001)
assert.Equal(t, int64(2), got.OrdersCount)
assert.InEpsilon(t, 20.0, got.MinAmount, 0.0001)
assert.InEpsilon(t, 100.0, got.MaxAmount, 0.0001)
assert.InEpsilon(t, 60.0, got.AvgAmount, 0.0001)
builderResetQuery := sq.Select("id").
Distinct().
FromSelect(activeUsers, "au").
RemoveColumns().
Columns("au.id", "au.name").
OrderBy("au.id").
Limit(1).
Offset(1).
RemoveLimit().
RemoveOffset().
Prefix("/* cleanup */").
PlaceholderFormat(sq.Dollar)
cleanedIDs, _ := queryInt64StringPairs(t, pool, ctx, builderResetQuery)
assert.Len(t, cleanedIDs, 4)
paginateByID := sq.Select("id").
From("users_all").
OrderBy("id").
PaginateByID(2, 1, "id").
PlaceholderFormat(sq.Dollar)
pageByID := queryInt64s(t, pool, ctx, paginateByID)
assert.Equal(t, []int64{2, 3}, pageByID)
paginateByPage := sq.Select("id").
From("users_all").
OrderBy("id").
PaginateByPage(2, 2).
PlaceholderFormat(sq.Dollar)
pageByPage := queryInt64s(t, pool, ctx, paginateByPage)
assert.Equal(t, []int64{3, 4}, pageByPage)
paginateByPaginator := sq.Select("id").
From("users_all").
OrderBy("id").
Paginate(sq.PaginatorByID(2, 1)).
SetIDColumn("id").
PlaceholderFormat(sq.Dollar)
pageByPaginator := queryInt64s(t, pool, ctx, paginateByPaginator)
assert.Equal(t, []int64{2, 3}, pageByPaginator)
paginateByPagePaginator := sq.Select("id").
From("users_all").
OrderBy("id").
Paginate(sq.PaginatorByPage(2, 2)).
PlaceholderFormat(sq.Dollar)
pageByPagePaginator := queryInt64s(t, pool, ctx, paginateByPagePaginator)
assert.Equal(t, []int64{3, 4}, pageByPagePaginator)
recursiveQuery := sq.WithRecursive("category_tree").As(
sq.Select("id", "name").
From("departments_all").
Where(sq.Eq{"id": 1}),
).Select(
sq.Select("id", "name").
From("category_tree"),
).PlaceholderFormat(sq.Dollar)
recursiveIDs, recursiveNames := queryInt64StringPairs(t, pool, ctx, recursiveQuery)
assert.Equal(t, []int64{1}, recursiveIDs)
assert.Equal(t, []string{"Engineering"}, recursiveNames)
}