Skip to content

Commit

Permalink
improve atomic
Browse files Browse the repository at this point in the history
  • Loading branch information
dantownsend committed Dec 23, 2023
1 parent 3d9ed62 commit fecc24b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
28 changes: 24 additions & 4 deletions piccolo/engine/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from piccolo.utils.warnings import Level, colored_string, colored_warning

if t.TYPE_CHECKING: # pragma: no cover
from piccolo.query.base import Query
from piccolo.query.base import DDL, Query


logger = logging.getLogger(__name__)
Expand All @@ -35,7 +35,7 @@ def validate_savepoint_name(savepoint_name: str) -> None:
)


class Batch:
class Batch(metaclass=ABCMeta):
@abc.abstractmethod
async def __aenter__(self, *args, **kwargs):
...
Expand All @@ -53,14 +53,34 @@ async def __anext__(self) -> t.List[t.Dict]:
...


class BaseTransaction:
class BaseTransaction(metaclass=ABCMeta):
@abc.abstractmethod
async def __aenter__(self, *args, **kwargs):
...

@abc.abstractmethod
async def __aexit__(self, *args, **kwargs):
...


class BaseAtomic(metaclass=ABCMeta):
@abc.abstractmethod
def add(self, *query: t.Union[Query, DDL]):
...

@abc.abstractmethod
async def run(self):
...

@abc.abstractmethod
def run_sync(self):
...

@abc.abstractmethod
def __await__(self):
...


TransactionClass = t.TypeVar("TransactionClass", bound=BaseTransaction)


Expand Down Expand Up @@ -125,7 +145,7 @@ def transaction(self) -> TransactionClass:
pass

@abstractmethod
def atomic(self):
def atomic(self) -> BaseAtomic:
pass

async def check_version(self):
Expand Down
3 changes: 2 additions & 1 deletion piccolo/engine/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing_extensions import Self

from piccolo.engine.base import (
BaseAtomic,
BaseTransaction,
Batch,
Engine,
Expand Down Expand Up @@ -86,7 +87,7 @@ async def __aexit__(self, exception_type, exception, traceback):
###############################################################################


class Atomic:
class Atomic(BaseAtomic):
"""
This is useful if you want to build up a transaction programatically, by
adding queries to it.
Expand Down
3 changes: 2 additions & 1 deletion piccolo/engine/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from typing_extensions import Self

from piccolo.engine.base import (
BaseAtomic,
BaseTransaction,
Batch,
Engine,
Expand Down Expand Up @@ -256,7 +257,7 @@ class TransactionType(enum.Enum):
exclusive = "EXCLUSIVE"


class Atomic:
class Atomic(BaseAtomic):
"""
Usage:
Expand Down

0 comments on commit fecc24b

Please sign in to comment.