20 def __init__(self, url: str, port: int, username: str, password: str, db_name: str, success: int = 0, error: int = 84, debug: bool =
False) ->
None:
22 This class is in charge of managing the connections to the sql database
25 url (str): _description_
26 port (int): _description_
27 username (str): _description_
28 password (str): _description_
29 db_name (str): _description_
30 succes (int, optional): _description_. Defaults to 0.
31 error (int, optional): _description_. Defaults to 84.
32 debug (bool, optional): _description_. Defaults to False.
49 logger=self.__class__.__name__
53 "pool_name": CONST.DATABASE_POOL_NAME,
54 "pool_size": CONST.DATABASE_MAX_POOL_CONNECTIONS,
55 "pool_reset_session": CONST.DATABASE_RESET_POOL_NODE_CONNECTION,
61 "collation": CONST.DATABASE_COLLATION,
62 "connection_timeout": CONST.DATABASE_CONNECTION_TIMEOUT,
63 "allow_local_infile": CONST.DATABASE_LOCAL_INFILE,
64 "init_command": CONST.DATABASE_INIT_COMMAND,
65 "option_files": CONST.DATABASE_DEFAULT_FILE,
66 "autocommit": CONST.DATABASE_AUTOCOMMIT,
67 "ssl_disabled":
not CONST.DATABASE_SSL,
68 "ssl_key": CONST.DATABASE_SSL_KEY,
69 "ssl_cert": CONST.DATABASE_SSL_CERT,
70 "ssl_ca": CONST.DATABASE_SSL_CA,
71 "ssl_cipher": CONST.DATABASE_SSL_CIPHER,
72 "ssl_verify_cert": CONST.DATABASE_SSL_VERIFY_CERT
77 mysql.connector.pooling.MySQLConnectionPool
91 Initialise a connection to the database (but within a pool)
94 RuntimeError: _description_: A runtime error is raised if it fails.
97 int: _description_: Returns self.success if the function succeeds.
99 title =
"initialise_pool"
100 self.
disp.log_debug(
"Initialising the connection pool.", title)
103 "pool_name": CONST.DATABASE_POOL_NAME,
104 "pool_size": CONST.DATABASE_MAX_POOL_CONNECTIONS,
105 "pool_reset_session": CONST.DATABASE_RESET_POOL_NODE_CONNECTION,
111 "collation": CONST.DATABASE_COLLATION,
112 "connection_timeout": CONST.DATABASE_CONNECTION_TIMEOUT,
113 "allow_local_infile": CONST.DATABASE_LOCAL_INFILE,
114 "init_command": CONST.DATABASE_INIT_COMMAND,
115 "option_files": CONST.DATABASE_DEFAULT_FILE,
116 "autocommit": CONST.DATABASE_AUTOCOMMIT,
117 "ssl_disabled":
not CONST.DATABASE_SSL,
118 "ssl_key": CONST.DATABASE_SSL_KEY,
119 "ssl_cert": CONST.DATABASE_SSL_CERT,
120 "ssl_ca": CONST.DATABASE_SSL_CA,
121 "ssl_cipher": CONST.DATABASE_SSL_CIPHER,
122 "ssl_verify_cert": CONST.DATABASE_SSL_VERIFY_CERT
124 for i
in SCONST.UNWANTED_ARGUMENTS:
127 f
"Removed '{i}' from the pool parameters.", title
131 self.
poolpool = mysql.connector.pooling.MySQLConnectionPool(
135 except mysql.connector.errors.ProgrammingError
as pe:
136 msg =
"ProgrammingError: The pool could not be initialized."
137 msg += f
"Original error: {str(pe)}"
138 self.
disp.log_critical(msg, title)
139 raise RuntimeError(msg)
from pe
140 except mysql.connector.errors.IntegrityError
as ie:
141 msg =
"IntegrityError: Integrity issue while initializing the pool."
142 msg += f
" Original error: {str(ie)}"
143 self.
disp.log_critical(msg, title)
144 raise RuntimeError(msg)
from ie
145 except mysql.connector.errors.OperationalError
as oe:
146 msg =
"OperationalError: Operational error occurred during pool initialization."
147 msg += f
" Original error: {str(oe)}"
148 self.
disp.log_critical(msg, title)
149 raise RuntimeError(msg)
from oe
150 except mysql.connector.Error
as e:
151 msg =
"MySQL Error: An unexpected error occurred during pool initialization."
152 msg += f
"Original error: {str(e)}"
153 self.
disp.log_critical(msg, title)
154 raise RuntimeError(msg)
from e
158 Retrieves a connection from the pool.
161 mysql.connector.pooling.PooledMySQLConnection: _description_: A pooled connection
163 title =
"get_connection"
165 raise RuntimeError(
"Connection pool is not initialized.")
167 self.
disp.log_debug(
"Getting an sql connection", title)
169 except mysql.connector.errors.OperationalError
as oe:
170 msg =
"OperationalError: Could not retrieve a connection from the pool."
171 msg += f
" Original error: {str(oe)}"
172 self.
disp.log_critical(msg, title)
173 raise RuntimeError(msg)
from oe
174 except mysql.connector.Error
as e:
175 msg =
"MySQL Error: An unexpected error occurred while getting the connection."
176 msg += f
" Original error: {str(e)}"
177 self.
disp.log_critical(msg, title)
178 raise RuntimeError(msg)
from e
263 def run_and_commit(self, query: str, cursor: Union[mysql.connector.cursor.MySQLCursor,
None] =
None) -> int:
265 Executes a query and commits changes.
268 cursor (mysql.connector.cursor.MySQLCursor): The active cursor.
269 query (str): The query to execute.
271 title =
"run_and_commit"
272 self.
disp.log_debug(
"Running and committing sql query.", title)
274 self.
disp.log_debug(
"No cursor found, generating one.", title)
276 if connection
is None:
277 self.
disp.log_critical(SCONST.CONNECTION_FAILED, title)
280 if internal_cursor
is None:
281 self.
disp.log_critical(SCONST.CURSOR_FAILED, title)
284 self.
disp.log_debug(
"Cursor found, using it.", title)
285 internal_cursor = cursor
287 self.
disp.log_debug(f
"Executing query: {query}.", title)
288 internal_cursor.execute(query)
289 self.
disp.log_debug(
"Committing content.", title)
290 internal_cursor._connection.commit()
293 "The cursor was generated by us, releasing.", title
298 "The cursor was provided, not releasing.", title
301 except mysql.connector.errors.ProgrammingError
as pe:
302 msg =
"ProgrammingError: Failed to execute the query."
303 msg += f
" Original error: {str(pe)}"
304 self.
disp.log_error(msg, title)
307 "The cursor was generated by us, releasing.", title
312 "The cursor was provided, not releasing.", title
314 raise RuntimeError(msg)
from pe
315 except mysql.connector.errors.IntegrityError
as ie:
316 msg =
"IntegrityError: Integrity constraint issue occurred during query execution."
317 msg += f
" Original error: {str(ie)}"
318 self.
disp.log_error(msg, title)
321 "The cursor was generated by us, releasing.", title
326 "The cursor was provided, not releasing.", title
328 raise RuntimeError(msg)
from ie
329 except mysql.connector.errors.OperationalError
as oe:
330 msg =
"OperationalError: Operational error occurred during query execution."
331 msg += f
" Original error: {str(oe)}"
332 self.
disp.log_error(msg, title)
335 "The cursor was generated by us, releasing.", title
340 "The cursor was provided, not releasing.", title
342 raise RuntimeError(msg)
from oe
343 except mysql.connector.Error
as e:
344 msg =
"MySQL Error: An unexpected error occurred during query execution."
345 msg += f
" Original error: {str(e)}"
346 self.
disp.log_error(msg, title)
349 "The cursor was generated by us, releasing.", title
354 "The cursor was provided, not releasing.", title
356 raise RuntimeError(msg)
from e
358 def run_and_fetch_all(self, query: str, cursor: Union[mysql.connector.cursor.MySQLCursor,
None] =
None) -> Union[int, Any]:
360 Executes a query and fetches all results.
363 cursor (mysql.connector.cursor.MySQLCursor): The active cursor.
364 query (str): The query to execute.
366 title =
"run_and_fetchall"
369 if connection
is None:
370 self.
disp.log_critical(SCONST.CONNECTION_FAILED, title)
373 if internal_cursor
is None:
374 self.
disp.log_critical(SCONST.CURSOR_FAILED, title)
377 internal_cursor = cursor
379 self.
disp.log_debug(f
"Executing query: {query}.", title)
380 internal_cursor.execute(query)
381 if internal_cursor
is None or internal_cursor.description
is None:
383 "Failed to gather data from the table, cursor is invalid.", title
387 "The cursor was generated by us, releasing.", title
390 connection, internal_cursor
394 "The cursor was provided, not releasing.", title
398 "Storing a copy of the content of the cursor.", title
400 raw_data = internal_cursor.fetchall()
401 self.
disp.log_debug(f
"Raw gathered data {raw_data}", title)
402 data = raw_data.copy()
403 self.
disp.log_debug(f
"Data gathered: {data}.", title)
406 "The cursor was generated by us, releasing.", title
411 "The cursor was provided, not releasing.", title
414 except mysql.connector.errors.ProgrammingError
as pe:
415 msg =
"ProgrammingError: Failed to execute the query."
416 msg += f
" Original error: {str(pe)}"
417 self.
disp.log_error(msg, title)
420 "The cursor was generated by us, releasing.", title
425 "The cursor was provided, not releasing.", title
427 raise RuntimeError(msg)
from pe
428 except mysql.connector.errors.IntegrityError
as ie:
429 msg =
"IntegrityError: Integrity constraint issue occurred during query execution."
430 msg += f
" Original error: {str(ie)}"
431 self.
disp.log_error(msg, title)
434 "The cursor was generated by us, releasing.", title
439 "The cursor was provided, not releasing.", title
441 raise RuntimeError(msg)
from ie
442 except mysql.connector.errors.OperationalError
as oe:
443 msg =
"OperationalError: Operational error occurred during query execution."
444 msg += f
" Original error: {str(oe)}"
445 self.
disp.log_error(msg, title)
448 "The cursor was generated by us, releasing.", title
453 "The cursor was provided, not releasing.", title
455 raise RuntimeError(msg)
from oe
456 except mysql.connector.Error
as e:
457 msg =
"MySQL Error: An unexpected error occurred during query execution."
458 msg += f
" Original error: {str(e)}"
459 self.
disp.log_error(msg, title)
462 "The cursor was generated by us, releasing.", title
467 "The cursor was provided, not releasing.", title
469 raise RuntimeError(msg)
from e