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
This commit is contained in:
parent
52d761cc7a
commit
78cff1837a
|
@ -5,6 +5,7 @@ Requires MySQLdb: http://sourceforge.net/projects/mysql-python
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from django.core.db import base, typecasts
|
from django.core.db import base, typecasts
|
||||||
|
from django.core.db.dicthelpers import *
|
||||||
import MySQLdb as Database
|
import MySQLdb as Database
|
||||||
from MySQLdb.converters import conversions
|
from MySQLdb.converters import conversions
|
||||||
from MySQLdb.constants import FIELD_TYPE
|
from MySQLdb.constants import FIELD_TYPE
|
||||||
|
@ -46,27 +47,6 @@ class DatabaseWrapper:
|
||||||
self.connection.close()
|
self.connection.close()
|
||||||
self.connection = None
|
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):
|
def get_last_insert_id(cursor, table_name, pk_name):
|
||||||
cursor.execute("SELECT LAST_INSERT_ID()")
|
cursor.execute("SELECT LAST_INSERT_ID()")
|
||||||
return cursor.fetchone()[0]
|
return cursor.fetchone()[0]
|
||||||
|
|
|
@ -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()]
|
Loading…
Reference in New Issue