base.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. """
  2. SQLite backend for the sqlite3 module in the standard library.
  3. """
  4. import datetime
  5. import decimal
  6. import functools
  7. import hashlib
  8. import math
  9. import operator
  10. import random
  11. import re
  12. import statistics
  13. import warnings
  14. from itertools import chain
  15. from sqlite3 import dbapi2 as Database
  16. import pytz
  17. from django.core.exceptions import ImproperlyConfigured
  18. from django.db import IntegrityError
  19. from django.db.backends import utils as backend_utils
  20. from django.db.backends.base.base import BaseDatabaseWrapper
  21. from django.utils import timezone
  22. from django.utils.asyncio import async_unsafe
  23. from django.utils.dateparse import parse_datetime, parse_time
  24. from django.utils.duration import duration_microseconds
  25. from django.utils.regex_helper import _lazy_re_compile
  26. from django.utils.version import PY38
  27. from .client import DatabaseClient
  28. from .creation import DatabaseCreation
  29. from .features import DatabaseFeatures
  30. from .introspection import DatabaseIntrospection
  31. from .operations import DatabaseOperations
  32. from .schema import DatabaseSchemaEditor
  33. def decoder(conv_func):
  34. """
  35. Convert bytestrings from Python's sqlite3 interface to a regular string.
  36. """
  37. return lambda s: conv_func(s.decode())
  38. def none_guard(func):
  39. """
  40. Decorator that returns None if any of the arguments to the decorated
  41. function are None. Many SQL functions return NULL if any of their arguments
  42. are NULL. This decorator simplifies the implementation of this for the
  43. custom functions registered below.
  44. """
  45. @functools.wraps(func)
  46. def wrapper(*args, **kwargs):
  47. return None if None in args else func(*args, **kwargs)
  48. return wrapper
  49. def list_aggregate(function):
  50. """
  51. Return an aggregate class that accumulates values in a list and applies
  52. the provided function to the data.
  53. """
  54. return type('ListAggregate', (list,), {'finalize': function, 'step': list.append})
  55. def check_sqlite_version():
  56. if Database.sqlite_version_info < (3, 9, 0):
  57. raise ImproperlyConfigured(
  58. 'SQLite 3.9.0 or later is required (found %s).' % Database.sqlite_version
  59. )
  60. check_sqlite_version()
  61. Database.register_converter("bool", b'1'.__eq__)
  62. Database.register_converter("time", decoder(parse_time))
  63. Database.register_converter("datetime", decoder(parse_datetime))
  64. Database.register_converter("timestamp", decoder(parse_datetime))
  65. Database.register_adapter(decimal.Decimal, str)
  66. class DatabaseWrapper(BaseDatabaseWrapper):
  67. vendor = 'sqlite'
  68. display_name = 'SQLite'
  69. # SQLite doesn't actually support most of these types, but it "does the right
  70. # thing" given more verbose field definitions, so leave them as is so that
  71. # schema inspection is more useful.
  72. data_types = {
  73. 'AutoField': 'integer',
  74. 'BigAutoField': 'integer',
  75. 'BinaryField': 'BLOB',
  76. 'BooleanField': 'bool',
  77. 'CharField': 'varchar(%(max_length)s)',
  78. 'DateField': 'date',
  79. 'DateTimeField': 'datetime',
  80. 'DecimalField': 'decimal',
  81. 'DurationField': 'bigint',
  82. 'FileField': 'varchar(%(max_length)s)',
  83. 'FilePathField': 'varchar(%(max_length)s)',
  84. 'FloatField': 'real',
  85. 'IntegerField': 'integer',
  86. 'BigIntegerField': 'bigint',
  87. 'IPAddressField': 'char(15)',
  88. 'GenericIPAddressField': 'char(39)',
  89. 'JSONField': 'text',
  90. 'NullBooleanField': 'bool',
  91. 'OneToOneField': 'integer',
  92. 'PositiveBigIntegerField': 'bigint unsigned',
  93. 'PositiveIntegerField': 'integer unsigned',
  94. 'PositiveSmallIntegerField': 'smallint unsigned',
  95. 'SlugField': 'varchar(%(max_length)s)',
  96. 'SmallAutoField': 'integer',
  97. 'SmallIntegerField': 'smallint',
  98. 'TextField': 'text',
  99. 'TimeField': 'time',
  100. 'UUIDField': 'char(32)',
  101. }
  102. data_type_check_constraints = {
  103. 'PositiveBigIntegerField': '"%(column)s" >= 0',
  104. 'JSONField': '(JSON_VALID("%(column)s") OR "%(column)s" IS NULL)',
  105. 'PositiveIntegerField': '"%(column)s" >= 0',
  106. 'PositiveSmallIntegerField': '"%(column)s" >= 0',
  107. }
  108. data_types_suffix = {
  109. 'AutoField': 'AUTOINCREMENT',
  110. 'BigAutoField': 'AUTOINCREMENT',
  111. 'SmallAutoField': 'AUTOINCREMENT',
  112. }
  113. # SQLite requires LIKE statements to include an ESCAPE clause if the value
  114. # being escaped has a percent or underscore in it.
  115. # See https://www.sqlite.org/lang_expr.html for an explanation.
  116. operators = {
  117. 'exact': '= %s',
  118. 'iexact': "LIKE %s ESCAPE '\\'",
  119. 'contains': "LIKE %s ESCAPE '\\'",
  120. 'icontains': "LIKE %s ESCAPE '\\'",
  121. 'regex': 'REGEXP %s',
  122. 'iregex': "REGEXP '(?i)' || %s",
  123. 'gt': '> %s',
  124. 'gte': '>= %s',
  125. 'lt': '< %s',
  126. 'lte': '<= %s',
  127. 'startswith': "LIKE %s ESCAPE '\\'",
  128. 'endswith': "LIKE %s ESCAPE '\\'",
  129. 'istartswith': "LIKE %s ESCAPE '\\'",
  130. 'iendswith': "LIKE %s ESCAPE '\\'",
  131. }
  132. # The patterns below are used to generate SQL pattern lookup clauses when
  133. # the right-hand side of the lookup isn't a raw string (it might be an expression
  134. # or the result of a bilateral transformation).
  135. # In those cases, special characters for LIKE operators (e.g. \, *, _) should be
  136. # escaped on database side.
  137. #
  138. # Note: we use str.format() here for readability as '%' is used as a wildcard for
  139. # the LIKE operator.
  140. pattern_esc = r"REPLACE(REPLACE(REPLACE({}, '\', '\\'), '%%', '\%%'), '_', '\_')"
  141. pattern_ops = {
  142. 'contains': r"LIKE '%%' || {} || '%%' ESCAPE '\'",
  143. 'icontains': r"LIKE '%%' || UPPER({}) || '%%' ESCAPE '\'",
  144. 'startswith': r"LIKE {} || '%%' ESCAPE '\'",
  145. 'istartswith': r"LIKE UPPER({}) || '%%' ESCAPE '\'",
  146. 'endswith': r"LIKE '%%' || {} ESCAPE '\'",
  147. 'iendswith': r"LIKE '%%' || UPPER({}) ESCAPE '\'",
  148. }
  149. Database = Database
  150. SchemaEditorClass = DatabaseSchemaEditor
  151. # Classes instantiated in __init__().
  152. client_class = DatabaseClient
  153. creation_class = DatabaseCreation
  154. features_class = DatabaseFeatures
  155. introspection_class = DatabaseIntrospection
  156. ops_class = DatabaseOperations
  157. def get_connection_params(self):
  158. settings_dict = self.settings_dict
  159. if not settings_dict['NAME']:
  160. raise ImproperlyConfigured(
  161. "settings.DATABASES is improperly configured. "
  162. "Please supply the NAME value.")
  163. kwargs = {
  164. # TODO: Remove str() when dropping support for PY36.
  165. # https://bugs.python.org/issue33496
  166. 'database': str(settings_dict['NAME']),
  167. 'detect_types': Database.PARSE_DECLTYPES | Database.PARSE_COLNAMES,
  168. **settings_dict['OPTIONS'],
  169. }
  170. # Always allow the underlying SQLite connection to be shareable
  171. # between multiple threads. The safe-guarding will be handled at a
  172. # higher level by the `BaseDatabaseWrapper.allow_thread_sharing`
  173. # property. This is necessary as the shareability is disabled by
  174. # default in pysqlite and it cannot be changed once a connection is
  175. # opened.
  176. if 'check_same_thread' in kwargs and kwargs['check_same_thread']:
  177. warnings.warn(
  178. 'The `check_same_thread` option was provided and set to '
  179. 'True. It will be overridden with False. Use the '
  180. '`DatabaseWrapper.allow_thread_sharing` property instead '
  181. 'for controlling thread shareability.',
  182. RuntimeWarning
  183. )
  184. kwargs.update({'check_same_thread': False, 'uri': True})
  185. return kwargs
  186. @async_unsafe
  187. def get_new_connection(self, conn_params):
  188. conn = Database.connect(**conn_params)
  189. if PY38:
  190. create_deterministic_function = functools.partial(
  191. conn.create_function,
  192. deterministic=True,
  193. )
  194. else:
  195. create_deterministic_function = conn.create_function
  196. create_deterministic_function('django_date_extract', 2, _sqlite_datetime_extract)
  197. create_deterministic_function('django_date_trunc', 4, _sqlite_date_trunc)
  198. create_deterministic_function('django_datetime_cast_date', 3, _sqlite_datetime_cast_date)
  199. create_deterministic_function('django_datetime_cast_time', 3, _sqlite_datetime_cast_time)
  200. create_deterministic_function('django_datetime_extract', 4, _sqlite_datetime_extract)
  201. create_deterministic_function('django_datetime_trunc', 4, _sqlite_datetime_trunc)
  202. create_deterministic_function('django_time_extract', 2, _sqlite_time_extract)
  203. create_deterministic_function('django_time_trunc', 4, _sqlite_time_trunc)
  204. create_deterministic_function('django_time_diff', 2, _sqlite_time_diff)
  205. create_deterministic_function('django_timestamp_diff', 2, _sqlite_timestamp_diff)
  206. create_deterministic_function('django_format_dtdelta', 3, _sqlite_format_dtdelta)
  207. create_deterministic_function('regexp', 2, _sqlite_regexp)
  208. create_deterministic_function('ACOS', 1, none_guard(math.acos))
  209. create_deterministic_function('ASIN', 1, none_guard(math.asin))
  210. create_deterministic_function('ATAN', 1, none_guard(math.atan))
  211. create_deterministic_function('ATAN2', 2, none_guard(math.atan2))
  212. create_deterministic_function('BITXOR', 2, none_guard(operator.xor))
  213. create_deterministic_function('CEILING', 1, none_guard(math.ceil))
  214. create_deterministic_function('COS', 1, none_guard(math.cos))
  215. create_deterministic_function('COT', 1, none_guard(lambda x: 1 / math.tan(x)))
  216. create_deterministic_function('DEGREES', 1, none_guard(math.degrees))
  217. create_deterministic_function('EXP', 1, none_guard(math.exp))
  218. create_deterministic_function('FLOOR', 1, none_guard(math.floor))
  219. create_deterministic_function('LN', 1, none_guard(math.log))
  220. create_deterministic_function('LOG', 2, none_guard(lambda x, y: math.log(y, x)))
  221. create_deterministic_function('LPAD', 3, _sqlite_lpad)
  222. create_deterministic_function('MD5', 1, none_guard(lambda x: hashlib.md5(x.encode()).hexdigest()))
  223. create_deterministic_function('MOD', 2, none_guard(math.fmod))
  224. create_deterministic_function('PI', 0, lambda: math.pi)
  225. create_deterministic_function('POWER', 2, none_guard(operator.pow))
  226. create_deterministic_function('RADIANS', 1, none_guard(math.radians))
  227. create_deterministic_function('REPEAT', 2, none_guard(operator.mul))
  228. create_deterministic_function('REVERSE', 1, none_guard(lambda x: x[::-1]))
  229. create_deterministic_function('RPAD', 3, _sqlite_rpad)
  230. create_deterministic_function('SHA1', 1, none_guard(lambda x: hashlib.sha1(x.encode()).hexdigest()))
  231. create_deterministic_function('SHA224', 1, none_guard(lambda x: hashlib.sha224(x.encode()).hexdigest()))
  232. create_deterministic_function('SHA256', 1, none_guard(lambda x: hashlib.sha256(x.encode()).hexdigest()))
  233. create_deterministic_function('SHA384', 1, none_guard(lambda x: hashlib.sha384(x.encode()).hexdigest()))
  234. create_deterministic_function('SHA512', 1, none_guard(lambda x: hashlib.sha512(x.encode()).hexdigest()))
  235. create_deterministic_function('SIGN', 1, none_guard(lambda x: (x > 0) - (x < 0)))
  236. create_deterministic_function('SIN', 1, none_guard(math.sin))
  237. create_deterministic_function('SQRT', 1, none_guard(math.sqrt))
  238. create_deterministic_function('TAN', 1, none_guard(math.tan))
  239. # Don't use the built-in RANDOM() function because it returns a value
  240. # in the range [2^63, 2^63 - 1] instead of [0, 1).
  241. conn.create_function('RAND', 0, random.random)
  242. conn.create_aggregate('STDDEV_POP', 1, list_aggregate(statistics.pstdev))
  243. conn.create_aggregate('STDDEV_SAMP', 1, list_aggregate(statistics.stdev))
  244. conn.create_aggregate('VAR_POP', 1, list_aggregate(statistics.pvariance))
  245. conn.create_aggregate('VAR_SAMP', 1, list_aggregate(statistics.variance))
  246. conn.execute('PRAGMA foreign_keys = ON')
  247. return conn
  248. def init_connection_state(self):
  249. pass
  250. def create_cursor(self, name=None):
  251. return self.connection.cursor(factory=SQLiteCursorWrapper)
  252. @async_unsafe
  253. def close(self):
  254. self.validate_thread_sharing()
  255. # If database is in memory, closing the connection destroys the
  256. # database. To prevent accidental data loss, ignore close requests on
  257. # an in-memory db.
  258. if not self.is_in_memory_db():
  259. BaseDatabaseWrapper.close(self)
  260. def _savepoint_allowed(self):
  261. # When 'isolation_level' is not None, sqlite3 commits before each
  262. # savepoint; it's a bug. When it is None, savepoints don't make sense
  263. # because autocommit is enabled. The only exception is inside 'atomic'
  264. # blocks. To work around that bug, on SQLite, 'atomic' starts a
  265. # transaction explicitly rather than simply disable autocommit.
  266. return self.in_atomic_block
  267. def _set_autocommit(self, autocommit):
  268. if autocommit:
  269. level = None
  270. else:
  271. # sqlite3's internal default is ''. It's different from None.
  272. # See Modules/_sqlite/connection.c.
  273. level = ''
  274. # 'isolation_level' is a misleading API.
  275. # SQLite always runs at the SERIALIZABLE isolation level.
  276. with self.wrap_database_errors:
  277. self.connection.isolation_level = level
  278. def disable_constraint_checking(self):
  279. with self.cursor() as cursor:
  280. cursor.execute('PRAGMA foreign_keys = OFF')
  281. # Foreign key constraints cannot be turned off while in a multi-
  282. # statement transaction. Fetch the current state of the pragma
  283. # to determine if constraints are effectively disabled.
  284. enabled = cursor.execute('PRAGMA foreign_keys').fetchone()[0]
  285. return not bool(enabled)
  286. def enable_constraint_checking(self):
  287. with self.cursor() as cursor:
  288. cursor.execute('PRAGMA foreign_keys = ON')
  289. def check_constraints(self, table_names=None):
  290. """
  291. Check each table name in `table_names` for rows with invalid foreign
  292. key references. This method is intended to be used in conjunction with
  293. `disable_constraint_checking()` and `enable_constraint_checking()`, to
  294. determine if rows with invalid references were entered while constraint
  295. checks were off.
  296. """
  297. if self.features.supports_pragma_foreign_key_check:
  298. with self.cursor() as cursor:
  299. if table_names is None:
  300. violations = cursor.execute('PRAGMA foreign_key_check').fetchall()
  301. else:
  302. violations = chain.from_iterable(
  303. cursor.execute(
  304. 'PRAGMA foreign_key_check(%s)'
  305. % self.ops.quote_name(table_name)
  306. ).fetchall()
  307. for table_name in table_names
  308. )
  309. # See https://www.sqlite.org/pragma.html#pragma_foreign_key_check
  310. for table_name, rowid, referenced_table_name, foreign_key_index in violations:
  311. foreign_key = cursor.execute(
  312. 'PRAGMA foreign_key_list(%s)' % self.ops.quote_name(table_name)
  313. ).fetchall()[foreign_key_index]
  314. column_name, referenced_column_name = foreign_key[3:5]
  315. primary_key_column_name = self.introspection.get_primary_key_column(cursor, table_name)
  316. primary_key_value, bad_value = cursor.execute(
  317. 'SELECT %s, %s FROM %s WHERE rowid = %%s' % (
  318. self.ops.quote_name(primary_key_column_name),
  319. self.ops.quote_name(column_name),
  320. self.ops.quote_name(table_name),
  321. ),
  322. (rowid,),
  323. ).fetchone()
  324. raise IntegrityError(
  325. "The row in table '%s' with primary key '%s' has an "
  326. "invalid foreign key: %s.%s contains a value '%s' that "
  327. "does not have a corresponding value in %s.%s." % (
  328. table_name, primary_key_value, table_name, column_name,
  329. bad_value, referenced_table_name, referenced_column_name
  330. )
  331. )
  332. else:
  333. with self.cursor() as cursor:
  334. if table_names is None:
  335. table_names = self.introspection.table_names(cursor)
  336. for table_name in table_names:
  337. primary_key_column_name = self.introspection.get_primary_key_column(cursor, table_name)
  338. if not primary_key_column_name:
  339. continue
  340. key_columns = self.introspection.get_key_columns(cursor, table_name)
  341. for column_name, referenced_table_name, referenced_column_name in key_columns:
  342. cursor.execute(
  343. """
  344. SELECT REFERRING.`%s`, REFERRING.`%s` FROM `%s` as REFERRING
  345. LEFT JOIN `%s` as REFERRED
  346. ON (REFERRING.`%s` = REFERRED.`%s`)
  347. WHERE REFERRING.`%s` IS NOT NULL AND REFERRED.`%s` IS NULL
  348. """
  349. % (
  350. primary_key_column_name, column_name, table_name,
  351. referenced_table_name, column_name, referenced_column_name,
  352. column_name, referenced_column_name,
  353. )
  354. )
  355. for bad_row in cursor.fetchall():
  356. raise IntegrityError(
  357. "The row in table '%s' with primary key '%s' has an "
  358. "invalid foreign key: %s.%s contains a value '%s' that "
  359. "does not have a corresponding value in %s.%s." % (
  360. table_name, bad_row[0], table_name, column_name,
  361. bad_row[1], referenced_table_name, referenced_column_name,
  362. )
  363. )
  364. def is_usable(self):
  365. return True
  366. def _start_transaction_under_autocommit(self):
  367. """
  368. Start a transaction explicitly in autocommit mode.
  369. Staying in autocommit mode works around a bug of sqlite3 that breaks
  370. savepoints when autocommit is disabled.
  371. """
  372. self.cursor().execute("BEGIN")
  373. def is_in_memory_db(self):
  374. return self.creation.is_in_memory_db(self.settings_dict['NAME'])
  375. FORMAT_QMARK_REGEX = _lazy_re_compile(r'(?<!%)%s')
  376. class SQLiteCursorWrapper(Database.Cursor):
  377. """
  378. Django uses "format" style placeholders, but pysqlite2 uses "qmark" style.
  379. This fixes it -- but note that if you want to use a literal "%s" in a query,
  380. you'll need to use "%%s".
  381. """
  382. def execute(self, query, params=None):
  383. if params is None:
  384. return Database.Cursor.execute(self, query)
  385. query = self.convert_query(query)
  386. return Database.Cursor.execute(self, query, params)
  387. def executemany(self, query, param_list):
  388. query = self.convert_query(query)
  389. return Database.Cursor.executemany(self, query, param_list)
  390. def convert_query(self, query):
  391. return FORMAT_QMARK_REGEX.sub('?', query).replace('%%', '%')
  392. def _sqlite_datetime_parse(dt, tzname=None, conn_tzname=None):
  393. if dt is None:
  394. return None
  395. try:
  396. dt = backend_utils.typecast_timestamp(dt)
  397. except (TypeError, ValueError):
  398. return None
  399. if conn_tzname:
  400. dt = dt.replace(tzinfo=pytz.timezone(conn_tzname))
  401. if tzname is not None and tzname != conn_tzname:
  402. sign_index = tzname.find('+') + tzname.find('-') + 1
  403. if sign_index > -1:
  404. sign = tzname[sign_index]
  405. tzname, offset = tzname.split(sign)
  406. if offset:
  407. hours, minutes = offset.split(':')
  408. offset_delta = datetime.timedelta(hours=int(hours), minutes=int(minutes))
  409. dt += offset_delta if sign == '+' else -offset_delta
  410. dt = timezone.localtime(dt, pytz.timezone(tzname))
  411. return dt
  412. def _sqlite_date_trunc(lookup_type, dt, tzname, conn_tzname):
  413. dt = _sqlite_datetime_parse(dt, tzname, conn_tzname)
  414. if dt is None:
  415. return None
  416. if lookup_type == 'year':
  417. return "%i-01-01" % dt.year
  418. elif lookup_type == 'quarter':
  419. month_in_quarter = dt.month - (dt.month - 1) % 3
  420. return '%i-%02i-01' % (dt.year, month_in_quarter)
  421. elif lookup_type == 'month':
  422. return "%i-%02i-01" % (dt.year, dt.month)
  423. elif lookup_type == 'week':
  424. dt = dt - datetime.timedelta(days=dt.weekday())
  425. return "%i-%02i-%02i" % (dt.year, dt.month, dt.day)
  426. elif lookup_type == 'day':
  427. return "%i-%02i-%02i" % (dt.year, dt.month, dt.day)
  428. def _sqlite_time_trunc(lookup_type, dt, tzname, conn_tzname):
  429. if dt is None:
  430. return None
  431. dt_parsed = _sqlite_datetime_parse(dt, tzname, conn_tzname)
  432. if dt_parsed is None:
  433. try:
  434. dt = backend_utils.typecast_time(dt)
  435. except (ValueError, TypeError):
  436. return None
  437. else:
  438. dt = dt_parsed
  439. if lookup_type == 'hour':
  440. return "%02i:00:00" % dt.hour
  441. elif lookup_type == 'minute':
  442. return "%02i:%02i:00" % (dt.hour, dt.minute)
  443. elif lookup_type == 'second':
  444. return "%02i:%02i:%02i" % (dt.hour, dt.minute, dt.second)
  445. def _sqlite_datetime_cast_date(dt, tzname, conn_tzname):
  446. dt = _sqlite_datetime_parse(dt, tzname, conn_tzname)
  447. if dt is None:
  448. return None
  449. return dt.date().isoformat()
  450. def _sqlite_datetime_cast_time(dt, tzname, conn_tzname):
  451. dt = _sqlite_datetime_parse(dt, tzname, conn_tzname)
  452. if dt is None:
  453. return None
  454. return dt.time().isoformat()
  455. def _sqlite_datetime_extract(lookup_type, dt, tzname=None, conn_tzname=None):
  456. dt = _sqlite_datetime_parse(dt, tzname, conn_tzname)
  457. if dt is None:
  458. return None
  459. if lookup_type == 'week_day':
  460. return (dt.isoweekday() % 7) + 1
  461. elif lookup_type == 'iso_week_day':
  462. return dt.isoweekday()
  463. elif lookup_type == 'week':
  464. return dt.isocalendar()[1]
  465. elif lookup_type == 'quarter':
  466. return math.ceil(dt.month / 3)
  467. elif lookup_type == 'iso_year':
  468. return dt.isocalendar()[0]
  469. else:
  470. return getattr(dt, lookup_type)
  471. def _sqlite_datetime_trunc(lookup_type, dt, tzname, conn_tzname):
  472. dt = _sqlite_datetime_parse(dt, tzname, conn_tzname)
  473. if dt is None:
  474. return None
  475. if lookup_type == 'year':
  476. return "%i-01-01 00:00:00" % dt.year
  477. elif lookup_type == 'quarter':
  478. month_in_quarter = dt.month - (dt.month - 1) % 3
  479. return '%i-%02i-01 00:00:00' % (dt.year, month_in_quarter)
  480. elif lookup_type == 'month':
  481. return "%i-%02i-01 00:00:00" % (dt.year, dt.month)
  482. elif lookup_type == 'week':
  483. dt = dt - datetime.timedelta(days=dt.weekday())
  484. return "%i-%02i-%02i 00:00:00" % (dt.year, dt.month, dt.day)
  485. elif lookup_type == 'day':
  486. return "%i-%02i-%02i 00:00:00" % (dt.year, dt.month, dt.day)
  487. elif lookup_type == 'hour':
  488. return "%i-%02i-%02i %02i:00:00" % (dt.year, dt.month, dt.day, dt.hour)
  489. elif lookup_type == 'minute':
  490. return "%i-%02i-%02i %02i:%02i:00" % (dt.year, dt.month, dt.day, dt.hour, dt.minute)
  491. elif lookup_type == 'second':
  492. return "%i-%02i-%02i %02i:%02i:%02i" % (dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second)
  493. def _sqlite_time_extract(lookup_type, dt):
  494. if dt is None:
  495. return None
  496. try:
  497. dt = backend_utils.typecast_time(dt)
  498. except (ValueError, TypeError):
  499. return None
  500. return getattr(dt, lookup_type)
  501. @none_guard
  502. def _sqlite_format_dtdelta(conn, lhs, rhs):
  503. """
  504. LHS and RHS can be either:
  505. - An integer number of microseconds
  506. - A string representing a datetime
  507. """
  508. try:
  509. real_lhs = datetime.timedelta(0, 0, lhs) if isinstance(lhs, int) else backend_utils.typecast_timestamp(lhs)
  510. real_rhs = datetime.timedelta(0, 0, rhs) if isinstance(rhs, int) else backend_utils.typecast_timestamp(rhs)
  511. if conn.strip() == '+':
  512. out = real_lhs + real_rhs
  513. else:
  514. out = real_lhs - real_rhs
  515. except (ValueError, TypeError):
  516. return None
  517. # typecast_timestamp returns a date or a datetime without timezone.
  518. # It will be formatted as "%Y-%m-%d" or "%Y-%m-%d %H:%M:%S[.%f]"
  519. return str(out)
  520. @none_guard
  521. def _sqlite_time_diff(lhs, rhs):
  522. left = backend_utils.typecast_time(lhs)
  523. right = backend_utils.typecast_time(rhs)
  524. return (
  525. (left.hour * 60 * 60 * 1000000) +
  526. (left.minute * 60 * 1000000) +
  527. (left.second * 1000000) +
  528. (left.microsecond) -
  529. (right.hour * 60 * 60 * 1000000) -
  530. (right.minute * 60 * 1000000) -
  531. (right.second * 1000000) -
  532. (right.microsecond)
  533. )
  534. @none_guard
  535. def _sqlite_timestamp_diff(lhs, rhs):
  536. left = backend_utils.typecast_timestamp(lhs)
  537. right = backend_utils.typecast_timestamp(rhs)
  538. return duration_microseconds(left - right)
  539. @none_guard
  540. def _sqlite_regexp(re_pattern, re_string):
  541. return bool(re.search(re_pattern, str(re_string)))
  542. @none_guard
  543. def _sqlite_lpad(text, length, fill_text):
  544. if len(text) >= length:
  545. return text[:length]
  546. return (fill_text * length)[:length - len(text)] + text
  547. @none_guard
  548. def _sqlite_rpad(text, length, fill_text):
  549. return (text + fill_text * length)[:length]