From prod instance
This commit is contained in:
parent
6d77822af1
commit
8e3a910597
2 changed files with 23 additions and 11 deletions
|
@ -4,8 +4,11 @@ import MySQLdb.cursors
|
||||||
from typing import Union, Optional
|
from typing import Union, Optional
|
||||||
|
|
||||||
from .connection import Connection
|
from .connection import Connection
|
||||||
|
from .logger import Logger
|
||||||
|
|
||||||
|
|
||||||
|
logger = Logger()
|
||||||
|
|
||||||
class Database:
|
class Database:
|
||||||
"""Class wrapping MySQL database connection
|
"""Class wrapping MySQL database connection
|
||||||
"""
|
"""
|
||||||
|
@ -17,9 +20,7 @@ class Database:
|
||||||
self._con = None
|
self._con = None
|
||||||
self._ssh = None
|
self._ssh = None
|
||||||
|
|
||||||
self._connect()
|
def _execute(self, query: str, parameters: Optional[tuple] = None, ctype: Optional[MySQLdb.cursors.BaseCursor] = None, retry: bool = True):
|
||||||
|
|
||||||
def _execute(self, query: str, parameters: Optional[tuple] = None, ctype: Optional[MySQLdb.cursors.BaseCursor] = None) -> None:
|
|
||||||
"""Execute a query on the database
|
"""Execute a query on the database
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
@ -27,12 +28,22 @@ class Database:
|
||||||
parameters (tuple, optional): Parameters to use to replace
|
parameters (tuple, optional): Parameters to use to replace
|
||||||
placeholders in the query, if any. Defaults to None.
|
placeholders in the query, if any. Defaults to None.
|
||||||
"""
|
"""
|
||||||
cur = self.getCursor(ctype)
|
try:
|
||||||
cur.execute(query, parameters)
|
cur = self.getCursor(ctype)
|
||||||
self.commit() # Instantly commit after every (potential) write action
|
cur.execute(query, parameters)
|
||||||
return cur.fetchall()
|
self.commit() # Instantly commit after every (potential) write action
|
||||||
|
return cur.fetchall()
|
||||||
|
except (AttributeError, MySQLdb.OperationalError):
|
||||||
|
if retry:
|
||||||
|
self._connect()
|
||||||
|
return self._execute(query, parameters, ctype, False)
|
||||||
|
raise
|
||||||
|
except MySQLdb.ProgrammingError:
|
||||||
|
logger.error(f"Error in query: {query}")
|
||||||
|
logger.error(f"Parameters: {str(parameters)}")
|
||||||
|
raise
|
||||||
|
|
||||||
def _connect(self):
|
def _connect(self) -> None:
|
||||||
if self.vessel.ssh:
|
if self.vessel.ssh:
|
||||||
self._ssh = Connection(self.vessel)
|
self._ssh = Connection(self.vessel)
|
||||||
port = self._ssh.forward_tcp(3306)
|
port = self._ssh.forward_tcp(3306)
|
||||||
|
@ -64,5 +75,5 @@ class Database:
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
"""Close database connection on removal of the Database object
|
"""Close database connection on removal of the Database object
|
||||||
"""
|
"""
|
||||||
self._con.close()
|
if self._con:
|
||||||
|
self._con.close()
|
|
@ -2,4 +2,5 @@ import logging
|
||||||
|
|
||||||
|
|
||||||
class Logger(logging.Logger):
|
class Logger(logging.Logger):
|
||||||
name = "ReportMonster"
|
def __init__(self, name="ReportMonster", *args, **kwargs):
|
||||||
|
super().__init__(name, *args, **kwargs)
|
||||||
|
|
Loading…
Reference in a new issue