You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

introspection.py 11 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import re
  2. from collections import namedtuple
  3. from django.db.backends.base.introspection import (
  4. BaseDatabaseIntrospection, FieldInfo, TableInfo,
  5. )
  6. field_size_re = re.compile(r'^\s*(?:var)?char\s*\(\s*(\d+)\s*\)\s*$')
  7. FieldInfo = namedtuple('FieldInfo', FieldInfo._fields + ('default',))
  8. def get_field_size(name):
  9. """ Extract the size number from a "varchar(11)" type name """
  10. m = field_size_re.search(name)
  11. return int(m.group(1)) if m else None
  12. # This light wrapper "fakes" a dictionary interface, because some SQLite data
  13. # types include variables in them -- e.g. "varchar(30)" -- and can't be matched
  14. # as a simple dictionary lookup.
  15. class FlexibleFieldLookupDict(object):
  16. # Maps SQL types to Django Field types. Some of the SQL types have multiple
  17. # entries here because SQLite allows for anything and doesn't normalize the
  18. # field type; it uses whatever was given.
  19. base_data_types_reverse = {
  20. 'bool': 'BooleanField',
  21. 'boolean': 'BooleanField',
  22. 'smallint': 'SmallIntegerField',
  23. 'smallint unsigned': 'PositiveSmallIntegerField',
  24. 'smallinteger': 'SmallIntegerField',
  25. 'int': 'IntegerField',
  26. 'integer': 'IntegerField',
  27. 'bigint': 'BigIntegerField',
  28. 'integer unsigned': 'PositiveIntegerField',
  29. 'decimal': 'DecimalField',
  30. 'real': 'FloatField',
  31. 'text': 'TextField',
  32. 'char': 'CharField',
  33. 'blob': 'BinaryField',
  34. 'date': 'DateField',
  35. 'datetime': 'DateTimeField',
  36. 'time': 'TimeField',
  37. }
  38. def __getitem__(self, key):
  39. key = key.lower()
  40. try:
  41. return self.base_data_types_reverse[key]
  42. except KeyError:
  43. size = get_field_size(key)
  44. if size is not None:
  45. return ('CharField', {'max_length': size})
  46. raise KeyError
  47. class DatabaseIntrospection(BaseDatabaseIntrospection):
  48. data_types_reverse = FlexibleFieldLookupDict()
  49. def get_table_list(self, cursor):
  50. """
  51. Returns a list of table and view names in the current database.
  52. """
  53. # Skip the sqlite_sequence system table used for autoincrement key
  54. # generation.
  55. cursor.execute("""
  56. SELECT name, type FROM sqlite_master
  57. WHERE type in ('table', 'view') AND NOT name='sqlite_sequence'
  58. ORDER BY name""")
  59. return [TableInfo(row[0], row[1][0]) for row in cursor.fetchall()]
  60. def get_table_description(self, cursor, table_name):
  61. "Returns a description of the table, with the DB-API cursor.description interface."
  62. return [
  63. FieldInfo(
  64. info['name'],
  65. info['type'],
  66. None,
  67. info['size'],
  68. None,
  69. None,
  70. info['null_ok'],
  71. info['default'],
  72. ) for info in self._table_info(cursor, table_name)
  73. ]
  74. def column_name_converter(self, name):
  75. """
  76. SQLite will in some cases, e.g. when returning columns from views and
  77. subselects, return column names in 'alias."column"' format instead of
  78. simply 'column'.
  79. Affects SQLite < 3.7.15, fixed by http://www.sqlite.org/src/info/5526e0aa3c
  80. """
  81. # TODO: remove when SQLite < 3.7.15 is sufficiently old.
  82. # 3.7.13 ships in Debian stable as of 2014-03-21.
  83. if self.connection.Database.sqlite_version_info < (3, 7, 15):
  84. return name.split('.')[-1].strip('"')
  85. else:
  86. return name
  87. def get_relations(self, cursor, table_name):
  88. """
  89. Return a dictionary of {field_name: (field_name_other_table, other_table)}
  90. representing all relationships to the given table.
  91. """
  92. # Dictionary of relations to return
  93. relations = {}
  94. # Schema for this table
  95. cursor.execute("SELECT sql FROM sqlite_master WHERE tbl_name = %s AND type = %s", [table_name, "table"])
  96. try:
  97. results = cursor.fetchone()[0].strip()
  98. except TypeError:
  99. # It might be a view, then no results will be returned
  100. return relations
  101. results = results[results.index('(') + 1:results.rindex(')')]
  102. # Walk through and look for references to other tables. SQLite doesn't
  103. # really have enforced references, but since it echoes out the SQL used
  104. # to create the table we can look for REFERENCES statements used there.
  105. for field_desc in results.split(','):
  106. field_desc = field_desc.strip()
  107. if field_desc.startswith("UNIQUE"):
  108. continue
  109. m = re.search('references (\S*) ?\(["|]?(.*)["|]?\)', field_desc, re.I)
  110. if not m:
  111. continue
  112. table, column = [s.strip('"') for s in m.groups()]
  113. if field_desc.startswith("FOREIGN KEY"):
  114. # Find name of the target FK field
  115. m = re.match('FOREIGN KEY\(([^\)]*)\).*', field_desc, re.I)
  116. field_name = m.groups()[0].strip('"')
  117. else:
  118. field_name = field_desc.split()[0].strip('"')
  119. cursor.execute("SELECT sql FROM sqlite_master WHERE tbl_name = %s", [table])
  120. result = cursor.fetchall()[0]
  121. other_table_results = result[0].strip()
  122. li, ri = other_table_results.index('('), other_table_results.rindex(')')
  123. other_table_results = other_table_results[li + 1:ri]
  124. for other_desc in other_table_results.split(','):
  125. other_desc = other_desc.strip()
  126. if other_desc.startswith('UNIQUE'):
  127. continue
  128. other_name = other_desc.split(' ', 1)[0].strip('"')
  129. if other_name == column:
  130. relations[field_name] = (other_name, table)
  131. break
  132. return relations
  133. def get_key_columns(self, cursor, table_name):
  134. """
  135. Returns a list of (column_name, referenced_table_name, referenced_column_name) for all
  136. key columns in given table.
  137. """
  138. key_columns = []
  139. # Schema for this table
  140. cursor.execute("SELECT sql FROM sqlite_master WHERE tbl_name = %s AND type = %s", [table_name, "table"])
  141. results = cursor.fetchone()[0].strip()
  142. results = results[results.index('(') + 1:results.rindex(')')]
  143. # Walk through and look for references to other tables. SQLite doesn't
  144. # really have enforced references, but since it echoes out the SQL used
  145. # to create the table we can look for REFERENCES statements used there.
  146. for field_index, field_desc in enumerate(results.split(',')):
  147. field_desc = field_desc.strip()
  148. if field_desc.startswith("UNIQUE"):
  149. continue
  150. m = re.search('"(.*)".*references (.*) \(["|](.*)["|]\)', field_desc, re.I)
  151. if not m:
  152. continue
  153. # This will append (column_name, referenced_table_name, referenced_column_name) to key_columns
  154. key_columns.append(tuple(s.strip('"') for s in m.groups()))
  155. return key_columns
  156. def get_indexes(self, cursor, table_name):
  157. indexes = {}
  158. for info in self._table_info(cursor, table_name):
  159. if info['pk'] != 0:
  160. indexes[info['name']] = {'primary_key': True,
  161. 'unique': False}
  162. cursor.execute('PRAGMA index_list(%s)' % self.connection.ops.quote_name(table_name))
  163. # seq, name, unique
  164. for index, unique in [(field[1], field[2]) for field in cursor.fetchall()]:
  165. cursor.execute('PRAGMA index_info(%s)' % self.connection.ops.quote_name(index))
  166. info = cursor.fetchall()
  167. # Skip indexes across multiple fields
  168. if len(info) != 1:
  169. continue
  170. name = info[0][2] # seqno, cid, name
  171. indexes[name] = {'primary_key': indexes.get(name, {}).get("primary_key", False),
  172. 'unique': unique}
  173. return indexes
  174. def get_primary_key_column(self, cursor, table_name):
  175. """
  176. Get the column name of the primary key for the given table.
  177. """
  178. # Don't use PRAGMA because that causes issues with some transactions
  179. cursor.execute("SELECT sql FROM sqlite_master WHERE tbl_name = %s AND type = %s", [table_name, "table"])
  180. row = cursor.fetchone()
  181. if row is None:
  182. raise ValueError("Table %s does not exist" % table_name)
  183. results = row[0].strip()
  184. results = results[results.index('(') + 1:results.rindex(')')]
  185. for field_desc in results.split(','):
  186. field_desc = field_desc.strip()
  187. m = re.search('"(.*)".*PRIMARY KEY( AUTOINCREMENT)?$', field_desc)
  188. if m:
  189. return m.groups()[0]
  190. return None
  191. def _table_info(self, cursor, name):
  192. cursor.execute('PRAGMA table_info(%s)' % self.connection.ops.quote_name(name))
  193. # cid, name, type, notnull, default_value, pk
  194. return [{
  195. 'name': field[1],
  196. 'type': field[2],
  197. 'size': get_field_size(field[2]),
  198. 'null_ok': not field[3],
  199. 'default': field[4],
  200. 'pk': field[5], # undocumented
  201. } for field in cursor.fetchall()]
  202. def get_constraints(self, cursor, table_name):
  203. """
  204. Retrieves any constraints or keys (unique, pk, fk, check, index) across one or more columns.
  205. """
  206. constraints = {}
  207. # Get the index info
  208. cursor.execute("PRAGMA index_list(%s)" % self.connection.ops.quote_name(table_name))
  209. for row in cursor.fetchall():
  210. # Sqlite3 3.8.9+ has 5 columns, however older versions only give 3
  211. # columns. Discard last 2 columns if there.
  212. number, index, unique = row[:3]
  213. # Get the index info for that index
  214. cursor.execute('PRAGMA index_info(%s)' % self.connection.ops.quote_name(index))
  215. for index_rank, column_rank, column in cursor.fetchall():
  216. if index not in constraints:
  217. constraints[index] = {
  218. "columns": [],
  219. "primary_key": False,
  220. "unique": bool(unique),
  221. "foreign_key": False,
  222. "check": False,
  223. "index": True,
  224. }
  225. constraints[index]['columns'].append(column)
  226. # Get the PK
  227. pk_column = self.get_primary_key_column(cursor, table_name)
  228. if pk_column:
  229. # SQLite doesn't actually give a name to the PK constraint,
  230. # so we invent one. This is fine, as the SQLite backend never
  231. # deletes PK constraints by name, as you can't delete constraints
  232. # in SQLite; we remake the table with a new PK instead.
  233. constraints["__primary__"] = {
  234. "columns": [pk_column],
  235. "primary_key": True,
  236. "unique": False, # It's not actually a unique constraint.
  237. "foreign_key": False,
  238. "check": False,
  239. "index": False,
  240. }
  241. return constraints