diff --git a/tools/python/jwutils/db/schema/Schema.py b/tools/python/jwutils/db/schema/Schema.py index 3ab372b..c14428a 100644 --- a/tools/python/jwutils/db/schema/Schema.py +++ b/tools/python/jwutils/db/schema/Schema.py @@ -16,6 +16,7 @@ class Schema(abc.ABC): # export def __init__(self) -> None: self.___tables: Optional[List[Table]] = None self.__foreign_keys: Optional[List[CompositeForeignKey]] = None + self.__access_defining_columns: Optional[List[str]] = None @property def __tables(self): @@ -44,6 +45,10 @@ class Schema(abc.ABC): # export def _foreign_keys(self) -> List[CompositeForeignKey]: pass + @abc.abstractmethod + def _access_defining_columns(self): + pass + # ------ API to be called @property @@ -54,6 +59,12 @@ class Schema(abc.ABC): # export def tables(self) -> Iterable[Table]: 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 def foreign_key_constraints(self) -> List[CompositeForeignKey]: if self.__foreign_keys is None: diff --git a/tools/python/jwutils/db/schema/Table.py b/tools/python/jwutils/db/schema/Table.py index 3e8c00a..8111357 100644 --- a/tools/python/jwutils/db/schema/Table.py +++ b/tools/python/jwutils/db/schema/Table.py @@ -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: