Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Made get_table_description also return the size of char fields on SQLite

  • Loading branch information...
commit a9a773ff38a9fd28085e8b62b8a160c4e2ea5efb 1 parent 879b245
@claudep claudep authored
View
4 django/db/backends/postgresql_psycopg2/introspection.py
@@ -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):
"""
View
18 django/db/backends/sqlite3/introspection.py
@@ -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 = field_size_re.search(name)
+ return int(m.group(1)) 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):
try:
return self.base_data_types_reverse[key]
except KeyError:
- import re
- m = re.search(r'^\s*(?:var)?char\s*\(\s*(\d+)\s*\)\s*$', key)
- if m:
- return ('CharField', {'max_length': int(m.group(1))})
+ 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()]
Please sign in to comment.
Something went wrong with that request. Please try again.