Ticket #4741: mysql_old_base3.diff

File mysql_old_base3.diff, 3.1 KB (added by django@…, 17 years ago)

Hardcoded encode to "utf-8"

  • ./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    def __init__(self, cursor):
     69        self.cursor = cursor
     70
     71    def _decode_results(self, result_raw):
     72        """
     73        decode all byte string to unicode with the server encoding.
     74        """
     75        if not result_raw:
     76            return result_raw
     77        result = []
     78        for item in result_raw:
     79            if isinstance(item, str):
     80                item = item.decode("utf-8")
     81            result.append(item)
     82
     83        return tuple(result)
     84
     85    def _decode_lines(self, result_raw):
     86        """
     87        decode every line in a resultset.
     88        """
     89        result = []
     90        for line in result_raw:
     91            result.append(self._decode_results(line))
     92        return tuple(result)
     93
     94    def fetchone(self):
     95        result_raw = self.cursor.fetchone()
     96        return self._decode_results(result_raw)
     97
     98    def fetchall(self):
     99        result_raw = self.cursor.fetchall()
     100        return self._decode_lines(result_raw)
     101
     102    def fetchmany(self, *args):
     103        result_raw = self.cursor.fetchmany(*args)
     104        return self._decode_lines(result_raw)
     105        return result_raw
     106
    65107
    66108class DatabaseFeatures(BaseDatabaseFeatures):
    67109    autoindexes_primary_keys = False
     
    193235                    self.connection.set_character_set('utf8')
    194236        else:
    195237            cursor = self.connection.cursor()
    196         return cursor
     238
     239        return MysqlUnicodeWrapper(cursor)
    197240
    198241    def make_debug_cursor(self, cursor):
    199         return BaseDatabaseWrapper.make_debug_cursor(self, MysqlDebugWrapper(cursor))
     242        cursor = MysqlDebugWrapper(cursor)
     243        cursor = MysqlUnicodeWrapper(cursor)
     244        return BaseDatabaseWrapper.make_debug_cursor(self, cursor)
    200245
    201246    def _rollback(self):
    202247        try:
Back to Top