-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
283 lines (237 loc) · 4.74 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
282
283
package pg_test
import (
"bytes"
"fmt"
"strings"
"time"
"gopkg.in/pg.v5"
"gopkg.in/pg.v5/orm"
)
var db *pg.DB
func init() {
db = connect()
}
func connect() *pg.DB {
return pg.Connect(pgOptions())
}
func ExampleConnect() {
db := pg.Connect(&pg.Options{
User: "postgres",
Password: "",
Database: "postgres",
})
var n int
_, err := db.QueryOne(pg.Scan(&n), "SELECT 1")
if err != nil {
panic(err)
}
fmt.Println(n)
err = db.Close()
if err != nil {
panic(err)
}
// Output: 1
}
func ExampleDB_QueryOne() {
var user struct {
Name string
}
res, err := db.QueryOne(&user, `
WITH users (name) AS (VALUES (?))
SELECT * FROM users
`, "admin")
if err != nil {
panic(err)
}
fmt.Println(res.RowsAffected())
fmt.Println(user)
// Output: 1
// {admin}
}
func ExampleDB_QueryOne_returning_id() {
_, err := db.Exec(`CREATE TEMP TABLE users(id serial, name varchar(500))`)
if err != nil {
panic(err)
}
var user struct {
Id int32
Name string
}
user.Name = "admin"
_, err = db.QueryOne(&user, `
INSERT INTO users (name) VALUES (?name) RETURNING id
`, &user)
if err != nil {
panic(err)
}
fmt.Println(user)
// Output: {1 admin}
}
func ExampleDB_Exec() {
res, err := db.Exec(`CREATE TEMP TABLE test()`)
fmt.Println(res.RowsAffected(), err)
// Output: -1 <nil>
}
func ExampleListener() {
ln := db.Listen("mychan")
defer ln.Close()
ch := ln.Channel()
go func() {
_, err := db.Exec("NOTIFY mychan, ?", "hello world")
if err != nil {
panic(err)
}
}()
notif := <-ch
fmt.Println(notif)
// Output: &{mychan hello world}
}
func txExample() *pg.DB {
db := pg.Connect(&pg.Options{
User: "postgres",
})
queries := []string{
`DROP TABLE IF EXISTS tx_test`,
`CREATE TABLE tx_test(counter int)`,
`INSERT INTO tx_test (counter) VALUES (0)`,
}
for _, q := range queries {
_, err := db.Exec(q)
if err != nil {
panic(err)
}
}
return db
}
func ExampleDB_Begin() {
db := txExample()
tx, err := db.Begin()
if err != nil {
panic(err)
}
var counter int
_, err = tx.QueryOne(pg.Scan(&counter), `SELECT counter FROM tx_test`)
if err != nil {
tx.Rollback()
panic(err)
}
counter++
_, err = tx.Exec(`UPDATE tx_test SET counter = ?`, counter)
if err != nil {
tx.Rollback()
panic(err)
}
err = tx.Commit()
if err != nil {
panic(err)
}
fmt.Println(counter)
// Output: 1
}
func ExampleDB_RunInTransaction() {
db := txExample()
var counter int
// Transaction is automatically rollbacked on error.
err := db.RunInTransaction(func(tx *pg.Tx) error {
_, err := tx.QueryOne(pg.Scan(&counter), `SELECT counter FROM tx_test`)
if err != nil {
return err
}
counter++
_, err = tx.Exec(`UPDATE tx_test SET counter = ?`, counter)
return err
})
if err != nil {
panic(err)
}
fmt.Println(counter)
// Output: 1
}
func ExampleDB_Prepare() {
stmt, err := db.Prepare(`SELECT $1::text, $2::text`)
if err != nil {
panic(err)
}
var s1, s2 string
_, err = stmt.QueryOne(pg.Scan(&s1, &s2), "foo", "bar")
if err != nil {
panic(err)
}
fmt.Println(s1, s2)
// Output: foo bar
}
func ExampleDB_CreateTable() {
type Model struct {
Id int
Name string
}
err := db.CreateTable(&Model{}, &orm.CreateTableOptions{
Temp: true, // create temp table
})
if err != nil {
panic(err)
}
var info []struct {
ColumnName string
DataType string
}
_, err = db.Query(&info, `
SELECT column_name, data_type
FROM information_schema.columns
WHERE table_name = 'models'
`)
if err != nil {
panic(err)
}
fmt.Println(info)
// Output: [{id bigint} {name text}]
}
func ExampleInts() {
var nums pg.Ints
_, err := db.Query(&nums, `SELECT generate_series(0, 10)`)
fmt.Println(nums, err)
// Output: [0 1 2 3 4 5 6 7 8 9 10] <nil>
}
func ExampleStrings() {
var strs pg.Strings
_, err := db.Query(&strs, `
WITH users AS (VALUES ('foo'), ('bar')) SELECT * FROM users
`)
fmt.Println(strs, err)
// Output: [foo bar] <nil>
}
func ExampleDB_CopyFrom() {
_, err := db.Exec(`CREATE TEMP TABLE words(word text, len int)`)
if err != nil {
panic(err)
}
r := strings.NewReader("hello,5\nfoo,3\n")
_, err = db.CopyFrom(r, `COPY words FROM STDIN WITH CSV`)
if err != nil {
panic(err)
}
var buf bytes.Buffer
_, err = db.CopyTo(&buf, `COPY words TO STDOUT WITH CSV`)
if err != nil {
panic(err)
}
fmt.Println(buf.String())
// Output: hello,5
// foo,3
}
func ExampleDB_WithTimeout() {
var count int
// Use bigger timeout since this query is known to be slow.
_, err := db.WithTimeout(time.Minute).QueryOne(pg.Scan(&count), `
SELECT count(*) FROM big_table
`)
if err != nil {
panic(err)
}
}
func ExampleScan() {
var s1, s2 string
_, err := db.QueryOne(pg.Scan(&s1, &s2), `SELECT ?, ?`, "foo", "bar")
fmt.Println(s1, s2, err)
// Output: foo bar <nil>
}