Ticket #4741: mysql_old_base.diff

File mysql_old_base.diff, 2.6 KB (added by django@…, 17 years ago)

decode all byte string to unicode with the server encoding.

  • ./django/db/backends/mysql_old/base.py

     
    3737# http://dev.mysql.com/doc/refman/5.0/en/news.html .
    3838server_version_re = re.compile(r'(\d{1,2})\.(\d{1,2})\.(\d{1,2})')
    3939
    40 # This is an extra debug layer over MySQL queries, to display warnings.
    41 # It's only used when DEBUG=True.
    42 class MysqlDebugWrapper:
     40class MysqlWrapper:
    4341    def __init__(self, cursor):
    4442        self.cursor = cursor
    4543
     44    def __getattr__(self, attr):
     45        if attr in self.__dict__:
     46            return self.__dict__[attr]
     47        else:
     48            return getattr(self.cursor, attr)
     49
     50# This is an extra debug layer over MySQL queries, to display warnings.
     51# It's only used when DEBUG=True.
     52class MysqlDebugWrapper(MysqlWrapper):
    4653    def execute(self, sql, params=()):
    4754        try:
    4855            return self.cursor.execute(sql, params)
     
    5764            self.cursor.execute("SHOW WARNINGS")
    5865            raise Database.Warning("%s: %s" % (w, self.cursor.fetchall()))
    5966
    60     def __getattr__(self, attr):
    61         if attr in self.__dict__:
    62             return self.__dict__[attr]
    63         else:
    64             return getattr(self.cursor, attr)
     67class MysqlUnicodeWrapper(MysqlWrapper):
     68
     69    def _decode_results(self, result_raw):
     70        """
     71        decode all byte string to unicode with the server encoding.
     72        """
     73        result = []
     74        for item in result_raw:
     75            if isinstance(item, str):
     76                item = item.decode(self.cursor.server_encoding)
     77            result.append(item)
     78        return tuple(result)
     79
     80    def fetchone(self):
     81        result_raw = self.cursor.fetchone()
     82        return self._decode_results(result_raw)
     83
     84    def fetchall(self):
     85        result_raw = self.cursor.fetchall()
     86        result = []
     87        for line in result_raw:
     88            result.append(self._decode_results(line))
     89        return tuple(result)
     90
    6591
    6692class DatabaseFeatures(BaseDatabaseFeatures):
    6793    autoindexes_primary_keys = False
     
    193219                    self.connection.set_character_set('utf8')
    194220        else:
    195221            cursor = self.connection.cursor()
    196         return cursor
     222
     223        cursor.execute("SHOW VARIABLES LIKE %s;", ("character_set_server",))
     224        cursor.server_encoding = cursor.fetchone()[1]
     225
     226        return MysqlUnicodeWrapper(cursor)
    197227
    198228    def make_debug_cursor(self, cursor):
    199229        return BaseDatabaseWrapper.make_debug_cursor(self, MysqlDebugWrapper(cursor))
Back to Top