Files

77 lines
2.1 KiB
Python
Executable File

"""
Base table class for database operations.
This module provides the base Table class that handles cursor management
and common database operations for table-specific implementations.
"""
import sqlite3
from typing import Optional, Any
class Table:
"""
Base class for database table operations.
Attributes:
connection: SQLite database connection
cursor: Database cursor for executing queries
"""
def __init__(self, connection: sqlite3.Connection):
"""
Initialize the table with a database connection.
Args:
connection: SQLite database connection
"""
self.connection = connection
self.cursor = self.connection.cursor()
def create(self):
"""Create the table structure. To be implemented by subclasses."""
raise NotImplementedError("Subclasses must implement create() method")
def exists(self, pk_value: Any) -> bool:
"""
Check if a record exists by primary key.
Args:
pk_value: Primary key value to check
Returns:
True if record exists, False otherwise
"""
raise NotImplementedError("Subclasses must implement exists() method")
def save(self, obj: Any) -> bool:
"""
Save a single object to the database.
Args:
obj: Object to save
Returns:
True if object was saved, False if it already exists
"""
raise NotImplementedError("Subclasses must implement save() method")
def load(self, pk_value: Any) -> Optional[Any]:
"""
Load a single object from the database by primary key.
Args:
pk_value: Primary key value to load
Returns:
Object if found, None otherwise
"""
raise NotImplementedError("Subclasses must implement load() method")
def commit(self):
"""Commit pending changes to the database."""
self.connection.commit()
def close(self):
"""Close the cursor."""
self.cursor.close()