Ticket #4741: mysql_old_base4.diff

File mysql_old_base4.diff, 2.2 KB (added by jedie, 16 years ago)

Updated diff against revision 7947

  • home/jens/workspace/PyLucid_trunk/pylucid/django/db/backends/mysql_old/base.py

     
    6363        else:
    6464            return getattr(self.cursor, attr)
    6565
     66class MysqlUnicodeWrapper:
     67    """
     68    A Wrapper who decode all byte strings to unicode.
     69    """
     70    def __init__(self, cursor):
     71        self.cursor = cursor
     72
     73    def _decode_results(self, result_raw):
     74        """
     75        decode all byte string to unicode with the server encoding.
     76        """
     77        if not result_raw:
     78            return result_raw
     79        result = []
     80        for item in result_raw:
     81            if isinstance(item, str):
     82                item = item.decode("utf-8")
     83            result.append(item)
     84
     85        return tuple(result)
     86
     87    def _decode_lines(self, result_raw):
     88        """
     89        decode every line in a resultset.
     90        """
     91        result = []
     92        for line in result_raw:
     93            result.append(self._decode_results(line))
     94        return tuple(result)
     95
     96    def fetchone(self):
     97        result_raw = self.cursor.fetchone()
     98        return self._decode_results(result_raw)
     99
     100    def fetchall(self):
     101        result_raw = self.cursor.fetchall()
     102        return self._decode_lines(result_raw)
     103
     104    def fetchmany(self, *args):
     105        result_raw = self.cursor.fetchmany(*args)
     106        return self._decode_lines(result_raw)
     107        return result_raw
     108
     109    def __getattr__(self, attr):
     110        if attr in self.__dict__:
     111            return self.__dict__[attr]
     112        else:
     113            return getattr(self.cursor, attr)
     114
    66115class DatabaseFeatures(BaseDatabaseFeatures):
    67116    inline_fk_references = False
    68117    empty_fetchmany_value = ()
     
    193242                    self.connection.set_character_set('utf8')
    194243        else:
    195244            cursor = self.connection.cursor()
     245
     246        cursor = MysqlUnicodeWrapper(cursor)
     247
    196248        return cursor
    197249
    198250    def make_debug_cursor(self, cursor):
Back to Top