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/CompositeForeignKey.py
Normal file
78
tools/python/jwutils/db/schema/CompositeForeignKey.py
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import Optional, List, Any
|
||||
|
||||
from .ColumnSet import ColumnSet
|
||||
from .SingleForeignKey import SingleForeignKey
|
||||
|
||||
class CompositeForeignKey: # export
|
||||
|
||||
def __init__(self, child_col_set: ColumnSet, parent_col_set: ColumnSet): # TODO: Implement alternative ways to construct
|
||||
|
||||
def __table(s):
|
||||
ret = None
|
||||
for c in s:
|
||||
if ret is None:
|
||||
ret = c.table
|
||||
else:
|
||||
assert(ret == c.table)
|
||||
assert(ret is not None)
|
||||
return ret
|
||||
|
||||
self.__child_col_set = child_col_set
|
||||
self.__parent_col_set = parent_col_set
|
||||
self.__child_table = __table(child_col_set)
|
||||
self.__parent_table = __table(parent_col_set)
|
||||
|
||||
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
|
||||
|
||||
def __table_rel_str(self):
|
||||
return f'{self.__child_table.name} => {self.__parent_table.name}'
|
||||
|
||||
def __cols_rel_str(self, child, parent):
|
||||
return f'{child.name} -> {parent.name}'
|
||||
|
||||
def __len__(self):
|
||||
return self.__len
|
||||
|
||||
def __iter__(self):
|
||||
yield from self.column_relations
|
||||
|
||||
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])
|
||||
return ret
|
||||
|
||||
def __eq__(self, rhs):
|
||||
if rhs.__child_col_set != self.__child_col_set:
|
||||
return False
|
||||
if rhs.__parent_col_set != self.__parent_col_set:
|
||||
return False
|
||||
return True
|
||||
|
||||
@property
|
||||
def child_table(self) -> Any:
|
||||
return self.__child_table
|
||||
|
||||
@property
|
||||
def parent_table(self) -> Any:
|
||||
return self.__parent_table
|
||||
|
||||
@property
|
||||
def child_columns(self) -> ColumnSet:
|
||||
return self.__child_col_set
|
||||
|
||||
@property
|
||||
def parent_columns(self) -> ColumnSet:
|
||||
return self.__parent_col_set
|
||||
|
||||
@property
|
||||
def column_relations(self) -> List[Any]:
|
||||
ret = []
|
||||
if self.__column_relations is None:
|
||||
for i in range(0, self.__len):
|
||||
ret.append(SingleForeignKey(self.__child_col_set[i], self.__parent_col_set[i]))
|
||||
self.__column_relations = ret
|
||||
return self.__column_relations
|
||||
Loading…
Add table
Add a link
Reference in a new issue