base.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. """
  2. SQLite backend for the sqlite3 module in the standard library.
  3. """
  4. import datetime
  5. import decimal
  6. import warnings
  7. from collections.abc import Mapping
  8. from itertools import chain, tee
  9. from sqlite3 import dbapi2 as Database
  10. from django.core.exceptions import ImproperlyConfigured
  11. from django.db import IntegrityError
  12. from django.db.backends.base.base import BaseDatabaseWrapper
  13. from django.utils.asyncio import async_unsafe
  14. from django.utils.dateparse import parse_date, parse_datetime, parse_time
  15. from django.utils.regex_helper import _lazy_re_compile
  16. from ._functions import register as register_functions
  17. from .client import DatabaseClient
  18. from .creation import DatabaseCreation
  19. from .features import DatabaseFeatures
  20. from .introspection import DatabaseIntrospection
  21. from .operations import DatabaseOperations
  22. from .schema import DatabaseSchemaEditor
  23. def decoder(conv_func):
  24. """
  25. Convert bytestrings from Python's sqlite3 interface to a regular string.
  26. """
  27. return lambda s: conv_func(s.decode())
  28. def adapt_date(val):
  29. return val.isoformat()
  30. def adapt_datetime(val):
  31. return val.isoformat(" ")
  32. Database.register_converter("bool", b"1".__eq__)
  33. Database.register_converter("date", decoder(parse_date))
  34. Database.register_converter("time", decoder(parse_time))
  35. Database.register_converter("datetime", decoder(parse_datetime))
  36. Database.register_converter("timestamp", decoder(parse_datetime))
  37. Database.register_adapter(decimal.Decimal, str)
  38. Database.register_adapter(datetime.date, adapt_date)
  39. Database.register_adapter(datetime.datetime, adapt_datetime)
  40. class DatabaseWrapper(BaseDatabaseWrapper):
  41. vendor = "sqlite"
  42. display_name = "SQLite"
  43. # SQLite doesn't actually support most of these types, but it "does the right
  44. # thing" given more verbose field definitions, so leave them as is so that
  45. # schema inspection is more useful.
  46. data_types = {
  47. "AutoField": "integer",
  48. "BigAutoField": "integer",
  49. "BinaryField": "BLOB",
  50. "BooleanField": "bool",
  51. "CharField": "varchar(%(max_length)s)",
  52. "DateField": "date",
  53. "DateTimeField": "datetime",
  54. "DecimalField": "decimal",
  55. "DurationField": "bigint",
  56. "FileField": "varchar(%(max_length)s)",
  57. "FilePathField": "varchar(%(max_length)s)",
  58. "FloatField": "real",
  59. "IntegerField": "integer",
  60. "BigIntegerField": "bigint",
  61. "IPAddressField": "char(15)",
  62. "GenericIPAddressField": "char(39)",
  63. "JSONField": "text",
  64. "OneToOneField": "integer",
  65. "PositiveBigIntegerField": "bigint unsigned",
  66. "PositiveIntegerField": "integer unsigned",
  67. "PositiveSmallIntegerField": "smallint unsigned",
  68. "SlugField": "varchar(%(max_length)s)",
  69. "SmallAutoField": "integer",
  70. "SmallIntegerField": "smallint",
  71. "TextField": "text",
  72. "TimeField": "time",
  73. "UUIDField": "char(32)",
  74. }
  75. data_type_check_constraints = {
  76. "PositiveBigIntegerField": '"%(column)s" >= 0',
  77. "JSONField": '(JSON_VALID("%(column)s") OR "%(column)s" IS NULL)',
  78. "PositiveIntegerField": '"%(column)s" >= 0',
  79. "PositiveSmallIntegerField": '"%(column)s" >= 0',
  80. }
  81. data_types_suffix = {
  82. "AutoField": "AUTOINCREMENT",
  83. "BigAutoField": "AUTOINCREMENT",
  84. "SmallAutoField": "AUTOINCREMENT",
  85. }
  86. # SQLite requires LIKE statements to include an ESCAPE clause if the value
  87. # being escaped has a percent or underscore in it.
  88. # See https://www.sqlite.org/lang_expr.html for an explanation.
  89. operators = {
  90. "exact": "= %s",
  91. "iexact": "LIKE %s ESCAPE '\\'",
  92. "contains": "LIKE %s ESCAPE '\\'",
  93. "icontains": "LIKE %s ESCAPE '\\'",
  94. "regex": "REGEXP %s",
  95. "iregex": "REGEXP '(?i)' || %s",
  96. "gt": "> %s",
  97. "gte": ">= %s",
  98. "lt": "< %s",
  99. "lte": "<= %s",
  100. "startswith": "LIKE %s ESCAPE '\\'",
  101. "endswith": "LIKE %s ESCAPE '\\'",
  102. "istartswith": "LIKE %s ESCAPE '\\'",
  103. "iendswith": "LIKE %s ESCAPE '\\'",
  104. }
  105. # The patterns below are used to generate SQL pattern lookup clauses when
  106. # the right-hand side of the lookup isn't a raw string (it might be an expression
  107. # or the result of a bilateral transformation).
  108. # In those cases, special characters for LIKE operators (e.g. \, *, _) should be
  109. # escaped on database side.
  110. #
  111. # Note: we use str.format() here for readability as '%' is used as a wildcard for
  112. # the LIKE operator.
  113. pattern_esc = r"REPLACE(REPLACE(REPLACE({}, '\', '\\'), '%%', '\%%'), '_', '\_')"
  114. pattern_ops = {
  115. "contains": r"LIKE '%%' || {} || '%%' ESCAPE '\'",
  116. "icontains": r"LIKE '%%' || UPPER({}) || '%%' ESCAPE '\'",
  117. "startswith": r"LIKE {} || '%%' ESCAPE '\'",
  118. "istartswith": r"LIKE UPPER({}) || '%%' ESCAPE '\'",
  119. "endswith": r"LIKE '%%' || {} ESCAPE '\'",
  120. "iendswith": r"LIKE '%%' || UPPER({}) ESCAPE '\'",
  121. }
  122. Database = Database
  123. SchemaEditorClass = DatabaseSchemaEditor
  124. # Classes instantiated in __init__().
  125. client_class = DatabaseClient
  126. creation_class = DatabaseCreation
  127. features_class = DatabaseFeatures
  128. introspection_class = DatabaseIntrospection
  129. ops_class = DatabaseOperations
  130. def get_connection_params(self):
  131. settings_dict = self.settings_dict
  132. if not settings_dict["NAME"]:
  133. raise ImproperlyConfigured(
  134. "settings.DATABASES is improperly configured. "
  135. "Please supply the NAME value."
  136. )
  137. kwargs = {
  138. "database": settings_dict["NAME"],
  139. "detect_types": Database.PARSE_DECLTYPES | Database.PARSE_COLNAMES,
  140. **settings_dict["OPTIONS"],
  141. }
  142. # Always allow the underlying SQLite connection to be shareable
  143. # between multiple threads. The safe-guarding will be handled at a
  144. # higher level by the `BaseDatabaseWrapper.allow_thread_sharing`
  145. # property. This is necessary as the shareability is disabled by
  146. # default in sqlite3 and it cannot be changed once a connection is
  147. # opened.
  148. if "check_same_thread" in kwargs and kwargs["check_same_thread"]:
  149. warnings.warn(
  150. "The `check_same_thread` option was provided and set to "
  151. "True. It will be overridden with False. Use the "
  152. "`DatabaseWrapper.allow_thread_sharing` property instead "
  153. "for controlling thread shareability.",
  154. RuntimeWarning,
  155. )
  156. kwargs.update({"check_same_thread": False, "uri": True})
  157. return kwargs
  158. def get_database_version(self):
  159. return self.Database.sqlite_version_info
  160. @async_unsafe
  161. def get_new_connection(self, conn_params):
  162. conn = Database.connect(**conn_params)
  163. register_functions(conn)
  164. conn.execute("PRAGMA foreign_keys = ON")
  165. # The macOS bundled SQLite defaults legacy_alter_table ON, which
  166. # prevents atomic table renames.
  167. conn.execute("PRAGMA legacy_alter_table = OFF")
  168. return conn
  169. def create_cursor(self, name=None):
  170. return self.connection.cursor(factory=SQLiteCursorWrapper)
  171. @async_unsafe
  172. def close(self):
  173. self.validate_thread_sharing()
  174. # If database is in memory, closing the connection destroys the
  175. # database. To prevent accidental data loss, ignore close requests on
  176. # an in-memory db.
  177. if not self.is_in_memory_db():
  178. BaseDatabaseWrapper.close(self)
  179. def _savepoint_allowed(self):
  180. # When 'isolation_level' is not None, sqlite3 commits before each
  181. # savepoint; it's a bug. When it is None, savepoints don't make sense
  182. # because autocommit is enabled. The only exception is inside 'atomic'
  183. # blocks. To work around that bug, on SQLite, 'atomic' starts a
  184. # transaction explicitly rather than simply disable autocommit.
  185. return self.in_atomic_block
  186. def _set_autocommit(self, autocommit):
  187. if autocommit:
  188. level = None
  189. else:
  190. # sqlite3's internal default is ''. It's different from None.
  191. # See Modules/_sqlite/connection.c.
  192. level = ""
  193. # 'isolation_level' is a misleading API.
  194. # SQLite always runs at the SERIALIZABLE isolation level.
  195. with self.wrap_database_errors:
  196. self.connection.isolation_level = level
  197. def disable_constraint_checking(self):
  198. with self.cursor() as cursor:
  199. cursor.execute("PRAGMA foreign_keys = OFF")
  200. # Foreign key constraints cannot be turned off while in a multi-
  201. # statement transaction. Fetch the current state of the pragma
  202. # to determine if constraints are effectively disabled.
  203. enabled = cursor.execute("PRAGMA foreign_keys").fetchone()[0]
  204. return not bool(enabled)
  205. def enable_constraint_checking(self):
  206. with self.cursor() as cursor:
  207. cursor.execute("PRAGMA foreign_keys = ON")
  208. def check_constraints(self, table_names=None):
  209. """
  210. Check each table name in `table_names` for rows with invalid foreign
  211. key references. This method is intended to be used in conjunction with
  212. `disable_constraint_checking()` and `enable_constraint_checking()`, to
  213. determine if rows with invalid references were entered while constraint
  214. checks were off.
  215. """
  216. with self.cursor() as cursor:
  217. if table_names is None:
  218. violations = cursor.execute("PRAGMA foreign_key_check").fetchall()
  219. else:
  220. violations = chain.from_iterable(
  221. cursor.execute(
  222. "PRAGMA foreign_key_check(%s)" % self.ops.quote_name(table_name)
  223. ).fetchall()
  224. for table_name in table_names
  225. )
  226. # See https://www.sqlite.org/pragma.html#pragma_foreign_key_check
  227. for (
  228. table_name,
  229. rowid,
  230. referenced_table_name,
  231. foreign_key_index,
  232. ) in violations:
  233. foreign_key = cursor.execute(
  234. "PRAGMA foreign_key_list(%s)" % self.ops.quote_name(table_name)
  235. ).fetchall()[foreign_key_index]
  236. column_name, referenced_column_name = foreign_key[3:5]
  237. primary_key_column_name = self.introspection.get_primary_key_column(
  238. cursor, table_name
  239. )
  240. primary_key_value, bad_value = cursor.execute(
  241. "SELECT %s, %s FROM %s WHERE rowid = %%s"
  242. % (
  243. self.ops.quote_name(primary_key_column_name),
  244. self.ops.quote_name(column_name),
  245. self.ops.quote_name(table_name),
  246. ),
  247. (rowid,),
  248. ).fetchone()
  249. raise IntegrityError(
  250. "The row in table '%s' with primary key '%s' has an "
  251. "invalid foreign key: %s.%s contains a value '%s' that "
  252. "does not have a corresponding value in %s.%s."
  253. % (
  254. table_name,
  255. primary_key_value,
  256. table_name,
  257. column_name,
  258. bad_value,
  259. referenced_table_name,
  260. referenced_column_name,
  261. )
  262. )
  263. def is_usable(self):
  264. return True
  265. def _start_transaction_under_autocommit(self):
  266. """
  267. Start a transaction explicitly in autocommit mode.
  268. Staying in autocommit mode works around a bug of sqlite3 that breaks
  269. savepoints when autocommit is disabled.
  270. """
  271. self.cursor().execute("BEGIN")
  272. def is_in_memory_db(self):
  273. return self.creation.is_in_memory_db(self.settings_dict["NAME"])
  274. FORMAT_QMARK_REGEX = _lazy_re_compile(r"(?<!%)%s")
  275. class SQLiteCursorWrapper(Database.Cursor):
  276. """
  277. Django uses the "format" and "pyformat" styles, but Python's sqlite3 module
  278. supports neither of these styles.
  279. This wrapper performs the following conversions:
  280. - "format" style to "qmark" style
  281. - "pyformat" style to "named" style
  282. In both cases, if you want to use a literal "%s", you'll need to use "%%s".
  283. """
  284. def execute(self, query, params=None):
  285. if params is None:
  286. return super().execute(query)
  287. # Extract names if params is a mapping, i.e. "pyformat" style is used.
  288. param_names = list(params) if isinstance(params, Mapping) else None
  289. query = self.convert_query(query, param_names=param_names)
  290. return super().execute(query, params)
  291. def executemany(self, query, param_list):
  292. # Extract names if params is a mapping, i.e. "pyformat" style is used.
  293. # Peek carefully as a generator can be passed instead of a list/tuple.
  294. peekable, param_list = tee(iter(param_list))
  295. if (params := next(peekable, None)) and isinstance(params, Mapping):
  296. param_names = list(params)
  297. else:
  298. param_names = None
  299. query = self.convert_query(query, param_names=param_names)
  300. return super().executemany(query, param_list)
  301. def convert_query(self, query, *, param_names=None):
  302. if param_names is None:
  303. # Convert from "format" style to "qmark" style.
  304. return FORMAT_QMARK_REGEX.sub("?", query).replace("%%", "%")
  305. else:
  306. # Convert from "pyformat" style to "named" style.
  307. return query % {name: f":{name}" for name in param_names}