Skip to content

Commit 7259ef4

Browse files
Merge pull request #120 from vartec/master
Column attributes as regular attributes instead of a dict
2 parents 0329902 + f67c8d7 commit 7259ef4

File tree

2 files changed

+46
-56
lines changed

2 files changed

+46
-56
lines changed

pymysqlreplication/column.py

Lines changed: 39 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,76 +10,70 @@ class Column(object):
1010
"""
1111

1212
def __init__(self, *args, **kwargs):
13-
self.data = {}
1413
if len(args) == 3:
1514
self.__parse_column_definition(*args)
1615
else:
17-
self.data = kwargs
16+
self.__dict__.update(kwargs)
1817

1918
def __parse_column_definition(self, column_type, column_schema, packet):
20-
self.data["type"] = column_type
21-
self.data["name"] = column_schema["COLUMN_NAME"]
22-
self.data["collation_name"] = column_schema["COLLATION_NAME"]
23-
self.data["character_set_name"] = column_schema["CHARACTER_SET_NAME"]
24-
self.data["comment"] = column_schema["COLUMN_COMMENT"]
25-
self.data["unsigned"] = False
26-
self.data["type_is_bool"] = False
27-
if column_schema["COLUMN_KEY"] == "PRI":
28-
self.data["is_primary"] = True
29-
else:
30-
self.data["is_primary"] = False
19+
self.type = column_type
20+
self.name = column_schema["COLUMN_NAME"]
21+
self.collation_name = column_schema["COLLATION_NAME"]
22+
self.character_set_name = column_schema["CHARACTER_SET_NAME"]
23+
self.comment = column_schema["COLUMN_COMMENT"]
24+
self.unsigned = column_schema["COLUMN_TYPE"].find("unsigned") != -1
25+
self.type_is_bool = False
26+
self.is_primary = column_schema["COLUMN_KEY"] == "PRI"
3127

32-
if column_schema["COLUMN_TYPE"].find("unsigned") != -1:
33-
self.data["unsigned"] = True
34-
if self.type == FIELD_TYPE.VAR_STRING or \
28+
if self.type == FIELD_TYPE.VARCHAR:
29+
self.max_length = struct.unpack('<H', packet.read(2))[0]
30+
elif self.type == FIELD_TYPE.DOUBLE:
31+
self.size = packet.read_uint8()
32+
elif self.type == FIELD_TYPE.FLOAT:
33+
self.size = packet.read_uint8()
34+
elif self.type == FIELD_TYPE.TIMESTAMP2:
35+
self.fsp = packet.read_uint8()
36+
elif self.type == FIELD_TYPE.DATETIME2:
37+
self.fsp = packet.read_uint8()
38+
elif self.type == FIELD_TYPE.TIME2:
39+
self.fsp = packet.read_uint8()
40+
elif self.type == FIELD_TYPE.TINY and \
41+
column_schema["COLUMN_TYPE"] == "tinyint(1)":
42+
self.type_is_bool = True
43+
elif self.type == FIELD_TYPE.VAR_STRING or \
3544
self.type == FIELD_TYPE.STRING:
3645
self.__read_string_metadata(packet, column_schema)
37-
elif self.type == FIELD_TYPE.VARCHAR:
38-
self.data["max_length"] = struct.unpack('<H', packet.read(2))[0]
3946
elif self.type == FIELD_TYPE.BLOB:
40-
self.data["length_size"] = packet.read_uint8()
47+
self.length_size = packet.read_uint8()
4148
elif self.type == FIELD_TYPE.GEOMETRY:
42-
self.data["length_size"] = packet.read_uint8()
49+
self.length_size = packet.read_uint8()
4350
elif self.type == FIELD_TYPE.NEWDECIMAL:
44-
self.data["precision"] = packet.read_uint8()
45-
self.data["decimals"] = packet.read_uint8()
46-
elif self.type == FIELD_TYPE.DOUBLE:
47-
self.data["size"] = packet.read_uint8()
48-
elif self.type == FIELD_TYPE.FLOAT:
49-
self.data["size"] = packet.read_uint8()
51+
self.precision = packet.read_uint8()
52+
self.decimals = packet.read_uint8()
5053
elif self.type == FIELD_TYPE.BIT:
5154
bits = packet.read_uint8()
5255
bytes = packet.read_uint8()
53-
self.data["bits"] = (bytes * 8) + bits
54-
self.data["bytes"] = int((self.bits + 7) / 8)
55-
elif self.type == FIELD_TYPE.TIMESTAMP2:
56-
self.data["fsp"] = packet.read_uint8()
57-
elif self.type == FIELD_TYPE.DATETIME2:
58-
self.data["fsp"] = packet.read_uint8()
59-
elif self.type == FIELD_TYPE.TIME2:
60-
self.data["fsp"] = packet.read_uint8()
61-
elif self.type == FIELD_TYPE.TINY and \
62-
column_schema["COLUMN_TYPE"] == "tinyint(1)":
63-
self.data["type_is_bool"] = True
56+
self.bits = (bytes * 8) + bits
57+
self.bytes = int((self.bits + 7) / 8)
6458

6559
def __read_string_metadata(self, packet, column_schema):
6660
metadata = (packet.read_uint8() << 8) + packet.read_uint8()
6761
real_type = metadata >> 8
6862
if real_type == FIELD_TYPE.SET or real_type == FIELD_TYPE.ENUM:
69-
self.data["type"] = real_type
70-
self.data["size"] = metadata & 0x00ff
63+
self.type = real_type
64+
self.size = metadata & 0x00ff
7165
self.__read_enum_metadata(column_schema)
7266
else:
73-
self.data["max_length"] = (((metadata >> 4) & 0x300) ^ 0x300) \
67+
self.max_length = (((metadata >> 4) & 0x300) ^ 0x300) \
7468
+ (metadata & 0x00ff)
7569

7670
def __read_enum_metadata(self, column_schema):
7771
enums = column_schema["COLUMN_TYPE"]
7872
if self.type == FIELD_TYPE.ENUM:
79-
self.data["enum_values"] = enums.replace('enum(', '')\
73+
self.enum_values = enums.replace('enum(', '')\
8074
.replace(')', '').replace('\'', '').split(',')
8175
else:
82-
self.data["set_values"] = enums.replace('set(', '')\
76+
self.set_values = enums.replace('set(', '')\
8377
.replace(')', '').replace('\'', '').split(',')
8478

8579
def __eq__(self, other):
@@ -91,8 +85,6 @@ def __ne__(self, other):
9185
def serializable_data(self):
9286
return self.data
9387

94-
def __getattr__(self, item):
95-
try:
96-
return self.data[item]
97-
except KeyError:
98-
raise AttributeError("{0} not found".format(item))
88+
@property
89+
def data(self):
90+
return dict((k, v) for (k, v) in self.__dict__.items() if not k.startswith('_'))

pymysqlreplication/table.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
class Table(object):
5-
def __init__(self, column_schemas, table_id, schema, table, columns, primary_key = None):
5+
def __init__(self, column_schemas, table_id, schema, table, columns, primary_key=None):
66
if primary_key is None:
77
primary_key = [c.data["name"] for c in columns if c.data["is_primary"]]
88
if len(primary_key) == 0:
@@ -12,26 +12,24 @@ def __init__(self, column_schemas, table_id, schema, table, columns, primary_key
1212
else:
1313
primary_key = tuple(primary_key)
1414

15-
self.data = {
15+
self.__dict__.update({
1616
"column_schemas": column_schemas,
1717
"table_id": table_id,
1818
"schema": schema,
1919
"table": table,
2020
"columns": columns,
2121
"primary_key": primary_key
22-
}
22+
})
2323

24-
def __getattr__(self, item):
25-
try:
26-
return self.data[item]
27-
except KeyError:
28-
raise AttributeError
24+
@property
25+
def data(self):
26+
return dict((k, v) for (k, v) in self.__dict__.items() if not k.startswith('_'))
2927

3028
def __eq__(self, other):
3129
return self.data == other.data
3230

3331
def __ne__(self, other):
34-
return self.__eq__(other)
32+
return not self.__eq__(other)
3533

3634
def serializable_data(self):
3735
return self.data

0 commit comments

Comments
 (0)