58 lines
1.4 KiB
Python
58 lines
1.4 KiB
Python
from flask import g
|
|
from sqlite3 import connect, Connection, Cursor
|
|
|
|
|
|
def get_db():
|
|
db = getattr(g, "_database", None)
|
|
if db is None:
|
|
db = g._database = connect("/data/tables.db")
|
|
return db
|
|
|
|
|
|
_game_table_create = """
|
|
CREATE TABLE IF NOT EXISTS "game_tables" (
|
|
"game_table_id" INTEGER NOT NULL UNIQUE,
|
|
"game_table_name" TEXT UNIQUE,
|
|
"game_table_link" TEXT UNIQUE,
|
|
"active" INTEGER DEFAULT 0,
|
|
"game_key" TEXT,
|
|
"docker_id" TEXT,
|
|
PRIMARY KEY("game_table_id")
|
|
);
|
|
"""
|
|
|
|
_key_table_create = """
|
|
CREATE TABLE IF NOT EXISTS "game_keys" (
|
|
"key" TEXT NOT NULL,
|
|
"game_table_id" INTEGER,
|
|
PRIMARY KEY("key")
|
|
);
|
|
"""
|
|
|
|
def init_db(connection):
|
|
with SmartCursor(connection=connection) as smart_cursor:
|
|
smart_cursor.execute(_game_table_create)
|
|
smart_cursor.execute(_key_table_create)
|
|
connection.commit()
|
|
|
|
|
|
class SmartCursor:
|
|
connection: Connection
|
|
curosr: Cursor
|
|
autoClose: bool
|
|
|
|
def __init__(self, cursor: Cursor = None, connection: Connectoin = None):
|
|
self.cursor = cursor
|
|
self.connection = connection
|
|
self.autoClose = False
|
|
|
|
def __enter__(self):
|
|
if self.cursor == None:
|
|
self.cursor = self.connection.cursor()
|
|
self.autoClose = True
|
|
return self.cursor
|
|
|
|
def __exit__(self, exc_type, exc_value, traceback):
|
|
if self.autoClose:
|
|
self.cursor.close()
|