2025-02-02 14:02:21 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
from typing import Any, Union
|
|
|
|
|
|
|
|
|
|
import abc
|
|
|
|
|
from enum import Enum, auto
|
|
|
|
|
|
|
|
|
|
from jwutils.log import *
|
|
|
|
|
from jwutils.Cmds import Cmds
|
|
|
|
|
from jwutils.db.DataBase import DataBase
|
2025-03-10 18:44:39 +01:00
|
|
|
from jwutils.db.Session import Session
|
2025-02-02 14:02:21 +01:00
|
|
|
|
|
|
|
|
class ResType(Enum): # export
|
2025-03-10 18:44:39 +01:00
|
|
|
Statement = auto()
|
|
|
|
|
Scalars = auto()
|
|
|
|
|
One = auto()
|
|
|
|
|
First = auto()
|
|
|
|
|
Pages = auto()
|
2025-02-02 14:02:21 +01:00
|
|
|
|
|
|
|
|
class QueryResult(abc.ABC): # export
|
|
|
|
|
|
2025-03-10 18:44:39 +01:00
|
|
|
def __init__(self, session: Session, query: Any) -> None:
|
2025-02-02 14:02:21 +01:00
|
|
|
self.__query = query
|
2025-03-10 18:44:39 +01:00
|
|
|
self.__session = session
|
2025-02-02 14:02:21 +01:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def db(self) -> DataBase:
|
|
|
|
|
return self.__query.db
|
|
|
|
|
|
2025-03-10 18:44:39 +01:00
|
|
|
@property
|
|
|
|
|
def query(self) -> DataBase:
|
|
|
|
|
return self.__query
|
|
|
|
|
|
|
|
|
|
@property
|
2025-04-29 11:54:56 +02:00
|
|
|
def session(self) -> Session:
|
2025-03-10 18:44:39 +01:00
|
|
|
return self.__session
|
|
|
|
|
|
2025-02-02 14:02:21 +01:00
|
|
|
@property
|
|
|
|
|
def schema(self):
|
|
|
|
|
return self.__query.db.schema
|
|
|
|
|
|
|
|
|
|
def rows(self) -> list[Any]:
|
|
|
|
|
return self._cast(ResType.Scalars)
|
|
|
|
|
|
2025-03-10 18:44:39 +01:00
|
|
|
def pages(self, per_page=20, page=1) -> Any:
|
|
|
|
|
return self._cast(ResType.Pages, per_page=per_page, page=page)
|
2025-02-02 14:02:21 +01:00
|
|
|
|
|
|
|
|
def one(self) -> Any:
|
|
|
|
|
return self._cast(ResType.One)
|
|
|
|
|
|
|
|
|
|
def first(self) -> Any:
|
|
|
|
|
return self._cast(ResType.First)
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def statement(self) -> Any:
|
|
|
|
|
return self._cast(ResType.Statement)
|
|
|
|
|
|
|
|
|
|
# -- pure virtuals
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
2025-04-29 11:54:56 +02:00
|
|
|
def _cast(self, res_type: ResType, **kwargs) -> Union[Any|list[Any]]:
|
2025-02-02 14:02:21 +01:00
|
|
|
pass
|