Skip to content

Commit

Permalink
woorking
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmancuso committed Nov 24, 2024
1 parent 7088421 commit 266f696
Showing 1 changed file with 59 additions and 5 deletions.
64 changes: 59 additions & 5 deletions playground.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,30 @@ def company(self):
related.owner = self
return HasOne(related, "company_id", "company_id")(self)

class CreditCard(Model):
__table__ = "company_credit_cards"
__primary_key__ = "credit_card_id"

def __call__(self):
return self.__dict__['related'].apply_query(self.builder)

def company(self):
related = Company()
related.owner = self
return HasOne(related, "company_id", "company_id")(self)

class Company(Model):
__table__ = "tbl_companies"
__primary_key__ = "company_id"

def __call__(self):
foreign_key_value = self.__dict__['related'].owner.__attributes__.get(self.__dict__['related'].foreign_key)
builder = self.builder.where(self.__dict__['related'].local_key, foreign_key_value)
return builder
return self.__dict__['related'].apply_query(self.builder)

@property
def cards(self):
related = CreditCard()
related.owner = self
return HasMany(related, "company_id", "company_id")(self)

class BaseRelationship:
def init(self, model_class, local_key=None, foreign_key=None):
Expand All @@ -40,6 +55,45 @@ def __init__(self, model_class, foreign_key=None, local_key=None):
self.model_class = model_class
self.foreign_key = foreign_key
self.local_key = local_key
self.owner = None

def apply_query(self, builder):
owner = self.owner
foreign_key_value = owner.__attributes__.get(self.foreign_key)
builder = builder.where(self.local_key, foreign_key_value)
return builder


def __call__(self, owner):
"""Fetch the related record when invoked."""
related_model = self.model_class
# print("related model", related_model)
related_model.owner = self
foreign_key_value = owner.__attributes__.get(self.foreign_key)
if not foreign_key_value:
return None
builder = related_model.builder.where(self.local_key, foreign_key_value)
result = builder.first()
self.owner = owner
result.__dict__['related'] = self
return result


class HasMany(BaseRelationship):
"""Belongs To Relationship Class."""

def __init__(self, model_class, foreign_key=None, local_key=None):
self.model_class = model_class
self.foreign_key = foreign_key
self.local_key = local_key
self.owner = None

def apply_query(self, builder):
owner = self.owner
foreign_key_value = owner.__attributes__.get(self.foreign_key)
builder = builder.where(self.local_key, foreign_key_value)
return builder


def __call__(self, owner):
"""Fetch the related record when invoked."""
Expand All @@ -60,7 +114,7 @@ def __call__(self, owner):
user = User.find(667)

# fetches record on related company record
print(user.company.company_name) # Output: Company name
print(user.company.cards) # Output: Company name

# user.company() part returns a query builder so we can fetch related queries on the fly
print(user.company().to_sql()) # Output: select * from tbl_companies where tbl_companies.company_id = 373849
# print(user.company().limit(1).to_sql()) # Output: select * from tbl_companies where tbl_companies.company_id = 373849

0 comments on commit 266f696

Please sign in to comment.