mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 09:53:32 +01:00
db.query: Add Query, QueryResult, Queries
Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
2b061a6dc4
commit
4cc9906055
4 changed files with 217 additions and 0 deletions
56
tools/python/jwutils/db/query/QueryResult.py
Normal file
56
tools/python/jwutils/db/query/QueryResult.py
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
# -*- 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
|
||||
|
||||
class ResType(Enum): # export
|
||||
Statement = auto()
|
||||
Scalars = auto()
|
||||
One = auto()
|
||||
First = auto()
|
||||
Pages = auto()
|
||||
|
||||
class QueryResult(abc.ABC): # export
|
||||
|
||||
def __init__(self, query: Any) -> None:
|
||||
self.__query = query
|
||||
|
||||
@property
|
||||
def app(self) -> Cmds:
|
||||
return self.__query.app
|
||||
|
||||
@property
|
||||
def db(self) -> DataBase:
|
||||
return self.__query.db
|
||||
|
||||
@property
|
||||
def schema(self):
|
||||
return self.__query.db.schema
|
||||
|
||||
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 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
|
||||
def _cast(self, res_type: ResType, **kwargs) -> Union[Any|list[Any]]:
|
||||
pass
|
||||
Loading…
Add table
Add a link
Reference in a new issue