-
Notifications
You must be signed in to change notification settings - Fork 4
/
abc.py
86 lines (66 loc) · 2.4 KB
/
abc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# pylint: disable=R0903
"""
dayong.abc
~~~~~~~~~~
Interfaces used within Dayong.
"""
from abc import ABC, abstractmethod
from typing import Any
import tanjun
from sqlmodel import SQLModel
from sqlmodel.engine.result import ScalarResult
from dayong.core.configs import DayongConfig
class Database(ABC):
"""Abstract base class of a database interface."""
@abstractmethod
async def connect(
self, config: DayongConfig = tanjun.injected(type=DayongConfig)
) -> None:
"""Create a database connection.
Args:
config (DayongConfig, optional): . Defaults to
tanjun.injected(type=DayongConfig).
"""
@abstractmethod
async def create_table(self) -> None:
"""Create physical tables for all the table models stored in `Any.metadata`."""
@abstractmethod
async def add_row(self, table_model: SQLModel) -> None:
"""Add a row to the message table.
Args:
table_model (Any): A subclass of SQLModel
"""
@abstractmethod
async def remove_row(self, table_model: SQLModel, attribute: str) -> None:
"""Remove a row from the message table.
Args:
table_model (Any): A subclass of SQLModel
attribute (str): A Table model attribute.
"""
@abstractmethod
async def get_row(self, table_model: SQLModel, attribute: str) -> ScalarResult[Any]:
"""Get row from the message table.
Args:
table_model (Any): A subclass of SQLModel.
attribute (str): A Table model attribute.
Returns:
ScalarResult[Any]: A `ScalarResult` which contains a scalar value or sequence of
scalar values.
"""
@abstractmethod
async def get_all_row(self, table_model: type[SQLModel]) -> ScalarResult[Any]:
"""Fetch all records in a database table.
Args:
table_model (type[SQLModel]): Type of the class which corresponds to a
database table.
Returns:
ScalarResult[Any]: A `ScalarResult` which contains a scalar value or sequence of
scalar values.
"""
@abstractmethod
async def update_row(self, table_model: SQLModel, attribute: str) -> None:
"""Update a database record/row.
Args:
table_model (Any): A subclass of SQLModel.
attribute (str): A Table model attribute.
"""