-
Notifications
You must be signed in to change notification settings - Fork 123
/
cte_test.go
195 lines (158 loc) · 5.73 KB
/
cte_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
// Copyright 2024 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.
package sqlbuilder
import (
"fmt"
"testing"
"github.com/huandu/go-assert"
)
func ExampleWith() {
sb := With(
CTETable("users", "id", "name").As(
Select("id", "name").From("users").Where("name IS NOT NULL"),
),
CTETable("devices").As(
Select("device_id").From("devices"),
),
).Select("users.id", "orders.id", "devices.device_id").Join(
"orders",
"users.id = orders.user_id",
"devices.device_id = orders.device_id",
)
fmt.Println(sb)
// Output:
// WITH users (id, name) AS (SELECT id, name FROM users WHERE name IS NOT NULL), devices AS (SELECT device_id FROM devices) SELECT users.id, orders.id, devices.device_id FROM users, devices JOIN orders ON users.id = orders.user_id AND devices.device_id = orders.device_id
}
func ExampleWithRecursive() {
sb := WithRecursive(
CTEQuery("source_accounts", "id", "parent_id").As(
UnionAll(
Select("p.id", "p.parent_id").
From("accounts AS p").
Where("p.id = 2"), // Show orders for account 2 and all its child accounts
Select("c.id", "c.parent_id").
From("accounts AS c").
Join("source_accounts AS sa", "c.parent_id = sa.id"),
),
),
).Select("o.id", "o.date", "o.amount").
From("orders AS o").
Join("source_accounts", "o.account_id = source_accounts.id")
fmt.Println(sb)
// Output:
// WITH RECURSIVE source_accounts (id, parent_id) AS ((SELECT p.id, p.parent_id FROM accounts AS p WHERE p.id = 2) UNION ALL (SELECT c.id, c.parent_id FROM accounts AS c JOIN source_accounts AS sa ON c.parent_id = sa.id)) SELECT o.id, o.date, o.amount FROM orders AS o JOIN source_accounts ON o.account_id = source_accounts.id
}
func ExampleCTEBuilder() {
usersBuilder := Select("id", "name", "level").From("users")
usersBuilder.Where(
usersBuilder.GreaterEqualThan("level", 10),
)
cteb := With(
CTETable("valid_users").As(usersBuilder),
)
fmt.Println(cteb)
sb := Select("valid_users.id", "valid_users.name", "orders.id").
From("users").With(cteb).
Join("orders", "users.id = orders.user_id")
sb.Where(
sb.LessEqualThan("orders.price", 200),
"valid_users.level < orders.min_level",
).OrderBy("orders.price").Desc()
sql, args := sb.Build()
fmt.Println(sql)
fmt.Println(args)
fmt.Println(sb.TableNames())
// Output:
// WITH valid_users AS (SELECT id, name, level FROM users WHERE level >= ?)
// WITH valid_users AS (SELECT id, name, level FROM users WHERE level >= ?) SELECT valid_users.id, valid_users.name, orders.id FROM users, valid_users JOIN orders ON users.id = orders.user_id WHERE orders.price <= ? AND valid_users.level < orders.min_level ORDER BY orders.price DESC
// [10 200]
// [users valid_users]
}
func ExampleCTEBuilder_update() {
builder := With(
CTETable("users", "user_id").As(
Select("user_id").From("vip_users"),
),
).Update("orders").Set(
"orders.transport_fee = 0",
).Where(
"users.user_id = orders.user_id",
)
sqlForMySQL, _ := builder.BuildWithFlavor(MySQL)
sqlForPostgreSQL, _ := builder.BuildWithFlavor(PostgreSQL)
fmt.Println(sqlForMySQL)
fmt.Println(sqlForPostgreSQL)
// Output:
// WITH users (user_id) AS (SELECT user_id FROM vip_users) UPDATE orders, users SET orders.transport_fee = 0 WHERE users.user_id = orders.user_id
// WITH users (user_id) AS (SELECT user_id FROM vip_users) UPDATE orders SET orders.transport_fee = 0 FROM users WHERE users.user_id = orders.user_id
}
func ExampleCTEBuilder_delete() {
sql := With(
CTETable("users", "user_id").As(
Select("user_id").From("cheaters"),
),
).DeleteFrom("awards").Where(
"users.user_id = awards.user_id",
).String()
fmt.Println(sql)
// Output:
// WITH users (user_id) AS (SELECT user_id FROM cheaters) DELETE FROM awards, users WHERE users.user_id = awards.user_id
}
func TestCTEBuilder(t *testing.T) {
a := assert.New(t)
cteb := newCTEBuilder()
ctetb := newCTEQueryBuilder()
cteb.SQL("/* init */")
cteb.With(ctetb)
cteb.SQL("/* after with */")
ctetb.SQL("/* table init */")
ctetb.Table("t", "a", "b")
ctetb.SQL("/* after table */")
ctetb.As(Select("a", "b").From("t"))
ctetb.SQL("/* after table as */")
a.Equal(cteb.TableNames(), []string{ctetb.TableName()})
sql, args := cteb.Build()
a.Equal(sql, "/* init */ WITH /* table init */ t (a, b) /* after table */ AS (SELECT a, b FROM t) /* after table as */ /* after with */")
a.Assert(args == nil)
sql = ctetb.String()
a.Equal(sql, "/* table init */ t (a, b) /* after table */ AS (SELECT a, b FROM t) /* after table as */")
}
func TestRecursiveCTEBuilder(t *testing.T) {
a := assert.New(t)
cteb := newCTEBuilder()
cteb.recursive = true
ctetb := newCTEQueryBuilder()
cteb.SQL("/* init */")
cteb.With(ctetb)
cteb.SQL("/* after with */")
ctetb.SQL("/* table init */")
ctetb.Table("t", "a", "b")
ctetb.SQL("/* after table */")
ctetb.As(Select("a", "b").From("t"))
ctetb.SQL("/* after table as */")
sql, args := cteb.Build()
a.Equal(sql, "/* init */ WITH RECURSIVE /* table init */ t (a, b) /* after table */ AS (SELECT a, b FROM t) /* after table as */ /* after with */")
a.Assert(args == nil)
sql = ctetb.String()
a.Equal(sql, "/* table init */ t (a, b) /* after table */ AS (SELECT a, b FROM t) /* after table as */")
}
func TestCTEGetFlavor(t *testing.T) {
a := assert.New(t)
cteb := newCTEBuilder()
cteb.SetFlavor(PostgreSQL)
flavor := cteb.Flavor()
a.Equal(PostgreSQL, flavor)
ctebClick := ClickHouse.NewCTEBuilder()
flavor = ctebClick.Flavor()
a.Equal(ClickHouse, flavor)
}
func TestCTEQueryBuilderGetFlavor(t *testing.T) {
a := assert.New(t)
ctetb := newCTEQueryBuilder()
ctetb.SetFlavor(PostgreSQL)
flavor := ctetb.Flavor()
a.Equal(PostgreSQL, flavor)
ctetbClick := ClickHouse.NewCTEQueryBuilder()
flavor = ctetbClick.Flavor()
a.Equal(ClickHouse, flavor)
}