You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
neeble/utils/database.py

110 lines
2.4 KiB
Python

"""
Database utils module.
"""
import logging
from collections import namedtuple
import MySQLdb
from settings.config import MYSQL_CONFIG
logger = logging.getLogger(__name__)
class Cursor:
"""
Context manage for database handler.
"""
def __init__(self, config: dict) -> None:
"""
Constructor.
"""
self.configuration = config
def __enter__(self) -> 'cursor':
"""
Context manager.
"""
self.conn = MySQLdb.connect(**self.configuration)
self.cursor = self.conn.cursor()
return self.cursor
def __exit__(self, exc_type, exc_value, exc_trace) -> None:
"""
Exit from context manager.
"""
self.conn.commit()
self.cursor.close()
self.conn.close()
def migrate() -> None:
"""
Create tables.
"""
_sql = '''
create table if not exists neeble_quotes(
id int auto_increment primary key,
user varchar(200) not null,
quote varchar(500) not null unique,
index quote_idx (quote)
);
'''
try:
with Cursor(MYSQL_CONFIG) as cursor:
cursor.execute(_sql)
except Exception as ex:
logger.error(ex.args)
def set_quote(user: str, quote: str) -> None:
"""
Set a quote into database.
"""
_sql = f'''
insert into neeble_quotes(user, quote)
value("{user}", "{quote}");
'''
with Cursor(MYSQL_CONFIG) as cursor:
cursor.execute(_sql)
def get_quotes(ids: list) -> tuple:
"""
Get the saved quotes.
ids: List of quote ID's
"""
_sql = f'''
select quote, user, id
from neeble_quotes
'''
_sql = _sql + f' where id not in ({",".join([str(id) for id in ids])});' if ids else _sql + ';'
response = []
obj = namedtuple('Quotes', ['quote', 'user', 'id'])
with Cursor(MYSQL_CONFIG) as cursor:
cursor.execute(_sql)
response = cursor.fetchall()
return tuple(obj(*r) for r in response)
def get_by_id(id: int) -> object:
"""
Get one quote by ID.
"""
obj = namedtuple('Quotes', ['quote', 'user', 'id'])
_sql = f'''
select quote, user, id
from neeble_quotes
where id={id};
'''
with Cursor(MYSQL_CONFIG) as cursor:
cursor.execute(_sql)
quote = cursor.fetchone()
if not quote:
return None
return obj(*quote)