forked from localhots/mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blt_api.go
72 lines (60 loc) · 1.41 KB
/
blt_api.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
package mysql
import (
"context"
"database/sql/driver"
"errors"
"time"
)
// ExtendConn creates an extended connection.
func ExtendConn(conn driver.Conn) (*ExtendedConn, error) {
if conn == nil {
return nil, errors.New("Connection is nil")
}
mc, ok := conn.(*mysqlConn)
if !ok || mc == nil {
return nil, errors.New("Invalid connection")
}
return &ExtendedConn{mc}, nil
}
// ExtendedConn provides access to internal packet functions.
type ExtendedConn struct {
*mysqlConn
}
// Close ...
func (c *ExtendedConn) Close() error {
c.buf.length = 0
return c.mysqlConn.Close()
}
// Exec ...
func (c *ExtendedConn) Exec(query string) error {
return c.exec(query)
}
// ReadPacket reads a packet from a given connection.
func (c *ExtendedConn) ReadPacket(ctx context.Context) ([]byte, error) {
if dl, ok := ctx.Deadline(); ok {
dur := dl.Sub(time.Now())
if dur < 0 {
return nil, context.DeadlineExceeded
}
c.buf.timeout = dur
} else {
c.buf.timeout = 0
}
return c.readPacket()
}
// WritePacket writes a packet to a given connection.
func (c *ExtendedConn) WritePacket(p []byte) error {
return c.writePacket(p)
}
// ReadResultOK ...
func (c *ExtendedConn) ReadResultOK() error {
return c.readResultOK()
}
// HandleErrorPacket ...
func (c *ExtendedConn) HandleErrorPacket(data []byte) error {
return c.handleErrorPacket(data)
}
// ResetSequence ...
func (c *ExtendedConn) ResetSequence() {
c.sequence = 0
}