mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 09:53:32 +01:00
db.Session: Add
Add database session API to db. This is a breaking change, because from this commit on, a session object has to be passed to every query. This commit also removes any reference to Cmds / App objects. An instantiated database object can be worked with outside of an App. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
841b5a3391
commit
495cebd769
5 changed files with 56 additions and 31 deletions
|
|
@ -8,27 +8,33 @@ from enum import Enum, auto
|
|||
from jwutils.log import *
|
||||
from jwutils.Cmds import Cmds
|
||||
from jwutils.db.DataBase import DataBase
|
||||
from jwutils.db.Session import Session
|
||||
|
||||
class ResType(Enum): # export
|
||||
Statement = auto()
|
||||
Scalars = auto()
|
||||
One = auto()
|
||||
First = auto()
|
||||
Pages = auto()
|
||||
Statement = auto()
|
||||
Scalars = auto()
|
||||
One = auto()
|
||||
First = auto()
|
||||
Pages = auto()
|
||||
|
||||
class QueryResult(abc.ABC): # export
|
||||
|
||||
def __init__(self, query: Any) -> None:
|
||||
def __init__(self, session: Session, query: Any) -> None:
|
||||
self.__query = query
|
||||
|
||||
@property
|
||||
def app(self) -> Cmds:
|
||||
return self.__query.app
|
||||
self.__session = session
|
||||
|
||||
@property
|
||||
def db(self) -> DataBase:
|
||||
return self.__query.db
|
||||
|
||||
@property
|
||||
def query(self) -> DataBase:
|
||||
return self.__query
|
||||
|
||||
@property
|
||||
def session(self) -> DataBase:
|
||||
return self.__session
|
||||
|
||||
@property
|
||||
def schema(self):
|
||||
return self.__query.db.schema
|
||||
|
|
@ -36,8 +42,8 @@ class QueryResult(abc.ABC): # export
|
|||
def rows(self) -> list[Any]:
|
||||
return self._cast(ResType.Scalars)
|
||||
|
||||
def pages(self, per_page=20) -> Any:
|
||||
return self._cast(ResType.Pages, per_page=per_page)
|
||||
def pages(self, per_page=20, page=1) -> Any:
|
||||
return self._cast(ResType.Pages, per_page=per_page, page=page)
|
||||
|
||||
def one(self) -> Any:
|
||||
return self._cast(ResType.One)
|
||||
|
|
@ -52,5 +58,5 @@ class QueryResult(abc.ABC): # export
|
|||
# -- pure virtuals
|
||||
|
||||
@abc.abstractmethod
|
||||
def _cast(self, res_type: ResType, **kwargs) -> Union[Any|list[Any]]:
|
||||
def _cast(self, session: Session, res_type: ResType, **kwargs) -> Union[Any|list[Any]]:
|
||||
pass
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue