From 90ca36f426ce325f6103aac7e574404488a163de Mon Sep 17 00:00:00 2001 From: C <0xicl33n@gmail.com> Date: Sat, 11 Oct 2025 11:33:00 -0700 Subject: [PATCH] big sexy sqlite db --- src/db.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/db.py diff --git a/src/db.py b/src/db.py new file mode 100644 index 0000000..5e5f4de --- /dev/null +++ b/src/db.py @@ -0,0 +1,53 @@ +import aiosqlite +from time import time +time = int(time()) # we dont need no stinkin floats + +# db = await aiosqlite.connect("edderkop.db") # type: ignore +async def fetch(sqobj): + """ + Asynchronously fetch a record from the REVIEW table by sq_object. + + Args: + sqobj (str): The sq_object identifier to search for. + Returns: + dict or None: The fetched record as a dictionary, or None if not found. + """ + async with aiosqlite.connect("edderkop.db") as db: # type: ignore + db.row_factory = aiosqlite.Row # type: ignore + async with db.execute( + "SELECT * FROM REVIEW WHERE sq_object = ?", (sqobj,) + ) as cursor: + row = await cursor.fetchone() + if row: + return dict(row) + return None + +async def upsert(table: str, data: dict, unique_key: str) -> None: + """ + Asynchronously insert or update a record in the specified table. + + Args: + sq_ib (aiosqlite.Connection): The SQLite database connection. + table (str): The name of the table to upsert into. + data (dict): A dictionary of column-value pairs to insert or update. + unique_key (str): The column name that serves as the unique key for upsert. + + Returns: + None + """ + print(f"Upserting into {table}: {data}") + + columns = ', '.join(data.keys()) + placeholders = ', '.join('?' * len(data)) + updates = ', '.join(f"{col}=excluded.{col}" for col in data.keys() if col != unique_key) + + sql = f""" + INSERT INTO {table} ({columns}) + VALUES ({placeholders}) + ON CONFLICT({unique_key}) DO UPDATE SET + {updates}; + """ + async with aiosqlite.connect("edderkop.db") as db: # type: ignore + async with db.execute(sql, tuple(data.values())) as cursor: + await db.commit() + #await db.close() \ No newline at end of file