mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 09:53:32 +01:00
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>
42 lines
905 B
Python
42 lines
905 B
Python
# -*- coding: utf-8 -*-
|
|
|
|
from typing import Any
|
|
|
|
import abc
|
|
from contextlib import contextmanager
|
|
|
|
from jwutils.Config import Config
|
|
from jwutils.db.schema.Schema import Schema
|
|
from jwutils import Cmds
|
|
from .Session import Session
|
|
from ..log import *
|
|
|
|
class DataBase(abc.ABC):
|
|
|
|
def __init__(self, schema: Schema, conf: Config) -> None:
|
|
self.__conf = conf
|
|
self.__schema = schema
|
|
conf.dump(NOTICE, "Initializing database with configuration")
|
|
|
|
@abc.abstractmethod
|
|
def _create_session(self):
|
|
pass
|
|
|
|
def _delete_session(self, session: Session):
|
|
del session
|
|
|
|
@property
|
|
def schema(self):
|
|
return self.__schema
|
|
|
|
@property
|
|
def conf(self):
|
|
return self.__conf
|
|
|
|
@contextmanager
|
|
def session(self):
|
|
ret = self._create_session()
|
|
try:
|
|
yield ret
|
|
finally:
|
|
self._delete_session(ret)
|