Table works

This commit is contained in:
2024-10-09 15:13:40 +02:00
parent a4d0803d20
commit 4199b1c347
7 changed files with 183 additions and 47 deletions

View File

@ -125,8 +125,8 @@ class DB():
id:str|None=None, sender:str|None=None, recipient:str|None=None,
processed:bool|None=None,
time_after:int|None=None, time_before:int|None=None,
limit:int=20, offset:int=0
) -> Generator[RowObject, None, None]:
limit:int=20, offset:int=0, _count_only:bool=False
) -> Generator[RowObject|int, None, None]:
where = []
params = {
@ -153,11 +153,29 @@ class DB():
where_clause = ""
with self.db:
for row in self.db.execute(
"SELECT * FROM Messages {} ORDER BY time DESC LIMIT :lim OFFSET :off".format(where_clause),
params
):
yield self._create_row_object(row)
if _count_only:
count = self.db.execute(
"SELECT COUNT(*) as count FROM Messages {}".format(where_clause),
params
).fetchone()
yield count['count']
else:
for row in self.db.execute(
"SELECT * FROM Messages {} ORDER BY time DESC LIMIT :lim OFFSET :off".format(where_clause),
params
):
yield self._create_row_object(row)
def __len__(self) -> int:
return self.len()
def len(self, **kwargs) -> int:
"""
See `DB.iterate` for possible values of `kwargs`.
"""
kwargs['_count_only'] = True
return next(self.iterate(**kwargs))
def _create_row_object(self, row:sqlite3.Row) -> RowObject:
return RowObject(