| | 66 | class 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 | |