2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2007-09-03 04:04:44 +08:00
|
|
|
SQLite3 backend for django.
|
|
|
|
|
2011-03-28 10:27:05 +08:00
|
|
|
Works with either the pysqlite2 module or the sqlite3 module in the
|
2009-03-20 11:57:21 +08:00
|
|
|
standard library.
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2011-07-13 17:35:51 +08:00
|
|
|
import datetime
|
|
|
|
import decimal
|
2010-03-23 21:51:11 +08:00
|
|
|
import re
|
2014-12-04 07:17:59 +08:00
|
|
|
import sys
|
2014-07-15 17:35:29 +08:00
|
|
|
import uuid
|
|
|
|
import warnings
|
2010-01-29 23:45:55 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
from django.conf import settings
|
2010-01-29 23:45:55 +08:00
|
|
|
from django.db import utils
|
2013-09-17 00:52:05 +08:00
|
|
|
from django.db.backends import (utils as backend_utils, BaseDatabaseFeatures,
|
2013-07-08 08:39:54 +08:00
|
|
|
BaseDatabaseOperations, BaseDatabaseWrapper, BaseDatabaseValidation)
|
2008-08-11 20:11:25 +08:00
|
|
|
from django.db.backends.sqlite3.client import DatabaseClient
|
|
|
|
from django.db.backends.sqlite3.creation import DatabaseCreation
|
|
|
|
from django.db.backends.sqlite3.introspection import DatabaseIntrospection
|
2012-08-18 21:16:52 +08:00
|
|
|
from django.db.backends.sqlite3.schema import DatabaseSchemaEditor
|
2013-12-25 21:13:18 +08:00
|
|
|
from django.db.models import fields, aggregates
|
2014-07-24 20:57:24 +08:00
|
|
|
from django.utils.dateparse import parse_date, parse_datetime, parse_time, parse_duration
|
|
|
|
from django.utils.duration import duration_string
|
2013-06-26 22:30:58 +08:00
|
|
|
from django.utils.encoding import force_text
|
2012-06-09 21:59:52 +08:00
|
|
|
from django.utils.functional import cached_property
|
2012-08-18 22:04:06 +08:00
|
|
|
from django.utils.safestring import SafeBytes
|
2012-07-20 18:18:38 +08:00
|
|
|
from django.utils import six
|
2012-02-28 05:15:25 +08:00
|
|
|
from django.utils import timezone
|
2008-08-11 20:11:25 +08:00
|
|
|
|
2006-05-27 02:58:46 +08:00
|
|
|
try:
|
2006-09-25 09:53:34 +08:00
|
|
|
try:
|
|
|
|
from pysqlite2 import dbapi2 as Database
|
2012-04-29 00:09:37 +08:00
|
|
|
except ImportError:
|
2009-03-20 11:57:21 +08:00
|
|
|
from sqlite3 import dbapi2 as Database
|
2012-04-29 00:09:37 +08:00
|
|
|
except ImportError as exc:
|
2006-05-27 02:58:46 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2011-03-28 10:27:05 +08:00
|
|
|
raise ImproperlyConfigured("Error loading either pysqlite2 or sqlite3 modules (tried in that order): %s" % exc)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-02-10 23:15:49 +08:00
|
|
|
try:
|
|
|
|
import pytz
|
|
|
|
except ImportError:
|
|
|
|
pytz = None
|
2007-05-21 09:29:58 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
DatabaseError = Database.DatabaseError
|
2007-04-25 18:18:56 +08:00
|
|
|
IntegrityError = Database.IntegrityError
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2011-11-18 21:01:06 +08:00
|
|
|
def parse_datetime_with_timezone_support(value):
|
|
|
|
dt = parse_datetime(value)
|
|
|
|
# Confirm that dt is naive before overwriting its tzinfo.
|
2012-02-28 05:15:25 +08:00
|
|
|
if dt is not None and settings.USE_TZ and timezone.is_naive(dt):
|
|
|
|
dt = dt.replace(tzinfo=timezone.utc)
|
2011-11-18 21:01:06 +08:00
|
|
|
return dt
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2012-02-28 05:15:25 +08:00
|
|
|
def adapt_datetime_with_timezone_support(value):
|
|
|
|
# Equivalent to DateTimeField.get_db_prep_value. Used only by raw SQL.
|
|
|
|
if settings.USE_TZ:
|
|
|
|
if timezone.is_naive(value):
|
2012-06-08 00:08:47 +08:00
|
|
|
warnings.warn("SQLite received a naive datetime (%s)"
|
|
|
|
" while time zone support is active." % value,
|
2012-02-28 05:15:25 +08:00
|
|
|
RuntimeWarning)
|
|
|
|
default_timezone = timezone.get_default_timezone()
|
|
|
|
value = timezone.make_aware(value, default_timezone)
|
|
|
|
value = value.astimezone(timezone.utc).replace(tzinfo=None)
|
2012-08-03 21:18:13 +08:00
|
|
|
return value.isoformat(str(" "))
|
2012-02-28 05:15:25 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2012-08-13 02:43:01 +08:00
|
|
|
def decoder(conv_func):
|
|
|
|
""" The Python sqlite3 interface returns always byte strings.
|
|
|
|
This function converts the received value to a regular string before
|
|
|
|
passing it to the receiver function.
|
|
|
|
"""
|
|
|
|
return lambda s: conv_func(s.decode('utf-8'))
|
|
|
|
|
|
|
|
Database.register_converter(str("bool"), decoder(lambda s: s == '1'))
|
|
|
|
Database.register_converter(str("time"), decoder(parse_time))
|
|
|
|
Database.register_converter(str("date"), decoder(parse_date))
|
|
|
|
Database.register_converter(str("datetime"), decoder(parse_datetime_with_timezone_support))
|
|
|
|
Database.register_converter(str("timestamp"), decoder(parse_datetime_with_timezone_support))
|
|
|
|
Database.register_converter(str("TIMESTAMP"), decoder(parse_datetime_with_timezone_support))
|
2013-09-17 00:52:05 +08:00
|
|
|
Database.register_converter(str("decimal"), decoder(backend_utils.typecast_decimal))
|
2012-08-13 02:43:01 +08:00
|
|
|
|
2012-02-28 05:15:25 +08:00
|
|
|
Database.register_adapter(datetime.datetime, adapt_datetime_with_timezone_support)
|
2013-09-17 00:52:05 +08:00
|
|
|
Database.register_adapter(decimal.Decimal, backend_utils.rev_typecast_decimal)
|
2013-09-02 18:06:32 +08:00
|
|
|
if six.PY2:
|
2013-08-29 14:35:17 +08:00
|
|
|
Database.register_adapter(str, lambda s: s.decode('utf-8'))
|
|
|
|
Database.register_adapter(SafeBytes, lambda s: s.decode('utf-8'))
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2007-08-20 10:20:33 +08:00
|
|
|
class DatabaseFeatures(BaseDatabaseFeatures):
|
2008-07-16 02:47:32 +08:00
|
|
|
# SQLite cannot handle us only partially reading from a cursor's result set
|
|
|
|
# and then writing the same rows to the database in another cursor. This
|
|
|
|
# setting ensures we always read result sets fully into memory all in one
|
|
|
|
# go.
|
|
|
|
can_use_chunked_reads = False
|
2010-10-11 20:55:17 +08:00
|
|
|
test_db_allows_multiple_connections = False
|
|
|
|
supports_unspecified_pk = True
|
2011-11-18 21:01:06 +08:00
|
|
|
supports_timezones = False
|
2010-10-19 08:14:41 +08:00
|
|
|
supports_1000_query_parameters = False
|
2010-12-22 11:34:04 +08:00
|
|
|
supports_mixed_date_datetime_comparisons = False
|
2011-09-10 03:22:28 +08:00
|
|
|
has_bulk_insert = True
|
2012-04-29 09:22:05 +08:00
|
|
|
can_combine_inserts_with_and_without_auto_increment_pk = False
|
2012-09-08 00:51:11 +08:00
|
|
|
supports_foreign_keys = False
|
2014-06-17 07:25:13 +08:00
|
|
|
supports_column_check_constraints = False
|
2013-03-05 05:17:35 +08:00
|
|
|
autocommits_when_autocommit_is_off = True
|
2014-06-06 06:29:24 +08:00
|
|
|
can_introspect_decimal_field = False
|
2014-05-08 04:14:39 +08:00
|
|
|
can_introspect_positive_integer_field = True
|
|
|
|
can_introspect_small_integer_field = True
|
2014-04-25 17:43:20 +08:00
|
|
|
supports_transactions = True
|
2013-09-23 04:14:17 +08:00
|
|
|
atomic_transactions = False
|
2014-04-25 17:43:20 +08:00
|
|
|
can_rollback_ddl = True
|
2013-06-28 11:15:03 +08:00
|
|
|
supports_paramstyle_pyformat = False
|
2013-09-07 00:18:16 +08:00
|
|
|
supports_sequence_reset = False
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2013-03-04 22:57:04 +08:00
|
|
|
@cached_property
|
|
|
|
def uses_savepoints(self):
|
|
|
|
return Database.sqlite_version_info >= (3, 6, 8)
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2014-07-28 20:30:41 +08:00
|
|
|
@cached_property
|
|
|
|
def can_release_savepoints(self):
|
|
|
|
return self.uses_savepoints
|
|
|
|
|
2014-12-04 07:17:59 +08:00
|
|
|
@cached_property
|
|
|
|
def can_share_in_memory_db(self):
|
|
|
|
return (
|
|
|
|
sys.version_info[:2] >= (3, 4) and
|
|
|
|
Database.__name__ == 'sqlite3.dbapi2' and
|
|
|
|
Database.sqlite_version_info >= (3, 7, 13)
|
|
|
|
)
|
|
|
|
|
2012-06-09 21:59:52 +08:00
|
|
|
@cached_property
|
|
|
|
def supports_stddev(self):
|
2010-10-11 20:55:17 +08:00
|
|
|
"""Confirm support for STDDEV and related stats functions
|
|
|
|
|
|
|
|
SQLite supports STDDEV as an extension package; so
|
|
|
|
connection.ops.check_aggregate_support() can't unilaterally
|
|
|
|
rule out support for STDDEV. We need to manually check
|
|
|
|
whether the call works.
|
|
|
|
"""
|
2014-01-09 23:05:15 +08:00
|
|
|
with self.connection.cursor() as cursor:
|
|
|
|
cursor.execute('CREATE TABLE STDDEV_TEST (X INT)')
|
|
|
|
try:
|
|
|
|
cursor.execute('SELECT STDDEV(*) FROM STDDEV_TEST')
|
|
|
|
has_support = True
|
|
|
|
except utils.DatabaseError:
|
|
|
|
has_support = False
|
|
|
|
cursor.execute('DROP TABLE STDDEV_TEST')
|
2010-10-11 20:55:17 +08:00
|
|
|
return has_support
|
2007-08-20 10:20:33 +08:00
|
|
|
|
2013-02-10 23:15:49 +08:00
|
|
|
@cached_property
|
|
|
|
def has_zoneinfo_database(self):
|
|
|
|
return pytz is not None
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2007-08-20 06:29:57 +08:00
|
|
|
class DatabaseOperations(BaseDatabaseOperations):
|
2012-04-29 09:22:05 +08:00
|
|
|
def bulk_batch_size(self, fields, objs):
|
|
|
|
"""
|
|
|
|
SQLite has a compile-time default (SQLITE_LIMIT_VARIABLE_NUMBER) of
|
|
|
|
999 variables per query.
|
2012-11-24 06:44:48 +08:00
|
|
|
|
|
|
|
If there is just single field to insert, then we can hit another
|
|
|
|
limit, SQLITE_MAX_COMPOUND_SELECT which defaults to 500.
|
2012-04-29 09:22:05 +08:00
|
|
|
"""
|
2012-11-24 06:44:48 +08:00
|
|
|
limit = 999 if len(fields) > 1 else 500
|
|
|
|
return (limit // len(fields)) if len(fields) > 0 else len(objs)
|
2012-04-29 09:22:05 +08:00
|
|
|
|
2013-01-11 13:57:54 +08:00
|
|
|
def check_aggregate_support(self, aggregate):
|
|
|
|
bad_fields = (fields.DateField, fields.DateTimeField, fields.TimeField)
|
|
|
|
bad_aggregates = (aggregates.Sum, aggregates.Avg,
|
|
|
|
aggregates.Variance, aggregates.StdDev)
|
2013-12-25 21:13:18 +08:00
|
|
|
if aggregate.refs_field(bad_aggregates, bad_fields):
|
2013-01-11 13:57:54 +08:00
|
|
|
raise NotImplementedError(
|
|
|
|
'You cannot use Sum, Avg, StdDev and Variance aggregations '
|
|
|
|
'on date/time fields in sqlite3 '
|
|
|
|
'since date/time is saved as text.')
|
|
|
|
|
2007-08-20 06:40:06 +08:00
|
|
|
def date_extract_sql(self, lookup_type, field_name):
|
|
|
|
# sqlite doesn't support extract, so we fake it with the user-defined
|
2013-02-10 23:15:49 +08:00
|
|
|
# function django_date_extract that's registered in connect(). Note that
|
2010-02-24 23:29:25 +08:00
|
|
|
# single quotes are used because this is a string (and could otherwise
|
|
|
|
# cause a collision with a field name).
|
2013-02-10 23:15:49 +08:00
|
|
|
return "django_date_extract('%s', %s)" % (lookup_type.lower(), field_name)
|
2007-08-20 06:29:57 +08:00
|
|
|
|
2014-07-24 20:57:24 +08:00
|
|
|
def date_interval_sql(self, timedelta):
|
|
|
|
return "'%s'" % duration_string(timedelta), []
|
|
|
|
|
|
|
|
def format_for_duration_arithmetic(self, sql):
|
|
|
|
"""Do nothing here, we will handle it in the custom function."""
|
|
|
|
return sql
|
2010-12-22 11:34:04 +08:00
|
|
|
|
2007-08-20 06:47:43 +08:00
|
|
|
def date_trunc_sql(self, lookup_type, field_name):
|
|
|
|
# sqlite doesn't support DATE_TRUNC, so we fake it with a user-defined
|
2010-02-24 23:29:25 +08:00
|
|
|
# function django_date_trunc that's registered in connect(). Note that
|
|
|
|
# single quotes are used because this is a string (and could otherwise
|
|
|
|
# cause a collision with a field name).
|
|
|
|
return "django_date_trunc('%s', %s)" % (lookup_type.lower(), field_name)
|
2007-08-20 06:47:43 +08:00
|
|
|
|
2013-02-10 23:15:49 +08:00
|
|
|
def datetime_extract_sql(self, lookup_type, field_name, tzname):
|
|
|
|
# Same comment as in date_extract_sql.
|
|
|
|
if settings.USE_TZ:
|
|
|
|
if pytz is None:
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
|
|
|
raise ImproperlyConfigured("This query requires pytz, "
|
|
|
|
"but it isn't installed.")
|
|
|
|
return "django_datetime_extract('%s', %s, %%s)" % (
|
|
|
|
lookup_type.lower(), field_name), [tzname]
|
|
|
|
|
|
|
|
def datetime_trunc_sql(self, lookup_type, field_name, tzname):
|
|
|
|
# Same comment as in date_trunc_sql.
|
|
|
|
if settings.USE_TZ:
|
|
|
|
if pytz is None:
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
|
|
|
raise ImproperlyConfigured("This query requires pytz, "
|
|
|
|
"but it isn't installed.")
|
|
|
|
return "django_datetime_trunc('%s', %s, %%s)" % (
|
|
|
|
lookup_type.lower(), field_name), [tzname]
|
|
|
|
|
2007-08-20 07:07:34 +08:00
|
|
|
def drop_foreignkey_sql(self):
|
|
|
|
return ""
|
|
|
|
|
2007-08-20 07:59:06 +08:00
|
|
|
def pk_default_value(self):
|
2011-09-10 03:22:28 +08:00
|
|
|
return "NULL"
|
2007-08-20 07:59:06 +08:00
|
|
|
|
2007-08-20 09:03:33 +08:00
|
|
|
def quote_name(self, name):
|
|
|
|
if name.startswith('"') and name.endswith('"'):
|
2013-11-03 05:02:56 +08:00
|
|
|
return name # Quoting once is enough.
|
2007-08-20 09:03:33 +08:00
|
|
|
return '"%s"' % name
|
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
def no_limit_value(self):
|
|
|
|
return -1
|
|
|
|
|
2013-06-10 03:04:36 +08:00
|
|
|
def sql_flush(self, style, tables, sequences, allow_cascade=False):
|
2007-08-20 08:15:53 +08:00
|
|
|
# NB: The generated SQL below is specific to SQLite
|
|
|
|
# Note: The DELETE FROM... SQL generated below works for SQLite databases
|
|
|
|
# because constraints don't exist
|
2013-06-10 03:04:36 +08:00
|
|
|
sql = ['%s %s %s;' % (
|
|
|
|
style.SQL_KEYWORD('DELETE'),
|
|
|
|
style.SQL_KEYWORD('FROM'),
|
|
|
|
style.SQL_FIELD(self.quote_name(table))
|
|
|
|
) for table in tables]
|
2007-08-20 08:15:53 +08:00
|
|
|
# Note: No requirement for reset of auto-incremented indices (cf. other
|
|
|
|
# sql_flush() implementations). Just return SQL at this point
|
|
|
|
return sql
|
|
|
|
|
2011-11-18 21:01:06 +08:00
|
|
|
def value_to_db_datetime(self, value):
|
|
|
|
if value is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
# SQLite doesn't support tz-aware datetimes
|
2012-02-28 05:15:25 +08:00
|
|
|
if timezone.is_aware(value):
|
2011-11-18 21:01:06 +08:00
|
|
|
if settings.USE_TZ:
|
2012-02-28 05:15:25 +08:00
|
|
|
value = value.astimezone(timezone.utc).replace(tzinfo=None)
|
2011-11-18 21:01:06 +08:00
|
|
|
else:
|
|
|
|
raise ValueError("SQLite backend does not support timezone-aware datetimes when USE_TZ is False.")
|
|
|
|
|
2012-07-20 20:48:51 +08:00
|
|
|
return six.text_type(value)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
|
|
def value_to_db_time(self, value):
|
|
|
|
if value is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
# SQLite doesn't support tz-aware datetimes
|
2012-02-28 05:15:25 +08:00
|
|
|
if timezone.is_aware(value):
|
2011-11-18 21:01:06 +08:00
|
|
|
raise ValueError("SQLite backend does not support timezone-aware times.")
|
|
|
|
|
2012-07-20 20:48:51 +08:00
|
|
|
return six.text_type(value)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
2014-08-12 20:08:40 +08:00
|
|
|
def get_db_converters(self, internal_type):
|
|
|
|
converters = super(DatabaseOperations, self).get_db_converters(internal_type)
|
|
|
|
if internal_type == 'DateTimeField':
|
|
|
|
converters.append(self.convert_datetimefield_value)
|
2009-01-15 19:06:34 +08:00
|
|
|
elif internal_type == 'DateField':
|
2014-08-12 20:08:40 +08:00
|
|
|
converters.append(self.convert_datefield_value)
|
2009-01-15 19:06:34 +08:00
|
|
|
elif internal_type == 'TimeField':
|
2014-08-12 20:08:40 +08:00
|
|
|
converters.append(self.convert_timefield_value)
|
|
|
|
elif internal_type == 'DecimalField':
|
|
|
|
converters.append(self.convert_decimalfield_value)
|
2014-07-15 17:35:29 +08:00
|
|
|
elif internal_type == 'UUIDField':
|
|
|
|
converters.append(self.convert_uuidfield_value)
|
2014-08-12 20:08:40 +08:00
|
|
|
return converters
|
|
|
|
|
|
|
|
def convert_decimalfield_value(self, value, field):
|
2014-12-01 14:11:23 +08:00
|
|
|
return backend_utils.typecast_decimal(field.format_number(value))
|
2014-08-12 20:08:40 +08:00
|
|
|
|
|
|
|
def convert_datefield_value(self, value, field):
|
|
|
|
if value is not None and not isinstance(value, datetime.date):
|
|
|
|
value = parse_date(value)
|
|
|
|
return value
|
|
|
|
|
|
|
|
def convert_datetimefield_value(self, value, field):
|
|
|
|
if value is not None and not isinstance(value, datetime.datetime):
|
|
|
|
value = parse_datetime_with_timezone_support(value)
|
|
|
|
return value
|
2009-01-15 19:06:34 +08:00
|
|
|
|
2014-08-12 20:08:40 +08:00
|
|
|
def convert_timefield_value(self, value, field):
|
|
|
|
if value is not None and not isinstance(value, datetime.time):
|
|
|
|
value = parse_time(value)
|
2009-01-15 19:06:34 +08:00
|
|
|
return value
|
|
|
|
|
2014-07-15 17:35:29 +08:00
|
|
|
def convert_uuidfield_value(self, value, field):
|
|
|
|
if value is not None:
|
|
|
|
value = uuid.UUID(value)
|
|
|
|
return value
|
|
|
|
|
2011-09-10 03:22:28 +08:00
|
|
|
def bulk_insert_sql(self, fields, num_values):
|
|
|
|
res = []
|
|
|
|
res.append("SELECT %s" % ", ".join(
|
|
|
|
"%%s AS %s" % self.quote_name(f.column) for f in fields
|
|
|
|
))
|
2012-11-24 06:28:20 +08:00
|
|
|
res.extend(["UNION ALL SELECT %s" % ", ".join(["%s"] * len(fields))] * (num_values - 1))
|
2011-09-10 03:22:28 +08:00
|
|
|
return " ".join(res)
|
|
|
|
|
2013-02-22 06:02:18 +08:00
|
|
|
def combine_expression(self, connector, sub_expressions):
|
|
|
|
# SQLite doesn't have a power function, so we fake it with a
|
|
|
|
# user-defined function django_power that's registered in connect().
|
|
|
|
if connector == '^':
|
|
|
|
return 'django_power(%s)' % ','.join(sub_expressions)
|
|
|
|
return super(DatabaseOperations, self).combine_expression(connector, sub_expressions)
|
|
|
|
|
2014-07-24 20:57:24 +08:00
|
|
|
def combine_duration_expression(self, connector, sub_expressions):
|
|
|
|
if connector not in ['+', '-']:
|
|
|
|
raise utils.DatabaseError('Invalid connector for timedelta: %s.' % connector)
|
|
|
|
fn_params = ["'%s'" % connector] + sub_expressions
|
|
|
|
if len(fn_params) > 3:
|
|
|
|
raise ValueError('Too many params for timedelta operations.')
|
|
|
|
return "django_format_dtdelta(%s)" % ', '.join(fn_params)
|
|
|
|
|
2014-03-04 09:12:42 +08:00
|
|
|
def integer_field_range(self, internal_type):
|
|
|
|
# SQLite doesn't enforce any integer constraints
|
|
|
|
return (None, None)
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2007-08-20 05:30:57 +08:00
|
|
|
class DatabaseWrapper(BaseDatabaseWrapper):
|
2010-10-11 20:55:17 +08:00
|
|
|
vendor = 'sqlite'
|
2007-08-20 11:26:55 +08:00
|
|
|
# SQLite requires LIKE statements to include an ESCAPE clause if the value
|
|
|
|
# being escaped has a percent or underscore in it.
|
|
|
|
# See http://www.sqlite.org/lang_expr.html for an explanation.
|
|
|
|
operators = {
|
|
|
|
'exact': '= %s',
|
|
|
|
'iexact': "LIKE %s ESCAPE '\\'",
|
|
|
|
'contains': "LIKE %s ESCAPE '\\'",
|
|
|
|
'icontains': "LIKE %s ESCAPE '\\'",
|
|
|
|
'regex': 'REGEXP %s',
|
|
|
|
'iregex': "REGEXP '(?i)' || %s",
|
|
|
|
'gt': '> %s',
|
|
|
|
'gte': '>= %s',
|
|
|
|
'lt': '< %s',
|
|
|
|
'lte': '<= %s',
|
|
|
|
'startswith': "LIKE %s ESCAPE '\\'",
|
|
|
|
'endswith': "LIKE %s ESCAPE '\\'",
|
|
|
|
'istartswith': "LIKE %s ESCAPE '\\'",
|
|
|
|
'iendswith': "LIKE %s ESCAPE '\\'",
|
|
|
|
}
|
|
|
|
|
2014-09-27 18:41:54 +08:00
|
|
|
# The patterns below are used to generate SQL pattern lookup clauses when
|
|
|
|
# the right-hand side of the lookup isn't a raw string (it might be an expression
|
|
|
|
# or the result of a bilateral transformation).
|
|
|
|
# In those cases, special characters for LIKE operators (e.g. \, *, _) should be
|
|
|
|
# escaped on database side.
|
|
|
|
#
|
|
|
|
# Note: we use str.format() here for readability as '%' is used as a wildcard for
|
|
|
|
# the LIKE operator.
|
|
|
|
pattern_esc = r"REPLACE(REPLACE(REPLACE({}, '\', '\\'), '%%', '\%%'), '_', '\_')"
|
2014-01-18 17:09:43 +08:00
|
|
|
pattern_ops = {
|
2014-09-27 18:41:54 +08:00
|
|
|
'contains': r"LIKE '%%' || {} || '%%' ESCAPE '\'",
|
|
|
|
'icontains': r"LIKE '%%' || UPPER({}) || '%%' ESCAPE '\'",
|
|
|
|
'startswith': r"LIKE {} || '%%' ESCAPE '\'",
|
|
|
|
'istartswith': r"LIKE UPPER({}) || '%%' ESCAPE '\'",
|
|
|
|
'endswith': r"LIKE '%%' || {} ESCAPE '\'",
|
|
|
|
'iendswith': r"LIKE '%%' || UPPER({}) ESCAPE '\'",
|
2014-01-18 17:09:43 +08:00
|
|
|
}
|
|
|
|
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
Database = Database
|
2014-09-26 01:59:03 +08:00
|
|
|
SchemaEditorClass = DatabaseSchemaEditor
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(DatabaseWrapper, self).__init__(*args, **kwargs)
|
2008-11-16 16:48:24 +08:00
|
|
|
|
2010-10-11 20:55:17 +08:00
|
|
|
self.features = DatabaseFeatures(self)
|
2011-04-05 08:19:17 +08:00
|
|
|
self.ops = DatabaseOperations(self)
|
2009-03-11 11:39:34 +08:00
|
|
|
self.client = DatabaseClient(self)
|
2008-08-11 20:11:25 +08:00
|
|
|
self.creation = DatabaseCreation(self)
|
|
|
|
self.introspection = DatabaseIntrospection(self)
|
2009-12-22 23:18:51 +08:00
|
|
|
self.validation = BaseDatabaseValidation(self)
|
2008-08-11 20:11:25 +08:00
|
|
|
|
2012-11-27 04:42:27 +08:00
|
|
|
def get_connection_params(self):
|
2012-02-11 11:09:54 +08:00
|
|
|
settings_dict = self.settings_dict
|
|
|
|
if not settings_dict['NAME']:
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2012-07-10 19:22:55 +08:00
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"settings.DATABASES is improperly configured. "
|
|
|
|
"Please supply the NAME value.")
|
2012-02-11 11:09:54 +08:00
|
|
|
kwargs = {
|
|
|
|
'database': settings_dict['NAME'],
|
|
|
|
'detect_types': Database.PARSE_DECLTYPES | Database.PARSE_COLNAMES,
|
|
|
|
}
|
|
|
|
kwargs.update(settings_dict['OPTIONS'])
|
|
|
|
# Always allow the underlying SQLite connection to be shareable
|
|
|
|
# between multiple threads. The safe-guarding will be handled at a
|
|
|
|
# higher level by the `BaseDatabaseWrapper.allow_thread_sharing`
|
|
|
|
# property. This is necessary as the shareability is disabled by
|
|
|
|
# default in pysqlite and it cannot be changed once a connection is
|
|
|
|
# opened.
|
|
|
|
if 'check_same_thread' in kwargs and kwargs['check_same_thread']:
|
|
|
|
warnings.warn(
|
|
|
|
'The `check_same_thread` option was provided and set to '
|
2013-07-28 09:45:25 +08:00
|
|
|
'True. It will be overridden with False. Use the '
|
2012-02-11 11:09:54 +08:00
|
|
|
'`DatabaseWrapper.allow_thread_sharing` property instead '
|
|
|
|
'for controlling thread shareability.',
|
|
|
|
RuntimeWarning
|
|
|
|
)
|
|
|
|
kwargs.update({'check_same_thread': False})
|
2014-12-04 07:17:59 +08:00
|
|
|
if self.features.can_share_in_memory_db:
|
|
|
|
kwargs.update({'uri': True})
|
2012-11-27 04:42:27 +08:00
|
|
|
return kwargs
|
|
|
|
|
|
|
|
def get_new_connection(self, conn_params):
|
|
|
|
conn = Database.connect(**conn_params)
|
2013-02-10 23:15:49 +08:00
|
|
|
conn.create_function("django_date_extract", 2, _sqlite_date_extract)
|
2012-11-27 04:42:27 +08:00
|
|
|
conn.create_function("django_date_trunc", 2, _sqlite_date_trunc)
|
2013-02-10 23:15:49 +08:00
|
|
|
conn.create_function("django_datetime_extract", 3, _sqlite_datetime_extract)
|
|
|
|
conn.create_function("django_datetime_trunc", 3, _sqlite_datetime_trunc)
|
2012-11-27 04:42:27 +08:00
|
|
|
conn.create_function("regexp", 2, _sqlite_regexp)
|
2014-07-24 20:57:24 +08:00
|
|
|
conn.create_function("django_format_dtdelta", 3, _sqlite_format_dtdelta)
|
2013-02-22 06:02:18 +08:00
|
|
|
conn.create_function("django_power", 2, _sqlite_power)
|
2012-11-27 04:42:27 +08:00
|
|
|
return conn
|
|
|
|
|
|
|
|
def init_connection_state(self):
|
|
|
|
pass
|
|
|
|
|
2013-02-19 00:12:42 +08:00
|
|
|
def create_cursor(self):
|
2007-08-20 05:30:57 +08:00
|
|
|
return self.connection.cursor(factory=SQLiteCursorWrapper)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-03-02 19:12:51 +08:00
|
|
|
def close(self):
|
|
|
|
self.validate_thread_sharing()
|
|
|
|
# If database is in memory, closing the connection destroys the
|
|
|
|
# database. To prevent accidental data loss, ignore close requests on
|
|
|
|
# an in-memory db.
|
2014-12-04 07:17:59 +08:00
|
|
|
if not self.is_in_memory_db(self.settings_dict['NAME']):
|
2013-03-02 19:12:51 +08:00
|
|
|
BaseDatabaseWrapper.close(self)
|
2013-02-18 18:37:26 +08:00
|
|
|
|
2013-03-04 22:57:04 +08:00
|
|
|
def _savepoint_allowed(self):
|
2013-10-07 16:47:50 +08:00
|
|
|
# Two conditions are required here:
|
|
|
|
# - A sufficiently recent version of SQLite to support savepoints,
|
|
|
|
# - Being in a transaction, which can only happen inside 'atomic'.
|
|
|
|
|
2013-03-05 05:17:35 +08:00
|
|
|
# When 'isolation_level' is not None, sqlite3 commits before each
|
|
|
|
# savepoint; it's a bug. When it is None, savepoints don't make sense
|
2013-10-07 16:47:50 +08:00
|
|
|
# because autocommit is enabled. The only exception is inside 'atomic'
|
|
|
|
# blocks. To work around that bug, on SQLite, 'atomic' starts a
|
2013-03-05 05:17:35 +08:00
|
|
|
# transaction explicitly rather than simply disable autocommit.
|
2013-10-07 16:47:50 +08:00
|
|
|
return self.features.uses_savepoints and self.in_atomic_block
|
2013-03-04 22:57:04 +08:00
|
|
|
|
2013-03-02 20:47:46 +08:00
|
|
|
def _set_autocommit(self, autocommit):
|
|
|
|
if autocommit:
|
|
|
|
level = None
|
|
|
|
else:
|
|
|
|
# sqlite3's internal default is ''. It's different from None.
|
|
|
|
# See Modules/_sqlite/connection.c.
|
|
|
|
level = ''
|
|
|
|
# 'isolation_level' is a misleading API.
|
|
|
|
# SQLite always runs at the SERIALIZABLE isolation level.
|
2014-03-24 06:09:26 +08:00
|
|
|
with self.wrap_database_errors:
|
|
|
|
self.connection.isolation_level = level
|
2013-03-02 20:47:46 +08:00
|
|
|
|
2011-08-07 08:43:26 +08:00
|
|
|
def check_constraints(self, table_names=None):
|
|
|
|
"""
|
2014-09-04 20:15:09 +08:00
|
|
|
Checks each table name in `table_names` for rows with invalid foreign
|
|
|
|
key references. This method is intended to be used in conjunction with
|
|
|
|
`disable_constraint_checking()` and `enable_constraint_checking()`, to
|
|
|
|
determine if rows with invalid references were entered while constraint
|
|
|
|
checks were off.
|
|
|
|
|
|
|
|
Raises an IntegrityError on the first invalid foreign key reference
|
|
|
|
encountered (if any) and provides detailed information about the
|
|
|
|
invalid reference in the error message.
|
|
|
|
|
|
|
|
Backends can override this method if they can more directly apply
|
|
|
|
constraint checking (e.g. via "SET CONSTRAINTS ALL IMMEDIATE")
|
2011-08-07 08:43:26 +08:00
|
|
|
"""
|
|
|
|
cursor = self.cursor()
|
|
|
|
if table_names is None:
|
2012-04-29 07:11:55 +08:00
|
|
|
table_names = self.introspection.table_names(cursor)
|
2011-08-07 08:43:26 +08:00
|
|
|
for table_name in table_names:
|
|
|
|
primary_key_column_name = self.introspection.get_primary_key_column(cursor, table_name)
|
|
|
|
if not primary_key_column_name:
|
|
|
|
continue
|
|
|
|
key_columns = self.introspection.get_key_columns(cursor, table_name)
|
|
|
|
for column_name, referenced_table_name, referenced_column_name in key_columns:
|
|
|
|
cursor.execute("""
|
|
|
|
SELECT REFERRING.`%s`, REFERRING.`%s` FROM `%s` as REFERRING
|
|
|
|
LEFT JOIN `%s` as REFERRED
|
|
|
|
ON (REFERRING.`%s` = REFERRED.`%s`)
|
|
|
|
WHERE REFERRING.`%s` IS NOT NULL AND REFERRED.`%s` IS NULL"""
|
|
|
|
% (primary_key_column_name, column_name, table_name, referenced_table_name,
|
|
|
|
column_name, referenced_column_name, column_name, referenced_column_name))
|
|
|
|
for bad_row in cursor.fetchall():
|
|
|
|
raise utils.IntegrityError("The row in table '%s' with primary key '%s' has an invalid "
|
|
|
|
"foreign key: %s.%s contains a value '%s' that does not have a corresponding value in %s.%s."
|
|
|
|
% (table_name, bad_row[0], table_name, column_name, bad_row[1],
|
|
|
|
referenced_table_name, referenced_column_name))
|
|
|
|
|
2013-03-02 19:12:51 +08:00
|
|
|
def is_usable(self):
|
|
|
|
return True
|
|
|
|
|
2013-03-05 05:17:35 +08:00
|
|
|
def _start_transaction_under_autocommit(self):
|
|
|
|
"""
|
|
|
|
Start a transaction explicitly in autocommit mode.
|
|
|
|
|
|
|
|
Staying in autocommit mode works around a bug of sqlite3 that breaks
|
|
|
|
savepoints when autocommit is disabled.
|
|
|
|
"""
|
|
|
|
self.cursor().execute("BEGIN")
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2014-12-04 07:17:59 +08:00
|
|
|
def is_in_memory_db(self, name):
|
|
|
|
return name == ":memory:" or "mode=memory" in name
|
|
|
|
|
2012-08-18 21:16:52 +08:00
|
|
|
|
2011-05-10 20:20:47 +08:00
|
|
|
FORMAT_QMARK_REGEX = re.compile(r'(?<!%)%s')
|
2010-03-23 21:51:11 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class SQLiteCursorWrapper(Database.Cursor):
|
|
|
|
"""
|
|
|
|
Django uses "format" style placeholders, but pysqlite2 uses "qmark" style.
|
|
|
|
This fixes it -- but note that if you want to use a literal "%s" in a query,
|
|
|
|
you'll need to use "%%s".
|
|
|
|
"""
|
2013-03-23 23:09:56 +08:00
|
|
|
def execute(self, query, params=None):
|
|
|
|
if params is None:
|
|
|
|
return Database.Cursor.execute(self, query)
|
2010-03-23 21:51:11 +08:00
|
|
|
query = self.convert_query(query)
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
return Database.Cursor.execute(self, query, params)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def executemany(self, query, param_list):
|
2010-03-23 21:51:11 +08:00
|
|
|
query = self.convert_query(query)
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
return Database.Cursor.executemany(self, query, param_list)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2010-03-23 21:51:11 +08:00
|
|
|
def convert_query(self, query):
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
return FORMAT_QMARK_REGEX.sub('?', query).replace('%%', '%')
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2013-02-10 23:15:49 +08:00
|
|
|
def _sqlite_date_extract(lookup_type, dt):
|
2008-11-16 16:48:24 +08:00
|
|
|
if dt is None:
|
|
|
|
return None
|
2006-05-02 09:31:56 +08:00
|
|
|
try:
|
2013-09-17 00:52:05 +08:00
|
|
|
dt = backend_utils.typecast_timestamp(dt)
|
2006-05-02 09:31:56 +08:00
|
|
|
except (ValueError, TypeError):
|
|
|
|
return None
|
2009-02-08 13:08:06 +08:00
|
|
|
if lookup_type == 'week_day':
|
2009-04-12 10:00:58 +08:00
|
|
|
return (dt.isoweekday() % 7) + 1
|
2009-02-08 13:08:06 +08:00
|
|
|
else:
|
2009-04-12 10:00:58 +08:00
|
|
|
return getattr(dt, lookup_type)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def _sqlite_date_trunc(lookup_type, dt):
|
|
|
|
try:
|
2013-09-17 00:52:05 +08:00
|
|
|
dt = backend_utils.typecast_timestamp(dt)
|
2006-05-02 09:31:56 +08:00
|
|
|
except (ValueError, TypeError):
|
|
|
|
return None
|
2013-02-10 23:15:49 +08:00
|
|
|
if lookup_type == 'year':
|
|
|
|
return "%i-01-01" % dt.year
|
|
|
|
elif lookup_type == 'month':
|
|
|
|
return "%i-%02i-01" % (dt.year, dt.month)
|
|
|
|
elif lookup_type == 'day':
|
|
|
|
return "%i-%02i-%02i" % (dt.year, dt.month, dt.day)
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2013-02-10 23:15:49 +08:00
|
|
|
def _sqlite_datetime_extract(lookup_type, dt, tzname):
|
|
|
|
if dt is None:
|
|
|
|
return None
|
|
|
|
try:
|
2013-09-17 00:52:05 +08:00
|
|
|
dt = backend_utils.typecast_timestamp(dt)
|
2013-02-10 23:15:49 +08:00
|
|
|
except (ValueError, TypeError):
|
|
|
|
return None
|
|
|
|
if tzname is not None:
|
|
|
|
dt = timezone.localtime(dt, pytz.timezone(tzname))
|
|
|
|
if lookup_type == 'week_day':
|
|
|
|
return (dt.isoweekday() % 7) + 1
|
|
|
|
else:
|
|
|
|
return getattr(dt, lookup_type)
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2013-02-10 23:15:49 +08:00
|
|
|
def _sqlite_datetime_trunc(lookup_type, dt, tzname):
|
|
|
|
try:
|
2013-09-17 00:52:05 +08:00
|
|
|
dt = backend_utils.typecast_timestamp(dt)
|
2013-02-10 23:15:49 +08:00
|
|
|
except (ValueError, TypeError):
|
|
|
|
return None
|
|
|
|
if tzname is not None:
|
|
|
|
dt = timezone.localtime(dt, pytz.timezone(tzname))
|
2006-05-02 09:31:56 +08:00
|
|
|
if lookup_type == 'year':
|
|
|
|
return "%i-01-01 00:00:00" % dt.year
|
|
|
|
elif lookup_type == 'month':
|
|
|
|
return "%i-%02i-01 00:00:00" % (dt.year, dt.month)
|
|
|
|
elif lookup_type == 'day':
|
|
|
|
return "%i-%02i-%02i 00:00:00" % (dt.year, dt.month, dt.day)
|
2013-02-10 23:15:49 +08:00
|
|
|
elif lookup_type == 'hour':
|
|
|
|
return "%i-%02i-%02i %02i:00:00" % (dt.year, dt.month, dt.day, dt.hour)
|
|
|
|
elif lookup_type == 'minute':
|
|
|
|
return "%i-%02i-%02i %02i:%02i:00" % (dt.year, dt.month, dt.day, dt.hour, dt.minute)
|
|
|
|
elif lookup_type == 'second':
|
|
|
|
return "%i-%02i-%02i %02i:%02i:%02i" % (dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2014-07-24 20:57:24 +08:00
|
|
|
def _sqlite_format_dtdelta(conn, lhs, rhs):
|
|
|
|
"""
|
|
|
|
LHS and RHS can be either:
|
|
|
|
- An integer number of microseconds
|
|
|
|
- A string representing a timedelta object
|
|
|
|
- A string representing a datetime
|
|
|
|
"""
|
2010-12-22 11:34:04 +08:00
|
|
|
try:
|
2014-12-23 18:01:58 +08:00
|
|
|
if isinstance(lhs, six.integer_types):
|
2014-07-24 20:57:24 +08:00
|
|
|
lhs = str(decimal.Decimal(lhs) / decimal.Decimal(1000000))
|
|
|
|
real_lhs = parse_duration(lhs)
|
|
|
|
if real_lhs is None:
|
|
|
|
real_lhs = backend_utils.typecast_timestamp(lhs)
|
2014-12-23 18:01:58 +08:00
|
|
|
if isinstance(rhs, six.integer_types):
|
2014-07-24 20:57:24 +08:00
|
|
|
rhs = str(decimal.Decimal(rhs) / decimal.Decimal(1000000))
|
|
|
|
real_rhs = parse_duration(rhs)
|
|
|
|
if real_rhs is None:
|
|
|
|
real_rhs = backend_utils.typecast_timestamp(rhs)
|
2010-12-22 11:34:04 +08:00
|
|
|
if conn.strip() == '+':
|
2014-07-24 20:57:24 +08:00
|
|
|
out = real_lhs + real_rhs
|
2010-12-22 11:34:04 +08:00
|
|
|
else:
|
2014-07-24 20:57:24 +08:00
|
|
|
out = real_lhs - real_rhs
|
2010-12-22 11:34:04 +08:00
|
|
|
except (ValueError, TypeError):
|
|
|
|
return None
|
2011-10-14 03:23:45 +08:00
|
|
|
# typecast_timestamp returns a date or a datetime without timezone.
|
|
|
|
# It will be formatted as "%Y-%m-%d" or "%Y-%m-%d %H:%M:%S[.%f]"
|
2014-07-24 20:57:24 +08:00
|
|
|
return str(out)
|
2010-12-22 11:34:04 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2007-06-28 02:58:10 +08:00
|
|
|
def _sqlite_regexp(re_pattern, re_string):
|
2013-06-26 22:30:58 +08:00
|
|
|
return bool(re.search(re_pattern, force_text(re_string))) if re_string is not None else False
|
2013-02-22 06:02:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
def _sqlite_power(x, y):
|
|
|
|
return x ** y
|