-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathexample_test.go
79 lines (67 loc) · 1.44 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
// Copyright 2021 Roxy Light
// SPDX-License-Identifier: ISC
package sqlitex_test
import (
"context"
"fmt"
"zombiezen.com/go/sqlite"
"zombiezen.com/go/sqlite/sqlitex"
)
func ExampleExecute() {
conn, err := sqlite.OpenConn(":memory:")
if err != nil {
// handle err
}
if err := sqlitex.Execute(conn, "CREATE TABLE t (a, b, c, d);", nil); err != nil {
// handle err
}
err = sqlitex.Execute(conn, "INSERT INTO t (a, b, c, d) VALUES (?, ?, ?, ?);", &sqlitex.ExecOptions{
Args: []any{"a1", 1, 42, 1},
})
if err != nil {
// handle err
}
var a []string
var b []int64
err = sqlitex.Execute(conn, "SELECT a, b FROM t WHERE c = ? AND d = ?;", &sqlitex.ExecOptions{
ResultFunc: func(stmt *sqlite.Stmt) error {
a = append(a, stmt.ColumnText(0))
b = append(b, stmt.ColumnInt64(1))
return nil
},
Args: []any{42, 1},
})
if err != nil {
// handle err
}
fmt.Println(a, b)
// Output:
// [a1] [1]
}
func ExampleSave() {
doWork := func(conn *sqlite.Conn) (err error) {
defer sqlitex.Save(conn)(&err)
// ... do work in the transaction
return nil
}
_ = doWork
}
func ExamplePool() {
// Open a pool.
dbpool, err := sqlitex.NewPool("foo.db", sqlitex.PoolOptions{})
if err != nil {
// handle err
}
defer func() {
if err := dbpool.Close(); err != nil {
// handle err
}
}()
// While handling a request:
ctx := context.TODO()
conn, err := dbpool.Take(ctx)
if err != nil {
// handle err
}
defer dbpool.Put(conn)
}