-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.py
77 lines (52 loc) · 1.95 KB
/
database.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
"""
database driver connection protocol
maybe dbapi provides database connection protocol class
"""
# pylint: disable=missing-class-docstring, missing-function-docstring
from __future__ import annotations
import typing
from pydantic import BaseModel
from uuid import UUID
ClauseElement = typing.TypeVar("ClauseElement")
class Repository(typing.Protocol):
"""
base repository protocol,
should not be instantiated
subclasses should also be protocols
"""
def add(self, model: BaseModel) -> None:
raise NotImplementedError()
def get(self, ref: UUID) -> BaseModel:
raise NotImplementedError()
class DbTransaction(typing.Protocol):
async def start(self) -> None:
raise NotImplementedError() # pragma: no cover
async def commit(self) -> None:
raise NotImplementedError() # pragma: no cover
async def rollback(self) -> None:
raise NotImplementedError() # pragma: no cover
class DbConnection(typing.Protocol):
async def commit(self) -> None:
raise NotImplementedError()
async def rollback(self) -> None:
raise NotImplementedError()
async def transaction(self) -> DbTransaction:
raise NotImplementedError()
async def execute(self, query: ClauseElement) -> typing.Any:
raise NotImplementedError()
async def execute_many(self, query: typing.List[ClauseElement]) -> None:
raise NotImplementedError()
async def fetch_all(
self, query: ClauseElement
) -> typing.List[typing.Mapping[str, typing.Any]]:
raise NotImplementedError()
async def fetch_one(
self, query: ClauseElement
) -> typing.Optional[typing.Mapping[str, typing.Any]]:
raise NotImplementedError()
async def fetch_val(self, query: ClauseElement) -> typing.Any:
raise NotImplementedError()
class SqlAlchemyRepository(Repository, typing.Protocol):
db: DbConnection
def __init__(self, db: DbConnection):
...