diff --git a/django/core/db/backends/mysql.py b/django/core/db/backends/mysql.py index 307a3e2713..66d31369b1 100644 --- a/django/core/db/backends/mysql.py +++ b/django/core/db/backends/mysql.py @@ -5,6 +5,7 @@ Requires MySQLdb: http://sourceforge.net/projects/mysql-python """ from django.core.db import base, typecasts +from django.core.db.dicthelpers import * import MySQLdb as Database from MySQLdb.converters import conversions from MySQLdb.constants import FIELD_TYPE @@ -46,27 +47,6 @@ class DatabaseWrapper: self.connection.close() self.connection = None -def _dict_helper(desc, row): - "Returns a dictionary for the given cursor.description and result row." - return dict([(desc[col[0]][0], col[1]) for col in enumerate(row)]) - -def dictfetchone(cursor): - "Returns a row from the cursor as a dict" - row = cursor.fetchone() - if not row: - return None - return _dict_helper(cursor.description, row) - -def dictfetchmany(cursor, number): - "Returns a certain number of rows from a cursor as a dict" - desc = cursor.description - return [_dict_helper(desc, row) for row in cursor.fetchmany(number)] - -def dictfetchall(cursor): - "Returns all rows from a cursor as a dict" - desc = cursor.description - return [_dict_helper(desc, row) for row in cursor.fetchall()] - def get_last_insert_id(cursor, table_name, pk_name): cursor.execute("SELECT LAST_INSERT_ID()") return cursor.fetchone()[0] diff --git a/django/core/db/dicthelpers.py b/django/core/db/dicthelpers.py new file mode 100644 index 0000000000..5aedc51aed --- /dev/null +++ b/django/core/db/dicthelpers.py @@ -0,0 +1,24 @@ +""" +Helper functions for dictfetch* for databases that don't natively support them. +""" + +def _dict_helper(desc, row): + "Returns a dictionary for the given cursor.description and result row." + return dict([(desc[col[0]][0], col[1]) for col in enumerate(row)]) + +def dictfetchone(cursor): + "Returns a row from the cursor as a dict" + row = cursor.fetchone() + if not row: + return None + return _dict_helper(cursor.description, row) + +def dictfetchmany(cursor, number): + "Returns a certain number of rows from a cursor as a dict" + desc = cursor.description + return [_dict_helper(desc, row) for row in cursor.fetchmany(number)] + +def dictfetchall(cursor): + "Returns all rows from a cursor as a dict" + desc = cursor.description + return [_dict_helper(desc, row) for row in cursor.fetchall()]