diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py index 5116f50668..78dbbc670b 100644 --- a/django/db/backends/__init__.py +++ b/django/db/backends/__init__.py @@ -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 diff --git a/django/db/backends/mysql/introspection.py b/django/db/backends/mysql/introspection.py index c552263e5e..52e97baedb 100644 --- a/django/db/backends/mysql/introspection.py +++ b/django/db/backends/mysql/introspection.py @@ -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): diff --git a/django/db/backends/oracle/introspection.py b/django/db/backends/oracle/introspection.py index 7d477f6924..2a68b999bc 100644 --- a/django/db/backends/oracle/introspection.py +++ b/django/db/backends/oracle/introspection.py @@ -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): diff --git a/django/db/backends/postgresql_psycopg2/introspection.py b/django/db/backends/postgresql_psycopg2/introspection.py index 5d30382ddf..440fa44c4e 100644 --- a/django/db/backends/postgresql_psycopg2/introspection.py +++ b/django/db/backends/postgresql_psycopg2/introspection.py @@ -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): diff --git a/django/db/backends/sqlite3/introspection.py b/django/db/backends/sqlite3/introspection.py index 1df4c18c1c..dfc1d94d47 100644 --- a/django/db/backends/sqlite3/introspection.py +++ b/django/db/backends/sqlite3/introspection.py @@ -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):