-
Notifications
You must be signed in to change notification settings - Fork 123
/
delete_test.go
98 lines (80 loc) · 2.08 KB
/
delete_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
// 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 ExampleDeleteFrom() {
sql := DeleteFrom("demo.user").
Where(
"status = 1",
).
Limit(10).
String()
fmt.Println(sql)
// Output:
// DELETE FROM demo.user WHERE status = 1 LIMIT 10
}
func ExampleDeleteBuilder() {
db := NewDeleteBuilder()
db.DeleteFrom("demo.user")
db.Where(
db.GreaterThan("id", 1234),
db.Like("name", "%Du"),
db.Or(
db.IsNull("id_card"),
db.In("status", 1, 2, 5),
),
"modified_at > created_at + "+db.Var(86400), // It's allowed to write arbitrary SQL.
)
sql, args := db.Build()
fmt.Println(sql)
fmt.Println(args)
// Output:
// DELETE FROM demo.user WHERE id > ? AND name LIKE ? AND (id_card IS NULL OR status IN (?, ?, ?)) AND modified_at > created_at + ?
// [1234 %Du 1 2 5 86400]
}
func ExampleDeleteBuilder_SQL() {
db := NewDeleteBuilder()
db.SQL(`/* before */`)
db.DeleteFrom("demo.user")
db.SQL("PARTITION (p0)")
db.Where(
db.GreaterThan("id", 1234),
)
db.SQL("/* after where */")
db.OrderBy("id")
db.SQL("/* after order by */")
db.Limit(10)
db.SQL("/* after limit */")
sql, args := db.Build()
fmt.Println(sql)
fmt.Println(args)
// Output:
// /* before */ DELETE FROM demo.user PARTITION (p0) WHERE id > ? /* after where */ ORDER BY id /* after order by */ LIMIT 10 /* after limit */
// [1234]
}
func ExampleDeleteBuilder_With() {
sql := With(
CTEQuery("users").As(
Select("id", "name").From("users").Where("name IS NULL"),
),
).DeleteFrom("orders").Where(
"users.id = orders.user_id",
).String()
fmt.Println(sql)
// Output:
// WITH users AS (SELECT id, name FROM users WHERE name IS NULL) DELETE FROM orders WHERE users.id = orders.user_id
}
func TestDeleteBuilderGetFlavor(t *testing.T) {
a := assert.New(t)
db := newDeleteBuilder()
db.SetFlavor(PostgreSQL)
flavor := db.Flavor()
a.Equal(PostgreSQL, flavor)
dbClick := ClickHouse.NewDeleteBuilder()
flavor = dbClick.Flavor()
a.Equal(ClickHouse, flavor)
}