mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 09:53:32 +01:00
Add db.schema framework
jw.db.schema is a set of classes meant as an interface to describe a database schema. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
cc7aeeac31
commit
17ab47e96a
8 changed files with 556 additions and 0 deletions
78
tools/python/jwutils/db/schema/Schema.py
Normal file
78
tools/python/jwutils/db/schema/Schema.py
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import Optional, List, Iterable
|
||||
|
||||
import abc
|
||||
|
||||
from ...log import *
|
||||
|
||||
from .Table import Table
|
||||
from .Column import Column
|
||||
from .DataType import DataType
|
||||
from .CompositeForeignKey import CompositeForeignKey
|
||||
|
||||
class Schema(abc.ABC): # export
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.___tables: Optional[List[Table]] = None
|
||||
self.__foreign_keys: Optional[List[CompositeForeignKey]] = None
|
||||
|
||||
@property
|
||||
def __tables(self):
|
||||
if self.___tables is None:
|
||||
ret = dict()
|
||||
for name in self._table_names():
|
||||
slog(WARNING, f'Caching metadata for table "{name}"')
|
||||
assert(isinstance(name, str))
|
||||
ret[name] = self._table(name)
|
||||
self.___tables = ret
|
||||
return self.___tables
|
||||
|
||||
# ------ API to be implemented
|
||||
|
||||
@abc.abstractmethod
|
||||
def _table_names(self) -> Iterable[str]:
|
||||
throw(ERR, "Called pure virtual base class method")
|
||||
return []
|
||||
|
||||
@abc.abstractmethod
|
||||
def _table(self, name: str) -> Table:
|
||||
throw(ERR, "Called pure virtual base class method")
|
||||
return None # type: ignore
|
||||
|
||||
@abc.abstractmethod
|
||||
def _foreign_keys(self) -> List[CompositeForeignKey]:
|
||||
pass
|
||||
|
||||
# ------ API to be called
|
||||
|
||||
@property
|
||||
def table_names(self) -> Iterable[str]:
|
||||
return self.__tables.keys()
|
||||
|
||||
@property
|
||||
def tables(self) -> Iterable[Table]:
|
||||
return self.__tables.values()
|
||||
|
||||
@property
|
||||
def foreign_key_constraints(self) -> List[CompositeForeignKey]:
|
||||
if self.__foreign_keys is None:
|
||||
self.__foreign_keys = self._foreign_keys()
|
||||
return self.__foreign_keys
|
||||
|
||||
def table(self, name: str) -> Table:
|
||||
return self.__tables[name]
|
||||
|
||||
def table_by_model_name(self, name: str, throw=False) -> Table:
|
||||
for table in self.__tables.values():
|
||||
if table.model_name == name:
|
||||
return table
|
||||
if throw:
|
||||
raise Exception(f'Table "{name}" not found in database metadata')
|
||||
return None # type: ignore
|
||||
|
||||
def primary_keys(self, table_name: str) -> Iterable[str]:
|
||||
return self.__tables[table_name].primary_keys
|
||||
|
||||
def columns(self, table_name: str) -> Iterable[Column]:
|
||||
return self.__tables[table_name].columns
|
||||
Loading…
Add table
Add a link
Reference in a new issue