-
Notifications
You must be signed in to change notification settings - Fork 4
/
exec.go
159 lines (129 loc) · 3.83 KB
/
exec.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
package scan
import (
"context"
"database/sql"
)
// One scans a single row from the query and maps it to T using a [Queryer]
func One[T any](ctx context.Context, exec Queryer, m Mapper[T], query string, args ...any) (T, error) {
var t T
rows, err := exec.QueryContext(ctx, query, args...)
if err != nil {
return t, err
}
defer rows.Close()
return OneFromRows(ctx, m, rows)
}
// OneFromRows scans a single row from the given [Rows] result and maps it to T using a [Queryer]
func OneFromRows[T any](ctx context.Context, m Mapper[T], rows Rows) (T, error) {
var t T
allowUnknown, _ := ctx.Value(CtxKeyAllowUnknownColumns).(bool)
v, err := wrapRows(rows, allowUnknown)
if err != nil {
return t, err
}
before, after := m(ctx, v.columnsCopy())
if !rows.Next() {
if err = rows.Err(); err != nil {
return t, err
}
return t, sql.ErrNoRows
}
t, err = scanOneRow(v, before, after)
if err != nil {
return t, err
}
return t, rows.Err()
}
// All scans all rows from the query and returns a slice []T of all rows using a [Queryer]
func All[T any](ctx context.Context, exec Queryer, m Mapper[T], query string, args ...any) ([]T, error) {
rows, err := exec.QueryContext(ctx, query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
return AllFromRows(ctx, m, rows)
}
// AllFromRows scans all rows from the given [Rows] and returns a slice []T of all rows using a [Queryer]
func AllFromRows[T any](ctx context.Context, m Mapper[T], rows Rows) ([]T, error) {
allowUnknown, _ := ctx.Value(CtxKeyAllowUnknownColumns).(bool)
v, err := wrapRows(rows, allowUnknown)
if err != nil {
return nil, err
}
before, after := m(ctx, v.columnsCopy())
var results []T
for rows.Next() {
one, err := scanOneRow(v, before, after)
if err != nil {
return nil, err
}
results = append(results, one)
}
return results, rows.Err()
}
// Cursor runs a query and returns a cursor that works similar to *sql.Rows
func Cursor[T any](ctx context.Context, exec Queryer, m Mapper[T], query string, args ...any) (ICursor[T], error) {
rows, err := exec.QueryContext(ctx, query, args...)
if err != nil {
return nil, err
}
return CursorFromRows(ctx, m, rows)
}
// Each returns a function that can be used to iterate over the rows of a query
// this function works with range-over-func so it is possible to do
//
// for val, err := range scan.Each(ctx, exec, m, query, args...) {
// if err != nil {
// return err
// }
// // do something with val
// }
func Each[T any](ctx context.Context, exec Queryer, m Mapper[T], query string, args ...any) func(func(T, error) bool) {
rows, err := exec.QueryContext(ctx, query, args...)
if err != nil {
return func(yield func(T, error) bool) { yield(*new(T), err) }
}
allowUnknown, _ := ctx.Value(CtxKeyAllowUnknownColumns).(bool)
wrapped, err := wrapRows(rows, allowUnknown)
if err != nil {
rows.Close()
return func(yield func(T, error) bool) { yield(*new(T), err) }
}
before, after := m(ctx, wrapped.columnsCopy())
return func(yield func(T, error) bool) {
defer rows.Close()
for rows.Next() {
val, err := scanOneRow(wrapped, before, after)
if !yield(val, err) {
return
}
}
}
}
// CursorFromRows returns a cursor from [Rows] that works similar to *sql.Rows
func CursorFromRows[T any](ctx context.Context, m Mapper[T], rows Rows) (ICursor[T], error) {
allowUnknown, _ := ctx.Value(CtxKeyAllowUnknownColumns).(bool)
v, err := wrapRows(rows, allowUnknown)
if err != nil {
return nil, err
}
before, after := m(ctx, v.columnsCopy())
return &cursor[T]{
v: v,
before: before,
after: after,
}, nil
}
func scanOneRow[T any](v *Row, before func(*Row) (any, error), after func(any) (T, error)) (T, error) {
val, err := before(v)
if err != nil {
var t T
return t, err
}
err = v.scanCurrentRow()
if err != nil {
var t T
return t, err
}
return after(val)
}