From 48570f4b8aeea2942dd4c0a214288cc7d61e4ea2 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Mon, 11 Jun 2012 17:22:48 +0530 Subject: [PATCH] sql result as utf8 --- py/webnotes/db.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/py/webnotes/db.py b/py/webnotes/db.py index 3bbeba93e5..fad5e0f90d 100644 --- a/py/webnotes/db.py +++ b/py/webnotes/db.py @@ -119,7 +119,7 @@ class Database: # ====================================================================================== - def sql(self, query, values=(), as_dict = 0, as_list = 0, formatted = 0, ignore_no_table = 1, debug=0, ignore_ddl=0): + def sql(self, query, values=(), as_dict = 0, as_list = 0, formatted = 0, ignore_no_table = 1, debug=0, ignore_ddl=0, as_utf8=0): """ * Execute a `query`, with given `values` * returns as a dictionary if as_dict = 1 @@ -149,6 +149,8 @@ class Database: return self.fetch_as_dict(formatted) elif as_list: return self.convert_to_lists(self._cursor.fetchall(), formatted) + elif as_utf8: + return self.convert_to_utf8(self._cursor.fetchall(), formatted) else: return self._cursor.fetchall() @@ -208,6 +210,22 @@ class Database: nr.append(self.convert_to_simple_type(c, formatted)) nres.append(nr) return nres + + # ====================================================================================== + + def convert_to_utf8(self, res, formatted=0): + """ + Convert the given result set to a list of lists and as utf8 (with cleaned up dates and decimals) + """ + nres = [] + for r in res: + nr = [] + for c in r: + if type(c) is unicode: + c = c.encode('utf-8') + nr.append(self.convert_to_simple_type(c, formatted)) + nres.append(nr) + return nres # ======================================================================================