Repository navigation
Expand file tree
/
Copy pathrows.go
More file actions
173 lines (153 loc) · 3.82 KB
/
Copy pathrows.go
File metadata and controls
173 lines (153 loc) · 3.82 KB
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
// SPDX-License-Identifier: Apache-2.0
package dbkit
import (
"context"
"database/sql"
"errors"
"slices"
"sync"
"sync/atomic"
)
// errRawBytes is what Scan answers to a *sql.RawBytes destination.
var errRawBytes = errors.New("dbkit: Scan takes no *sql.RawBytes")
// Rows is a *sql.Rows that gives its slot back exactly once, read within the statement timeout.
type Rows struct {
*sql.Rows
ctx context.Context
cancel context.CancelFunc
give func()
stop func() bool
once sync.Once
cause atomic.Pointer[error]
kept atomic.Pointer[error]
atEnd bool
}
// newRows wraps rows read under ctx, which cancel ends and give frees, and closes them once ctx ends.
func newRows(ctx context.Context, cancel context.CancelFunc, give func(), rows *sql.Rows) *Rows {
r := &Rows{Rows: rows, ctx: ctx, cancel: cancel, give: give}
r.stop = context.AfterFunc(ctx, r.expire)
return r
}
// Next prepares the next row for Scan and gives the slot back once the last result set ends.
func (r *Rows) Next() bool {
if r.Rows.Next() {
return true
}
if r.cause.Load() == nil {
r.atEnd = true
}
if _, err := r.Columns(); err != nil {
r.endRead()
}
return false
}
// NextResultSet moves to the next result set and gives the slot back when none follows.
func (r *Rows) NextResultSet() bool {
if r.Rows.NextResultSet() {
r.atEnd = false
return true
}
r.endRead()
return false
}
// Scan copies the columns of the current row into dest, refusing a *sql.RawBytes destination.
func (r *Rows) Scan(dest ...any) error {
if slices.ContainsFunc(dest, isRawBytes) {
return errRawBytes
}
return r.Rows.Scan(dest...)
}
// Close closes the rows and gives the slot back, the first time only.
func (r *Rows) Close() error {
r.stop()
var err error
r.once.Do(func() {
live := r.ctx.Err() == nil
err = r.Rows.Close()
if live {
r.keep(nil)
}
r.release()
})
return err
}
// Err returns the error met while reading, marked as a deadline error when the statement deadline stopped it.
func (r *Rows) Err() error {
if kept := r.kept.Load(); kept != nil {
return *kept
}
return r.readErr()
}
// readErr returns the error of the read so far, the end of the statement context included.
func (r *Rows) readErr() error {
err := r.Rows.Err()
if cause := r.cause.Load(); err == nil && cause != nil && !r.atEnd {
err = *cause
}
return ended(r.ctx, err)
}
// endRead closes the rows the reader came to the end of, keeping their error, and gives the slot back.
func (r *Rows) endRead() {
r.stop()
r.once.Do(func() {
_ = r.Rows.Close()
r.keep(r.readErr())
r.release()
})
}
// expire records the end of the statement context and closes the rows once it ends.
func (r *Rows) expire() {
cause := r.ctx.Err()
r.cause.Store(&cause)
r.once.Do(func() {
_ = r.Rows.Close()
r.release()
})
}
// keep fixes the error Err reports from now on.
func (r *Rows) keep(err error) {
r.kept.Store(&err)
}
// release cancels the statement context and gives the slot back.
func (r *Rows) release() {
r.cancel()
r.give()
}
// Row is the result of QueryRow, holding its slot until Scan.
type Row struct {
rows *Rows
err error
}
// Scan copies the first row into dest and closes the rows.
func (r *Row) Scan(dest ...any) error {
if r.err != nil {
return r.err
}
err := r.scan(dest)
if closeErr := r.rows.Close(); err == nil {
err = closeErr
}
return err
}
// scan copies the first row into dest.
func (r *Row) scan(dest []any) error {
if slices.ContainsFunc(dest, isRawBytes) {
return errRawBytes
}
if !r.rows.Next() {
if err := r.rows.Err(); err != nil {
return err
}
return sql.ErrNoRows
}
return r.rows.Scan(dest...)
}
// Err returns the error of the query behind the row.
func (r *Row) Err() error {
return r.err
}
// isRawBytes reports whether dest is a *sql.RawBytes.
func isRawBytes(dest any) bool {
_, ok := dest.(*sql.RawBytes)
return ok
}