-
-
Notifications
You must be signed in to change notification settings - Fork 955
Expand file tree
/
Copy pathstmt.go
More file actions
162 lines (143 loc) · 3.14 KB
/
stmt.go
File metadata and controls
162 lines (143 loc) · 3.14 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
package pq
import (
"context"
"database/sql/driver"
"fmt"
"os"
"github.com/lib/pq/internal/proto"
"github.com/lib/pq/oid"
)
type stmt struct {
cn *conn
name string
rowsHeader
colFmtData []byte
paramTyps []oid.Oid
closed bool
}
func (st *stmt) Close() error {
if st.closed {
return nil
}
if err := st.cn.err.get(); err != nil {
return err
}
w := st.cn.writeBuf(proto.Close)
w.byte(proto.Sync)
w.string(st.name)
err := st.cn.send(w)
if err != nil {
return st.cn.handleError(err)
}
err = st.cn.send(st.cn.writeBuf(proto.Sync))
if err != nil {
return st.cn.handleError(err)
}
t, _, err := st.cn.recv1()
if err != nil {
return st.cn.handleError(err)
}
if t != proto.CloseComplete {
st.cn.err.set(driver.ErrBadConn)
return fmt.Errorf("pq: unexpected close response: %q", t)
}
st.closed = true
t, r, err := st.cn.recv1()
if err != nil {
return st.cn.handleError(err)
}
if t != proto.ReadyForQuery {
st.cn.err.set(driver.ErrBadConn)
return fmt.Errorf("pq: expected ready for query, but got: %q", t)
}
st.cn.processReadyForQuery(r)
return nil
}
// Implement [driver.StmtQueryContext].
func (st *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
finish := st.cn.watchCancel(ctx, true)
if err := st.cn.err.get(); err != nil {
return nil, err
}
err := st.exec(args)
if err != nil {
finish()
return nil, st.cn.handleError(err)
}
return &rows{
cn: st.cn,
rowsHeader: st.rowsHeader,
finish: finish,
}, nil
}
// Implement [driver.StmtExecContext].
func (st *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
defer st.cn.watchCancel(ctx, true)()
if err := st.cn.err.get(); err != nil {
return nil, err
}
err := st.exec(args)
if err != nil {
return nil, st.cn.handleError(err)
}
res, _, err := st.cn.readExecuteResponse("simple query")
return res, st.cn.handleError(err)
}
func (st *stmt) exec(v []driver.NamedValue) error {
if debugProto {
fmt.Fprintf(os.Stderr, " START stmt.exec\n")
defer fmt.Fprintf(os.Stderr, " END stmt.exec\n")
}
if len(v) >= 65536 {
return fmt.Errorf("pq: got %d parameters but PostgreSQL only supports 65535 parameters", len(v))
}
if len(v) != len(st.paramTyps) {
return fmt.Errorf("pq: got %d parameters but the statement requires %d", len(v), len(st.paramTyps))
}
cn := st.cn
w := cn.writeBuf(proto.Bind)
w.byte(0) // unnamed portal
w.string(st.name)
if cn.cfg.BinaryParameters {
err := cn.sendBinaryParameters(w, v)
if err != nil {
return err
}
} else {
w.int16(0)
w.int16(len(v))
for i, x := range v {
if x.Value == nil {
w.int32(-1)
} else {
b, err := encode(x.Value, st.paramTyps[i])
if err != nil {
return err
}
if b == nil {
w.int32(-1)
} else {
w.int32(len(b))
w.bytes(b)
}
}
}
}
w.bytes(st.colFmtData)
w.next(proto.Execute)
w.byte(0)
w.int32(0)
w.next(proto.Sync)
err := cn.send(w)
if err != nil {
return err
}
err = cn.readBindResponse()
if err != nil {
return err
}
return cn.postExecuteWorkaround()
}
func (st *stmt) NumInput() int {
return len(st.paramTyps)
}