From 9d43dfae57336f1ad6316713ca2300defe7d5a45 Mon Sep 17 00:00:00 2001 From: Darren Martz Date: Tue, 10 Dec 2019 13:32:49 -0800 Subject: [PATCH 1/4] Allows for results to be returned as_dict straight from the cursor and with column names for keys --- python/turbodbc/connection.py | 13 +++++++++++-- python/turbodbc/cursor.py | 22 +++++++++++++++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/python/turbodbc/connection.py b/python/turbodbc/connection.py index 59e1879ea..5f8e46894 100644 --- a/python/turbodbc/connection.py +++ b/python/turbodbc/connection.py @@ -14,16 +14,25 @@ def _assert_valid(self): def __init__(self, impl): self.impl = impl self.cursors = WeakSet([]) + self._as_dict = False + + @property + def as_dict(self): + return self._as_dict + + @as_dict.setter + def as_dict(self, value): + self._as_dict = True if value is True else False @translate_exceptions - def cursor(self): + def cursor(self, as_dict=None): """ Create a new ``Cursor`` instance associated with this ``Connection`` :return: A new ``Cursor`` instance """ self._assert_valid() - c = Cursor(self.impl.cursor()) + c = Cursor(self.impl.cursor(), as_dict=self.as_dict if as_dict is None else as_dict) self.cursors.add(c) return c diff --git a/python/turbodbc/cursor.py b/python/turbodbc/cursor.py index 1671188c0..132adf4d9 100644 --- a/python/turbodbc/cursor.py +++ b/python/turbodbc/cursor.py @@ -66,11 +66,13 @@ class Cursor(object): This class allows you to send SQL commands and queries to a database and retrieve associated result sets. """ - def __init__(self, impl): + def __init__(self, impl, as_dict=False): self.impl = impl self.result_set = None self.rowcount = -1 self.arraysize = 1 + self._column_names = None + self._as_dict = True if as_dict is True else False def __iter__(self): return self @@ -109,6 +111,7 @@ def description(self): return None def _execute(self): + self._column_names = None self.impl.execute() self.rowcount = self.impl.get_row_count() cpp_result_set = self.impl.get_result_set() @@ -228,6 +231,21 @@ def _num_chunks(c): return self._execute() + + def _to_dict(self, result): + if not self.result_set or len(result) == 0: + return dict() + + if self._column_names is None: + info = self.result_set.get_column_info() + self._column_names [c.name for c in info] + + data = dict() + for i in range(len(self._column_names)): + data[self._column_names[i]] = result[i] + return data + + @translate_exceptions def fetchone(self): """ @@ -238,6 +256,8 @@ def fetchone(self): """ self._assert_valid_result_set() result = self.result_set.fetch_row() + if self._as_dict: + return self._to_dict(result) if len(result) == 0: return None else: From fd22426077f6f5cb19f2c86eb3e1d3337054a669 Mon Sep 17 00:00:00 2001 From: Darren Martz Date: Wed, 11 Dec 2019 12:44:14 -0800 Subject: [PATCH 2/4] missed checkin --- python/turbodbc/connection.py | 3 ++- python/turbodbc/cursor.py | 12 ++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/python/turbodbc/connection.py b/python/turbodbc/connection.py index 5f8e46894..38d9d38f0 100644 --- a/python/turbodbc/connection.py +++ b/python/turbodbc/connection.py @@ -32,7 +32,8 @@ def cursor(self, as_dict=None): :return: A new ``Cursor`` instance """ self._assert_valid() - c = Cursor(self.impl.cursor(), as_dict=self.as_dict if as_dict is None else as_dict) + c = Cursor(self.impl.cursor()) + c.as_dict = self._as_dict self.cursors.add(c) return c diff --git a/python/turbodbc/cursor.py b/python/turbodbc/cursor.py index 132adf4d9..0147b3359 100644 --- a/python/turbodbc/cursor.py +++ b/python/turbodbc/cursor.py @@ -66,13 +66,13 @@ class Cursor(object): This class allows you to send SQL commands and queries to a database and retrieve associated result sets. """ - def __init__(self, impl, as_dict=False): + def __init__(self, impl): self.impl = impl self.result_set = None self.rowcount = -1 self.arraysize = 1 self._column_names = None - self._as_dict = True if as_dict is True else False + self._as_dict = False def __iter__(self): return self @@ -94,6 +94,14 @@ def _assert_valid_result_set(self): if self.result_set is None: raise InterfaceError("No active result set") + @property + def as_dict(self): + return self._as_dict + + @as_dict.setter + def as_dict(self, value): + self._as_dict = True if value is True else False + @property def description(self): """ From c39657a4a820125742d9ebfbc4d076822d75f93e Mon Sep 17 00:00:00 2001 From: Darren Martz Date: Wed, 11 Dec 2019 12:54:30 -0800 Subject: [PATCH 3/4] cannot change signature of connection.cursor --- python/turbodbc/connection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/turbodbc/connection.py b/python/turbodbc/connection.py index 38d9d38f0..19bb88e9d 100644 --- a/python/turbodbc/connection.py +++ b/python/turbodbc/connection.py @@ -25,7 +25,7 @@ def as_dict(self, value): self._as_dict = True if value is True else False @translate_exceptions - def cursor(self, as_dict=None): + def cursor(self): """ Create a new ``Cursor`` instance associated with this ``Connection`` From 304cf4a2301ecf469a9d796f2f038fd7316c77c2 Mon Sep 17 00:00:00 2001 From: Darren Martz Date: Thu, 12 Dec 2019 09:35:19 -0800 Subject: [PATCH 4/4] syntax error fixed --- python/turbodbc/cursor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/turbodbc/cursor.py b/python/turbodbc/cursor.py index 0147b3359..ab122d054 100644 --- a/python/turbodbc/cursor.py +++ b/python/turbodbc/cursor.py @@ -246,7 +246,7 @@ def _to_dict(self, result): if self._column_names is None: info = self.result_set.get_column_info() - self._column_names [c.name for c in info] + self._column_names = [c.name for c in info] data = dict() for i in range(len(self._column_names)):