Skip to content

Commit

Permalink
Return namedtuple from get_table_description
Browse files Browse the repository at this point in the history
We don't make use of it currently to not break third-party db
backends.
  • Loading branch information
claudep committed Jan 12, 2013
1 parent 17f8496 commit 223fc8e
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
7 changes: 7 additions & 0 deletions django/db/backends/__init__.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions 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 `([^`]*)` \(`([^`]*)`\)")
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions 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

Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions 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):
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions 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*$')

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 223fc8e

Please sign in to comment.