-
Notifications
You must be signed in to change notification settings - Fork 123
/
update_test.go
164 lines (137 loc) · 3.86 KB
/
update_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
// Copyright 2018 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 ExampleUpdate() {
sql := Update("demo.user").
Set(
"visited = visited + 1",
).
Where(
"id = 1234",
).
String()
fmt.Println(sql)
// Output:
// UPDATE demo.user SET visited = visited + 1 WHERE id = 1234
}
func ExampleUpdateBuilder() {
ub := NewUpdateBuilder()
ub.Update("demo.user")
ub.Set(
ub.Assign("type", "sys"),
ub.Incr("credit"),
"modified_at = UNIX_TIMESTAMP(NOW())", // It's allowed to write arbitrary SQL.
)
ub.Where(
ub.GreaterThan("id", 1234),
ub.Like("name", "%Du"),
ub.Or(
ub.IsNull("id_card"),
ub.In("status", 1, 2, 5),
),
"modified_at > created_at + "+ub.Var(86400), // It's allowed to write arbitrary SQL.
)
ub.OrderBy("id").Asc()
sql, args := ub.Build()
fmt.Println(sql)
fmt.Println(args)
// Output:
// UPDATE demo.user SET type = ?, credit = credit + 1, modified_at = UNIX_TIMESTAMP(NOW()) WHERE id > ? AND name LIKE ? AND (id_card IS NULL OR status IN (?, ?, ?)) AND modified_at > created_at + ? ORDER BY id ASC
// [sys 1234 %Du 1 2 5 86400]
}
func TestUpdateAssignments(t *testing.T) {
a := assert.New(t)
cases := map[string]func(ub *UpdateBuilder) string{
"f = f + 1|[]": func(ub *UpdateBuilder) string { return ub.Incr("f") },
"f = f - 1|[]": func(ub *UpdateBuilder) string { return ub.Decr("f") },
"f = f + $1|[123]": func(ub *UpdateBuilder) string { return ub.Add("f", 123) },
"f = f - $1|[123]": func(ub *UpdateBuilder) string { return ub.Sub("f", 123) },
"f = f * $1|[123]": func(ub *UpdateBuilder) string { return ub.Mul("f", 123) },
"f = f / $1|[123]": func(ub *UpdateBuilder) string { return ub.Div("f", 123) },
}
for expected, f := range cases {
ub := NewUpdateBuilder()
s := f(ub)
ub.Set(s)
_, args := ub.Build()
actual := fmt.Sprintf("%v|%v", s, args)
a.Equal(actual, expected)
}
}
func ExampleUpdateBuilder_SetMore() {
ub := NewUpdateBuilder()
ub.Update("demo.user")
ub.Set(
ub.Assign("type", "sys"),
ub.Incr("credit"),
)
ub.SetMore(
"modified_at = UNIX_TIMESTAMP(NOW())", // It's allowed to write arbitrary SQL.
)
sql, args := ub.Build()
fmt.Println(sql)
fmt.Println(args)
// Output:
// UPDATE demo.user SET type = ?, credit = credit + 1, modified_at = UNIX_TIMESTAMP(NOW())
// [sys]
}
func ExampleUpdateBuilder_SQL() {
ub := NewUpdateBuilder()
ub.SQL("/* before */")
ub.Update("demo.user")
ub.SQL("/* after update */")
ub.Set(
ub.Assign("type", "sys"),
)
ub.SQL("/* after set */")
ub.OrderBy("id").Desc()
ub.SQL("/* after order by */")
ub.Limit(10)
ub.SQL("/* after limit */")
sql := ub.String()
fmt.Println(sql)
// Output:
// /* before */ UPDATE demo.user /* after update */ SET type = ? /* after set */ ORDER BY id DESC /* after order by */ LIMIT 10 /* after limit */
}
func ExampleUpdateBuilder_NumAssignment() {
ub := NewUpdateBuilder()
ub.Update("demo.user")
ub.Set(
ub.Assign("type", "sys"),
ub.Incr("credit"),
"modified_at = UNIX_TIMESTAMP(NOW())",
)
// Count the number of assignments.
fmt.Println(ub.NumAssignment())
// Output:
// 3
}
func ExampleUpdateBuilder_With() {
sql := With(
CTEQuery("users").As(
Select("id", "name").From("users").Where("prime IS NOT NULL"),
),
).Update("orders").Set(
"orders.transport_fee = 0",
).Where(
"users.id = orders.user_id",
).String()
fmt.Println(sql)
// Output:
// WITH users AS (SELECT id, name FROM users WHERE prime IS NOT NULL) UPDATE orders SET orders.transport_fee = 0 WHERE users.id = orders.user_id
}
func TestUpdateBuilderGetFlavor(t *testing.T) {
a := assert.New(t)
ub := newUpdateBuilder()
ub.SetFlavor(PostgreSQL)
flavor := ub.Flavor()
a.Equal(PostgreSQL, flavor)
ubClick := ClickHouse.NewUpdateBuilder()
flavor = ubClick.Flavor()
a.Equal(ClickHouse, flavor)
}