From 2565a0395fd56fce550967507c1e9d4def25b926 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louis=20V=C3=A9zina?= <5130500+morpheus65535@users.noreply.github.com> Date: Thu, 31 Oct 2019 06:41:34 -0400 Subject: [PATCH] Changed row factory. --- libs/sqlite3worker.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/libs/sqlite3worker.py b/libs/sqlite3worker.py index fc589cc0b..61be9d423 100644 --- a/libs/sqlite3worker.py +++ b/libs/sqlite3worker.py @@ -66,11 +66,12 @@ class Sqlite3Worker(threading.Thread): file_name, check_same_thread=False, detect_types=sqlite3.PARSE_DECLTYPES) if as_dict: - self.sqlite3_conn.row_factory = dict_factory + self.sqlite3_conn.row_factory = sqlite3.Row self.sqlite3_cursor = self.sqlite3_conn.cursor() self.sql_queue = Queue.Queue(maxsize=max_queue_size) self.results = {} self.max_queue_size = max_queue_size + self.as_dict = as_dict self.exit_set = False # Token that is put into queue when close() is called. self.exit_token = str(uuid.uuid4()) @@ -126,7 +127,10 @@ class Sqlite3Worker(threading.Thread): if only_one: self.results[token] = self.sqlite3_cursor.fetchone() else: - self.results[token] = self.sqlite3_cursor.fetchall() + if self.as_dict: + self.results[token] = [dict(row) for row in self.sqlite3_cursor.fetchall()] + else: + self.results[token] = self.sqlite3_cursor.fetchall() except sqlite3.Error as err: # Put the error into the output queue since a response # is required. @@ -183,9 +187,11 @@ class Sqlite3Worker(threading.Thread): Args: query: The sql string using ? for placeholders of dynamic values. values: A tuple of values to be replaced into the ? of the query. + only_one: A boolean to use fetchone() instead of fetchall(). Returns: If it's a select query it will return the results of the query. + Normally it return a list of lists/dicts but if only_one it return a single list/dict. """ if self.exit_set: LOGGER.debug("Exit set, not running: %s, %s", query, values) @@ -201,10 +207,3 @@ class Sqlite3Worker(threading.Thread): return self.query_results(token) else: self.sql_queue.put((token, query, values, only_one), timeout=5) - - -def dict_factory(cursor, row): - d = {} - for idx, col in enumerate(cursor.description): - d[col[0]] = row[idx] - return d