Skip to content

Commit 3eca767

Browse files
committed
Handle null comparisons and added order_by, limit and offset params to models finder methods
1 parent 7992b3e commit 3eca767

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

sqlorm/model.py

+12
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,9 @@ def find_all(
397397
with_rels=None,
398398
with_joins=None,
399399
with_lazy=False,
400+
order_by=None,
401+
limit=None,
402+
offset=None,
400403
**cols,
401404
) -> CompositeResultSet:
402405
where = SQL.And([])
@@ -407,6 +410,12 @@ def find_all(
407410
stmt = cls.select_from(with_rels=with_rels, with_joins=with_joins, with_lazy=with_lazy)
408411
if where:
409412
stmt = stmt.where(where)
413+
if order_by:
414+
stmt = stmt.order_by(order_by)
415+
if limit:
416+
stmt = stmt.limit(limit)
417+
if offset:
418+
stmt = stmt.offset(offset)
410419
return cls.query(stmt)
411420

412421
@classmethod
@@ -416,6 +425,7 @@ def find_one(
416425
with_rels=None,
417426
with_joins=None,
418427
with_lazy=False,
428+
order_by=None,
419429
**cols,
420430
):
421431
where = SQL.And([])
@@ -426,6 +436,8 @@ def find_one(
426436
stmt = cls.select_from(with_rels=with_rels, with_joins=with_joins, with_lazy=with_lazy)
427437
if where:
428438
stmt = stmt.where(where)
439+
if order_by:
440+
stmt = stmt.order_by(order_by)
429441
return cls.query(stmt.limit(1)).first()
430442

431443
@classmethod

sqlorm/sql.py

+4
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,13 @@ def __le__(self, other):
7474
return self.op("<=", other)
7575

7676
def __eq__(self, other):
77+
if other is None:
78+
return SQL(self, "IS NULL")
7779
return self.op("=", other)
7880

7981
def __ne__(self, other):
82+
if other is None:
83+
return SQL(self, "IS NOT NULL")
8084
return self.op("!=", other)
8185

8286
def __gt__(self, other):

0 commit comments

Comments
 (0)