stmt.go
6.3 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
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
package pg
import (
"context"
"errors"
"time"
"github.com/go-pg/pg/internal"
"github.com/go-pg/pg/internal/pool"
"github.com/go-pg/pg/orm"
)
var errStmtClosed = errors.New("pg: statement is closed")
// Stmt is a prepared statement. Stmt is safe for concurrent use by
// multiple goroutines.
type Stmt struct {
db *baseDB
stickyErr error
q string
name string
columns [][]byte
}
func prepareStmt(db *baseDB, q string) (*Stmt, error) {
stmt := &Stmt{
db: db,
q: q,
}
err := stmt.prepare(q)
if err != nil {
_ = stmt.Close()
return nil, err
}
return stmt, nil
}
func (stmt *Stmt) prepare(q string) error {
var lastErr error
for attempt := 0; attempt <= stmt.db.opt.MaxRetries; attempt++ {
if attempt > 0 {
time.Sleep(stmt.db.retryBackoff(attempt - 1))
err := stmt.db.pool.(*pool.SingleConnPool).Reset()
if err != nil {
internal.Logf(err.Error())
continue
}
}
lastErr = stmt.withConn(nil, func(cn *pool.Conn) error {
var err error
stmt.name, stmt.columns, err = stmt.db.prepare(cn, q)
return err
})
if !stmt.db.shouldRetry(lastErr) {
break
}
}
return lastErr
}
func (stmt *Stmt) withConn(c context.Context, fn func(cn *pool.Conn) error) error {
if stmt.stickyErr != nil {
return stmt.stickyErr
}
err := stmt.db.withConn(c, fn)
if err == pool.ErrClosed {
return errStmtClosed
}
return err
}
// Exec executes a prepared statement with the given parameters.
func (stmt *Stmt) Exec(params ...interface{}) (Result, error) {
return stmt.exec(nil, params...)
}
// ExecContext executes a prepared statement with the given parameters.
func (stmt *Stmt) ExecContext(c context.Context, params ...interface{}) (Result, error) {
return stmt.exec(c, params...)
}
func (stmt *Stmt) exec(c context.Context, params ...interface{}) (res Result, err error) {
for attempt := 0; attempt <= stmt.db.opt.MaxRetries; attempt++ {
if attempt > 0 {
time.Sleep(stmt.db.retryBackoff(attempt - 1))
}
err = stmt.withConn(c, func(cn *pool.Conn) error {
event := stmt.db.queryStarted(c, stmt.db.db, stmt.q, params, attempt)
res, err = stmt.extQuery(cn, stmt.name, params...)
stmt.db.queryProcessed(res, err, event)
return err
})
if !stmt.db.shouldRetry(err) {
break
}
}
if err != nil {
return nil, err
}
return res, nil
}
// ExecOne acts like Exec, but query must affect only one row. It
// returns ErrNoRows error when query returns zero rows or
// ErrMultiRows when query returns multiple rows.
func (stmt *Stmt) ExecOne(params ...interface{}) (Result, error) {
return stmt.execOne(nil, params...)
}
func (stmt *Stmt) ExecOneContext(c context.Context, params ...interface{}) (Result, error) {
return stmt.execOne(c, params...)
}
func (stmt *Stmt) execOne(c context.Context, params ...interface{}) (Result, error) {
res, err := stmt.ExecContext(c, params...)
if err != nil {
return nil, err
}
if err := internal.AssertOneRow(res.RowsAffected()); err != nil {
return nil, err
}
return res, nil
}
// Query executes a prepared query statement with the given parameters.
func (stmt *Stmt) Query(model interface{}, params ...interface{}) (Result, error) {
return stmt.query(nil, model, params...)
}
func (stmt *Stmt) QueryContext(c context.Context, model interface{}, params ...interface{}) (Result, error) {
return stmt.query(c, model, params...)
}
func (stmt *Stmt) query(c context.Context, model interface{}, params ...interface{}) (res Result, err error) {
for attempt := 0; attempt <= stmt.db.opt.MaxRetries; attempt++ {
if attempt > 0 {
time.Sleep(stmt.db.retryBackoff(attempt - 1))
}
err = stmt.withConn(c, func(cn *pool.Conn) error {
event := stmt.db.queryStarted(c, stmt.db.db, stmt.q, params, attempt)
res, err = stmt.extQueryData(cn, stmt.name, model, stmt.columns, params...)
stmt.db.queryProcessed(res, err, event)
return err
})
if !stmt.db.shouldRetry(err) {
break
}
}
if err != nil {
return nil, err
}
if mod := res.Model(); mod != nil && res.RowsReturned() > 0 {
if err = mod.AfterQuery(c, stmt.db.db); err != nil {
return res, err
}
}
return res, nil
}
// QueryOne acts like Query, but query must return only one row. It
// returns ErrNoRows error when query returns zero rows or
// ErrMultiRows when query returns multiple rows.
func (stmt *Stmt) QueryOne(model interface{}, params ...interface{}) (Result, error) {
return stmt.queryOne(nil, model, params...)
}
func (stmt *Stmt) QueryOneContext(c context.Context, model interface{}, params ...interface{}) (Result, error) {
return stmt.queryOne(c, model, params...)
}
func (stmt *Stmt) queryOne(c context.Context, model interface{}, params ...interface{}) (Result, error) {
mod, err := orm.NewModel(model)
if err != nil {
return nil, err
}
res, err := stmt.QueryContext(c, mod, params...)
if err != nil {
return nil, err
}
if err := internal.AssertOneRow(res.RowsAffected()); err != nil {
return nil, err
}
return res, nil
}
// Close closes the statement.
func (stmt *Stmt) Close() error {
var firstErr error
if stmt.name != "" {
firstErr = stmt.closeStmt()
}
err := stmt.db.Close()
if err != nil && firstErr == nil {
firstErr = err
}
return firstErr
}
func (stmt *Stmt) extQuery(cn *pool.Conn, name string, params ...interface{}) (Result, error) {
err := cn.WithWriter(stmt.db.opt.WriteTimeout, func(wb *pool.WriteBuffer) error {
return writeBindExecuteMsg(wb, name, params...)
})
if err != nil {
return nil, err
}
var res Result
err = cn.WithReader(stmt.db.opt.ReadTimeout, func(rd *internal.BufReader) error {
res, err = readExtQuery(rd)
return err
})
if err != nil {
return nil, err
}
return res, nil
}
func (stmt *Stmt) extQueryData(
cn *pool.Conn, name string, model interface{}, columns [][]byte, params ...interface{},
) (Result, error) {
err := cn.WithWriter(stmt.db.opt.WriteTimeout, func(wb *pool.WriteBuffer) error {
return writeBindExecuteMsg(wb, name, params...)
})
if err != nil {
return nil, err
}
var res Result
err = cn.WithReader(stmt.db.opt.ReadTimeout, func(rd *internal.BufReader) error {
res, err = readExtQueryData(rd, model, columns)
return err
})
if err != nil {
return nil, err
}
return res, nil
}
func (stmt *Stmt) closeStmt() error {
return stmt.withConn(nil, func(cn *pool.Conn) error {
return stmt.db.closeStmt(cn, stmt.name)
})
}