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.
exclamation-mark-charity/exclamation_mark_charity/database.py

82 lines
2.3 KiB
Python

3 years ago
import sqlite3
import sys
from contextlib import closing
3 years ago
from sqlite3 import Connection
from exclamation_mark_charity.constants import DB_FILE
from exclamation_mark_charity.logger_factory import LoggerFactory
3 years ago
logger = LoggerFactory.get_logger(__name__)
3 years ago
class Database:
3 years ago
def __init__(self):
pass
3 years ago
@staticmethod
def __connect() -> Connection:
logger.info(f"Connecting to {DB_FILE}...")
3 years ago
try:
conn = sqlite3.connect(DB_FILE)
conn.execute("PRAGMA foreign_keys = ON;")
except sqlite3.Error as err:
logger.critical(err)
3 years ago
sys.exit(err)
logger.info(f"Connection to '{DB_FILE}' Successful!")
3 years ago
return conn
@staticmethod
def create_tournament(name: str) -> (bool, int):
sql = '''INSERT INTO Tournament (name) VALUES (?)'''
tournament_id = -1
try:
with closing(Database.__connect()) as conn:
with conn:
with closing(conn.cursor()) as cur:
cur.execute(sql, [name])
tournament_id = cur.lastrowid
except sqlite3.Error as err:
logger.error(err)
return False, tournament_id
return True, tournament_id
3 years ago
@staticmethod
def view_all_tournaments() -> (bool, list):
sql = '''SELECT * FROM Tournament'''
results = []
try:
with closing(Database.__connect()) as conn:
with conn:
with closing(conn.cursor()) as cur:
cursor = cur.execute(sql)
results.append([desc[0] for desc in cursor.description[1:]])
for row in cursor:
results.append(row[1:])
except sqlite3.Error as err:
logger.error(err)
return False, results
return True, results
@staticmethod
def delete_many_tournaments(tables: []) -> bool:
sql = '''DELETE FROM Tournament WHERE name = (?)'''
try:
with closing(Database.__connect()) as conn:
with conn:
conn.executemany(sql, tables)
except sqlite3.Error as err:
logger.error(err)
return False
return True
3 years ago
def insert(self):
pass