Skip to content
Snippets Groups Projects
Commit 2ac2159a authored by Renaud Le Gac's avatar Renaud Le Gac
Browse files

Add a method to build the query to resolve foreign key.

parent 705f25c4
No related branches found
No related tags found
No related merge requests found
...@@ -14,11 +14,13 @@ K_KEY = "k_key" ...@@ -14,11 +14,13 @@ K_KEY = "k_key"
class ForeignField(dict): class ForeignField(dict):
def __init__(self, db): def __init__(self, db):
""" Scan the database model to identify foreign key, """Scan the database model to identify foreign key,
Store the pointing table and field. and to store the parent table, field and key.
""" """
self._db = db
for table in db.tables: for table in db.tables:
fields = [db[table][el] for el in db[table].fields] fields = [db[table][el] for el in db[table].fields]
...@@ -50,7 +52,7 @@ class ForeignField(dict): ...@@ -50,7 +52,7 @@ class ForeignField(dict):
def is_foreign_field(self, table, field): def is_foreign_field(self, table, field):
""" Return true is the table.field is a foreign key. """Return true is the table.field is a foreign key.
""" """
if table in self: if table in self:
...@@ -65,8 +67,8 @@ class ForeignField(dict): ...@@ -65,8 +67,8 @@ class ForeignField(dict):
def get_foreign_data(self, table, field): def get_foreign_data(self, table, field):
""" Return a tuple containing the pointing table, the pointing field """Return a tuple containing the parent table, the parent field
and key for the join: and primary key of the parent table for the join:
table.field --> k_table.k_field table.field --> k_table.k_field
table.field == k_table.k_key table.field == k_table.k_key
...@@ -80,3 +82,28 @@ class ForeignField(dict): ...@@ -80,3 +82,28 @@ class ForeignField(dict):
else: else:
return None return None
def get_where_query(self, table):
"""Return the query in order to handle foreign table.
Return None is there is no foreign keys.
"""
if not self.is_table_with_foreign_fields(table):
return None
db, query = self._db, None
for field in self[table]:
k_table = self[table][field][K_TABLE]
k_key = self[table][field][K_KEY]
condition = db[table][field] == db[k_table][k_key]
if query:
query = ((query) & (condition))
else:
query = condition
return query
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment