-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_builder_test.go
More file actions
163 lines (143 loc) · 3.56 KB
/
delete_builder_test.go
File metadata and controls
163 lines (143 loc) · 3.56 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
package qrb_test
import (
"testing"
"github.com/networkteam/qrb"
"github.com/networkteam/qrb/builder"
"github.com/networkteam/qrb/fn"
"github.com/networkteam/qrb/internal/testhelper"
)
func TestDeleteBuilder(t *testing.T) {
t.Run("examples", func(t *testing.T) {
// From https://www.postgresql.org/docs/15/sql-delete.html#id-1.9.3.100.8
t.Run("example 0.1", func(t *testing.T) {
q := qrb.
DeleteFrom(qrb.N("films")).
Using(qrb.N("producers")).
Where(qrb.And(
qrb.N("producer_id").Eq(qrb.N("producers.id")),
qrb.N("producers.name").Eq(qrb.String("foo")),
))
testhelper.AssertSQLWriterEquals(
t,
`
DELETE FROM films USING producers
WHERE producer_id = producers.id AND producers.name = 'foo'
`,
nil,
q,
)
})
t.Run("example 0.2", func(t *testing.T) {
q := qrb.
DeleteFrom(qrb.N("films")).
Where(qrb.N("producer_id").In(
qrb.Select(qrb.N("id")).From(qrb.N("producers")).Where(qrb.N("name").Eq(qrb.String("foo"))),
))
testhelper.AssertSQLWriterEquals(
t,
`
DELETE FROM films
WHERE producer_id IN (SELECT id FROM producers WHERE name = 'foo')
`,
nil,
q,
)
})
// From https://www.postgresql.org/docs/15/sql-delete.html#id-1.9.3.100.9
t.Run("example 1.1", func(t *testing.T) {
q := qrb.
DeleteFrom(qrb.N("films")).
Where(qrb.N("kind").Neq(qrb.String("Musical")))
testhelper.AssertSQLWriterEquals(
t,
`
DELETE FROM films WHERE kind <> 'Musical'
`,
nil,
q,
)
})
t.Run("example 1.2", func(t *testing.T) {
q := qrb.
DeleteFrom(qrb.N("films"))
testhelper.AssertSQLWriterEquals(
t,
`
DELETE FROM films
`,
nil,
q,
)
})
t.Run("example 1.3", func(t *testing.T) {
q := qrb.
DeleteFrom(qrb.N("tasks")).
Where(qrb.N("status").Eq(qrb.String("DONE"))).
Returning(qrb.N("*"))
testhelper.AssertSQLWriterEquals(
t,
`
DELETE FROM tasks WHERE status = 'DONE' RETURNING *
`,
nil,
q,
)
})
})
t.Run("with", func(t *testing.T) {
// Example borrowed from https://stackoverflow.com/a/37225172/749191
var listens builder.Identer = qrb.N("listens")
q := qrb.
With("max_table").As(
qrb.Select(qrb.N("uid")).Select(fn.Max(qrb.N("ts")).Minus(qrb.Int(10000))).As("mx").From(qrb.N("listens")).GroupBy(qrb.N("uid")),
).
DeleteFrom(listens).
Where(qrb.N("ts").Lt(qrb.Select(qrb.N("mx")).From(qrb.N("max_table")).Where(qrb.N("max_table.uid").Eq(qrb.N("listens.uid")))))
testhelper.AssertSQLWriterEquals(
t,
`
WITH max_table AS (
SELECT uid, max(ts) - 10000 AS mx
FROM listens
GROUP BY uid
)
DELETE FROM listens
WHERE ts < (SELECT mx
FROM max_table
WHERE max_table.uid = listens.uid)
`,
nil,
q,
)
})
t.Run("", func(t *testing.T) {
q := qrb.DeleteFrom(qrb.N("list_line_items")).
Where(qrb.And(
qrb.N("shopping_cart_id").Eq(qrb.Arg(99)),
qrb.Exps(qrb.N("supplier_id"), qrb.N("article_id"), qrb.N("unit_id")).In(
qrb.Select(
fn.Unnest(qrb.Arg([]int{1, 2, 3}).Cast("integer[]")),
fn.Unnest(qrb.Arg([]int{4, 5, 6}).Cast("integer[]")),
fn.Unnest(qrb.Arg([]int{7, 8, 9}).Cast("integer[]")),
),
),
))
testhelper.AssertSQLWriterEquals(
t,
`
DELETE FROM list_line_items
WHERE shopping_cart_id = $1
AND (supplier_id, article_id, unit_id) IN (
SELECT unnest($2::integer[]), unnest($3::integer[]), unnest($4::integer[])
)
`,
[]any{
99,
[]int{1, 2, 3},
[]int{4, 5, 6},
[]int{7, 8, 9},
},
q,
)
})
}