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
43
tools/python/jwutils/db/schema/ColumnSet.py
Normal file
43
tools/python/jwutils/db/schema/ColumnSet.py
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import Optional, List, Iterable, Any
|
||||
|
||||
class ColumnSet: # export
|
||||
|
||||
def __init__(self, *args: List[Any], columns: List[Any]=[], table: Optional[Any]=None, names: Optional[List[str]]=None):
|
||||
self.__columns: List[Any] = [*args]
|
||||
self.__columns.extend(columns)
|
||||
self.__table = table
|
||||
if names is not None:
|
||||
assert(table is not None)
|
||||
for name in names:
|
||||
self.__columns.append(table.column(name))
|
||||
if self.__table is not None:
|
||||
for col in columns:
|
||||
assert(col.table == self.__table)
|
||||
|
||||
def __len__(self):
|
||||
return len(self.__columns)
|
||||
|
||||
def __iter__(self):
|
||||
yield from self.__columns
|
||||
|
||||
def __repr__(self):
|
||||
return '|'.join([col.name for col in self.__columns])
|
||||
|
||||
def __getitem__(self, index):
|
||||
return self.__columns[index]
|
||||
|
||||
def __eq__(self, rhs):
|
||||
if self.__table != rhs.__table:
|
||||
return False
|
||||
if len(self.__columns) != len(rhs.__columns):
|
||||
return False
|
||||
for i in range(0, len(self.__columns)):
|
||||
if self.__columns[i].name != rhs.__columns[i].name:
|
||||
return False
|
||||
return True
|
||||
|
||||
@property
|
||||
def columns(self) -> List[Any]:
|
||||
return self.__columns
|
||||
Loading…
Add table
Add a link
Reference in a new issue