-
Notifications
You must be signed in to change notification settings - Fork 93
/
example_test.go
281 lines (223 loc) · 5.98 KB
/
example_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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package dbx_test
import (
"fmt"
"github.com/go-ozzo/ozzo-dbx"
)
// This example shows how to populate DB data in different ways.
func Example_dbQueries() {
db, _ := dbx.Open("mysql", "user:pass@/example")
// create a new query
q := db.NewQuery("SELECT id, name FROM users LIMIT 10")
// fetch all rows into a struct array
var users []struct {
ID, Name string
}
q.All(&users)
// fetch a single row into a struct
var user struct {
ID, Name string
}
q.One(&user)
// fetch a single row into a string map
data := dbx.NullStringMap{}
q.One(data)
// fetch row by row
rows2, _ := q.Rows()
for rows2.Next() {
rows2.ScanStruct(&user)
// rows.ScanMap(data)
// rows.Scan(&id, &name)
}
}
// This example shows how to use query builder to build DB queries.
func Example_queryBuilder() {
db, _ := dbx.Open("mysql", "user:pass@/example")
// build a SELECT query
// SELECT `id`, `name` FROM `users` WHERE `name` LIKE '%Charles%' ORDER BY `id`
q := db.Select("id", "name").
From("users").
Where(dbx.Like("name", "Charles")).
OrderBy("id")
// fetch all rows into a struct array
var users []struct {
ID, Name string
}
q.All(&users)
// build an INSERT query
// INSERT INTO `users` (name) VALUES ('James')
db.Insert("users", dbx.Params{
"name": "James",
}).Execute()
}
// This example shows how to use query builder in transactions.
func Example_transactions() {
db, _ := dbx.Open("mysql", "user:pass@/example")
db.Transactional(func(tx *dbx.Tx) error {
_, err := tx.Insert("user", dbx.Params{
"name": "user1",
}).Execute()
if err != nil {
return err
}
_, err = tx.Insert("user", dbx.Params{
"name": "user2",
}).Execute()
return err
})
}
type Customer struct {
ID string
Name string
}
// This example shows how to do CRUD operations.
func Example_crudOperations() {
db, _ := dbx.Open("mysql", "user:pass@/example")
var customer Customer
// read a customer: SELECT * FROM customer WHERE id=100
db.Select().Model(100, &customer)
// create a customer: INSERT INTO customer (name) VALUES ('test')
db.Model(&customer).Insert()
// update a customer: UPDATE customer SET name='test' WHERE id=100
db.Model(&customer).Update()
// delete a customer: DELETE FROM customer WHERE id=100
db.Model(&customer).Delete()
}
func ExampleSchemaBuilder() {
db, _ := dbx.Open("mysql", "user:pass@/example")
db.Insert("users", dbx.Params{
"name": "James",
"age": 30,
}).Execute()
}
func ExampleRows_ScanMap() {
db, _ := dbx.Open("mysql", "user:pass@/example")
user := dbx.NullStringMap{}
sql := "SELECT id, name FROM users LIMIT 10"
rows, _ := db.NewQuery(sql).Rows()
for rows.Next() {
rows.ScanMap(user)
// ...
}
}
func ExampleRows_ScanStruct() {
db, _ := dbx.Open("mysql", "user:pass@/example")
var user struct {
ID, Name string
}
sql := "SELECT id, name FROM users LIMIT 10"
rows, _ := db.NewQuery(sql).Rows()
for rows.Next() {
rows.ScanStruct(&user)
// ...
}
}
func ExampleQuery_All() {
db, _ := dbx.Open("mysql", "user:pass@/example")
sql := "SELECT id, name FROM users LIMIT 10"
// fetches data into a slice of struct
var users []struct {
ID, Name string
}
db.NewQuery(sql).All(&users)
// fetches data into a slice of NullStringMap
var users2 []dbx.NullStringMap
db.NewQuery(sql).All(&users2)
for _, user := range users2 {
fmt.Println(user["id"].String, user["name"].String)
}
}
func ExampleQuery_One() {
db, _ := dbx.Open("mysql", "user:pass@/example")
sql := "SELECT id, name FROM users LIMIT 10"
// fetches data into a struct
var user struct {
ID, Name string
}
db.NewQuery(sql).One(&user)
// fetches data into a NullStringMap
var user2 dbx.NullStringMap
db.NewQuery(sql).All(user2)
fmt.Println(user2["id"].String, user2["name"].String)
}
func ExampleQuery_Row() {
db, _ := dbx.Open("mysql", "user:pass@/example")
sql := "SELECT id, name FROM users LIMIT 10"
// fetches data into a struct
var (
id int
name string
)
db.NewQuery(sql).Row(&id, &name)
}
func ExampleQuery_Rows() {
var user struct {
ID, Name string
}
db, _ := dbx.Open("mysql", "user:pass@/example")
sql := "SELECT id, name FROM users LIMIT 10"
rows, _ := db.NewQuery(sql).Rows()
for rows.Next() {
rows.ScanStruct(&user)
// ...
}
}
func ExampleQuery_Bind() {
var user struct {
ID, Name string
}
db, _ := dbx.Open("mysql", "user:pass@/example")
sql := "SELECT id, name FROM users WHERE age>{:age} AND status={:status}"
q := db.NewQuery(sql)
q.Bind(dbx.Params{"age": 30, "status": 1}).One(&user)
}
func ExampleQuery_Prepare() {
var users1, users2, users3 []struct {
ID, Name string
}
db, _ := dbx.Open("mysql", "user:pass@/example")
sql := "SELECT id, name FROM users WHERE age>{:age} AND status={:status}"
q := db.NewQuery(sql).Prepare()
defer q.Close()
q.Bind(dbx.Params{"age": 30, "status": 1}).All(&users1)
q.Bind(dbx.Params{"age": 20, "status": 1}).All(&users2)
q.Bind(dbx.Params{"age": 10, "status": 1}).All(&users3)
}
func ExampleDB() {
db, _ := dbx.Open("mysql", "user:pass@/example")
// queries data through a plain SQL
var users []struct {
ID, Name string
}
db.NewQuery("SELECT id, name FROM users WHERE age=30").All(&users)
// queries data using query builder
db.Select("id", "name").From("users").Where(dbx.HashExp{"age": 30}).All(&users)
// executes a plain SQL
db.NewQuery("INSERT INTO users (name) SET ({:name})").Bind(dbx.Params{"name": "James"}).Execute()
// executes a SQL using query builder
db.Insert("users", dbx.Params{"name": "James"}).Execute()
}
func ExampleDB_Open() {
db, err := dbx.Open("mysql", "user:pass@/example")
if err != nil {
panic(err)
}
var users []dbx.NullStringMap
if err := db.NewQuery("SELECT * FROM users LIMIT 10").All(&users); err != nil {
panic(err)
}
}
func ExampleDB_Begin() {
db, _ := dbx.Open("mysql", "user:pass@/example")
tx, _ := db.Begin()
_, err1 := tx.Insert("user", dbx.Params{
"name": "user1",
}).Execute()
_, err2 := tx.Insert("user", dbx.Params{
"name": "user2",
}).Execute()
if err1 == nil && err2 == nil {
tx.Commit()
} else {
tx.Rollback()
}
}