-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
116 lines (95 loc) · 2.68 KB
/
example_test.go
File metadata and controls
116 lines (95 loc) · 2.68 KB
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
package dbx
import (
"fmt"
"log"
_ "modernc.org/sqlite"
)
func ExampleQuery_Bind() {
db, err := Open("sqlite", ":memory:")
if err != nil {
log.Fatal(err)
}
defer db.Close()
_, _ = db.NewQuery(`CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, status TEXT)`).Execute()
_, _ = db.Insert("users", Params{"name": "alice", "status": "active"}).Execute()
_, _ = db.Insert("users", Params{"name": "bob", "status": "disabled"}).Execute()
var names []string
err = db.NewQuery(`SELECT name FROM users WHERE status={:status} ORDER BY id`).
Bind(Params{"status": "active"}).
Column(&names)
if err != nil {
log.Fatal(err)
}
fmt.Println(names)
// Output: [alice]
}
func ExampleDB_Select() {
db, err := Open("sqlite", ":memory:")
if err != nil {
log.Fatal(err)
}
defer db.Close()
_, _ = db.NewQuery(`CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, status TEXT)`).Execute()
_, _ = db.Insert("users", Params{"name": "alice", "status": "active"}).Execute()
_, _ = db.Insert("users", Params{"name": "alex", "status": "active"}).Execute()
_, _ = db.Insert("users", Params{"name": "bob", "status": "disabled"}).Execute()
type row struct {
ID int64 `db:"id"`
Name string `db:"name"`
}
var rows []row
err = db.Select("id", "name").
From("users").
Where(And(HashExp{"status": "active"}, Like("name", "al"))).
OrderBy("id ASC").
All(&rows)
if err != nil {
log.Fatal(err)
}
fmt.Println(len(rows), rows[0].Name, rows[1].Name)
// Output: 2 alice alex
}
func ExampleDB_Model() {
db, err := Open("sqlite", ":memory:")
if err != nil {
log.Fatal(err)
}
defer db.Close()
_, _ = db.NewQuery(`CREATE TABLE user (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, email TEXT UNIQUE)`).Execute()
type user struct {
ID int64 `db:"id,pk"`
Name string `db:"name"`
Email string `db:"email"`
}
u := &user{Name: "alice", Email: "a@example.com"}
if err := db.Model(u).Insert(); err != nil {
log.Fatal(err)
}
fmt.Println(u.ID > 0)
// Output: true
}
func ExampleDB_Upsert() {
db, err := Open("sqlite", ":memory:")
if err != nil {
log.Fatal(err)
}
defer db.Close()
_, _ = db.NewQuery(`CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, email TEXT UNIQUE, name TEXT)`).Execute()
_, _ = db.Insert("users", Params{"email": "a@example.com", "name": "alice"}).Execute()
_, err = db.Upsert("users", Params{
"email": "a@example.com",
"name": "alice-v2",
}, "email").Execute()
if err != nil {
log.Fatal(err)
}
var name string
err = db.NewQuery("SELECT name FROM users WHERE email={:email}").
Bind(Params{"email": "a@example.com"}).
Row(&name)
if err != nil {
log.Fatal(err)
}
fmt.Println(name)
// Output: alice-v2
}