|
1 | 1 | # This file is from sqlite-utils and copyright and license is the same as that project
|
2 | 2 | __all__ = ['Database', 'Queryable', 'Table', 'View']
|
3 | 3 |
|
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 |
5 | 5 | from collections import namedtuple
|
6 | 6 | from collections.abc import Mapping
|
7 | 7 | from typing import cast, Any, Callable, Dict, Generator, Iterable, Union, Optional, List, Tuple, Iterator
|
@@ -414,10 +414,8 @@ def query(
|
414 | 414 | parameters, or a dictionary for ``where id = :id``
|
415 | 415 | """
|
416 | 416 | 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 |
421 | 419 |
|
422 | 420 | def execute(
|
423 | 421 | self, sql: str, parameters: Optional[Union[Iterable, dict]] = None
|
@@ -1295,11 +1293,8 @@ def rows_where(
|
1295 | 1293 | if offset is not None:
|
1296 | 1294 | sql += f" offset {offset}"
|
1297 | 1295 | 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 |
1303 | 1298 |
|
1304 | 1299 | def pks_and_rows_where(
|
1305 | 1300 | self,
|
@@ -2670,10 +2665,8 @@ def search(
|
2670 | 2665 | ),
|
2671 | 2666 | args,
|
2672 | 2667 | )
|
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 |
2677 | 2670 |
|
2678 | 2671 | def value_or_default(self, key, value):
|
2679 | 2672 | return self._defaults[key] if value is DEFAULT else value
|
@@ -2764,11 +2757,8 @@ def update(
|
2764 | 2757 | self.result = []
|
2765 | 2758 | try:
|
2766 | 2759 | 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) |
2772 | 2762 | except apsw.SQLError as e:
|
2773 | 2763 | if alter and (" column" in e.args[0]):
|
2774 | 2764 | # Attempt to add any missing columns, then try again
|
@@ -2930,19 +2920,15 @@ def insert_chunk(
|
2930 | 2920 | for query, params in queries_and_params:
|
2931 | 2921 | try:
|
2932 | 2922 | 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) |
2937 | 2925 | except apsw.SQLError as e:
|
2938 | 2926 | if alter and (" column" in e.args[0]):
|
2939 | 2927 | # Attempt to add any missing columns, then try again
|
2940 | 2928 | self.add_missing_columns(chunk)
|
2941 | 2929 | 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) |
2946 | 2932 | elif e.args[0] == "too many SQL variables":
|
2947 | 2933 | first_half = chunk[: len(chunk) // 2]
|
2948 | 2934 | second_half = chunk[len(chunk) // 2 :]
|
|
0 commit comments