Skip to content

Commit

Permalink
database.cursors:Cursors - rename entries to row
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoCampinoti94 committed Oct 22, 2024
1 parent bf9972c commit 0a5dbfe
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions acacore/database/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,26 @@ def __init__(self, cursor: SQLiteCursor, model: Type[M], columns: list[ColumnSpe
self.cursor.row_factory = Row
self.model: Type[M] = model
self.columns: list[ColumnSpec] = columns
self._cols: dict[str, Callable[[SQLValue], Any]] = {c.name: c.from_sql for c in columns}
self._load: Callable[[Row], M] = lambda r: self.model.model_validate(
{k: f(r[k]) for k, f in self._cols.items()}
)

_cols: dict[str, Callable[[SQLValue], Any]] = {c.name: c.from_sql for c in columns}

def _loader(row: Row) -> M:
return self.model.model_validate({k: f(row[k]) for k, f in _cols.items()})

self.entries: Generator[M, None, None] = (_loader(row) for row in self.cursor)
@property
def rows(self) -> Generator[M, None, None]:
return (self._load(row) for row in self.cursor)

def __iter__(self) -> Generator[M, None, None]:
yield from self.entries
yield from self.rows

def __next__(self) -> M:
return next(self.entries)
return next(self.rows)

def fetchone(self) -> M | None:
return next(self, None)
return next(self.rows, None)

def fetchmany(self, size: int) -> list[M]:
return list(islice(self, size))
return list(islice(self.rows, size))

def fetchall(self) -> list[M]:
return list(self)
return list(self.rows)

0 comments on commit 0a5dbfe

Please sign in to comment.