2026-06-10 13:28:35 +02:00
|
|
|
from __future__ import annotations
|
2025-02-02 14:02:21 +01:00
|
|
|
|
|
|
|
|
import abc
|
|
|
|
|
|
2026-07-24 21:44:20 +02:00
|
|
|
from typing import TYPE_CHECKING, Any, cast
|
2025-02-02 14:02:21 +01:00
|
|
|
|
2026-06-10 13:28:35 +02:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from ..DataBase import DataBase
|
|
|
|
|
from ..Session import Session
|
|
|
|
|
from .QueryResult import QueryResult
|
|
|
|
|
|
|
|
|
|
class Query(abc.ABC): # export
|
2025-02-02 14:02:21 +01:00
|
|
|
|
|
|
|
|
def __init__(self, parent: Any) -> None:
|
|
|
|
|
self.__parent = parent
|
|
|
|
|
|
|
|
|
|
# -- pure virtuals
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
2025-03-10 18:44:39 +01:00
|
|
|
def _run(self, session: Session, *args, **kwargs) -> QueryResult:
|
2025-02-02 14:02:21 +01:00
|
|
|
raise Exception('Called pure virtual _run()')
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
def _register(self):
|
|
|
|
|
raise Exception('Called pure virtual _register()')
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
def _column_names(self) -> list[str]:
|
|
|
|
|
raise Exception('Called pure virtual _column()')
|
|
|
|
|
|
|
|
|
|
# -- used by Queries class
|
|
|
|
|
|
|
|
|
|
def register(self):
|
|
|
|
|
return self._register()
|
|
|
|
|
|
|
|
|
|
# -- to be used
|
|
|
|
|
|
|
|
|
|
def _add(self, query_name: str, location: str, func: Any):
|
|
|
|
|
return self.__parent.add(self, query_name, location, func)
|
|
|
|
|
|
2025-03-10 18:44:39 +01:00
|
|
|
def run(self, session: Session, *args, **kwargs) -> QueryResult:
|
|
|
|
|
return self._run(session, *args, **kwargs)
|
2025-02-02 14:02:21 +01:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def parent(self):
|
|
|
|
|
return self.__parent
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def db(self) -> DataBase:
|
2026-07-24 21:44:20 +02:00
|
|
|
return cast('DataBase', self.__parent.db)
|
2025-02-02 14:02:21 +01:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def schema(self):
|
|
|
|
|
return self.__parent.db.schema
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def column_names(self) -> list[str]:
|
|
|
|
|
return self._column_names()
|