forked from huandu/go-sqlbuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_test.go
67 lines (56 loc) · 1.42 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
// Copyright 2018 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.
package sqlbuilder
import (
"fmt"
)
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]
}