Return namedtuple from get_table_description

We don't make use of it currently to not break third-party db
backends.
This commit is contained in:
Claude Paroz 2013-01-12 21:10:08 +01:00
parent 17f8496fea
commit 223fc8eaf9
5 changed files with 15 additions and 8 deletions

View File

@ -4,6 +4,7 @@ try:
from django.utils.six.moves import _thread as thread
except ImportError:
from django.utils.six.moves import _dummy_thread as thread
from collections import namedtuple
from contextlib import contextmanager
from django.conf import settings
@ -918,6 +919,12 @@ class BaseDatabaseOperations(object):
"""
return params
# Structure returned by the DB-API cursor.description interface (PEP 249)
FieldInfo = namedtuple('FieldInfo',
'name type_code display_size internal_size precision scale null_ok'
)
class BaseDatabaseIntrospection(object):
"""
This class encapsulates all backend-specific introspection utilities

View File

@ -1,7 +1,7 @@
import re
from .base import FIELD_TYPE
from django.db.backends import BaseDatabaseIntrospection
from django.db.backends import BaseDatabaseIntrospection, FieldInfo
foreign_key_re = re.compile(r"\sCONSTRAINT `[^`]*` FOREIGN KEY \(`([^`]*)`\) REFERENCES `([^`]*)` \(`([^`]*)`\)")
@ -47,7 +47,7 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
length_map = dict(cursor.fetchall())
cursor.execute("SELECT * FROM %s LIMIT 1" % self.connection.ops.quote_name(table_name))
return [line[:3] + (length_map.get(line[0], line[3]),) + line[4:]
return [FieldInfo(*(line[:3] + (length_map.get(line[0], line[3]),) + line[4:]))
for line in cursor.description]
def _name_to_index(self, cursor, table_name):

View File

@ -1,4 +1,4 @@
from django.db.backends import BaseDatabaseIntrospection
from django.db.backends import BaseDatabaseIntrospection, FieldInfo
import cx_Oracle
import re
@ -47,7 +47,7 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
cursor.execute("SELECT * FROM %s WHERE ROWNUM < 2" % self.connection.ops.quote_name(table_name))
description = []
for desc in cursor.description:
description.append((desc[0].lower(),) + desc[1:])
description.append(FieldInfo(*((desc[0].lower(),) + desc[1:])))
return description
def table_name_converter(self, name):

View File

@ -1,6 +1,6 @@
from __future__ import unicode_literals
from django.db.backends import BaseDatabaseIntrospection
from django.db.backends import BaseDatabaseIntrospection, FieldInfo
class DatabaseIntrospection(BaseDatabaseIntrospection):
@ -45,7 +45,7 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
WHERE table_name = %s""", [table_name])
null_map = dict(cursor.fetchall())
cursor.execute("SELECT * FROM %s LIMIT 1" % self.connection.ops.quote_name(table_name))
return [line[:6] + (null_map[line[0]]=='YES',)
return [FieldInfo(*(line[:6] + (null_map[line[0]]=='YES',)))
for line in cursor.description]
def get_relations(self, cursor, table_name):

View File

@ -1,5 +1,5 @@
import re
from django.db.backends import BaseDatabaseIntrospection
from django.db.backends import BaseDatabaseIntrospection, FieldInfo
field_size_re = re.compile(r'^\s*(?:var)?char\s*\(\s*(\d+)\s*\)\s*$')
@ -60,7 +60,7 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
def get_table_description(self, cursor, table_name):
"Returns a description of the table, with the DB-API cursor.description interface."
return [(info['name'], info['type'], None, info['size'], None, None,
return [FieldInfo(info['name'], info['type'], None, info['size'], None, None,
info['null_ok']) for info in self._table_info(cursor, table_name)]
def get_relations(self, cursor, table_name):