-
Notifications
You must be signed in to change notification settings - Fork 28
/
with_test.go
67 lines (63 loc) · 1.68 KB
/
with_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
package sqlz
import (
"testing"
)
func TestWith(t *testing.T) {
runTests(t, func(dbz *DB) []test {
return []test{
{
"WITH with one auxiliary query",
dbz.With(
dbz.Select("id").
From("table").
Where(Eq("something", 3)),
"aux",
).Then(
dbz.InsertInto("table2").
Columns("something_id", "other_value").
Values(Indirect("aux.id"), 4),
),
"WITH aux AS (SELECT id FROM table WHERE something = ?) INSERT INTO table2 (something_id, other_value) VALUES (aux.id, ?)",
[]interface{}{3, 4},
},
{
"WITH with multiple auxiliary queries",
dbz.With(
dbz.Select("id").
From("table").
Where(Eq("something", 3)),
"somethings",
).And(
dbz.Select("MAX(value) AS max").
From("other_table").
Where(Eq("something", 3)),
"values",
).Then(
dbz.DeleteFrom("ref_table").
Where(
Eq("something_id", Indirect("somethings.id")),
Lt("value", Indirect("values.max")),
),
),
"WITH somethings AS (SELECT id FROM table WHERE something = ?), values AS (SELECT MAX(value) AS max FROM other_table WHERE something = ?) DELETE FROM ref_table WHERE something_id = somethings.id AND value < values.max",
[]interface{}{3, 3},
},
{
"INSERT query that insert from a WITH-ed SELECT",
dbz.With(
dbz.Select("id").
From("table").
Where(Eq("something", 3)),
"somethings",
).Then(
dbz.InsertInto("ref_table").
FromSelect(
dbz.Select("*").From("somethings"),
),
),
"WITH somethings AS (SELECT id FROM table WHERE something = ?) INSERT INTO ref_table SELECT * FROM somethings",
[]interface{}{3},
},
}
})
}