Browse files

Made get_table_description also return the size of char fields on SQLite

  • Loading branch information...
claudep committed Aug 30, 2012
1 parent 879b245 commit a9a773ff38a9fd28085e8b62b8a160c4e2ea5efb
@@ -45,8 +45,8 @@ 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 [tuple([item for item in line[:6]] + [null_map[line[0]]=='YES'])
for line in cursor.description]
return [line[:6] + (null_map[line[0]]=='YES',)
for line in cursor.description]
def get_relations(self, cursor, table_name):
@@ -1,6 +1,14 @@
import re
from django.db.backends import BaseDatabaseIntrospection
field_size_re = re.compile(r'^\s*(?:var)?char\s*\(\s*(\d+)\s*\)\s*$')
def get_field_size(name):
""" Extract the size number from a "varchar(11)" type name """
m =
return int( if m else None
# This light wrapper "fakes" a dictionary interface, because some SQLite data
# types include variables in them -- e.g. "varchar(30)" -- and can't be matched
# as a simple dictionary lookup.
@@ -32,10 +40,9 @@ def __getitem__(self, key):
return self.base_data_types_reverse[key]
except KeyError:
import re
m ='^\s*(?:var)?char\s*\(\s*(\d+)\s*\)\s*$', key)
if m:
return ('CharField', {'max_length': int(})
size = get_field_size(key)
if size is not None:
return ('CharField', {'max_length': size})
raise KeyError
class DatabaseIntrospection(BaseDatabaseIntrospection):
@@ -53,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, None, None, None,
return [(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):
@@ -171,6 +178,7 @@ def _table_info(self, cursor, name):
# cid, name, type, notnull, dflt_value, pk
return [{'name': field[1],
'type': field[2],
'size': get_field_size(field[2]),
'null_ok': not field[3],
'pk': field[5] # undocumented
} for field in cursor.fetchall()]

0 comments on commit a9a773f

Please sign in to comment.