Skip to content

Commit

Permalink
Fix formal string representation operator Query.__repr__(), Ref. #94
Browse files Browse the repository at this point in the history
  • Loading branch information
RKrahl committed Aug 24, 2024
1 parent 4d58e3d commit 397c453
Showing 1 changed file with 45 additions and 8 deletions.
53 changes: 45 additions & 8 deletions src/icat/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ def __init__(self, obj):
self.attr = attr
self.jpql_func = jpql_func

@property
def obj(self):
if self.jpql_func:
return "%s(%s)" % (self.jpql_func, self.attr)
else:
return self.attr

def __repr__(self):
return repr(self.obj)

class OrderItem(ItemBase):
"""Represent an item in the ORDER BY clause.
"""
Expand Down Expand Up @@ -113,6 +123,13 @@ def formatstr(self):
else:
return "%s"

@property
def obj(self):
obj = super().obj
if self.direction:
obj = (obj, self.direction)
return obj


class ConditionItem(ItemBase):
"""Represent an item in the WHERE clause.
Expand All @@ -129,6 +146,10 @@ def formatstr(self):
else:
return "%%s %s" % (rhs)

@property
def obj(self):
return (super().obj, self.rhs)

# ========================== class Query =============================

class Query():
Expand Down Expand Up @@ -629,14 +650,30 @@ def limit_clause(self):
def __repr__(self):
"""Return a formal representation of the query.
"""
return ("%s(%s, %s, attributes=%s, aggregate=%s, order=%s, "
"conditions=%s, includes=%s, limit=%s, join_specs=%s)"
% (self.__class__.__name__,
repr(self.client), repr(self.entity.BeanName),
repr(self.attributes), repr(self.aggregate),
repr(self.order), repr(self.conditions),
repr(self.includes), repr(self.limit),
repr(self.join_specs)))
kwargs = []
if self.attributes:
kwargs.append("attributes=%s" % repr(self.attributes))
if self.aggregate:
kwargs.append("aggregate=%s" % repr(self.aggregate))
if self.order:
kwargs.append("order=%s" % repr(self.order))
if self.conditions:
kwargs.append("conditions=%s" % repr(self.conditions))
if self.includes:
kwargs.append("includes=%s" % repr(self.includes))
if self.limit:
kwargs.append("limit=%s" % repr(self.limit))
if self.join_specs:
kwargs.append("join_specs=%s" % repr(self.join_specs))
if kwargs:
return ("%s(%s, %s, %s)"
% (self.__class__.__name__,
repr(self.client), repr(self.entity.BeanName),
", ".join(kwargs)))
else:
return ("%s(%s, %s)"
% (self.__class__.__name__,
repr(self.client), repr(self.entity.BeanName)))

def __str__(self):
"""Return a string representation of the query.
Expand Down

0 comments on commit 397c453

Please sign in to comment.