From ad81e641b395b2b80c5e61c119c95759eceaf838 Mon Sep 17 00:00:00 2001 From: Michele Comitini Date: Fri, 25 Oct 2013 23:09:46 +0200 Subject: [PATCH 1/2] optionally use OrderedDict in place of dict in executesql --- gluon/dal.py | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/gluon/dal.py b/gluon/dal.py index 01445358..6bfb2c94 100644 --- a/gluon/dal.py +++ b/gluon/dal.py @@ -172,6 +172,7 @@ import uuid import glob import traceback import platform +import collections PYTHON_VERSION = sys.version_info[0] if PYTHON_VERSION == 2: @@ -8132,23 +8133,26 @@ def index(): if not db_group: del THREAD_LOCAL.db_instances[self._db_uid] - def executesql(self, query, placeholders=None, as_dict=False, - fields=None, colnames=None): - """ - placeholders is optional and will always be None. + def executesql(self, query, placeholders=None, as_dict=False, + fields=None, colnames=None, as_ordered_dict=False): + """placeholders is optional and will always be None. If using raw SQL with placeholders, placeholders may be a sequence of values to be substituted in or, (if supported by the DB driver), a dictionary with keys matching named placeholders in your SQL. Added 2009-12-05 "as_dict" optional argument. Will always be - None when using DAL. If using raw SQL can be set to True - and the results cursor returned by the DB driver will be - converted to a sequence of dictionaries keyed with the db - field names. Tested with SQLite but should work with any database - since the cursor.description used to get field names is part of the - Python dbi 2.0 specs. Results returned with as_dict=True are - the same as those returned when applying .to_list() to a DAL query. + None when using DAL. If using raw SQL can be set to True and + the results cursor returned by the DB driver will be converted + to a sequence of dictionaries keyed with the db field + names. Tested with SQLite but should work with any database + since the cursor.description used to get field names is part + of the Python dbi 2.0 specs. Results returned with + as_dict=True are the same as those returned when applying + .to_list() to a DAL query. If "as_ordered_dict"=True the + behaviour is the same as when "as_dict"=True with the keys + (field names) guaranteed to be in the same order as returned + by the select name executed on the database. [{field1: value1, field2: value2}, {field1: value1b, field2: value2b}] @@ -8180,13 +8184,14 @@ def index(): be dummy tables and do not have to represent any real tables in the database. Also, note that the "fields" and "colnames" must be in the same order as the fields in the results cursor returned from the DB. + """ adapter = self._adapter if placeholders: adapter.execute(query, placeholders) else: adapter.execute(query) - if as_dict: + if as_dict or as_ordered_dict: if not hasattr(adapter.cursor,'description'): raise RuntimeError("database does not support executesql(...,as_dict=True)") # Non-DAL legacy db query, converts cursor results to dict. @@ -8199,7 +8204,11 @@ def index(): data = adapter._fetchall() # convert the list for each row into a dictionary so it's # easier to work with. row['field_name'] rather than row[0] - return [dict(zip(fields,row)) for row in data] + if as_ordered_dict: + _dict = collections.OrderedDict + else: + _dict = dict + return [_dict(zip(fields,row)) for row in data] try: data = adapter._fetchall() except: From 50543724d4415f5b586a95233779b19c48ac9665 Mon Sep 17 00:00:00 2001 From: Michele Comitini Date: Fri, 25 Oct 2013 23:12:43 +0200 Subject: [PATCH 2/2] fix a docstring --- gluon/dal.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gluon/dal.py b/gluon/dal.py index 6bfb2c94..a23af012 100644 --- a/gluon/dal.py +++ b/gluon/dal.py @@ -8135,7 +8135,8 @@ def index(): def executesql(self, query, placeholders=None, as_dict=False, fields=None, colnames=None, as_ordered_dict=False): - """placeholders is optional and will always be None. + """ + placeholders is optional and will always be None. If using raw SQL with placeholders, placeholders may be a sequence of values to be substituted in or, (if supported by the DB driver), a dictionary with keys