Browse Source

sql result as utf-8

version-14
Nabin Hait 13 years ago
parent
commit
e741aa829a
1 changed files with 13 additions and 7 deletions
  1. +13
    -7
      py/webnotes/db.py

+ 13
- 7
py/webnotes/db.py View File

@@ -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') 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' 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 Internal - get results as dictionary
""" """
@@ -107,7 +107,10 @@ class Database:
for r in result: for r in result:
dict = {} dict = {}
for i in range(len(r)): 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) ret.append(dict)
return ret return ret
@@ -146,11 +149,11 @@ class Database:


# scrub output if required # scrub output if required
if as_dict: if as_dict:
return self.fetch_as_dict(formatted)
return self.fetch_as_dict(formatted, as_utf8)
elif as_list: 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: elif as_utf8:
return self.convert_to_utf8(self._cursor.fetchall(), formatted)
return self.convert_to_lists(self._cursor.fetchall(), formatted, as_utf8)
else: else:
return self._cursor.fetchall() 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) 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: for r in res:
nr = [] nr = []
for c in r: 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) nres.append(nr)
return nres return nres


Loading…
Cancel
Save