-
Notifications
You must be signed in to change notification settings - Fork 3
/
exec.go
120 lines (95 loc) · 2.79 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
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() {
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)
}
// 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)
}