forked from go-mysql-org/go-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstmt.go
285 lines (238 loc) · 6.92 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package client
import (
"encoding/binary"
"encoding/json"
"fmt"
"math"
"runtime"
. "github.com/go-mysql-org/go-mysql/mysql"
"github.com/go-mysql-org/go-mysql/utils"
"github.com/pingcap/errors"
)
type Stmt struct {
conn *Conn
id uint32
params int
columns int
warnings int
}
func (s *Stmt) ParamNum() int {
return s.params
}
func (s *Stmt) ColumnNum() int {
return s.columns
}
func (s *Stmt) WarningsNum() int {
return s.warnings
}
func (s *Stmt) Execute(args ...interface{}) (*Result, error) {
if err := s.write(args...); err != nil {
return nil, errors.Trace(err)
}
return s.conn.readResult(true)
}
func (s *Stmt) ExecuteSelectStreaming(result *Result, perRowCb SelectPerRowCallback, perResCb SelectPerResultCallback, args ...interface{}) error {
if err := s.write(args...); err != nil {
return errors.Trace(err)
}
return s.conn.readResultStreaming(true, result, perRowCb, perResCb)
}
func (s *Stmt) Close() error {
if err := s.conn.writeCommandUint32(COM_STMT_CLOSE, s.id); err != nil {
return errors.Trace(err)
}
return nil
}
// https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_com_stmt_execute.html
func (s *Stmt) write(args ...interface{}) error {
paramsNum := s.params
if len(args) != paramsNum {
return fmt.Errorf("argument mismatch, need %d but got %d", s.params, len(args))
}
if s.conn.includeLine >= 0 {
_, file, line, ok := runtime.Caller(s.conn.includeLine)
if ok {
lineAttr := QueryAttribute{
Name: "_line",
Value: fmt.Sprintf("%s:%d", file, line),
}
s.conn.queryAttributes = append(s.conn.queryAttributes, lineAttr)
}
}
qaLen := len(s.conn.queryAttributes)
paramTypes := make([][]byte, paramsNum+qaLen)
paramFlags := make([][]byte, paramsNum+qaLen)
paramValues := make([][]byte, paramsNum+qaLen)
paramNames := make([][]byte, paramsNum+qaLen)
//NULL-bitmap, length: (num-params+7)
nullBitmap := make([]byte, (paramsNum+qaLen+7)>>3)
length := 1 + 4 + 1 + 4 + ((paramsNum + 7) >> 3) + 1 + (paramsNum << 1)
var newParamBoundFlag byte = 0
for i := range args {
if args[i] == nil {
nullBitmap[i/8] |= 1 << (uint(i) % 8)
paramTypes[i] = []byte{MYSQL_TYPE_NULL}
paramNames[i] = []byte{0} // length encoded, no name
paramFlags[i] = []byte{0}
continue
}
newParamBoundFlag = 1
switch v := args[i].(type) {
case int8:
paramTypes[i] = []byte{MYSQL_TYPE_TINY}
paramValues[i] = []byte{byte(v)}
case int16:
paramTypes[i] = []byte{MYSQL_TYPE_SHORT}
paramValues[i] = Uint16ToBytes(uint16(v))
case int32:
paramTypes[i] = []byte{MYSQL_TYPE_LONG}
paramValues[i] = Uint32ToBytes(uint32(v))
case int:
paramTypes[i] = []byte{MYSQL_TYPE_LONGLONG}
paramValues[i] = Uint64ToBytes(uint64(v))
case int64:
paramTypes[i] = []byte{MYSQL_TYPE_LONGLONG}
paramValues[i] = Uint64ToBytes(uint64(v))
case uint8:
paramTypes[i] = []byte{MYSQL_TYPE_TINY}
paramFlags[i] = []byte{PARAM_UNSIGNED}
paramValues[i] = []byte{v}
case uint16:
paramTypes[i] = []byte{MYSQL_TYPE_SHORT}
paramFlags[i] = []byte{PARAM_UNSIGNED}
paramValues[i] = Uint16ToBytes(v)
case uint32:
paramTypes[i] = []byte{MYSQL_TYPE_LONG}
paramFlags[i] = []byte{PARAM_UNSIGNED}
paramValues[i] = Uint32ToBytes(v)
case uint:
paramTypes[i] = []byte{MYSQL_TYPE_LONGLONG}
paramFlags[i] = []byte{PARAM_UNSIGNED}
paramValues[i] = Uint64ToBytes(uint64(v))
case uint64:
paramTypes[i] = []byte{MYSQL_TYPE_LONGLONG}
paramFlags[i] = []byte{PARAM_UNSIGNED}
paramValues[i] = Uint64ToBytes(v)
case bool:
paramTypes[i] = []byte{MYSQL_TYPE_TINY}
if v {
paramValues[i] = []byte{1}
} else {
paramValues[i] = []byte{0}
}
case float32:
paramTypes[i] = []byte{MYSQL_TYPE_FLOAT}
paramValues[i] = Uint32ToBytes(math.Float32bits(v))
case float64:
paramTypes[i] = []byte{MYSQL_TYPE_DOUBLE}
paramValues[i] = Uint64ToBytes(math.Float64bits(v))
case string:
paramTypes[i] = []byte{MYSQL_TYPE_STRING}
paramValues[i] = append(PutLengthEncodedInt(uint64(len(v))), v...)
case []byte:
paramTypes[i] = []byte{MYSQL_TYPE_STRING}
paramValues[i] = append(PutLengthEncodedInt(uint64(len(v))), v...)
case json.RawMessage:
paramTypes[i] = []byte{MYSQL_TYPE_STRING}
paramValues[i] = append(PutLengthEncodedInt(uint64(len(v))), v...)
default:
return fmt.Errorf("invalid argument type %T", args[i])
}
paramNames[i] = []byte{0} // length encoded, no name
if paramFlags[i] == nil {
paramFlags[i] = []byte{0}
}
length += len(paramValues[i])
}
for i, qa := range s.conn.queryAttributes {
tf := qa.TypeAndFlag()
paramTypes[(i + paramsNum)] = []byte{tf[0]}
paramFlags[i+paramsNum] = []byte{tf[1]}
paramValues[i+paramsNum] = qa.ValueBytes()
paramNames[i+paramsNum] = PutLengthEncodedString([]byte(qa.Name))
}
data := utils.BytesBufferGet()
defer func() {
utils.BytesBufferPut(data)
}()
if data.Len() < length+4 {
data.Grow(4 + length)
}
data.Write([]byte{0, 0, 0, 0})
data.WriteByte(COM_STMT_EXECUTE)
data.Write([]byte{byte(s.id), byte(s.id >> 8), byte(s.id >> 16), byte(s.id >> 24)})
flags := CURSOR_TYPE_NO_CURSOR
if paramsNum > 0 {
flags |= PARAMETER_COUNT_AVAILABLE
}
data.WriteByte(flags)
//iteration-count, always 1
data.Write([]byte{1, 0, 0, 0})
if paramsNum > 0 || (s.conn.capability&CLIENT_QUERY_ATTRIBUTES > 0 && (flags&PARAMETER_COUNT_AVAILABLE > 0)) {
if s.conn.capability&CLIENT_QUERY_ATTRIBUTES > 0 {
paramsNum += len(s.conn.queryAttributes)
data.Write(PutLengthEncodedInt(uint64(paramsNum)))
}
if paramsNum > 0 {
data.Write(nullBitmap)
//new-params-bound-flag
data.WriteByte(newParamBoundFlag)
if newParamBoundFlag == 1 {
for i := 0; i < paramsNum; i++ {
data.Write(paramTypes[i])
data.Write(paramFlags[i])
if s.conn.capability&CLIENT_QUERY_ATTRIBUTES > 0 {
data.Write(paramNames[i])
}
}
//value of each parameter
for _, v := range paramValues {
data.Write(v)
}
}
}
}
s.conn.ResetSequence()
s.conn.queryAttributes = nil
return s.conn.WritePacket(data.Bytes())
}
func (c *Conn) Prepare(query string) (*Stmt, error) {
if err := c.writeCommandStr(COM_STMT_PREPARE, query); err != nil {
return nil, errors.Trace(err)
}
data, err := c.ReadPacket()
if err != nil {
return nil, errors.Trace(err)
}
if data[0] == ERR_HEADER {
return nil, c.handleErrorPacket(data)
} else if data[0] != OK_HEADER {
return nil, ErrMalformPacket
}
s := new(Stmt)
s.conn = c
pos := 1
//for statement id
s.id = binary.LittleEndian.Uint32(data[pos:])
pos += 4
//number columns
s.columns = int(binary.LittleEndian.Uint16(data[pos:]))
pos += 2
//number params
s.params = int(binary.LittleEndian.Uint16(data[pos:]))
pos += 2
//warnings
s.warnings = int(binary.LittleEndian.Uint16(data[pos:]))
// pos += 2
if s.params > 0 {
if err := s.conn.readUntilEOF(); err != nil {
return nil, errors.Trace(err)
}
}
if s.columns > 0 {
if err := s.conn.readUntilEOF(); err != nil {
return nil, errors.Trace(err)
}
}
return s, nil
}