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
|
from abc import ABC, abstractmethod
import MySQLdb
class Wrapper(ABC):
def __init__(self):
self.schemes = {}
@abstractmethod
def clear_table(self, table_name):
pass
@abstractmethod
def get_column_names(self):
pass
@abstractmethod
def insert_one(self, table_name, data_row: dict):
pass
@abstractmethod
def update(self, table_name, expressions, conditions):
pass
@abstractmethod
def delete_from(self, table_name, conditions):
pass
@abstractmethod
def get_data(self, table_name):
pass
@abstractmethod
def get_rows(self, table_name, conditions):
pass
class MySQLWrapper(Wrapper):
def __init__(self, host, username, password, db_name):
super().__init__()
self.connection = MySQLdb.connect(
host=host,
user=username,
passwd=password,
db=db_name
)
def clear_table(self, table_name):
cursor = self.connection.cursor()
cursor.execute(f"START TRANSACTION; DELETE FROM `{table_name}`; COMMIT;")
cursor.close()
def get_column_names(self):
cursor = self.connection.cursor()
cursor.execute('DESCRIBE table_task1;')
table_structure = cursor.fetchall()
table_headers = [field[0] for field in table_structure]
return table_headers
def insert_one(self, table_name, data_row: dict):
cursor = self.connection.cursor()
scheme = self.schemes[table_name]
field_names = []
values = []
for field_name, value in data_row.items():
field_names.append(f'`{field_name}`')
if scheme.fields[field_name].data_type == str:
values.append(f'"{value}"')
else:
values.append(value)
request = "START TRANSACTION; INSERT INTO `{}` ({}) VALUES ({}); COMMIT;".format(
table_name, ",".join(field_names), ",".join(values)
)
cursor.execute(request)
cursor.close()
def update(self, table_name, expressions, conditions):
cursor = self.connection.cursor()
expressions_formatted = []
for field_name, value in expressions.items():
if value != 'NULL' or not value.isnumeric():
value = f'"{value}"'
expressions_formatted.append(f'`{field_name}`={value}')
conditions_formatted = []
for field_name, value in conditions.items():
if value != 'NULL' or not value.isnumeric():
value = f'"{value}"'
conditions_formatted.append(f'`{field_name}`={value}')
cursor.execute("START TRANSACTION; UPDATE `{}` SET {} WHERE {}; COMMIT;".format(
table_name, ','.join(expressions_formatted), ' AND '.join(conditions_formatted)
))
cursor.close()
def delete_from(self, table_name, conditions):
cursor = self.connection.cursor()
conditions_formatted = []
for field_name, value in conditions.items():
if value != 'NULL' or not value.isnumeric():
value = f'"{value}"'
conditions_formatted.append(f'`{field_name}`={value}')
cursor.execute("START TRANSACTION; DELETE FROM `{}` WHERE {}; COMMIT;".format(
table_name, ' AND '.join(conditions_formatted)
))
cursor.close()
def get_data(self, table_name):
cursor = self.connection.cursor()
cursor.execute(f'SELECT * FROM `{table_name}`;')
content = list(map(list, cursor.fetchall()))
cursor.close()
return content
def get_rows(self, table_name, conditions):
cursor = self.connection.cursor()
conditions_formatted = []
for field_name, value in conditions.items():
if value != 'NULL' or not value.isnumeric():
value = f'"{value}"'
conditions_formatted.append(f'`{field_name}`={value}')
cursor.execute(f'SELECT * FROM `{table_name}` WHERE {" AND ".join(conditions_formatted)};')
content = list(map(list, cursor.fetchall()))
cursor.close()
return content
|