forked from go-pg/pg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stmt.go
237 lines (201 loc) · 4.95 KB
/
stmt.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
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
package pg
import (
"sync"
"time"
"gopkg.in/pg.v5/internal"
"gopkg.in/pg.v5/internal/pool"
"gopkg.in/pg.v5/orm"
"gopkg.in/pg.v5/types"
)
// Stmt is a prepared statement. Stmt is safe for concurrent use by
// multiple goroutines.
type Stmt struct {
db *DB
mu sync.Mutex
_cn *pool.Conn
inTx bool
q string
name string
columns [][]byte
stickyErr error
}
// Prepare creates a prepared statement for later queries or
// executions. Multiple queries or executions may be run concurrently
// from the returned statement.
func (db *DB) Prepare(q string) (*Stmt, error) {
cn, err := db.conn()
if err != nil {
return nil, err
}
return prepare(db, cn, q)
}
func (stmt *Stmt) conn() (*pool.Conn, error) {
if stmt._cn == nil {
if stmt.stickyErr != nil {
return nil, stmt.stickyErr
}
return nil, errStmtClosed
}
stmt._cn.SetReadTimeout(stmt.db.opt.ReadTimeout)
stmt._cn.SetWriteTimeout(stmt.db.opt.WriteTimeout)
return stmt._cn, nil
}
func (stmt *Stmt) exec(params ...interface{}) (*types.Result, error) {
defer stmt.mu.Unlock()
stmt.mu.Lock()
cn, err := stmt.conn()
if err != nil {
return nil, err
}
return extQuery(cn, stmt.name, params...)
}
// Exec executes a prepared statement with the given parameters.
func (stmt *Stmt) Exec(params ...interface{}) (res *types.Result, err error) {
for i := 0; i < 3; i++ {
res, err = stmt.exec(params...)
if i >= stmt.db.opt.MaxRetries {
break
}
if !stmt.db.shouldRetry(err) {
break
}
time.Sleep(internal.RetryBackoff << uint(i))
}
if err != nil {
stmt.setErr(err)
}
return
}
// 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{}) (*types.Result, error) {
res, err := stmt.Exec(params...)
if err != nil {
return nil, err
}
if err := internal.AssertOneRow(res.RowsAffected()); err != nil {
return nil, err
}
return res, nil
}
func (stmt *Stmt) query(model interface{}, params ...interface{}) (*types.Result, error) {
defer stmt.mu.Unlock()
stmt.mu.Lock()
cn, err := stmt.conn()
if err != nil {
return nil, err
}
res, mod, err := extQueryData(cn, stmt.name, model, stmt.columns, params...)
if err != nil {
return nil, err
}
if res.RowsReturned() > 0 && mod != nil {
if err = mod.AfterQuery(stmt.db); err != nil {
return res, err
}
}
return res, nil
}
// Query executes a prepared query statement with the given parameters.
func (stmt *Stmt) Query(model interface{}, params ...interface{}) (res *types.Result, err error) {
for i := 0; i < 3; i++ {
res, err = stmt.query(model, params...)
if i >= stmt.db.opt.MaxRetries {
break
}
if !stmt.db.shouldRetry(err) {
break
}
time.Sleep(internal.RetryBackoff << uint(i))
}
if err != nil {
stmt.setErr(err)
}
return
}
// 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{}) (*types.Result, error) {
mod, err := orm.NewModel(model)
if err != nil {
return nil, err
}
res, err := stmt.Query(mod, params...)
if err != nil {
return nil, err
}
if err := internal.AssertOneRow(res.RowsAffected()); err != nil {
return nil, err
}
return res, nil
}
func (stmt *Stmt) setErr(e error) {
if stmt.stickyErr == nil {
stmt.stickyErr = e
}
}
// Close closes the statement.
func (stmt *Stmt) Close() error {
defer stmt.mu.Unlock()
stmt.mu.Lock()
if stmt._cn == nil {
return errStmtClosed
}
err := closeStmt(stmt._cn, stmt.name)
if !stmt.inTx {
_ = stmt.db.freeConn(stmt._cn, err)
}
stmt._cn = nil
return err
}
func prepare(db *DB, cn *pool.Conn, q string) (*Stmt, error) {
name := cn.NextId()
writeParseDescribeSyncMsg(cn.Wr, name, q)
if err := cn.Wr.Flush(); err != nil {
db.freeConn(cn, err)
return nil, err
}
columns, err := readParseDescribeSync(cn)
if err != nil {
db.freeConn(cn, err)
return nil, err
}
stmt := &Stmt{
db: db,
_cn: cn,
q: q,
name: name,
columns: columns,
}
return stmt, nil
}
func extQuery(cn *pool.Conn, name string, params ...interface{}) (*types.Result, error) {
if err := writeBindExecuteMsg(cn.Wr, name, params...); err != nil {
return nil, err
}
if err := cn.Wr.Flush(); err != nil {
return nil, err
}
return readExtQuery(cn)
}
func extQueryData(
cn *pool.Conn, name string, model interface{}, columns [][]byte, params ...interface{},
) (*types.Result, orm.Model, error) {
if err := writeBindExecuteMsg(cn.Wr, name, params...); err != nil {
return nil, nil, err
}
if err := cn.Wr.Flush(); err != nil {
return nil, nil, err
}
return readExtQueryData(cn, model, columns)
}
func closeStmt(cn *pool.Conn, name string) error {
writeCloseMsg(cn.Wr, name)
writeFlushMsg(cn.Wr)
if err := cn.Wr.Flush(); err != nil {
return err
}
return readCloseCompleteMsg(cn)
}