2023-01-07 02:19:18 +01:00
|
|
|
""" Basic SQL DB client """
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Exceptions
|
|
|
|
#
|
|
|
|
|
2023-01-16 12:56:12 +01:00
|
|
|
|
2023-01-07 02:19:18 +01:00
|
|
|
class DBException(Exception):
|
2023-01-16 12:56:12 +01:00
|
|
|
"""That is the base exception class for all the other exceptions provided by this module."""
|
2023-01-07 02:19:18 +01:00
|
|
|
|
|
|
|
def __init__(self, error, *args, **kwargs):
|
|
|
|
for arg, value in kwargs.items():
|
|
|
|
setattr(self, arg, value)
|
|
|
|
super().__init__(error.format(*args, **kwargs))
|
|
|
|
|
|
|
|
|
|
|
|
class DBNotImplemented(DBException, RuntimeError):
|
|
|
|
"""
|
|
|
|
Raised when calling a method not implemented in child class
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, method, class_name):
|
|
|
|
super().__init__(
|
|
|
|
"The method {method} is not yet implemented in class {class_name}",
|
2023-01-16 12:56:12 +01:00
|
|
|
method=method,
|
|
|
|
class_name=class_name,
|
2023-01-07 02:19:18 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class DBFailToConnect(DBException, RuntimeError):
|
|
|
|
"""
|
|
|
|
Raised on connecting error occurred
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, uri):
|
2023-01-16 12:56:12 +01:00
|
|
|
super().__init__("An error occured during database connection ({uri})", uri=uri)
|
2023-01-07 02:19:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
class DBDuplicatedSQLParameter(DBException, KeyError):
|
|
|
|
"""
|
|
|
|
Raised when trying to set a SQL query parameter
|
|
|
|
and an other parameter with the same name is already set
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, parameter_name):
|
|
|
|
super().__init__(
|
2023-01-16 12:56:12 +01:00
|
|
|
"Duplicated SQL parameter '{parameter_name}'", parameter_name=parameter_name
|
2023-01-07 02:19:18 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class DBUnsupportedWHEREClauses(DBException, TypeError):
|
|
|
|
"""
|
|
|
|
Raised when trying to execute query with unsupported
|
|
|
|
WHERE clauses provided
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, where_clauses):
|
2023-01-16 12:56:12 +01:00
|
|
|
super().__init__("Unsupported WHERE clauses: {where_clauses}", where_clauses=where_clauses)
|
2023-01-07 02:19:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
class DBInvalidOrderByClause(DBException, TypeError):
|
|
|
|
"""
|
|
|
|
Raised when trying to select on table with invalid
|
|
|
|
ORDER BY clause provided
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, order_by):
|
|
|
|
super().__init__(
|
2023-01-16 12:56:12 +01:00
|
|
|
"Invalid ORDER BY clause: {order_by}. Must be a string or a list of two values"
|
|
|
|
" (ordering field name and direction)",
|
|
|
|
order_by=order_by,
|
2023-01-07 02:19:18 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class DB:
|
2023-01-16 12:56:12 +01:00
|
|
|
"""Database client"""
|
2023-01-07 02:19:18 +01:00
|
|
|
|
|
|
|
just_try = False
|
|
|
|
|
|
|
|
def __init__(self, just_try=False, **kwargs):
|
|
|
|
self.just_try = just_try
|
|
|
|
self._conn = None
|
|
|
|
for arg, value in kwargs.items():
|
2023-01-16 12:56:12 +01:00
|
|
|
setattr(self, f"_{arg}", value)
|
2023-01-07 02:19:18 +01:00
|
|
|
|
|
|
|
def connect(self, exit_on_error=True):
|
2023-01-16 12:56:12 +01:00
|
|
|
"""Connect to DB server"""
|
|
|
|
raise DBNotImplemented("connect", self.__class__.__name__)
|
2023-01-07 02:19:18 +01:00
|
|
|
|
|
|
|
def close(self):
|
2023-01-16 12:56:12 +01:00
|
|
|
"""Close connection with DB server (if opened)"""
|
2023-01-07 02:19:18 +01:00
|
|
|
if self._conn:
|
|
|
|
self._conn.close()
|
|
|
|
self._conn = None
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _log_query(sql, params):
|
|
|
|
log.debug(
|
|
|
|
'Run SQL query "%s" %s',
|
|
|
|
sql,
|
2023-01-16 12:56:12 +01:00
|
|
|
"with params = {}".format( # pylint: disable=consider-using-f-string
|
|
|
|
", ".join([f"{key} = {value}" for key, value in params.items()])
|
|
|
|
if params
|
|
|
|
else "without params"
|
|
|
|
),
|
2023-01-07 02:19:18 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _log_query_exception(sql, params):
|
|
|
|
log.exception(
|
|
|
|
'Error during SQL query "%s" %s',
|
|
|
|
sql,
|
2023-01-16 12:56:12 +01:00
|
|
|
"with params = {}".format( # pylint: disable=consider-using-f-string
|
|
|
|
", ".join([f"{key} = {value}" for key, value in params.items()])
|
|
|
|
if params
|
|
|
|
else "without params"
|
|
|
|
),
|
2023-01-07 02:19:18 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def doSQL(self, sql, params=None):
|
|
|
|
"""
|
|
|
|
Run SQL query and commit changes (rollback on error)
|
|
|
|
|
|
|
|
:param sql: The SQL query
|
|
|
|
:param params: The SQL query's parameters as dict (optional)
|
|
|
|
|
|
|
|
:return: True on success, False otherwise
|
|
|
|
:rtype: bool
|
|
|
|
"""
|
2023-01-16 12:56:12 +01:00
|
|
|
raise DBNotImplemented("doSQL", self.__class__.__name__)
|
2023-01-07 02:19:18 +01:00
|
|
|
|
|
|
|
def doSelect(self, sql, params=None):
|
|
|
|
"""
|
|
|
|
Run SELECT SQL query and return list of selected rows as dict
|
|
|
|
|
|
|
|
:param sql: The SQL query
|
|
|
|
:param params: The SQL query's parameters as dict (optional)
|
|
|
|
|
|
|
|
:return: List of selected rows as dict on success, False otherwise
|
|
|
|
:rtype: list, bool
|
|
|
|
"""
|
2023-01-16 12:56:12 +01:00
|
|
|
raise DBNotImplemented("doSelect", self.__class__.__name__)
|
2023-01-07 02:19:18 +01:00
|
|
|
|
|
|
|
#
|
|
|
|
# SQL helpers
|
|
|
|
#
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _quote_table_name(table):
|
2023-01-16 12:56:12 +01:00
|
|
|
"""Quote table name"""
|
|
|
|
return '"{}"'.format( # pylint: disable=consider-using-f-string
|
|
|
|
'"."'.join(table.split("."))
|
2023-01-07 02:19:18 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _quote_field_name(field):
|
2023-01-16 12:56:12 +01:00
|
|
|
"""Quote table name"""
|
2023-01-07 02:19:18 +01:00
|
|
|
return f'"{field}"'
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def format_param(param):
|
2023-01-16 12:56:12 +01:00
|
|
|
"""Format SQL query parameter for prepared query"""
|
|
|
|
return f"%({param})s"
|
2023-01-07 02:19:18 +01:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _combine_params(cls, params, to_add=None, **kwargs):
|
|
|
|
if to_add:
|
|
|
|
assert isinstance(to_add, dict), "to_add must be a dict or None"
|
|
|
|
params = cls._combine_params(params, **to_add)
|
|
|
|
|
|
|
|
for param, value in kwargs.items():
|
|
|
|
if param in params:
|
|
|
|
raise DBDuplicatedSQLParameter(param)
|
|
|
|
params[param] = value
|
|
|
|
return params
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _format_where_clauses(cls, where_clauses, params=None, where_op=None):
|
|
|
|
"""
|
|
|
|
Format WHERE clauses
|
|
|
|
|
|
|
|
:param where_clauses: The WHERE clauses. Could be:
|
|
|
|
- a raw SQL WHERE clause as string
|
|
|
|
- a tuple of two elements: a raw WHERE clause and its parameters as dict
|
|
|
|
- a dict of WHERE clauses with field name as key and WHERE clause value as value
|
|
|
|
- a list of any of previous valid WHERE clauses
|
|
|
|
:param params: Dict of other already set SQL query parameters (optional)
|
2023-01-16 12:56:12 +01:00
|
|
|
:param where_op: SQL operator used to combine WHERE clauses together (optional, default:
|
|
|
|
AND)
|
2023-01-07 02:19:18 +01:00
|
|
|
|
|
|
|
:return: A tuple of two elements: raw SQL WHERE combined clauses and parameters on success
|
|
|
|
:rtype: string, bool
|
|
|
|
"""
|
|
|
|
if params is None:
|
|
|
|
params = {}
|
|
|
|
if where_op is None:
|
2023-01-16 12:56:12 +01:00
|
|
|
where_op = "AND"
|
2023-01-07 02:19:18 +01:00
|
|
|
|
|
|
|
if isinstance(where_clauses, str):
|
|
|
|
return (where_clauses, params)
|
|
|
|
|
2023-01-16 12:56:12 +01:00
|
|
|
if (
|
|
|
|
isinstance(where_clauses, tuple)
|
|
|
|
and len(where_clauses) == 2
|
|
|
|
and isinstance(where_clauses[1], dict)
|
|
|
|
):
|
2023-01-07 02:19:18 +01:00
|
|
|
cls._combine_params(params, where_clauses[1])
|
|
|
|
return (where_clauses[0], params)
|
|
|
|
|
|
|
|
if isinstance(where_clauses, (list, tuple)):
|
|
|
|
sql_where_clauses = []
|
|
|
|
for where_clause in where_clauses:
|
2023-01-16 12:56:12 +01:00
|
|
|
sql2, params = cls._format_where_clauses(
|
|
|
|
where_clause, params=params, where_op=where_op
|
|
|
|
)
|
2023-01-07 02:19:18 +01:00
|
|
|
sql_where_clauses.append(sql2)
|
2023-01-16 12:56:12 +01:00
|
|
|
return (f" {where_op} ".join(sql_where_clauses), params)
|
2023-01-07 02:19:18 +01:00
|
|
|
|
|
|
|
if isinstance(where_clauses, dict):
|
|
|
|
sql_where_clauses = []
|
|
|
|
for field, value in where_clauses.items():
|
|
|
|
param = field
|
|
|
|
if field in params:
|
|
|
|
idx = 1
|
|
|
|
while param in params:
|
2023-01-16 12:56:12 +01:00
|
|
|
param = f"{field}_{idx}"
|
2023-01-07 02:19:18 +01:00
|
|
|
idx += 1
|
|
|
|
cls._combine_params(params, {param: value})
|
|
|
|
sql_where_clauses.append(
|
2023-01-16 12:56:12 +01:00
|
|
|
f"{cls._quote_field_name(field)} = {cls.format_param(param)}"
|
2023-01-07 02:19:18 +01:00
|
|
|
)
|
2023-01-16 12:56:12 +01:00
|
|
|
return (f" {where_op} ".join(sql_where_clauses), params)
|
2023-01-07 02:19:18 +01:00
|
|
|
raise DBUnsupportedWHEREClauses(where_clauses)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _add_where_clauses(cls, sql, params, where_clauses, where_op=None):
|
|
|
|
"""
|
|
|
|
Add WHERE clauses to an SQL query
|
|
|
|
|
|
|
|
:param sql: The SQL query to complete
|
|
|
|
:param params: The dict of parameters of the SQL query to complete
|
|
|
|
:param where_clauses: The WHERE clause (see _format_where_clauses())
|
2023-01-16 12:56:12 +01:00
|
|
|
:param where_op: SQL operator used to combine WHERE clauses together (optional, default:
|
|
|
|
see _format_where_clauses())
|
2023-01-07 02:19:18 +01:00
|
|
|
|
|
|
|
:return:
|
|
|
|
:rtype: A tuple of two elements: raw SQL WHERE combined clauses and parameters
|
|
|
|
"""
|
|
|
|
if where_clauses:
|
2023-01-16 12:56:12 +01:00
|
|
|
sql_where, params = cls._format_where_clauses(
|
|
|
|
where_clauses, params=params, where_op=where_op
|
|
|
|
)
|
2023-01-07 02:19:18 +01:00
|
|
|
sql += " WHERE " + sql_where
|
|
|
|
return (sql, params)
|
|
|
|
|
|
|
|
def insert(self, table, values, just_try=False):
|
2023-01-16 12:56:12 +01:00
|
|
|
"""Run INSERT SQL query"""
|
2023-01-07 02:19:18 +01:00
|
|
|
# pylint: disable=consider-using-f-string
|
2023-01-16 12:56:12 +01:00
|
|
|
sql = "INSERT INTO {} ({}) VALUES ({})".format(
|
2023-01-07 02:19:18 +01:00
|
|
|
self._quote_table_name(table),
|
2023-01-16 12:56:12 +01:00
|
|
|
", ".join([self._quote_field_name(field) for field in values.keys()]),
|
|
|
|
", ".join([self.format_param(key) for key in values]),
|
2023-01-07 02:19:18 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
if just_try:
|
|
|
|
log.debug("Just-try mode: execute INSERT query: %s", sql)
|
|
|
|
return True
|
|
|
|
|
|
|
|
log.debug(sql)
|
|
|
|
if not self.doSQL(sql, params=values):
|
|
|
|
log.error("Fail to execute INSERT query (SQL: %s)", sql)
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def update(self, table, values, where_clauses, where_op=None, just_try=False):
|
2023-01-16 12:56:12 +01:00
|
|
|
"""Run UPDATE SQL query"""
|
2023-01-07 02:19:18 +01:00
|
|
|
# pylint: disable=consider-using-f-string
|
2023-01-16 12:56:12 +01:00
|
|
|
sql = "UPDATE {} SET {}".format(
|
2023-01-07 02:19:18 +01:00
|
|
|
self._quote_table_name(table),
|
2023-01-16 12:56:12 +01:00
|
|
|
", ".join(
|
|
|
|
[f"{self._quote_field_name(key)} = {self.format_param(key)}" for key in values]
|
|
|
|
),
|
2023-01-07 02:19:18 +01:00
|
|
|
)
|
|
|
|
params = values
|
|
|
|
|
|
|
|
try:
|
|
|
|
sql, params = self._add_where_clauses(sql, params, where_clauses, where_op=where_op)
|
|
|
|
except (DBDuplicatedSQLParameter, DBUnsupportedWHEREClauses):
|
2023-01-16 12:56:12 +01:00
|
|
|
log.error("Fail to add WHERE clauses", exc_info=True)
|
2023-01-07 02:19:18 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
if just_try:
|
|
|
|
log.debug("Just-try mode: execute UPDATE query: %s", sql)
|
|
|
|
return True
|
|
|
|
|
|
|
|
log.debug(sql)
|
|
|
|
if not self.doSQL(sql, params=params):
|
|
|
|
log.error("Fail to execute UPDATE query (SQL: %s)", sql)
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2023-01-16 12:56:12 +01:00
|
|
|
def delete(self, table, where_clauses, where_op="AND", just_try=False):
|
|
|
|
"""Run DELETE SQL query"""
|
|
|
|
sql = f"DELETE FROM {self._quote_table_name(table)}"
|
2023-01-07 02:19:18 +01:00
|
|
|
params = {}
|
|
|
|
|
|
|
|
try:
|
|
|
|
sql, params = self._add_where_clauses(sql, params, where_clauses, where_op=where_op)
|
|
|
|
except (DBDuplicatedSQLParameter, DBUnsupportedWHEREClauses):
|
2023-01-16 12:56:12 +01:00
|
|
|
log.error("Fail to add WHERE clauses", exc_info=True)
|
2023-01-07 02:19:18 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
if just_try:
|
|
|
|
log.debug("Just-try mode: execute UPDATE query: %s", sql)
|
|
|
|
return True
|
|
|
|
|
|
|
|
log.debug(sql)
|
|
|
|
if not self.doSQL(sql, params=params):
|
|
|
|
log.error("Fail to execute UPDATE query (SQL: %s)", sql)
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def truncate(self, table, just_try=False):
|
2023-01-16 12:56:12 +01:00
|
|
|
"""Run TRUNCATE SQL query"""
|
|
|
|
sql = f"TRUNCATE TABLE {self._quote_table_name(table)}"
|
2023-01-07 02:19:18 +01:00
|
|
|
|
|
|
|
if just_try:
|
|
|
|
log.debug("Just-try mode: execute TRUNCATE query: %s", sql)
|
|
|
|
return True
|
|
|
|
|
|
|
|
log.debug(sql)
|
|
|
|
if not self.doSQL(sql):
|
|
|
|
log.error("Fail to execute TRUNCATE query (SQL: %s)", sql)
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2023-01-16 12:56:12 +01:00
|
|
|
def select(
|
|
|
|
self, table, where_clauses=None, fields=None, where_op="AND", order_by=None, just_try=False
|
|
|
|
):
|
|
|
|
"""Run SELECT SQL query"""
|
2023-01-07 02:19:18 +01:00
|
|
|
sql = "SELECT "
|
|
|
|
if fields is None:
|
|
|
|
sql += "*"
|
|
|
|
elif isinstance(fields, str):
|
2023-01-16 12:56:12 +01:00
|
|
|
sql += f"{self._quote_field_name(fields)}"
|
2023-01-07 02:19:18 +01:00
|
|
|
else:
|
2023-01-16 12:56:12 +01:00
|
|
|
sql += ", ".join([self._quote_field_name(field) for field in fields])
|
2023-01-07 02:19:18 +01:00
|
|
|
|
2023-01-16 12:56:12 +01:00
|
|
|
sql += f" FROM {self._quote_table_name(table)}"
|
2023-01-07 02:19:18 +01:00
|
|
|
params = {}
|
|
|
|
|
|
|
|
try:
|
|
|
|
sql, params = self._add_where_clauses(sql, params, where_clauses, where_op=where_op)
|
|
|
|
except (DBDuplicatedSQLParameter, DBUnsupportedWHEREClauses):
|
2023-01-16 12:56:12 +01:00
|
|
|
log.error("Fail to add WHERE clauses", exc_info=True)
|
2023-01-07 02:19:18 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
if order_by:
|
|
|
|
if isinstance(order_by, str):
|
2023-01-16 12:56:12 +01:00
|
|
|
sql += f" ORDER BY {order_by}"
|
2023-01-07 02:19:18 +01:00
|
|
|
elif (
|
2023-01-16 12:56:12 +01:00
|
|
|
isinstance(order_by, (list, tuple))
|
|
|
|
and len(order_by) == 2
|
2023-01-07 02:19:18 +01:00
|
|
|
and isinstance(order_by[0], str)
|
|
|
|
and isinstance(order_by[1], str)
|
2023-01-16 12:56:12 +01:00
|
|
|
and order_by[1].upper() in ("ASC", "UPPER")
|
2023-01-07 02:19:18 +01:00
|
|
|
):
|
|
|
|
sql += f' ORDER BY "{order_by[0]}" {order_by[1].upper()}'
|
|
|
|
else:
|
|
|
|
raise DBInvalidOrderByClause(order_by)
|
|
|
|
|
|
|
|
if just_try:
|
|
|
|
log.debug("Just-try mode: execute SELECT query : %s", sql)
|
|
|
|
return just_try
|
|
|
|
|
|
|
|
return self.doSelect(sql, params=params)
|