From 78cff1837aed5fccb35e378bd2dd27a3d09a7a00 Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Thu, 21 Jul 2005 15:48:20 +0000 Subject: [PATCH] Refactor dictfetch* methods from mysql backend out into a seperate module; this is so that future db backends that need these functions can share them. git-svn-id: http://code.djangoproject.com/svn/django/trunk@276 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/db/backends/mysql.py | 22 +--------------------- django/core/db/dicthelpers.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 21 deletions(-) create mode 100644 django/core/db/dicthelpers.py 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()]