summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Kaplan-Moss <jacob@jacobian.org>2005-07-21 15:48:20 +0000
committerJacob Kaplan-Moss <jacob@jacobian.org>2005-07-21 15:48:20 +0000
commit78cff1837aed5fccb35e378bd2dd27a3d09a7a00 (patch)
tree167269864e254c80e8eb53ff1ec8c060ca67bc2b
parent52d761cc7a2296e75f4e97e1c0e501f2623fe08c (diff)
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
-rw-r--r--django/core/db/backends/mysql.py22
-rw-r--r--django/core/db/dicthelpers.py24
2 files changed, 25 insertions, 21 deletions
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()]