Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Return namedtuple from get_table_description

We don't make use of it currently to not break third-party db
backends.
  • Loading branch information...
commit 223fc8eaf9d04eb2c277a30f4f46745d4403b69b 1 parent 17f8496
@claudep claudep authored
View
7 django/db/backends/__init__.py
@@ -4,6 +4,7 @@
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 @@ def modify_insert_params(self, placeholders, params):
"""
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
4 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 @@ def get_table_description(self, cursor, table_name):
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
4 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 @@ def get_table_description(self, cursor, table_name):
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
4 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 @@ def get_table_description(self, cursor, table_name):
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
4 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 @@ def get_table_list(self, cursor):
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):
Please sign in to comment.
Something went wrong with that request. Please try again.