-
Notifications
You must be signed in to change notification settings - Fork 4
/
common.go
115 lines (95 loc) · 2.5 KB
/
common.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
package pg
import (
"context"
)
// QuerySlice executes the given query and returns a list of T entries.
// Note that the rows scanner will directly scan an element of T, meaning
// that the type of T should be a database scannabled type (e.g. string, int, time.Time, etc.).
//
// The ErrNoRows is discarded, an empty list and a nil error will be returned instead.
// If a string column is empty then it's skipped from the returning list.
// Example:
//
// names, err := QuerySlice[string](ctx, db, "SELECT name FROM users;")
func QuerySlice[T any](ctx context.Context, db *DB, query string, args ...any) ([]T, error) {
rows, err := db.Query(ctx, query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
var t T
_, isString := any(t).(string)
var list []T
for rows.Next() {
var entry T
if err = rows.Scan(&entry); err != nil {
return nil, err
}
if isString {
if any(entry).(string) == "" {
continue
}
}
list = append(list, entry)
}
if err = rows.Err(); err != nil && err != ErrNoRows {
return nil, err
}
return list, nil
}
// QueryTwoSlices executes the given query and returns two lists of T and V entries.
// Same behavior as QuerySlice but with two lists.
func QueryTwoSlices[T, V any](ctx context.Context, db *DB, query string, args ...any) ([]T, []V, error) {
rows, err := db.Query(ctx, query, args...)
if err != nil {
return nil, nil, err
}
defer rows.Close()
var (
tList []T
vList []V
)
for rows.Next() {
var (
t T
v V
)
if err = rows.Scan(&t, v); err != nil {
return nil, nil, err
}
tList = append(tList, t)
vList = append(vList, v)
}
if err = rows.Err(); err != nil && err != ErrNoRows {
return nil, nil, err
}
return tList, vList, nil
}
// QuerySingle executes the given query and returns a single T entry.
//
// Example:
//
// names, err := QuerySingle[MyType](ctx, db, "SELECT a_json_field FROM users;")
func QuerySingle[T any](ctx context.Context, db *DB, query string, args ...any) (entry T, err error) {
err = db.QueryRow(ctx, query, args...).Scan(&entry)
return
}
func scanQuery[T any](ctx context.Context, db *DB, scanner func(rows Rows) (T, error), query string, args ...any) ([]T, error) {
rows, err := db.Query(ctx, query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
var list []T
for rows.Next() {
entry, err := scanner(rows)
if err != nil {
return nil, err
}
list = append(list, entry)
}
if err = rows.Err(); err != nil && err != ErrNoRows {
return nil, err
}
return list, nil
}