Skip to content

Commit

Permalink
Handle null comparisons and added order_by, limit and offset params t…
Browse files Browse the repository at this point in the history
…o models finder methods
  • Loading branch information
maximebf committed Aug 21, 2024
1 parent 7992b3e commit 3eca767
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
12 changes: 12 additions & 0 deletions sqlorm/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,9 @@ def find_all(
with_rels=None,
with_joins=None,
with_lazy=False,
order_by=None,
limit=None,
offset=None,
**cols,
) -> CompositeResultSet:
where = SQL.And([])
Expand All @@ -407,6 +410,12 @@ def find_all(
stmt = cls.select_from(with_rels=with_rels, with_joins=with_joins, with_lazy=with_lazy)
if where:
stmt = stmt.where(where)
if order_by:
stmt = stmt.order_by(order_by)
if limit:
stmt = stmt.limit(limit)
if offset:
stmt = stmt.offset(offset)
return cls.query(stmt)

@classmethod
Expand All @@ -416,6 +425,7 @@ def find_one(
with_rels=None,
with_joins=None,
with_lazy=False,
order_by=None,
**cols,
):
where = SQL.And([])
Expand All @@ -426,6 +436,8 @@ def find_one(
stmt = cls.select_from(with_rels=with_rels, with_joins=with_joins, with_lazy=with_lazy)
if where:
stmt = stmt.where(where)
if order_by:
stmt = stmt.order_by(order_by)
return cls.query(stmt.limit(1)).first()

@classmethod
Expand Down
4 changes: 4 additions & 0 deletions sqlorm/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,13 @@ def __le__(self, other):
return self.op("<=", other)

def __eq__(self, other):
if other is None:
return SQL(self, "IS NULL")
return self.op("=", other)

def __ne__(self, other):
if other is None:
return SQL(self, "IS NOT NULL")
return self.op("!=", other)

def __gt__(self, other):
Expand Down

0 comments on commit 3eca767

Please sign in to comment.