Compare commits

..

2 commits

View file

@ -99,12 +99,13 @@ class AsyncSelectPagination(Pagination):
select = select_q.limit(self.per_page).offset(self._query_offset) select = select_q.limit(self.per_page).offset(self._query_offset)
session: AsyncSession = self._query_args["session"] session: AsyncSession = self._query_args["session"]
out = (await session.execute(select)).scalars() out = (await session.execute(select)).scalars()
return out
async def _query_count(self) -> int: async def _query_count(self) -> int:
select_q: Select = self._query_args["select"] select_q: Select = self._query_args["select"]
sub = select_q.options(lazyload("*")).order_by(None).subquery() sub = select_q.options(lazyload("*")).order_by(None).subquery()
session: AsyncSession = self._query_args["session"] session: AsyncSession = self._query_args["session"]
out = await session.execute(select(func.count()).select_from(sub)) out = (await session.execute(select(func.count()).select_from(sub))).scalar()
return out return out
def __init__(self, def __init__(self,
@ -138,16 +139,14 @@ class AsyncSelectPagination(Pagination):
self.error_out = error_out self.error_out = error_out
self.has_count = count self.has_count = count
async def __await__(self): async def __aiter__(self):
self.items = await self._query_items() self.items = await self._query_items()
if self.items is None:
raise RuntimeError('query returned None')
if not self.items and self.page != 1 and self.error_out: if not self.items and self.page != 1 and self.error_out:
raise self.error_out raise self.error_out
if self.has_count: if self.has_count:
self.total = await self._query_count() self.total = await self._query_count()
return self
async def __aiter__(self):
await self
for i in self.items: for i in self.items:
yield i yield i