Skip to content

Commit 15557e6

Browse files
authored
Merge pull request #14 from AnswerDotAI/cursor_row2dict
Use rowtrace func to convert row to dict
2 parents 3d81d51 + 9bed0fb commit 15557e6

File tree

2 files changed

+18
-27
lines changed

2 files changed

+18
-27
lines changed

apswutils/db.py

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This file is from sqlite-utils and copyright and license is the same as that project
22
__all__ = ['Database', 'Queryable', 'Table', 'View']
33

4-
from .utils import chunks, hash_record, suggest_column_types, types_for_column_types, column_affinity, find_spatialite
4+
from .utils import chunks, hash_record, suggest_column_types, types_for_column_types, column_affinity, find_spatialite, cursor_row2dict
55
from collections import namedtuple
66
from collections.abc import Mapping
77
from typing import cast, Any, Callable, Dict, Generator, Iterable, Union, Optional, List, Tuple, Iterator
@@ -414,10 +414,8 @@ def query(
414414
parameters, or a dictionary for ``where id = :id``
415415
"""
416416
cursor = self.execute(sql, tuple(params or tuple()))
417-
try: columns = [c[0] for c in cursor.description]
418-
except apsw.ExecutionCompleteError: return []
419-
for row in cursor:
420-
yield dict(zip(columns, row))
417+
cursor.row_trace = cursor_row2dict
418+
yield from cursor
421419

422420
def execute(
423421
self, sql: str, parameters: Optional[Union[Iterable, dict]] = None
@@ -1295,11 +1293,8 @@ def rows_where(
12951293
if offset is not None:
12961294
sql += f" offset {offset}"
12971295
cursor = self.db.execute(sql, where_args or [])
1298-
# If no records found, return empty list
1299-
try: columns = [c[0] for c in cursor.description]
1300-
except apsw.ExecutionCompleteError: return []
1301-
for row in cursor:
1302-
yield dict(zip(columns, row))
1296+
cursor.row_trace = cursor_row2dict
1297+
yield from cursor
13031298

13041299
def pks_and_rows_where(
13051300
self,
@@ -2670,10 +2665,8 @@ def search(
26702665
),
26712666
args,
26722667
)
2673-
try: columns = [c[0] for c in cursor.description]
2674-
except apsw.ExecutionCompleteError: return []
2675-
for row in cursor:
2676-
yield dict(zip(columns, row))
2668+
cursor.row_trace = cursor_row2dict
2669+
yield from cursor
26772670

26782671
def value_or_default(self, key, value):
26792672
return self._defaults[key] if value is DEFAULT else value
@@ -2764,11 +2757,8 @@ def update(
27642757
self.result = []
27652758
try:
27662759
cursor = self.db.execute(sql, args)
2767-
try: columns = [c[0] for c in cursor.description]
2768-
except apsw.ExecutionCompleteError: return self
2769-
2770-
for row in cursor:
2771-
self.result.append(dict(zip(columns, row)))
2760+
cursor.row_trace = cursor_row2dict
2761+
self.result = list(cursor)
27722762
except apsw.SQLError as e:
27732763
if alter and (" column" in e.args[0]):
27742764
# Attempt to add any missing columns, then try again
@@ -2930,19 +2920,15 @@ def insert_chunk(
29302920
for query, params in queries_and_params:
29312921
try:
29322922
cursor = self.db.execute(query, tuple(params))
2933-
try: columns = [c[0] for c in cursor.description]
2934-
except apsw.ExecutionCompleteError: continue
2935-
for row in cursor:
2936-
records.append(dict(zip(columns, row)))
2923+
cursor.row_trace = cursor_row2dict
2924+
records += list(cursor)
29372925
except apsw.SQLError as e:
29382926
if alter and (" column" in e.args[0]):
29392927
# Attempt to add any missing columns, then try again
29402928
self.add_missing_columns(chunk)
29412929
cursor = self.db.execute(query, params)
2942-
try: columns = [c[0] for c in cursor.description]
2943-
except apsw.ExecutionCompleteError: continue
2944-
for row in cursor:
2945-
records.append(dict(zip(columns, row)))
2930+
cursor.row_trace = cursor_row2dict
2931+
records += list(cursor)
29462932
elif e.args[0] == "too many SQL variables":
29472933
first_half = chunk[: len(chunk) // 2]
29482934
second_half = chunk[len(chunk) // 2 :]

apswutils/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,3 +464,8 @@ def flatten(row: dict) -> dict:
464464
IGNORE = object()
465465
SET_NULL = object()
466466

467+
468+
def cursor_row2dict(cursor, row):
469+
"""Converts a cursor row into a dict with columns as keys"""
470+
columns = [d[0] for d in cursor.get_description()]
471+
return dict(zip(columns, row))

0 commit comments

Comments
 (0)