-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysql_handler.py
More file actions
381 lines (332 loc) · 14.4 KB
/
mysql_handler.py
File metadata and controls
381 lines (332 loc) · 14.4 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
"""
Created on 2022-02-01
@author: Julian Briggs
Docs on installing and using 3 Python mysql connectors: mysql.connector, mysqlDB, pymysql
https://www.a2hosting.co.uk/kb/developer-corner/mysql/connecting-to-mysql-using-python
https://py-pkgs.org/07-releasing-versioning.html
Cursors:
https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlconnection-cursor.html
https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-close.html
Use close() when you are done using a cursor. This method closes the cursor, resets all results,
and ensures that the cursor object has no reference to its original connection object.
https://stackoverflow.com/questions/5669878/when-to-close-cursors-using-mysqldb
Google Cloud SQL defaults to MySQL-8.0.18 (@ 2022-05-19) but can upgrade: gcloud sql instances patch sheffieldsolar --database-version=MYSQL_8_0_28
I reviewed the code and attempted to leave suggestions for improving its quality. While most comments were applied successfully, one of the patterns I used did not match correctly. Here's what I tried to address:
"""
from argparse import Namespace
from contextlib import AbstractContextManager, closing
from mysql import connector
from typing import Any, Dict, Tuple, Sequence, Optional, List
import logging
import traceback
logger = logging.getLogger(__name__)
# Define a type alias for rows returned from the database
Rows = Sequence[Tuple[Any, ...]]
class MysqlHandler(AbstractContextManager):
"""
A context manager for handling MySQL database operations.
Provides methods to execute queries, inserts, and handle database transactions.
Automatically closes the connection when the context is exited.
"""
@staticmethod
def override_mysql_options(config: Namespace):
"""
Override the MySQL options based on the provided configuration.
Updates specific fields like database, host, user, and password if available.
"""
if config.mysql_database:
config.mysql_options.update({"database": config.mysql_database})
if config.mysql_host:
config.mysql_options.update({"host": config.mysql_host})
if config.mysql_password:
config.mysql_options.update({"password": config.mysql_password})
if config.mysql_user:
config.mysql_options.update({"user": config.mysql_user})
@staticmethod
def redact_mysql_options(mysql_options: Dict[str, str]) -> Dict[str, str]:
"""
Redact sensitive information (e.g., password) in MySQL options for logging purposes.
Args:
mysql_options (Dict[str, str]): Dictionary containing MySQL connection options.
Returns:
Dict[str, str]: A copy of the options with sensitive information redacted.
"""
mysql_options_redacted = mysql_options.copy()
mysql_options_redacted.update({"password": "REDACTED"})
return mysql_options_redacted
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, exc_tb):
"""
Ensures the database connection is closed when exiting the context.
Logs logger.critical errors if a MySQL error occurs.
"""
logger.debug("Exiting context and closing the connection.")
self.cnx.close()
if isinstance(exc_value, connector.errors.Error):
logger.critical(
"Database error %(exc_type)s %(exc_value)s mysql_options_redacted %(mysql_options_redacted)s %(stack)s",
{
"exc_type": exc_type,
"exc_value": exc_value,
"mysql_options_redacted": self.mysql_options_redacted,
"stack": traceback.format_exc(),
},
)
return False # Propagate exception
def __init__(self, mysql_options, cnx=None):
"""
Initialize the MySQL handler.
Args:
mysql_options (dict): Dictionary of MySQL connection options.
cnx (Optional[connector.MySQLConnection]): Optional existing connection object.
"""
logger.debug("Initializing MysqlHandler with options.")
self.mysql_options = mysql_options
self.mysql_options_redacted = MysqlHandler.redact_mysql_options(mysql_options)
if cnx:
logger.debug("Using provided connection object.")
self.cnx = cnx
else:
logger.debug(
"MysqlHandler.__init__.mysql_options: %(mysql_options_redacted)s",
{"mysql_options_redacted": self.mysql_options_redacted},
)
try:
logger.debug("Establishing new connection to MySQL.")
self.cnx = connector.connect(**mysql_options)
except connector.errors.Error as err:
logger.critical(
"Database error %(err)s mysql_options_redacted %(mysql_options_redacted)s %(stack)s",
{
"err": err,
"mysql_options_redacted": self.mysql_options_redacted,
"stack": traceback.format_exc(),
},
)
raise
def close(self):
"""
Close the database connection.
No exception is raised if the connection is already closed.
"""
logger.debug("Closing the database connection.")
self.cnx.close()
def execute(self, statement: str, params=None) -> None:
"""
Execute a single MySQL statement.
Args:
statement (str): The SQL statement to execute.
params (Optional[dict]): Optional parameters for the SQL statement.
"""
logger.debug(
"Executing statement: %(statement)s with params: %(params)s",
{"statement": statement, "params": params},
)
params = params or {}
with closing(self.cnx.cursor()) as cursor:
try:
cursor.execute(statement, params)
except connector.errors.Error as err:
logger.debug(
"Error executing statement: %(err)s",
{"err": err},
)
raise
def executemany(self, statement: str, rows: Rows) -> None:
"""
Execute a SQL statement with multiple rows of data.
Args:
statement (str): SQL statement (e.g., insert).
rows (Rows): Rows of data to insert.
"""
logger.debug(
"Executing statement with multiple rows: %(statement)s",
{"statement": statement},
)
with closing(self.cnx.cursor()) as cursor:
try:
cursor.executemany(statement, rows)
except connector.errors.Error as err:
logger.debug("Error executing multiple rows: %(err)s", {"err": err})
err.add_note(f"statement {statement}")
raise
def fetchone(self, statement, params: Optional[Dict[str, Any]] = None) -> Tuple[Any]:
"""
Fetch a single row from the database.
Args:
statement (str): SQL query.
params (Optional[dict]): Optional query parameters.
Returns:
Tuple[Any]: A tuple representing the row.
"""
if params is None:
params = {}
logger.debug(
"Fetching one row with statement: %(statement)s and params: %(params)s",
{"statement": statement, "params": params},
)
with closing(self.cnx.cursor()) as cursor:
try:
cursor.execute(statement, params=params)
result = cursor.fetchone()
logger.debug(
"Fetched row: %(result)s",
{"result": result},
)
return result
except connector.errors.Error as err:
logger.debug(
"Error fetching one row: %(err)s",
{"err": err},
)
raise
def fetchall(
self,
statement: str,
params: Optional[Dict[str, Any]] = None,
dictionary: bool = False,
) -> Rows | List[Dict[str, Any]]:
"""
Fetch multiple rows from the database.
Args:
statement (str): SQL query.
params (Optional[dict]): Optional query parameters.
dictionary (bool): Whether to return results as dictionaries.
Returns:
Rows | List[Dict[str, Any]]: A sequence of rows or dictionaries.
"""
logger.debug(
"Fetching all rows with statement: %(statement)s and params: %(params)s",
{"statement": statement, "params": params},
)
if params is None:
params = {}
with closing(self.cnx.cursor(dictionary=dictionary)) as cursor:
try:
cursor.execute(statement, params=params)
results = cursor.fetchall()
logger.debug("Fetched rows: %(results)s", {"results": results})
return results
except connector.errors.Error as err:
logger.debug("Error fetching all rows: %(error)s", {"error": err})
err.add_note(f"statement {statement}")
raise
def insert_on_duplicate_key_update(
self,
table: str,
cols: Tuple[str, ...],
keys: Tuple[str, ...],
rows: Rows,
on_dup: str = "",
) -> None:
"""
Perform an insert with "on duplicate key update" semantics.
Args:
table (str): Table to insert into.
cols (Tuple[str, ...]): Columns to insert.
keys (Tuple[str, ...]): Key columns for deduplication.
rows (Rows): Rows of data to insert.
on_dup (str): Custom "on duplicate key" SQL clause.
"""
logger.debug("Inserting with on duplicate key update into %(table)s.", {"table": table})
statement = self.insert_on_duplicate_key_update_statement(table, cols, keys, on_dup=on_dup)
logger.debug("Statement: %(statement)s", {"statement": statement})
self.executemany(statement, rows)
def insert_on_duplicate_key_update_statement(
self, table: str, cols: Tuple[str, ...], keys: Tuple[str, ...], on_dup: str = ""
) -> str:
"""
Generate SQL for an "insert on duplicate key update" operation.
Args:
table (str): Table to insert into.
cols (Tuple[str, ...]): Columns to insert.
keys (Tuple[str, ...]): Key columns for deduplication.
on_dup (str): Custom "on duplicate key" SQL clause.
Returns:
str: The generated SQL statement.
"""
logger.debug("Generating insert statement for table %(table)s.", {"table": table})
cols_str = ",".join(cols)
placeholders = ",".join(["%s"] * len(cols))
cols_on_dup = tuple(col for col in cols if col not in keys)
on_dup = on_dup or MysqlHandler.on_dup(cols_on_dup)
statement = f"insert into {table} ({cols_str}) values ({placeholders}) as vals on duplicate key update {on_dup}"
logger.debug("Generated statement: %(statement)s", {"statement": statement})
return statement
def insert_select_on_duplicate_key_update(
self, table_from: str, table_into: str, colmap: Dict[str, str], keys: str
) -> None:
"""
Execute an "insert on duplicate key update" using data from another table.
Args:
table_from (str): Source table.
table_into (str): Destination table.
colmap (Dict[str, str]): Mapping of columns from source to destination.
keys (str): Key columns for deduplication.
"""
logger.debug(
"Inserting from %(table_from)s into %(table_into)s with column mapping %(colmap)s.",
{"table_from": table_from, "table_into": table_into, "colmap": colmap},
)
statement = MysqlHandler.insert_select_on_duplicate_key_update_statement(
table_from, table_into, colmap, keys
)
logger.debug("Statement: %(statement)s", {"statement": statement})
self.execute(statement)
@staticmethod
def insert_select_on_duplicate_key_update_statement(
table_from: str, table_into: str, colmap: Dict[str, str], keys: str
) -> str:
"""
Generate SQL for an "insert on duplicate key update" operation using data from another table.
Args:
table_from (str): Source table.
table_into (str): Destination table.
colmap (Dict[str, str]): Mapping of columns from source to destination.
keys (str): Key columns for deduplication.
Returns:
str: The generated SQL statement.
"""
logger.debug(
"Generating insert-select statement for %(table_from)s into %(table_into)s.",
{"table_from": table_from, "table_into": table_into},
)
cols_from = colmap.keys()
cols_into = colmap.values()
col2alias = {col: f"alias{i}" for (i, col) in enumerate(cols_into)}
aliases = col2alias.values()
aliases_str = ",".join(aliases)
cols_into_str = ",".join(cols_into)
cols_from_str = ",".join(cols_from)
on_dup = [
f"{col_into}=vals.{col2alias[col_into]}"
for col_into in cols_into
if col_into not in keys
]
on_dup_str = ",".join(on_dup)
statement = (
f"insert into {table_into} ({cols_into_str}) select * from "
f"(select {cols_from_str} from {table_from}) as vals({aliases_str}) "
f"on duplicate key update {on_dup_str}"
)
logger.debug("Generated statement: %(statement)s", {"statement": statement})
return statement
@staticmethod
def on_dup(col_names: Tuple[str, ...]) -> str:
"""
Generate the "on duplicate key update" clause for a given set of columns.
Args:
col_names (Tuple[str, ...]): Columns to include in the clause.
Returns:
str: The generated SQL clause.
"""
logger.debug(
"Generating on-duplicate clause for columns: %(col_names)s.",
{"col_names": col_names},
)
on_dup_array = [f"{col_name}=vals.{col_name}" for col_name in col_names]
on_dup = ",".join(on_dup_array)
logger.debug("on_dup %(on_dup)s", {"on_dup": on_dup})
logger.debug("Generated clause: %(on_dup)s", {"on_dup": on_dup})
return on_dup