Skip to content

Commit 98a4ecf

Browse files
Merge pull request #348 from hkwi/pymysql_1_0
Add pymysql 1.0 support
2 parents 33d93d9 + bac1bb4 commit 98a4ecf

File tree

5 files changed

+16
-23
lines changed

5 files changed

+16
-23
lines changed

pymysqlreplication/binlogstream.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
from pymysql.constants.COMMAND import COM_BINLOG_DUMP, COM_REGISTER_SLAVE
88
from pymysql.cursors import DictCursor
9-
from pymysql.util import int2byte
109

1110
from .packet import BinLogPacketWrapper
1211
from .constants.BINLOG import TABLE_MAP_EVENT, ROTATE_EVENT
@@ -109,7 +108,7 @@ def encoded(self, server_id, master_id=0):
109108
MAX_STRING_LEN = 257 # one byte for length + 256 chars
110109

111110
return (struct.pack('<i', packet_len) +
112-
int2byte(COM_REGISTER_SLAVE) +
111+
bytes(bytearray([COM_REGISTER_SLAVE])) +
113112
struct.pack('<L', server_id) +
114113
struct.pack('<%dp' % min(MAX_STRING_LEN, lhostname + 1),
115114
self.hostname.encode()) +
@@ -321,7 +320,7 @@ def __connect_to_stream(self):
321320
cur.close()
322321

323322
prelude = struct.pack('<i', len(self.log_file) + 11) \
324-
+ int2byte(COM_BINLOG_DUMP)
323+
+ bytes(bytearray([COM_BINLOG_DUMP]))
325324

326325
if self.__resume_stream:
327326
prelude += struct.pack('<I', self.log_pos)
@@ -382,7 +381,7 @@ def __connect_to_stream(self):
382381
4) # encoded_data_size
383382

384383
prelude = b'' + struct.pack('<i', header_size + encoded_data_size)\
385-
+ int2byte(COM_BINLOG_DUMP_GTID)
384+
+ bytes(bytearray([COM_BINLOG_DUMP_GTID]))
386385

387386
flags = 0
388387
if not self.__blocking:

pymysqlreplication/event.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import struct
55
import datetime
66

7-
from pymysql.util import byte2int, int2byte
8-
97

108
class BinLogEvent(object):
119
def __init__(self, from_packet, event_size, table_map, ctl_connection,
@@ -30,7 +28,7 @@ def __init__(self, from_packet, event_size, table_map, ctl_connection,
3028
def _read_table_id(self):
3129
# Table ID is 6 byte
3230
# pad little-endian number
33-
table_id = self.packet.read(6) + int2byte(0) + int2byte(0)
31+
table_id = self.packet.read(6) + b"\x00\x00"
3432
return struct.unpack('<Q', table_id)[0]
3533

3634
def dump(self):
@@ -55,7 +53,7 @@ def __init__(self, from_packet, event_size, table_map, ctl_connection, **kwargs)
5553
super(GtidEvent, self).__init__(from_packet, event_size, table_map,
5654
ctl_connection, **kwargs)
5755

58-
self.commit_flag = byte2int(self.packet.read(1)) == 1
56+
self.commit_flag = struct.unpack("!B", self.packet.read(1))[0] == 1
5957
self.sid = self.packet.read(16)
6058
self.gno = struct.unpack('<Q', self.packet.read(8))[0]
6159

@@ -164,7 +162,7 @@ def __init__(self, from_packet, event_size, table_map, ctl_connection, **kwargs)
164162
# Post-header
165163
self.slave_proxy_id = self.packet.read_uint32()
166164
self.execution_time = self.packet.read_uint32()
167-
self.schema_length = byte2int(self.packet.read(1))
165+
self.schema_length = struct.unpack("!B", self.packet.read(1))[0]
168166
self.error_code = self.packet.read_uint16()
169167
self.status_vars_length = self.packet.read_uint16()
170168

pymysqlreplication/packet.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import struct
44

5-
from pymysql.util import byte2int
6-
75
from pymysqlreplication import constants, event, row_event
86

97
# Constants from PyMYSQL source code
@@ -107,11 +105,11 @@ def __init__(self, from_packet, table_map, ctl_connection, use_checksum,
107105
# server_id
108106
# log_pos
109107
# flags
110-
unpack = struct.unpack('<cIcIIIH', self.packet.read(20))
108+
unpack = struct.unpack('<cIBIIIH', self.packet.read(20))
111109

112110
# Header
113111
self.timestamp = unpack[1]
114-
self.event_type = byte2int(unpack[2])
112+
self.event_type = unpack[2]
115113
self.server_id = unpack[3]
116114
self.event_size = unpack[4]
117115
# position of the next event
@@ -178,7 +176,7 @@ def read_length_coded_binary(self):
178176
179177
From PyMYSQL source code
180178
"""
181-
c = byte2int(self.read(1))
179+
c = struct.unpack("!B", self.read(1))[0]
182180
if c == NULL_COLUMN:
183181
return None
184182
if c < UNSIGNED_CHAR_COLUMN:
@@ -263,7 +261,7 @@ def read_variable_length_string(self):
263261
length = 0
264262
bits_read = 0
265263
while byte & 0x80 != 0:
266-
byte = byte2int(self.read(1))
264+
byte = struct.unpack("!B", self.read(1))[0]
267265
length = length | ((byte & 0x7f) << bits_read)
268266
bits_read = bits_read + 7
269267
return self.read(length)

pymysqlreplication/row_event.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import datetime
66
import json
77

8-
from pymysql.util import byte2int
98
from pymysql.charset import charset_by_name
109

1110
from .event import BinLogEvent
@@ -556,10 +555,10 @@ def __init__(self, from_packet, event_size, table_map, ctl_connection, **kwargs)
556555
self.flags = struct.unpack('<H', self.packet.read(2))[0]
557556

558557
# Payload
559-
self.schema_length = byte2int(self.packet.read(1))
558+
self.schema_length = struct.unpack("!B", self.packet.read(1))[0]
560559
self.schema = self.packet.read(self.schema_length).decode()
561560
self.packet.advance(1)
562-
self.table_length = byte2int(self.packet.read(1))
561+
self.table_length = struct.unpack("!B", self.packet.read(1))[0]
563562
self.table = self.packet.read(self.table_length).decode()
564563

565564
if self.__only_tables is not None and self.table not in self.__only_tables:
@@ -590,7 +589,7 @@ def __init__(self, from_packet, event_size, table_map, ctl_connection, **kwargs)
590589

591590
if len(self.column_schemas) != 0:
592591
# Read columns meta data
593-
column_types = list(self.packet.read(self.column_count))
592+
column_types = bytearray(self.packet.read(self.column_count))
594593
self.packet.read_length_coded_binary()
595594
for i in range(0, len(column_types)):
596595
column_type = column_types[i]
@@ -617,7 +616,7 @@ def __init__(self, from_packet, event_size, table_map, ctl_connection, **kwargs)
617616
'COLUMN_TYPE': 'BLOB', # we don't know what it is, so let's not do anything with it.
618617
'COLUMN_KEY': '',
619618
}
620-
col = Column(byte2int(column_type), column_schema, from_packet)
619+
col = Column(column_type, column_schema, from_packet)
621620
self.columns.append(col)
622621

623622
self.table_obj = Table(self.column_schemas, self.table_id, self.schema,

pymysqlreplication/tests/binlogfilereader.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'''Read binlog files'''
22
import struct
33

4-
from pymysql.util import byte2int
54
from pymysqlreplication import constants
65
from pymysqlreplication.event import FormatDescriptionEvent
76
from pymysqlreplication.event import QueryEvent
@@ -111,9 +110,9 @@ class SimpleBinLogEvent(object):
111110

112111
def __init__(self, header):
113112
'''Initialize the Event with the event header'''
114-
unpacked = struct.unpack('<IcIIIH', header)
113+
unpacked = struct.unpack('<IBIIIH', header)
115114
self.timestamp = unpacked[0]
116-
self.event_type = byte2int(unpacked[1])
115+
self.event_type = unpacked[1]
117116
self.server_id = unpacked[2]
118117
self.event_size = unpacked[3]
119118
self.log_pos = unpacked[4]

0 commit comments

Comments
 (0)