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.

base.py 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. """
  2. SQLite3 backend for the sqlite3 module in the standard library.
  3. """
  4. import datetime
  5. import decimal
  6. import math
  7. import operator
  8. import re
  9. import warnings
  10. from sqlite3 import dbapi2 as Database
  11. import pytz
  12. from django.core.exceptions import ImproperlyConfigured
  13. from django.db import utils
  14. from django.db.backends import utils as backend_utils
  15. from django.db.backends.base.base import BaseDatabaseWrapper
  16. from django.utils import timezone
  17. from django.utils.dateparse import parse_datetime, parse_time
  18. from django.utils.duration import duration_microseconds
  19. from .client import DatabaseClient # isort:skip
  20. from .creation import DatabaseCreation # isort:skip
  21. from .features import DatabaseFeatures # isort:skip
  22. from .introspection import DatabaseIntrospection # isort:skip
  23. from .operations import DatabaseOperations # isort:skip
  24. from .schema import DatabaseSchemaEditor # isort:skip
  25. def decoder(conv_func):
  26. """
  27. Convert bytestrings from Python's sqlite3 interface to a regular string.
  28. """
  29. return lambda s: conv_func(s.decode())
  30. Database.register_converter("bool", b'1'.__eq__)
  31. Database.register_converter("time", decoder(parse_time))
  32. Database.register_converter("datetime", decoder(parse_datetime))
  33. Database.register_converter("timestamp", decoder(parse_datetime))
  34. Database.register_converter("TIMESTAMP", decoder(parse_datetime))
  35. Database.register_adapter(decimal.Decimal, backend_utils.rev_typecast_decimal)
  36. class DatabaseWrapper(BaseDatabaseWrapper):
  37. vendor = 'sqlite'
  38. display_name = 'SQLite'
  39. # SQLite doesn't actually support most of these types, but it "does the right
  40. # thing" given more verbose field definitions, so leave them as is so that
  41. # schema inspection is more useful.
  42. data_types = {
  43. 'AutoField': 'integer',
  44. 'BigAutoField': 'integer',
  45. 'BinaryField': 'BLOB',
  46. 'BooleanField': 'bool',
  47. 'CharField': 'varchar(%(max_length)s)',
  48. 'DateField': 'date',
  49. 'DateTimeField': 'datetime',
  50. 'DecimalField': 'decimal',
  51. 'DurationField': 'bigint',
  52. 'FileField': 'varchar(%(max_length)s)',
  53. 'FilePathField': 'varchar(%(max_length)s)',
  54. 'FloatField': 'real',
  55. 'IntegerField': 'integer',
  56. 'BigIntegerField': 'bigint',
  57. 'IPAddressField': 'char(15)',
  58. 'GenericIPAddressField': 'char(39)',
  59. 'NullBooleanField': 'bool',
  60. 'OneToOneField': 'integer',
  61. 'PositiveIntegerField': 'integer unsigned',
  62. 'PositiveSmallIntegerField': 'smallint unsigned',
  63. 'SlugField': 'varchar(%(max_length)s)',
  64. 'SmallIntegerField': 'smallint',
  65. 'TextField': 'text',
  66. 'TimeField': 'time',
  67. 'UUIDField': 'char(32)',
  68. }
  69. data_types_suffix = {
  70. 'AutoField': 'AUTOINCREMENT',
  71. 'BigAutoField': 'AUTOINCREMENT',
  72. }
  73. # SQLite requires LIKE statements to include an ESCAPE clause if the value
  74. # being escaped has a percent or underscore in it.
  75. # See http://www.sqlite.org/lang_expr.html for an explanation.
  76. operators = {
  77. 'exact': '= %s',
  78. 'iexact': "LIKE %s ESCAPE '\\'",
  79. 'contains': "LIKE %s ESCAPE '\\'",
  80. 'icontains': "LIKE %s ESCAPE '\\'",
  81. 'regex': 'REGEXP %s',
  82. 'iregex': "REGEXP '(?i)' || %s",
  83. 'gt': '> %s',
  84. 'gte': '>= %s',
  85. 'lt': '< %s',
  86. 'lte': '<= %s',
  87. 'startswith': "LIKE %s ESCAPE '\\'",
  88. 'endswith': "LIKE %s ESCAPE '\\'",
  89. 'istartswith': "LIKE %s ESCAPE '\\'",
  90. 'iendswith': "LIKE %s ESCAPE '\\'",
  91. }
  92. # The patterns below are used to generate SQL pattern lookup clauses when
  93. # the right-hand side of the lookup isn't a raw string (it might be an expression
  94. # or the result of a bilateral transformation).
  95. # In those cases, special characters for LIKE operators (e.g. \, *, _) should be
  96. # escaped on database side.
  97. #
  98. # Note: we use str.format() here for readability as '%' is used as a wildcard for
  99. # the LIKE operator.
  100. pattern_esc = r"REPLACE(REPLACE(REPLACE({}, '\', '\\'), '%%', '\%%'), '_', '\_')"
  101. pattern_ops = {
  102. 'contains': r"LIKE '%%' || {} || '%%' ESCAPE '\'",
  103. 'icontains': r"LIKE '%%' || UPPER({}) || '%%' ESCAPE '\'",
  104. 'startswith': r"LIKE {} || '%%' ESCAPE '\'",
  105. 'istartswith': r"LIKE UPPER({}) || '%%' ESCAPE '\'",
  106. 'endswith': r"LIKE '%%' || {} ESCAPE '\'",
  107. 'iendswith': r"LIKE '%%' || UPPER({}) ESCAPE '\'",
  108. }
  109. Database = Database
  110. SchemaEditorClass = DatabaseSchemaEditor
  111. # Classes instantiated in __init__().
  112. client_class = DatabaseClient
  113. creation_class = DatabaseCreation
  114. features_class = DatabaseFeatures
  115. introspection_class = DatabaseIntrospection
  116. ops_class = DatabaseOperations
  117. def get_connection_params(self):
  118. settings_dict = self.settings_dict
  119. if not settings_dict['NAME']:
  120. raise ImproperlyConfigured(
  121. "settings.DATABASES is improperly configured. "
  122. "Please supply the NAME value.")
  123. kwargs = {
  124. 'database': settings_dict['NAME'],
  125. 'detect_types': Database.PARSE_DECLTYPES | Database.PARSE_COLNAMES,
  126. **settings_dict['OPTIONS'],
  127. }
  128. # Always allow the underlying SQLite connection to be shareable
  129. # between multiple threads. The safe-guarding will be handled at a
  130. # higher level by the `BaseDatabaseWrapper.allow_thread_sharing`
  131. # property. This is necessary as the shareability is disabled by
  132. # default in pysqlite and it cannot be changed once a connection is
  133. # opened.
  134. if 'check_same_thread' in kwargs and kwargs['check_same_thread']:
  135. warnings.warn(
  136. 'The `check_same_thread` option was provided and set to '
  137. 'True. It will be overridden with False. Use the '
  138. '`DatabaseWrapper.allow_thread_sharing` property instead '
  139. 'for controlling thread shareability.',
  140. RuntimeWarning
  141. )
  142. kwargs.update({'check_same_thread': False, 'uri': True})
  143. return kwargs
  144. def get_new_connection(self, conn_params):
  145. conn = Database.connect(**conn_params)
  146. conn.create_function("django_date_extract", 2, _sqlite_date_extract)
  147. conn.create_function("django_date_trunc", 2, _sqlite_date_trunc)
  148. conn.create_function("django_datetime_cast_date", 2, _sqlite_datetime_cast_date)
  149. conn.create_function("django_datetime_cast_time", 2, _sqlite_datetime_cast_time)
  150. conn.create_function("django_datetime_extract", 3, _sqlite_datetime_extract)
  151. conn.create_function("django_datetime_trunc", 3, _sqlite_datetime_trunc)
  152. conn.create_function("django_time_extract", 2, _sqlite_time_extract)
  153. conn.create_function("django_time_trunc", 2, _sqlite_time_trunc)
  154. conn.create_function("django_time_diff", 2, _sqlite_time_diff)
  155. conn.create_function("django_timestamp_diff", 2, _sqlite_timestamp_diff)
  156. conn.create_function("regexp", 2, _sqlite_regexp)
  157. conn.create_function("django_format_dtdelta", 3, _sqlite_format_dtdelta)
  158. conn.create_function("django_power", 2, _sqlite_power)
  159. conn.create_function('LPAD', 3, _sqlite_lpad)
  160. conn.create_function('REPEAT', 2, operator.mul)
  161. conn.create_function('RPAD', 3, _sqlite_rpad)
  162. conn.execute('PRAGMA foreign_keys = ON')
  163. return conn
  164. def init_connection_state(self):
  165. pass
  166. def create_cursor(self, name=None):
  167. return self.connection.cursor(factory=SQLiteCursorWrapper)
  168. def close(self):
  169. self.validate_thread_sharing()
  170. # If database is in memory, closing the connection destroys the
  171. # database. To prevent accidental data loss, ignore close requests on
  172. # an in-memory db.
  173. if not self.is_in_memory_db():
  174. BaseDatabaseWrapper.close(self)
  175. def _savepoint_allowed(self):
  176. # Two conditions are required here:
  177. # - A sufficiently recent version of SQLite to support savepoints,
  178. # - Being in a transaction, which can only happen inside 'atomic'.
  179. # When 'isolation_level' is not None, sqlite3 commits before each
  180. # savepoint; it's a bug. When it is None, savepoints don't make sense
  181. # because autocommit is enabled. The only exception is inside 'atomic'
  182. # blocks. To work around that bug, on SQLite, 'atomic' starts a
  183. # transaction explicitly rather than simply disable autocommit.
  184. return self.features.uses_savepoints and self.in_atomic_block
  185. def _set_autocommit(self, autocommit):
  186. if autocommit:
  187. level = None
  188. else:
  189. # sqlite3's internal default is ''. It's different from None.
  190. # See Modules/_sqlite/connection.c.
  191. level = ''
  192. # 'isolation_level' is a misleading API.
  193. # SQLite always runs at the SERIALIZABLE isolation level.
  194. with self.wrap_database_errors:
  195. self.connection.isolation_level = level
  196. def disable_constraint_checking(self):
  197. if self.in_atomic_block:
  198. # sqlite3 cannot disable constraint checking inside a transaction.
  199. return False
  200. self.cursor().execute('PRAGMA foreign_keys = OFF')
  201. return True
  202. def enable_constraint_checking(self):
  203. self.cursor().execute('PRAGMA foreign_keys = ON')
  204. def check_constraints(self, table_names=None):
  205. """
  206. Check each table name in `table_names` for rows with invalid foreign
  207. key references. This method is intended to be used in conjunction with
  208. `disable_constraint_checking()` and `enable_constraint_checking()`, to
  209. determine if rows with invalid references were entered while constraint
  210. checks were off.
  211. """
  212. with self.cursor() as cursor:
  213. if table_names is None:
  214. table_names = self.introspection.table_names(cursor)
  215. for table_name in table_names:
  216. primary_key_column_name = self.introspection.get_primary_key_column(cursor, table_name)
  217. if not primary_key_column_name:
  218. continue
  219. key_columns = self.introspection.get_key_columns(cursor, table_name)
  220. for column_name, referenced_table_name, referenced_column_name in key_columns:
  221. cursor.execute(
  222. """
  223. SELECT REFERRING.`%s`, REFERRING.`%s` FROM `%s` as REFERRING
  224. LEFT JOIN `%s` as REFERRED
  225. ON (REFERRING.`%s` = REFERRED.`%s`)
  226. WHERE REFERRING.`%s` IS NOT NULL AND REFERRED.`%s` IS NULL
  227. """
  228. % (
  229. primary_key_column_name, column_name, table_name,
  230. referenced_table_name, column_name, referenced_column_name,
  231. column_name, referenced_column_name,
  232. )
  233. )
  234. for bad_row in cursor.fetchall():
  235. raise utils.IntegrityError(
  236. "The row in table '%s' with primary key '%s' has an "
  237. "invalid foreign key: %s.%s contains a value '%s' that "
  238. "does not have a corresponding value in %s.%s." % (
  239. table_name, bad_row[0], table_name, column_name,
  240. bad_row[1], referenced_table_name, referenced_column_name,
  241. )
  242. )
  243. def is_usable(self):
  244. return True
  245. def _start_transaction_under_autocommit(self):
  246. """
  247. Start a transaction explicitly in autocommit mode.
  248. Staying in autocommit mode works around a bug of sqlite3 that breaks
  249. savepoints when autocommit is disabled.
  250. """
  251. self.cursor().execute("BEGIN")
  252. def is_in_memory_db(self):
  253. return self.creation.is_in_memory_db(self.settings_dict['NAME'])
  254. FORMAT_QMARK_REGEX = re.compile(r'(?<!%)%s')
  255. class SQLiteCursorWrapper(Database.Cursor):
  256. """
  257. Django uses "format" style placeholders, but pysqlite2 uses "qmark" style.
  258. This fixes it -- but note that if you want to use a literal "%s" in a query,
  259. you'll need to use "%%s".
  260. """
  261. def execute(self, query, params=None):
  262. if params is None:
  263. return Database.Cursor.execute(self, query)
  264. query = self.convert_query(query)
  265. return Database.Cursor.execute(self, query, params)
  266. def executemany(self, query, param_list):
  267. query = self.convert_query(query)
  268. return Database.Cursor.executemany(self, query, param_list)
  269. def convert_query(self, query):
  270. return FORMAT_QMARK_REGEX.sub('?', query).replace('%%', '%')
  271. def _sqlite_date_extract(lookup_type, dt):
  272. if dt is None:
  273. return None
  274. try:
  275. dt = backend_utils.typecast_timestamp(dt)
  276. except (ValueError, TypeError):
  277. return None
  278. if lookup_type == 'week_day':
  279. return (dt.isoweekday() % 7) + 1
  280. elif lookup_type == 'week':
  281. return dt.isocalendar()[1]
  282. elif lookup_type == 'quarter':
  283. return math.ceil(dt.month / 3)
  284. else:
  285. return getattr(dt, lookup_type)
  286. def _sqlite_date_trunc(lookup_type, dt):
  287. try:
  288. dt = backend_utils.typecast_timestamp(dt)
  289. except (ValueError, TypeError):
  290. return None
  291. if lookup_type == 'year':
  292. return "%i-01-01" % dt.year
  293. elif lookup_type == 'quarter':
  294. month_in_quarter = dt.month - (dt.month - 1) % 3
  295. return '%i-%02i-01' % (dt.year, month_in_quarter)
  296. elif lookup_type == 'month':
  297. return "%i-%02i-01" % (dt.year, dt.month)
  298. elif lookup_type == 'week':
  299. dt = dt - datetime.timedelta(days=dt.weekday())
  300. return "%i-%02i-%02i" % (dt.year, dt.month, dt.day)
  301. elif lookup_type == 'day':
  302. return "%i-%02i-%02i" % (dt.year, dt.month, dt.day)
  303. def _sqlite_time_trunc(lookup_type, dt):
  304. try:
  305. dt = backend_utils.typecast_time(dt)
  306. except (ValueError, TypeError):
  307. return None
  308. if lookup_type == 'hour':
  309. return "%02i:00:00" % dt.hour
  310. elif lookup_type == 'minute':
  311. return "%02i:%02i:00" % (dt.hour, dt.minute)
  312. elif lookup_type == 'second':
  313. return "%02i:%02i:%02i" % (dt.hour, dt.minute, dt.second)
  314. def _sqlite_datetime_parse(dt, tzname):
  315. if dt is None:
  316. return None
  317. try:
  318. dt = backend_utils.typecast_timestamp(dt)
  319. except (ValueError, TypeError):
  320. return None
  321. if tzname is not None:
  322. dt = timezone.localtime(dt, pytz.timezone(tzname))
  323. return dt
  324. def _sqlite_datetime_cast_date(dt, tzname):
  325. dt = _sqlite_datetime_parse(dt, tzname)
  326. if dt is None:
  327. return None
  328. return dt.date().isoformat()
  329. def _sqlite_datetime_cast_time(dt, tzname):
  330. dt = _sqlite_datetime_parse(dt, tzname)
  331. if dt is None:
  332. return None
  333. return dt.time().isoformat()
  334. def _sqlite_datetime_extract(lookup_type, dt, tzname):
  335. dt = _sqlite_datetime_parse(dt, tzname)
  336. if dt is None:
  337. return None
  338. if lookup_type == 'week_day':
  339. return (dt.isoweekday() % 7) + 1
  340. elif lookup_type == 'week':
  341. return dt.isocalendar()[1]
  342. elif lookup_type == 'quarter':
  343. return math.ceil(dt.month / 3)
  344. else:
  345. return getattr(dt, lookup_type)
  346. def _sqlite_datetime_trunc(lookup_type, dt, tzname):
  347. dt = _sqlite_datetime_parse(dt, tzname)
  348. if dt is None:
  349. return None
  350. if lookup_type == 'year':
  351. return "%i-01-01 00:00:00" % dt.year
  352. elif lookup_type == 'quarter':
  353. month_in_quarter = dt.month - (dt.month - 1) % 3
  354. return '%i-%02i-01 00:00:00' % (dt.year, month_in_quarter)
  355. elif lookup_type == 'month':
  356. return "%i-%02i-01 00:00:00" % (dt.year, dt.month)
  357. elif lookup_type == 'week':
  358. dt = dt - datetime.timedelta(days=dt.weekday())
  359. return "%i-%02i-%02i 00:00:00" % (dt.year, dt.month, dt.day)
  360. elif lookup_type == 'day':
  361. return "%i-%02i-%02i 00:00:00" % (dt.year, dt.month, dt.day)
  362. elif lookup_type == 'hour':
  363. return "%i-%02i-%02i %02i:00:00" % (dt.year, dt.month, dt.day, dt.hour)
  364. elif lookup_type == 'minute':
  365. return "%i-%02i-%02i %02i:%02i:00" % (dt.year, dt.month, dt.day, dt.hour, dt.minute)
  366. elif lookup_type == 'second':
  367. return "%i-%02i-%02i %02i:%02i:%02i" % (dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second)
  368. def _sqlite_time_extract(lookup_type, dt):
  369. if dt is None:
  370. return None
  371. try:
  372. dt = backend_utils.typecast_time(dt)
  373. except (ValueError, TypeError):
  374. return None
  375. return getattr(dt, lookup_type)
  376. def _sqlite_format_dtdelta(conn, lhs, rhs):
  377. """
  378. LHS and RHS can be either:
  379. - An integer number of microseconds
  380. - A string representing a datetime
  381. """
  382. try:
  383. real_lhs = datetime.timedelta(0, 0, lhs) if isinstance(lhs, int) else backend_utils.typecast_timestamp(lhs)
  384. real_rhs = datetime.timedelta(0, 0, rhs) if isinstance(rhs, int) else backend_utils.typecast_timestamp(rhs)
  385. if conn.strip() == '+':
  386. out = real_lhs + real_rhs
  387. else:
  388. out = real_lhs - real_rhs
  389. except (ValueError, TypeError):
  390. return None
  391. # typecast_timestamp returns a date or a datetime without timezone.
  392. # It will be formatted as "%Y-%m-%d" or "%Y-%m-%d %H:%M:%S[.%f]"
  393. return str(out)
  394. def _sqlite_time_diff(lhs, rhs):
  395. left = backend_utils.typecast_time(lhs)
  396. right = backend_utils.typecast_time(rhs)
  397. return (
  398. (left.hour * 60 * 60 * 1000000) +
  399. (left.minute * 60 * 1000000) +
  400. (left.second * 1000000) +
  401. (left.microsecond) -
  402. (right.hour * 60 * 60 * 1000000) -
  403. (right.minute * 60 * 1000000) -
  404. (right.second * 1000000) -
  405. (right.microsecond)
  406. )
  407. def _sqlite_timestamp_diff(lhs, rhs):
  408. left = backend_utils.typecast_timestamp(lhs)
  409. right = backend_utils.typecast_timestamp(rhs)
  410. return duration_microseconds(left - right)
  411. def _sqlite_regexp(re_pattern, re_string):
  412. return bool(re.search(re_pattern, str(re_string))) if re_string is not None else False
  413. def _sqlite_lpad(text, length, fill_text):
  414. if len(text) >= length:
  415. return text[:length]
  416. return (fill_text * length)[:length - len(text)] + text
  417. def _sqlite_rpad(text, length, fill_text):
  418. return (text + fill_text * length)[:length]
  419. def _sqlite_power(x, y):
  420. return x ** y