From 88338b0d2ffc23d1c967cedb31a0e67d84dad73e Mon Sep 17 00:00:00 2001 From: Bradley Bickford Date: Sat, 18 Oct 2025 19:00:35 -0400 Subject: [PATCH] A number of database related additions --- config.json | 6 +++++ sc_database_generator.py | 47 ++++++++++++++++++++++++++++++++++++ utilities/database/sqlite.py | 2 ++ utilities/database/tools.py | 15 ++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 config.json create mode 100644 sc_database_generator.py create mode 100644 utilities/database/tools.py diff --git a/config.json b/config.json new file mode 100644 index 0000000..72d53c8 --- /dev/null +++ b/config.json @@ -0,0 +1,6 @@ +{ + "database": { + "type": "SQLite", + "name": "scdb.db" + } +} \ No newline at end of file diff --git a/sc_database_generator.py b/sc_database_generator.py new file mode 100644 index 0000000..f715e7d --- /dev/null +++ b/sc_database_generator.py @@ -0,0 +1,47 @@ +import json +from utilities.database.sqlite import SQLite + +with open("config.json", 'r') as config_file: + config_json = json.load(config_file.read()) + +my_db = SQLite(config_json["database"]["name"]) + +my_db.query(""" + CREATE TABLE IF NOT EXISTS media ( + media_id INTEGER PRIMARY KEY AUTOINCREMENT, + media_name TEXT NOT NULL, + media_uri TEXT, + media_preparing INTEGER DEFAULT 0 CHECK(media_preparing IN (0, 1)), + media_active INTEGER DEFAULT 0 CHECK(media_active IN (0, 1)) + ) +""") + +my_db.query(""" + CREATE TABLE IF NOT EXISTS media_preparation ( + media_preparation_id INTEGER PRIMARY KEY AUTOINCREMENT, + media_id INTEGER NOT NULL, + media_preparation_started INTEGER DEFAULT 0 CHECK(media_preparation_started IN (0, 1)), + media_preparation_finished INTEGER DEFAULT 0 CHECK(media_preparation_finished IN (0, 1)), + media_preparation_time_between_components INTEGER + ) +""") + +my_db.query(""" + CREATE TABLE IF NOT EXISTS media_component ( + media_component_id INTEGER PRIMARY KEY AUTOINCREMENT, + media_id INTEGER NOT NULL, + media_component_uri NOT NULL, + media_component_index INTEGER CHECK(media_component_index >= 1), + media_component_deleted INTEGER DEFAULT 0 CHECK(media_component_deleted IN (0, 1)) + ) +""") + +my_db.query(""" + CREATE TABLE IF NOT EXISTS media_playback_option ( + media_playback_option_id INTEGER PRIMARY KEY AUTOINCREMENT, + media_playback_option TEXT NOT NULL, + media_playback_option_weight NOT NULL CHECK(media_playback_option_weight >= 1) + ) +""") + +my_db.close() \ No newline at end of file diff --git a/utilities/database/sqlite.py b/utilities/database/sqlite.py index 28bd927..5e15af4 100644 --- a/utilities/database/sqlite.py +++ b/utilities/database/sqlite.py @@ -21,6 +21,8 @@ class SQLite(Database): rows = list(cursor) return (len(rows), rows) + elif query.casefold().startswith("INSERT".casefold()): + return (cursor.lastrowid, None) else: return (cursor.rowcount, None) diff --git a/utilities/database/tools.py b/utilities/database/tools.py new file mode 100644 index 0000000..5234edc --- /dev/null +++ b/utilities/database/tools.py @@ -0,0 +1,15 @@ +from flask import g +from sqlite import SQLite +import json + +def get_db(): + db = getattr(g, "_database", None) + + if db is None: + with open('../../config.json') as config_file: + config_json = json.load(config_file.read()) + + if str(config_json["database"]["type"]).casefold() == "SQLite".casefold(): + db = g._database = SQLite(config_json["database"]["name"]) + + return db \ No newline at end of file