* Fix pylint warnings across all 24 Python files in web_server - Add module, class, and function docstrings (C0114, C0115, C0116) - Fix import ordering: stdlib before third-party before local (C0411) - Replace wildcard imports with explicit named imports (W0401) - Remove trailing whitespace and add missing final newlines (C0303, C0304) - Replace dict() with dict literals (R1735) - Remove unused imports and variables (W0611, W0612) - Narrow broad Exception catches to specific exceptions (W0718) - Replace f-string logging with lazy % formatting (W1203) - Fix variable naming: UPPER_CASE for constants, snake_case for locals (C0103) - Add pylint disable comments for necessary global statements (W0603) - Fix no-else-return, simplifiable-if-expression, singleton-comparison - Fix bad indentation in stripe.py (W0311) - Add encoding="utf-8" to open() calls (W1514) - Add check=True to subprocess.run() calls (W1510) - Register Celery task modules via conf.include * Update `package-lock.json` add peer dependencies
74 lines
2.6 KiB
Python
74 lines
2.6 KiB
Python
"""SQLite database connection management with context manager support."""
|
|
|
|
import sqlite3
|
|
import os
|
|
|
|
|
|
class Database:
|
|
"""Database wrapper providing connection management and query execution."""
|
|
|
|
def __init__(self) -> None:
|
|
self._db = os.path.join(os.path.abspath(os.path.dirname(__file__)), "app.db")
|
|
self._conn = None
|
|
self.cursor = None
|
|
self.create_connection()
|
|
|
|
def __enter__(self):
|
|
"""Returns db on using with clause"""
|
|
return self
|
|
|
|
def __exit__(self, exc_type, exc_value, traceback):
|
|
"""Closes db connection after with clause"""
|
|
self.close_connection()
|
|
|
|
def create_connection(self) -> None:
|
|
"""Create a database connection if not already established."""
|
|
if self._conn is None:
|
|
self._conn = sqlite3.connect(self._db)
|
|
self._conn.row_factory = sqlite3.Row
|
|
self.cursor = self._conn.cursor()
|
|
|
|
def close_connection(self) -> None:
|
|
"""Close the database connection."""
|
|
if self._conn:
|
|
self._conn.close()
|
|
self._conn = None
|
|
self.cursor = None
|
|
|
|
def fetchall(self, query: str, parameters=None) -> list[dict]:
|
|
"""Fetch all records from the database."""
|
|
self.create_connection()
|
|
self.cursor.execute(query, parameters or ())
|
|
result = self.cursor.fetchall()
|
|
return self.convert_to_list_dict(result)
|
|
|
|
def fetchone(self, query: str, parameters=None) -> dict | None:
|
|
"""Fetch one record from the database."""
|
|
self.create_connection()
|
|
self.cursor.execute(query, parameters or ())
|
|
result = self.cursor.fetchone()
|
|
return self.convert_to_list_dict(result) if result else None
|
|
|
|
def execute(self, query: str, parameters=None) -> None:
|
|
"""Execute an INSERT, UPDATE, or DELETE command and commit changes."""
|
|
self.create_connection()
|
|
try:
|
|
self.cursor.execute(query, parameters or ())
|
|
self._conn.commit()
|
|
except sqlite3.DatabaseError as e:
|
|
print(f"Database error: {e}")
|
|
raise
|
|
|
|
def get_last_insert_id(self) -> int:
|
|
"""Get the ID of the last inserted row."""
|
|
return self.cursor.lastrowid if self.cursor else None
|
|
|
|
def convert_to_list_dict(self, result):
|
|
"""Convert query result to a list of dictionaries."""
|
|
if not result:
|
|
return []
|
|
columns = [desc[0] for desc in self.cursor.description]
|
|
if isinstance(result, list):
|
|
return [dict(zip(columns, row)) for row in result]
|
|
return dict(zip(columns, result))
|