| | 31 | |
| | 32 | class MySQLUnicodeCursor(MySQLdb.cursors.Cursor): |
| | 33 | def _convert_row(self, row): |
| | 34 | return tuple([(isinstance(v, str) and [v.decode('utf-8')] or [v])[0] |
| | 35 | for v in row]) |
| | 36 | def fetchone(self): |
| | 37 | row = super(MySQLUnicodeCursor, self).fetchone() |
| | 38 | return row and self._convert_row(row) or None |
| | 39 | def fetchmany(self, num): |
| | 40 | rows = super(MySQLUnicodeCursor, self).fetchmany(num) |
| | 41 | return rows != None and [self._convert_row(row) |
| | 42 | for row in rows] or [] |
| | 43 | def fetchall(self): |
| | 44 | rows = super(MySQLUnicodeCursor, self).fetchall() |
| | 45 | return rows != None and [self._convert_row(row) |
| | 46 | for row in rows] or [] |
| 134 | | def _mysqldb_gt_or_eq(self, v): |
| 135 | | """This function checks whether the version of python-mysqldb |
| 136 | | is greater than or equal to the version that's passed to it. |
| 137 | | Note that the tuple only checks the major, minor, and sub versions; |
| 138 | | the sub-sub version is weird, so we only check for 'final' versions. |
| 139 | | """ |
| 140 | | ver = MySQLdb.version_info |
| 141 | | if ver[0] < v[0] or ver[1] < v[1] or ver[2] < v[2]: |
| 142 | | return False |
| 143 | | if ver[3] != 'final': |
| 144 | | return False |
| 145 | | return True |
| 146 | | |
| 147 | | def _set_character_set(self, cnx, charset): |
| 148 | | vers = tuple([ int(n) for n in cnx.get_server_info().split('.')[:2] ]) |
| 149 | | if vers < (4, 1): |
| 150 | | raise TracError(_('MySQL servers older than 4.1 are not ' |
| 151 | | 'supported!')) |
| 152 | | cnx.query('SET NAMES %s' % charset) |
| 153 | | cnx.store_result() |
| 154 | | cnx.charset = charset |
| 155 | | |
| 164 | | |
| 165 | | # python-mysqldb 1.2.1 added a 'charset' arg that is required for |
| 166 | | # unicode stuff. We hack around that here for older versions; at |
| 167 | | # some point, this hack should be removed, and a strict requirement |
| 168 | | # on 1.2.1 made. -dilinger |
| 169 | | if (self._mysqldb_gt_or_eq((1, 2, 1))): |
| 170 | | cnx = MySQLdb.connect(db=path, user=user, passwd=password, |
| 171 | | host=host, port=port, charset='utf8') |
| 172 | | else: |
| 173 | | cnx = MySQLdb.connect(db=path, user=user, passwd=password, |
| 174 | | host=host, port=port, use_unicode=True) |
| 175 | | self._set_character_set(cnx, 'utf8') |
| | 159 | cnx = MySQLdb.connect(db=path, user=user, passwd=password, |
| | 160 | host=host, port=port, charset='utf8') |