forked from go-sql-driver/mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper.go
219 lines (186 loc) · 4.64 KB
/
wrapper.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
package mysql
import (
"database/sql/driver"
"encoding/binary"
"io"
)
type Packet struct {
data []byte
pos int
}
func NewPacket(data []byte) *Packet {
return &Packet{data: data}
}
func (p *Packet) Raw() []byte {
return p.data
}
// Len returns the total length of the buf of this packet.
func (p *Packet) Len() int {
return len(p.data)
}
// EOF returns if the buffer of this packet has been consumed completely.
func (p *Packet) EOF() bool {
return p.pos == len(p.data)
}
func (p *Packet) SliceRight(length int) (slice []byte) {
if length > len(p.data) {
return []byte{}
}
offset := len(p.data) - length
p.data, slice = p.data[:offset], p.data[offset:]
return
}
func (p *Packet) Skip(step int) {
p.pos += step
}
func (p *Packet) Read(size int) (result []byte) {
if size < 0 {
result = p.data[p.pos:]
p.pos = len(p.data)
} else {
result = p.data[p.pos : p.pos+size]
p.pos += size
}
return
}
func (p *Packet) ReadUintBySize(size int) (u uint64) {
switch {
case size == 0:
u = 0
case size == 1:
u = uint64(p.data[p.pos])
case size == 2:
u = uint64(binary.LittleEndian.Uint16(p.data[p.pos:]))
case size >= 3 && size <= 4:
var u32 uint32
for i := 0; i < size; i++ {
u32 |= uint32(p.data[p.pos+i]) << (uint(i) * 8)
}
u = uint64(u32)
case size >= 5 && size <= 8:
for i := 0; i < size; i++ {
u |= uint64(p.data[p.pos+i]) << (uint(i) * 8)
}
default:
panic("size must be between 0 and 8")
}
p.pos += size
return
}
func (p *Packet) ReadUintBySizeBE(size int) (u uint64) {
switch {
case size == 0:
u = 0
case size == 1:
u = uint64(p.data[p.pos])
case size == 2:
u = uint64(binary.BigEndian.Uint16(p.data[p.pos:]))
case size >= 3 && size <= 4:
var u32 uint32
for i := 0; i < size; i++ {
u32 |= uint32(p.data[p.pos+i]) << (uint(size-i-1) * 8)
}
u = uint64(u32)
case size >= 5 && size <= 8:
for i := 0; i < size; i++ {
u |= uint64(p.data[p.pos+i]) << (uint(size-i-1) * 8)
}
default:
panic("size must be between 0 and 8")
}
p.pos += size
return
}
func (p *Packet) ReadPackedInteger() uint64 {
num, _, n := readLengthEncodedInteger(p.data[p.pos:])
p.pos += n
return num
}
func (p *Packet) ReadPackedString() ([]byte, error) {
data, _, n, err := readLengthEncodedString(p.data[p.pos:])
if err != nil {
return nil, err
}
p.pos += n
return data, nil
}
// ConnWrapper wraps the unexported `mysqlConn` to export its functionalities.
type ConnWrapper struct {
*mysqlConn
drv driver.Driver
}
// NewConnWrapper create a new `mysql.ConnWrapper` instance.
func NewConnWrapper() *ConnWrapper {
return &ConnWrapper{drv: &MySQLDriver{}}
}
// Connect to the MySQL server.
func (cw *ConnWrapper) Connect(dsn string) error {
conn, err := cw.drv.Open(dsn)
if err != nil {
return err
}
cw.mysqlConn = conn.(*mysqlConn)
return nil
}
// ReadOK reads and checks the OK packet returned from the MySQL server.
func (cw *ConnWrapper) ReadOK() error {
_, err := cw.readResultOK()
return err
}
// ReadPacket read returned data from the MySQL server.
func (cw *ConnWrapper) ReadPacket() ([]byte, error) {
data, err := cw.readPacket()
if err != nil {
return nil, err
}
switch data[0] {
case iEOF:
return nil, io.EOF
case iERR:
return nil, cw.handleErrorPacket(data)
default:
buf := make([]byte, len(data)-1)
copy(buf, data[1:])
return buf, nil
}
}
// WriteRegisterSlaveCommand send `RegisterSlave` command to the MySQL server.
func (cw *ConnWrapper) WriteRegisterSlaveCommand(serverID uint32, localhost, user, password string, port uint16) error {
data := make([]byte, 4+1+len(localhost)+1+len(user)+1+len(password)+2+4+4)
pos := 0
binary.LittleEndian.PutUint32(data[pos:], serverID)
pos += 4
data[pos] = byte(len(localhost))
pos++
n := copy(data[pos:], localhost)
pos += n
data[pos] = byte(len(user))
pos++
n = copy(data[pos:], user)
pos += n
data[pos] = byte(len(password))
pos++
n = copy(data[pos:], password)
pos += n
binary.LittleEndian.PutUint16(data[pos:], port)
pos += 2
// replication rank, not used
binary.LittleEndian.PutUint32(data[pos:], 0)
pos += 4
// master ID, 0 is OK
binary.LittleEndian.PutUint32(data[pos:], 0)
return cw.writeCommandPacketStr(comRegisterSlave, string(data))
}
// WriteBinlogDumpCommand sends the `BinlogDump` command to the MySQL server.
func (cw *ConnWrapper) WriteBinlogDumpCommand(serverID uint32, file string, position uint32) error {
data := make([]byte, 4+2+4+len(file))
pos := 0
binary.LittleEndian.PutUint32(data[pos:], position)
pos += 4
binary.LittleEndian.PutUint16(data[pos:], 0)
pos += 2
binary.LittleEndian.PutUint32(data[pos:], serverID)
pos += 4
copy(data[pos:], file)
return cw.writeCommandPacketStr(comBinlogDump, string(data))
}