schema: Continue

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2025-01-30 20:34:44 +01:00
commit c50c614f13
7 changed files with 223 additions and 52 deletions

View file

@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
from typing import Optional, List, Any
from typing import Optional, Any
from jwutils.log import *
from .ColumnSet import ColumnSet
from .SingleForeignKey import SingleForeignKey
@ -26,7 +28,9 @@ class CompositeForeignKey: # export
assert(len(self.__child_col_set) == len(self.__parent_col_set))
self.__len = len(self.__child_col_set)
self.__column_relations: Optional[List[SingleForeignKey]] = None
self.__column_relations: Optional[list[SingleForeignKey]] = None
self.__parent_columns_by_child_column: Optional[dict[str, Column]] = None
self.__child_columns_by_parent_column: Optional[dict[str, Column]] = None
def __table_rel_str(self):
return f'{self.__child_table.name} => {self.__parent_table.name}'
@ -42,7 +46,7 @@ class CompositeForeignKey: # export
def __repr__(self):
ret = self.__table_rel_str()
ret += '|| ' + ' | '.join([self.__cols_rel_str(rel.child_col, rel.parent_col) for rel in self.column_relations])
ret += ': ' + ', '.join([self.__cols_rel_str(rel.child_column, rel.parent_column) for rel in self.column_relations])
return ret
def __eq__(self, rhs):
@ -68,8 +72,29 @@ class CompositeForeignKey: # export
def parent_columns(self) -> ColumnSet:
return self.__parent_col_set
def parent_column(self, child_column):
child_column_name = child_column if isinstance(child_column, str) else child_column.name
if self.__parent_columns_by_child_column is None:
d: dict[str, Column] = {}
assert(len(self.__child_col_set) == len(self.__parent_col_set))
for i in range(0, len(self.__child_col_set)):
d[self.__child_col_set[i].name] = self.__parent_col_set[i]
self.__parent_columns_by_child_column = d
return self.__parent_columns_by_child_column[child_column]
def child_column(self, parent_column):
slog(WARNING, f'{self}: Looking for child column belonging to parent column "{parent_column}"')
parent_column_name = parent_column if isinstance(parent_column, str) else parent_column.name
if self.__child_columns_by_parent_column is None:
d: dict[str, Column] = {}
assert(len(self.__parent_col_set) == len(self.__child_col_set))
for i in range(0, len(self.__parent_col_set)):
d[self.__parent_col_set[i].name] = self.__child_col_set[i]
self.__child_columns_by_parent_column = d
return self.__child_columns_by_parent_column[parent_column]
@property
def column_relations(self) -> List[Any]:
def column_relations(self) -> list[Any]:
ret = []
if self.__column_relations is None:
for i in range(0, self.__len):