db.schema: Add location definition to API

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2025-01-30 14:26:43 +01:00
commit 9cfcc1bd68
2 changed files with 57 additions and 0 deletions

View file

@ -29,6 +29,10 @@ class Table(abc.ABC): # export
self.__log_columns: Optional[Iterable[str]] = 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:
return self.__name
@ -75,6 +79,30 @@ class Table(abc.ABC): # export
slog(WARNING, f'Returning None model name for table {self.name}')
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
def column_default(self, name) -> Any:
@ -108,6 +136,24 @@ class Table(abc.ABC): # export
def model_name(self) -> Optional[str]:
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
def primary_keys(self) -> Iterable[str]:
if self.__primary_keys is None: