Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Claude Paroz authored January 12, 2013
7  django/db/backends/__init__.py
@@ -4,6 +4,7 @@
4 4
     from django.utils.six.moves import _thread as thread
5 5
 except ImportError:
6 6
     from django.utils.six.moves import _dummy_thread as thread
  7
+from collections import namedtuple
7 8
 from contextlib import contextmanager
8 9
 
9 10
 from django.conf import settings
@@ -918,6 +919,12 @@ def modify_insert_params(self, placeholders, params):
918 919
         """
919 920
         return params
920 921
 
  922
+
  923
+# Structure returned by the DB-API cursor.description interface (PEP 249)
  924
+FieldInfo = namedtuple('FieldInfo',
  925
+    'name type_code display_size internal_size precision scale null_ok'
  926
+)
  927
+
921 928
 class BaseDatabaseIntrospection(object):
922 929
     """
923 930
     This class encapsulates all backend-specific introspection utilities
4  django/db/backends/mysql/introspection.py
... ...
@@ -1,7 +1,7 @@
1 1
 import re
2 2
 from .base import FIELD_TYPE
3 3
 
4  
-from django.db.backends import BaseDatabaseIntrospection
  4
+from django.db.backends import BaseDatabaseIntrospection, FieldInfo
5 5
 
6 6
 
7 7
 foreign_key_re = re.compile(r"\sCONSTRAINT `[^`]*` FOREIGN KEY \(`([^`]*)`\) REFERENCES `([^`]*)` \(`([^`]*)`\)")
@@ -47,7 +47,7 @@ def get_table_description(self, cursor, table_name):
47 47
         length_map = dict(cursor.fetchall())
48 48
 
49 49
         cursor.execute("SELECT * FROM %s LIMIT 1" % self.connection.ops.quote_name(table_name))
50  
-        return [line[:3] + (length_map.get(line[0], line[3]),) + line[4:]
  50
+        return [FieldInfo(*(line[:3] + (length_map.get(line[0], line[3]),) + line[4:]))
51 51
             for line in cursor.description]
52 52
 
53 53
     def _name_to_index(self, cursor, table_name):
4  django/db/backends/oracle/introspection.py
... ...
@@ -1,4 +1,4 @@
1  
-from django.db.backends import BaseDatabaseIntrospection
  1
+from django.db.backends import BaseDatabaseIntrospection, FieldInfo
2 2
 import cx_Oracle
3 3
 import re
4 4
 
@@ -47,7 +47,7 @@ def get_table_description(self, cursor, table_name):
47 47
         cursor.execute("SELECT * FROM %s WHERE ROWNUM < 2" % self.connection.ops.quote_name(table_name))
48 48
         description = []
49 49
         for desc in cursor.description:
50  
-            description.append((desc[0].lower(),) + desc[1:])
  50
+            description.append(FieldInfo(*((desc[0].lower(),) + desc[1:])))
51 51
         return description
52 52
 
53 53
     def table_name_converter(self, name):
4  django/db/backends/postgresql_psycopg2/introspection.py
... ...
@@ -1,6 +1,6 @@
1 1
 from __future__ import unicode_literals
2 2
 
3  
-from django.db.backends import BaseDatabaseIntrospection
  3
+from django.db.backends import BaseDatabaseIntrospection, FieldInfo
4 4
 
5 5
 
6 6
 class DatabaseIntrospection(BaseDatabaseIntrospection):
@@ -45,7 +45,7 @@ def get_table_description(self, cursor, table_name):
45 45
             WHERE table_name = %s""", [table_name])
46 46
         null_map = dict(cursor.fetchall())
47 47
         cursor.execute("SELECT * FROM %s LIMIT 1" % self.connection.ops.quote_name(table_name))
48  
-        return [line[:6] + (null_map[line[0]]=='YES',)
  48
+        return [FieldInfo(*(line[:6] + (null_map[line[0]]=='YES',)))
49 49
                 for line in cursor.description]
50 50
 
51 51
     def get_relations(self, cursor, table_name):
4  django/db/backends/sqlite3/introspection.py
... ...
@@ -1,5 +1,5 @@
1 1
 import re
2  
-from django.db.backends import BaseDatabaseIntrospection
  2
+from django.db.backends import BaseDatabaseIntrospection, FieldInfo
3 3
 
4 4
 field_size_re = re.compile(r'^\s*(?:var)?char\s*\(\s*(\d+)\s*\)\s*$')
5 5
 
@@ -60,7 +60,7 @@ def get_table_list(self, cursor):
60 60
 
61 61
     def get_table_description(self, cursor, table_name):
62 62
         "Returns a description of the table, with the DB-API cursor.description interface."
63  
-        return [(info['name'], info['type'], None, info['size'], None, None,
  63
+        return [FieldInfo(info['name'], info['type'], None, info['size'], None, None,
64 64
                  info['null_ok']) for info in self._table_info(cursor, table_name)]
65 65
 
66 66
     def get_relations(self, cursor, table_name):

0 notes on commit 223fc8e

Please sign in to comment.
Something went wrong with that request. Please try again.