Add initial SQLmem package structure with SQL parser, cache manager, column registry, and tests

This commit is contained in:
Jan Doubravský
2026-06-01 16:44:25 +02:00
parent 54879ef9d0
commit 74772cee4a
18 changed files with 835 additions and 0 deletions
+43
View File
@@ -0,0 +1,43 @@
import sqlite3
from loguru import logger
from sqlalchemy.engine import Engine
from .cache import CacheManager
from .config import BACKUP_INTERVAL_SECONDS, CACHE_DB_PATH
from .executor import QueryExecutor
from .parser import parse
from .registry import ColumnRegistry
class CachingEngine:
"""Transparent SQLAlchemy-compatible cache layer."""
def __init__(self, source_engine: Engine) -> None:
self._source_engine = source_engine
self._cache = CacheManager(CACHE_DB_PATH, BACKUP_INTERVAL_SECONDS)
self._registry = ColumnRegistry(self._cache.connection)
logger.info("CachingEngine initialized.")
def execute(self, sql: str) -> list[dict]:
parsed = parse(sql)
with self._source_engine.connect() as sa_conn:
raw_conn: sqlite3.Connection = sa_conn.connection.dbapi_connection
executor = QueryExecutor(self._cache, self._registry, raw_conn)
return executor.execute(parsed)
def invalidate(self, table: str) -> None:
logger.info(f"Manually invalidating cache for table {table!r}")
with self._cache._lock:
self._cache.connection.execute(f"DROP TABLE IF EXISTS {table}")
self._cache.connection.execute(
"DELETE FROM _sqlmem_tables WHERE table_name = ?", (table,)
)
self._cache.connection.execute(
"DELETE FROM _sqlmem_columns WHERE table_name = ?", (table,)
)
self._cache.connection.commit()
def close(self) -> None:
self._cache.close()
logger.info("CachingEngine closed.")