-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
405 lines (328 loc) · 9.22 KB
/
conn.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package client
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"net"
"strings"
"time"
. "github.com/892294101/go-mysql/mysql"
"github.com/892294101/go-mysql/packet"
"github.com/892294101/go-mysql/utils"
"github.com/pingcap/errors"
)
type Conn struct {
*packet.Conn
user string
password string
db string
tlsConfig *tls.Config
proto string
// server capabilities
capability uint32
// client-set capabilities only
ccaps uint32
attributes map[string]string
status uint16
charset string
salt []byte
authPluginName string
connectionID uint32
}
// This function will be called for every row in resultset from ExecuteSelectStreaming.
type SelectPerRowCallback func(row []FieldValue) error
// This function will be called once per result from ExecuteSelectStreaming
type SelectPerResultCallback func(result *Result) error
// This function will be called once per result from ExecuteMultiple
type ExecPerResultCallback func(result *Result, err error)
func getNetProto(addr string) string {
proto := "tcp"
if strings.Contains(addr, "/") {
proto = "unix"
}
return proto
}
// Connect to a MySQL server, addr can be ip:port, or a unix socket domain like /var/sock.
// Accepts a series of configuration functions as a variadic argument.
func Connect(addr string, user string, password string, dbName string, options ...func(*Conn)) (*Conn, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
dialer := &net.Dialer{}
return ConnectWithDialer(ctx, "", addr, user, password, dbName, dialer.DialContext, options...)
}
// Dialer connects to the address on the named network using the provided context.
type Dialer func(ctx context.Context, network, address string) (net.Conn, error)
// Connect to a MySQL server using the given Dialer.
func ConnectWithDialer(ctx context.Context, network string, addr string, user string, password string, dbName string, dialer Dialer, options ...func(*Conn)) (*Conn, error) {
c := new(Conn)
if network == "" {
network = getNetProto(addr)
}
var err error
conn, err := dialer(ctx, network, addr)
if err != nil {
return nil, errors.Trace(err)
}
c.user = user
c.password = password
c.db = dbName
c.proto = network
c.Conn = packet.NewConn(conn)
// use default charset here, utf-8
c.charset = DEFAULT_CHARSET
// Apply configuration functions.
for i := range options {
options[i](c)
}
if c.tlsConfig != nil {
seq := c.Conn.Sequence
c.Conn = packet.NewTLSConn(conn)
c.Conn.Sequence = seq
}
if err = c.handshake(); err != nil {
return nil, errors.Trace(err)
}
return c, nil
}
func (c *Conn) handshake() error {
var err error
if err = c.readInitialHandshake(); err != nil {
c.Close()
return errors.Trace(err)
}
if err := c.writeAuthHandshake(); err != nil {
c.Close()
return errors.Trace(err)
}
if err := c.handleAuthResult(); err != nil {
c.Close()
return errors.Trace(err)
}
return nil
}
func (c *Conn) Close() error {
return c.Conn.Close()
}
func (c *Conn) Ping() error {
if err := c.writeCommand(COM_PING); err != nil {
return errors.Trace(err)
}
if _, err := c.readOK(); err != nil {
return errors.Trace(err)
}
return nil
}
// SetCapability enables the use of a specific capability
func (c *Conn) SetCapability(cap uint32) {
c.ccaps |= cap
}
// UnsetCapability disables the use of a specific capability
func (c *Conn) UnsetCapability(cap uint32) {
c.ccaps &= ^cap
}
// UseSSL: use default SSL
// pass to options when connect
func (c *Conn) UseSSL(insecureSkipVerify bool) {
c.tlsConfig = &tls.Config{InsecureSkipVerify: insecureSkipVerify}
}
// SetTLSConfig: use user-specified TLS config
// pass to options when connect
func (c *Conn) SetTLSConfig(config *tls.Config) {
c.tlsConfig = config
}
func (c *Conn) UseDB(dbName string) error {
if c.db == dbName {
return nil
}
if err := c.writeCommandStr(COM_INIT_DB, dbName); err != nil {
return errors.Trace(err)
}
if _, err := c.readOK(); err != nil {
return errors.Trace(err)
}
c.db = dbName
return nil
}
func (c *Conn) GetDB() string {
return c.db
}
func (c *Conn) Execute(command string, args ...interface{}) (*Result, error) {
if len(args) == 0 {
return c.exec(command)
} else {
if s, err := c.Prepare(command); err != nil {
return nil, errors.Trace(err)
} else {
var r *Result
r, err = s.Execute(args...)
s.Close()
return r, err
}
}
}
// ExecuteMultiple will call perResultCallback for every result of the multiple queries
// that are executed.
//
// When ExecuteMultiple is used, the connection should have the SERVER_MORE_RESULTS_EXISTS
// flag set to signal the server multiple queries are executed. Handling the responses
// is up to the implementation of perResultCallback.
//
// Example:
//
// queries := "SELECT 1; SELECT NOW();"
// conn.ExecuteMultiple(queries, func(result *mysql.Result, err error) {
// // Use the result as you want
// })
//
func (c *Conn) ExecuteMultiple(query string, perResultCallback ExecPerResultCallback) (*Result, error) {
if err := c.writeCommandStr(COM_QUERY, query); err != nil {
return nil, errors.Trace(err)
}
var err error
var result *Result
bs := utils.ByteSliceGet(16)
defer utils.ByteSlicePut(bs)
for {
bs.B, err = c.ReadPacketReuseMem(bs.B[:0])
if err != nil {
return nil, errors.Trace(err)
}
switch bs.B[0] {
case OK_HEADER:
result, err = c.handleOKPacket(bs.B)
case ERR_HEADER:
err = c.handleErrorPacket(bytes.Repeat(bs.B, 1))
result = nil
case LocalInFile_HEADER:
err = ErrMalformPacket
result = nil
default:
result, err = c.readResultset(bs.B, false)
}
// call user-defined callback
perResultCallback(result, err)
// if there was an error of this was the last result, stop looping
if err != nil || result.Status&SERVER_MORE_RESULTS_EXISTS == 0 {
break
}
}
// return an empty result(set) signaling we're done streaming a multiple
// streaming session
// if this would end up in WriteValue, it would just be ignored as all
// responses should have been handled in perResultCallback
return &Result{Resultset: &Resultset{
Streaming: StreamingMultiple,
StreamingDone: true,
}}, nil
}
// ExecuteSelectStreaming will call perRowCallback for every row in resultset
// WITHOUT saving any row data to Result.{Values/RawPkg/RowDatas} fields.
// When given, perResultCallback will be called once per result
//
// ExecuteSelectStreaming should be used only for SELECT queries with a large response resultset for memory preserving.
//
// Example:
//
// var result mysql.Result
// conn.ExecuteSelectStreaming(`SELECT ... LIMIT 100500`, &result, func(row []mysql.FieldValue) error {
// // Use the row as you want.
// // You must not save FieldValue.AsString() value after this callback is done. Copy it if you need.
// return nil
// }, nil)
//
func (c *Conn) ExecuteSelectStreaming(command string, result *Result, perRowCallback SelectPerRowCallback, perResultCallback SelectPerResultCallback) error {
if err := c.writeCommandStr(COM_QUERY, command); err != nil {
return errors.Trace(err)
}
return c.readResultStreaming(false, result, perRowCallback, perResultCallback)
}
func (c *Conn) Begin() error {
_, err := c.exec("BEGIN")
return errors.Trace(err)
}
func (c *Conn) Commit() error {
_, err := c.exec("COMMIT")
return errors.Trace(err)
}
func (c *Conn) Rollback() error {
_, err := c.exec("ROLLBACK")
return errors.Trace(err)
}
func (c *Conn) SetAttributes(attributes map[string]string) {
c.attributes = attributes
}
func (c *Conn) SetCharset(charset string) error {
if c.charset == charset {
return nil
}
if _, err := c.exec(fmt.Sprintf("SET NAMES %s", charset)); err != nil {
return errors.Trace(err)
} else {
c.charset = charset
return nil
}
}
func (c *Conn) FieldList(table string, wildcard string) ([]*Field, error) {
if err := c.writeCommandStrStr(COM_FIELD_LIST, table, wildcard); err != nil {
return nil, errors.Trace(err)
}
data, err := c.ReadPacket()
if err != nil {
return nil, errors.Trace(err)
}
fs := make([]*Field, 0, 4)
var f *Field
if data[0] == ERR_HEADER {
return nil, c.handleErrorPacket(data)
}
for {
if data, err = c.ReadPacket(); err != nil {
return nil, errors.Trace(err)
}
// EOF Packet
if c.isEOFPacket(data) {
return fs, nil
}
if f, err = FieldData(data).Parse(); err != nil {
return nil, errors.Trace(err)
}
fs = append(fs, f)
}
}
func (c *Conn) SetAutoCommit() error {
if !c.IsAutoCommit() {
if _, err := c.exec("SET AUTOCOMMIT = 1"); err != nil {
return errors.Trace(err)
}
}
return nil
}
func (c *Conn) IsAutoCommit() bool {
return c.status&SERVER_STATUS_AUTOCOMMIT > 0
}
func (c *Conn) IsInTransaction() bool {
return c.status&SERVER_STATUS_IN_TRANS > 0
}
func (c *Conn) GetCharset() string {
return c.charset
}
func (c *Conn) GetConnectionID() uint32 {
return c.connectionID
}
func (c *Conn) HandleOKPacket(data []byte) *Result {
r, _ := c.handleOKPacket(data)
return r
}
func (c *Conn) HandleErrorPacket(data []byte) error {
return c.handleErrorPacket(data)
}
func (c *Conn) ReadOKPacket() (*Result, error) {
return c.readOK()
}
func (c *Conn) exec(query string) (*Result, error) {
if err := c.writeCommandStr(COM_QUERY, query); err != nil {
return nil, errors.Trace(err)
}
return c.readResult(false)
}