mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-28 12:54:15 +02:00
subclass namedtuple for foreign key
This commit is contained in:
parent
597965a5a9
commit
99992deb31
1 changed files with 16 additions and 42 deletions
|
|
@ -51,22 +51,26 @@ except ImportError:
|
||||||
Column = namedtuple(
|
Column = namedtuple(
|
||||||
"Column", ("cid", "name", "type", "notnull", "default_value", "is_pk")
|
"Column", ("cid", "name", "type", "notnull", "default_value", "is_pk")
|
||||||
)
|
)
|
||||||
|
ForeignKeyBase = namedtuple(
|
||||||
|
"ForeignKeyBase", ("table", "column", "other_table", "other_column")
|
||||||
|
)
|
||||||
Index = namedtuple("Index", ("seq", "name", "unique", "origin", "partial", "columns"))
|
Index = namedtuple("Index", ("seq", "name", "unique", "origin", "partial", "columns"))
|
||||||
Trigger = namedtuple("Trigger", ("name", "table", "sql"))
|
Trigger = namedtuple("Trigger", ("name", "table", "sql"))
|
||||||
|
|
||||||
|
|
||||||
class ForeignKey:
|
class ForeignKey(ForeignKeyBase):
|
||||||
def __init__(self, table, column, other_table, other_column):
|
def __new__(cls, table, column, other_table, other_column):
|
||||||
self.table = table
|
# column and other_column should be a tuple
|
||||||
if isinstance(column, str):
|
if isinstance(column, (tuple, list)):
|
||||||
self.column = (column,)
|
column = tuple(column)
|
||||||
elif isinstance(column, (tuple, list)):
|
else:
|
||||||
self.column = tuple(column)
|
column = (column,)
|
||||||
self.other_table = other_table
|
if isinstance(other_column, (tuple, list)):
|
||||||
if isinstance(other_column, str):
|
other_column = tuple(other_column)
|
||||||
self.other_column = (other_column,)
|
else:
|
||||||
elif isinstance(other_column, (tuple, list)):
|
other_column = (other_column,)
|
||||||
self.other_column = tuple(other_column)
|
self = super(ForeignKey, cls).__new__(cls, table, column, other_table, other_column)
|
||||||
|
return self
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def column_str(self):
|
def column_str(self):
|
||||||
|
|
@ -76,36 +80,6 @@ class ForeignKey:
|
||||||
def other_column_str(self):
|
def other_column_str(self):
|
||||||
return ",".join(["[{}]".format(c) for c in self.other_column])
|
return ",".join(["[{}]".format(c) for c in self.other_column])
|
||||||
|
|
||||||
def __eq__(self, other):
|
|
||||||
if isinstance(other, ForeignKey):
|
|
||||||
return all(
|
|
||||||
(
|
|
||||||
self.table == other.table,
|
|
||||||
self.column == other.column,
|
|
||||||
self.other_table == other.other_table,
|
|
||||||
self.other_column == other.other_column,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
return False
|
|
||||||
|
|
||||||
def __lt__(self, other):
|
|
||||||
if isinstance(other, ForeignKey):
|
|
||||||
return (self.table, self.column, self.other_table, self.other_column) < (
|
|
||||||
other.table,
|
|
||||||
other.column,
|
|
||||||
other.other_table,
|
|
||||||
other.other_column,
|
|
||||||
)
|
|
||||||
return False
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
return "ForeignKey({table}({column}), {other_table}({other_column}))".format(
|
|
||||||
table=self.table,
|
|
||||||
column=self.column_str,
|
|
||||||
other_table=self.other_table,
|
|
||||||
other_column=self.other_column_str,
|
|
||||||
)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def sql(self):
|
def sql(self):
|
||||||
return (
|
return (
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue