Add method to create tournaments

main
sgoudham 3 years ago
parent e298835fbd
commit eb40dba493
Signed by: hammy
GPG Key ID: 44E818FD5457EEA4

@ -1,46 +1,48 @@
import sqlite3 import sqlite3
import sys import sys
from abc import ABC, abstractmethod from contextlib import closing
from sqlite3 import Connection from sqlite3 import Connection
from exclamation_mark_charity import DB_FILE, LOGGER from exclamation_mark_charity.constants import DB_FILE
from exclamation_mark_charity.logger_factory import LoggerFactory
logger = LoggerFactory.get_logger(__name__)
class DatabaseInterface(ABC):
@abstractmethod class Database:
def connect(self):
pass
@abstractmethod
def insert(self):
pass
@abstractmethod
def create(self):
pass
class Sqlite(DatabaseInterface):
def __init__(self): def __init__(self):
self.conn: Connection = self.connect() pass
def connect(self) -> Connection: @staticmethod
LOGGER.info(f"Connecting to {DB_FILE}...") def __connect() -> Connection:
logger.info(f"Connecting to {DB_FILE}...")
try: try:
conn = sqlite3.connect(DB_FILE) conn = sqlite3.connect(DB_FILE)
conn.execute("PRAGMA foreign_keys = ON;") conn.execute("PRAGMA foreign_keys = ON;")
except sqlite3.Error as err: except sqlite3.Error as err:
LOGGER.error(err) logger.critical(err)
sys.exit(err) sys.exit(err)
LOGGER.info(f"Connection to {DB_FILE} successful!") logger.info(f"Connection to '{DB_FILE}' Successful!")
return conn return conn
def create(self): @staticmethod
pass 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
def insert(self): def insert(self):
pass pass

Loading…
Cancel
Save