from sqlite3 import Cursor

from ._Row import Row


class CursorIterator(object):
    def __init__(self, cursor):
        self.cursor: Cursor = cursor
        self.col_names = {}
        for i in range(len(self.cursor.description)):
            self.col_names[self.cursor.description[i][0]] = i

    def __iter__(self):
        return self

    def __next__(self):
        row = self.cursor.fetchone()
        if row is None:
            raise StopIteration()
        return Row(
            row=row,
            col_names=self.col_names,
        )
