From e741aa829a2276d0941329ba1d0804f190e3f7d4 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Mon, 11 Jun 2012 17:44:13 +0530 Subject: [PATCH] sql result as utf-8 --- py/webnotes/db.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/py/webnotes/db.py b/py/webnotes/db.py index fad5e0f90d..be468e7a70 100644 --- a/py/webnotes/db.py +++ b/py/webnotes/db.py @@ -98,7 +98,7 @@ class Database: webnotes.msgprint('A very long query was encountered. If you are trying to import data, please do so using smaller files') raise Exception, 'Bad Query!!! Too many writes' - def fetch_as_dict(self, formatted=0): + def fetch_as_dict(self, formatted=0, as_utf8=0): """ Internal - get results as dictionary """ @@ -107,7 +107,10 @@ class Database: for r in result: dict = {} for i in range(len(r)): - dict[self._cursor.description[i][0]] = self.convert_to_simple_type(r[i], formatted) + val = self.convert_to_simple_type(r[i], formatted) + if as_utf8 and type(val) is unicode: + val = val.encode('utf-8') + dict[self._cursor.description[i][0]] = val ret.append(dict) return ret @@ -146,11 +149,11 @@ class Database: # scrub output if required if as_dict: - return self.fetch_as_dict(formatted) + return self.fetch_as_dict(formatted, as_utf8) elif as_list: - return self.convert_to_lists(self._cursor.fetchall(), formatted) + return self.convert_to_lists(self._cursor.fetchall(), formatted, as_utf8) elif as_utf8: - return self.convert_to_utf8(self._cursor.fetchall(), formatted) + return self.convert_to_lists(self._cursor.fetchall(), formatted, as_utf8) else: return self._cursor.fetchall() @@ -199,7 +202,7 @@ class Database: # ====================================================================================== - def convert_to_lists(self, res, formatted=0): + def convert_to_lists(self, res, formatted=0, as_utf8=0): """ Convert the given result set to a list of lists (with cleaned up dates and decimals) """ @@ -207,7 +210,10 @@ class Database: for r in res: nr = [] for c in r: - nr.append(self.convert_to_simple_type(c, formatted)) + val = self.convert_to_simple_type(c, formatted) + if as_utf8 and type(val) is unicode: + val = val.encode('utf-8') + nr.append(val) nres.append(nr) return nres