mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 09:53:32 +01:00
db.schema: Add location definition to API
Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
e688cd2364
commit
9cfcc1bd68
2 changed files with 57 additions and 0 deletions
|
|
@ -16,6 +16,7 @@ class Schema(abc.ABC): # export
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.___tables: Optional[List[Table]] = None
|
self.___tables: Optional[List[Table]] = None
|
||||||
self.__foreign_keys: Optional[List[CompositeForeignKey]] = None
|
self.__foreign_keys: Optional[List[CompositeForeignKey]] = None
|
||||||
|
self.__access_defining_columns: Optional[List[str]] = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def __tables(self):
|
def __tables(self):
|
||||||
|
|
@ -44,6 +45,10 @@ class Schema(abc.ABC): # export
|
||||||
def _foreign_keys(self) -> List[CompositeForeignKey]:
|
def _foreign_keys(self) -> List[CompositeForeignKey]:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@abc.abstractmethod
|
||||||
|
def _access_defining_columns(self):
|
||||||
|
pass
|
||||||
|
|
||||||
# ------ API to be called
|
# ------ API to be called
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
@ -54,6 +59,12 @@ class Schema(abc.ABC): # export
|
||||||
def tables(self) -> Iterable[Table]:
|
def tables(self) -> Iterable[Table]:
|
||||||
return self.__tables.values()
|
return self.__tables.values()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def access_defining_columns(self):
|
||||||
|
if self.__access_defining_columns is None:
|
||||||
|
self.__access_defining_columns = self._access_defining_columns()
|
||||||
|
return self.__access_defining_columns
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def foreign_key_constraints(self) -> List[CompositeForeignKey]:
|
def foreign_key_constraints(self) -> List[CompositeForeignKey]:
|
||||||
if self.__foreign_keys is None:
|
if self.__foreign_keys is None:
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,10 @@ class Table(abc.ABC): # export
|
||||||
self.__log_columns: Optional[Iterable[str]] = None
|
self.__log_columns: Optional[Iterable[str]] = None
|
||||||
self.__column_default: Optional[dict[str, Any]] = None
|
self.__column_default: Optional[dict[str, Any]] = None
|
||||||
|
|
||||||
|
self.__base_location: Optional[Iterable[str]] = None
|
||||||
|
self.__location: Optional[Iterable[str]] = None
|
||||||
|
self.__row_location: Optional[Iterable[str]] = None
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return self.__name
|
return self.__name
|
||||||
|
|
||||||
|
|
@ -75,6 +79,30 @@ class Table(abc.ABC): # export
|
||||||
slog(WARNING, f'Returning None model name for table {self.name}')
|
slog(WARNING, f'Returning None model name for table {self.name}')
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def _model_class(self) -> Optional[Any]:
|
||||||
|
slog(WARNING, f'Returning None model class for table {self.name}')
|
||||||
|
throw(ERR, "Not implemented")
|
||||||
|
return None
|
||||||
|
|
||||||
|
# -- common URL schema for all data
|
||||||
|
def _base_location(self) -> Optional[str]:
|
||||||
|
return f'/self.name'
|
||||||
|
|
||||||
|
def _location(self) -> Optional[str]:
|
||||||
|
ret = ''
|
||||||
|
for col in self.__schema.access_defining_columns:
|
||||||
|
if col in self.primary_keys:
|
||||||
|
ret += f'/<{col}>'
|
||||||
|
ret += '/' + self.base_location
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def _row_location(self) -> Optional[str]:
|
||||||
|
ret = self._location()
|
||||||
|
for col in self.primary_keys:
|
||||||
|
if col not in self.__schema.access_defining_columns:
|
||||||
|
ret += f'/<{col}>'
|
||||||
|
return ret
|
||||||
|
|
||||||
# -- To be used
|
# -- To be used
|
||||||
|
|
||||||
def column_default(self, name) -> Any:
|
def column_default(self, name) -> Any:
|
||||||
|
|
@ -108,6 +136,24 @@ class Table(abc.ABC): # export
|
||||||
def model_name(self) -> Optional[str]:
|
def model_name(self) -> Optional[str]:
|
||||||
return self._model_name()
|
return self._model_name()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def base_location(self):
|
||||||
|
if self.__base_location is None:
|
||||||
|
self.__base_location = self._base_location()
|
||||||
|
return self.__base_location
|
||||||
|
|
||||||
|
@property
|
||||||
|
def location(self):
|
||||||
|
if self.__location is None:
|
||||||
|
self.__location = self._base_location()
|
||||||
|
return self.__location
|
||||||
|
|
||||||
|
@property
|
||||||
|
def row_location(self):
|
||||||
|
if self.__row_location is None:
|
||||||
|
self.__row_location = self._row_location()
|
||||||
|
return self.__row_location
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def primary_keys(self) -> Iterable[str]:
|
def primary_keys(self) -> Iterable[str]:
|
||||||
if self.__primary_keys is None:
|
if self.__primary_keys is None:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue