diff --git a/aiosql/adapters/duckdb.py b/aiosql/adapters/duckdb.py index 426c0870..c010511f 100644 --- a/aiosql/adapters/duckdb.py +++ b/aiosql/adapters/duckdb.py @@ -45,42 +45,36 @@ def insert_returning(self, conn, query_name, sql, parameters): # pragma: no cov def select(self, conn, query_name: str, sql: str, parameters, record_class=None): column_names: List[str] = [] cur = self._cursor(conn) - try: - cur.execute(sql, parameters) - if record_class is None: - first = True - for row in cur.fetchall(): - if first: # get column names on the fly - column_names = [c[0] for c in cur.description or []] - first = False - if self._convert_row_to_dict: # pragma: no cover - # strict=False: requires 3.10 - yield dict(zip(column_names, row)) - else: - yield row - else: # pragma: no cover - first = True - for row in cur.fetchall(): - if first: # only get description on the fly, for apsw - column_names = [c[0] for c in cur.description or []] - first = False + cur.execute(sql, parameters) + if record_class is None: + first = True + for row in cur.fetchall(): + if first: # get column names on the fly + column_names = [c[0] for c in cur.description or []] + first = False + if self._convert_row_to_dict: # pragma: no cover # strict=False: requires 3.10 - yield record_class(**dict(zip(column_names, row))) - finally: - cur.close() + yield dict(zip(column_names, row)) + else: + yield row + else: # pragma: no cover + first = True + for row in cur.fetchall(): + if first: # only get description on the fly, for apsw + column_names = [c[0] for c in cur.description or []] + first = False + # strict=False: requires 3.10 + yield record_class(**dict(zip(column_names, row))) def select_one(self, conn, query_name, sql, parameters, record_class=None): cur = self._cursor(conn) - try: - cur.execute(sql, parameters) - result = cur.fetchone() - if result is not None and record_class is not None: # pragma: no cover - column_names = [c[0] for c in cur.description or []] - # strict=False: requires 3.10 - result = record_class(**dict(zip(column_names, result))) - elif result is not None and self._convert_row_to_dict: # pragma: no cover - column_names = [c[0] for c in cur.description or []] - result = dict(zip(column_names, result)) - finally: - cur.close() + cur.execute(sql, parameters) + result = cur.fetchone() + if result is not None and record_class is not None: # pragma: no cover + column_names = [c[0] for c in cur.description or []] + # strict=False: requires 3.10 + result = record_class(**dict(zip(column_names, result))) + elif result is not None and self._convert_row_to_dict: # pragma: no cover + column_names = [c[0] for c in cur.description or []] + result = dict(zip(column_names, result)) return result