- Timestamp:
- 01/30/08 23:16:51 (17 years ago)
- Location:
- pykota/trunk/pykota
- Files:
-
- 2 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/pykota/storages/mysqlstorage.py
r3308 r3310 58 58 self.cursor.execute("SET NAMES 'utf8';") 59 59 self.cursor.execute("SET TRANSACTION ISOLATION LEVEL READ COMMITTED;") # Same as PostgreSQL and Oracle's default 60 self.closed = 060 self.closed = False 61 61 self.tool.logdebug("Database opened (host=%s, port=%s, dbname=%s, user=%s)" % (host, port, dbname, user)) 62 try : 63 # Here we try to select a string (an é) which is 64 # already encoded in UTF-8. If python-mysqldb suffers from 65 # the double encoding problem, we will catch the exception 66 # and activate a workaround. 67 self.cursor.execute("SELECT '%s';" % (chr(0xc3) + chr(0xa9))) # é in UTF-8 68 self.cursor.fetchall() 69 except UnicodeDecodeError : 70 self.needsworkaround = True 71 self.tool.logdebug("Database needs encoding workaround.") 72 else : 73 self.needsworkaround = False 74 self.tool.logdebug("Database doesn't need encoding workaround.") 62 75 63 76 def close(self) : … … 66 79 self.cursor.close() 67 80 self.database.close() 68 self.closed = 181 self.closed = True 69 82 self.tool.logdebug("Database closed.") 70 83 … … 89 102 if not query.endswith(';') : 90 103 query += ';' 104 self.querydebug("QUERY : %s" % query) 105 if self.needsworkaround : 106 query = query.decode("UTF-8") 91 107 try : 92 self.querydebug("QUERY : %s" % query)93 108 self.cursor.execute(query) 94 109 except self.database.Error, msg : … … 96 111 else : 97 112 # This returns a list of lists. Integers are returned as longs. 98 result = self.cursor.fetchall() 99 return result 113 return self.cursor.fetchall() 100 114 101 115 def doSearch(self, query) : … … 110 124 rowdict = {} 111 125 for field in fields.keys() : 112 value = row[field] 113 try : 114 value = value.encode("UTF-8") 115 except: 116 pass 117 rowdict[fields[field]] = value 126 rowdict[fields[field]] = row[field] 118 127 rows.append(rowdict) 119 128 # returns a list of dicts … … 125 134 if not query.endswith(';') : 126 135 query += ';' 136 self.querydebug("QUERY : %s" % query) 137 if self.needsworkaround : 138 query = query.decode("UTF-8") 127 139 try : 128 self.querydebug("QUERY : %s" % query)129 140 self.cursor.execute(query) 130 141 except self.database.Error, msg : … … 141 152 return field 142 153 elif field is not None : 143 newfield = self.database.string_literal(field) 144 try : 145 return newfield.encode("UTF-8") 146 except : 147 return newfield 154 return self.database.string_literal(field) 148 155 else : 149 self.tool.logdebug("WARNING: field has no type, returning NULL")150 156 return "NULL" 151 157 … … 153 159 """Prepares a raw result by including the headers.""" 154 160 if result : 155 entries = [tuple([f[0] for f in self.cursor.description])] 156 for entry in result : 157 row = [] 158 for value in entry : 159 try : 160 value = value.encode("UTF-8") 161 except : 162 pass 163 row.append(value) 164 entries.append(tuple(row)) 165 return entries 161 return [tuple([f[0] for f in self.cursor.description])] \ 162 + list(result) -
pykota/trunk/pykota/utils.py
r3298 r3310 26 26 import locale 27 27 import gettext 28 from types import UnicodeType 28 29 29 30 def initlocale(lang="", cset=None) : … … 94 95 """ 95 96 if text is not None : 96 return text.decode("UTF-8", "replace") 97 if not isinstance(text, UnicodeType) : 98 return text.decode("UTF-8", "replace") 99 else : 100 # MySQL already returns unicode objects 101 return text 97 102 else : 98 103 return None