Development of an internal social media platform with personalised dashboards for students
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 11KB

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