web/lib/django/db/backends/sqlite3/base.py
changeset 0 0d40e90630ef
child 29 cc9b7e14412b
equal deleted inserted replaced
-1:000000000000 0:0d40e90630ef
       
     1 """
       
     2 SQLite3 backend for django.
       
     3 
       
     4 Python 2.3 and 2.4 require pysqlite2 (http://pysqlite.org/).
       
     5 
       
     6 Python 2.5 and later can use a pysqlite2 module or the sqlite3 module in the
       
     7 standard library.
       
     8 """
       
     9 
       
    10 from django.db.backends import *
       
    11 from django.db.backends.signals import connection_created
       
    12 from django.db.backends.sqlite3.client import DatabaseClient
       
    13 from django.db.backends.sqlite3.creation import DatabaseCreation
       
    14 from django.db.backends.sqlite3.introspection import DatabaseIntrospection
       
    15 from django.utils.safestring import SafeString
       
    16 
       
    17 try:
       
    18     try:
       
    19         from pysqlite2 import dbapi2 as Database
       
    20     except ImportError, e1:
       
    21         from sqlite3 import dbapi2 as Database
       
    22 except ImportError, exc:
       
    23     import sys
       
    24     from django.core.exceptions import ImproperlyConfigured
       
    25     if sys.version_info < (2, 5, 0):
       
    26         module = 'pysqlite2 module'
       
    27         exc = e1
       
    28     else:
       
    29         module = 'either pysqlite2 or sqlite3 modules (tried in that order)'
       
    30     raise ImproperlyConfigured, "Error loading %s: %s" % (module, exc)
       
    31 
       
    32 try:
       
    33     import decimal
       
    34 except ImportError:
       
    35     from django.utils import _decimal as decimal # for Python 2.3
       
    36 
       
    37 DatabaseError = Database.DatabaseError
       
    38 IntegrityError = Database.IntegrityError
       
    39 
       
    40 Database.register_converter("bool", lambda s: str(s) == '1')
       
    41 Database.register_converter("time", util.typecast_time)
       
    42 Database.register_converter("date", util.typecast_date)
       
    43 Database.register_converter("datetime", util.typecast_timestamp)
       
    44 Database.register_converter("timestamp", util.typecast_timestamp)
       
    45 Database.register_converter("TIMESTAMP", util.typecast_timestamp)
       
    46 Database.register_converter("decimal", util.typecast_decimal)
       
    47 Database.register_adapter(decimal.Decimal, util.rev_typecast_decimal)
       
    48 if Database.version_info >= (2,4,1):
       
    49     # Starting in 2.4.1, the str type is not accepted anymore, therefore,
       
    50     # we convert all str objects to Unicode
       
    51     # As registering a adapter for a primitive type causes a small
       
    52     # slow-down, this adapter is only registered for sqlite3 versions
       
    53     # needing it.
       
    54     Database.register_adapter(str, lambda s:s.decode('utf-8'))
       
    55     Database.register_adapter(SafeString, lambda s:s.decode('utf-8'))
       
    56 
       
    57 class DatabaseFeatures(BaseDatabaseFeatures):
       
    58     # SQLite cannot handle us only partially reading from a cursor's result set
       
    59     # and then writing the same rows to the database in another cursor. This
       
    60     # setting ensures we always read result sets fully into memory all in one
       
    61     # go.
       
    62     can_use_chunked_reads = False
       
    63 
       
    64 class DatabaseOperations(BaseDatabaseOperations):
       
    65     def date_extract_sql(self, lookup_type, field_name):
       
    66         # sqlite doesn't support extract, so we fake it with the user-defined
       
    67         # function django_extract that's registered in connect().
       
    68         return 'django_extract("%s", %s)' % (lookup_type.lower(), field_name)
       
    69 
       
    70     def date_trunc_sql(self, lookup_type, field_name):
       
    71         # sqlite doesn't support DATE_TRUNC, so we fake it with a user-defined
       
    72         # function django_date_trunc that's registered in connect().
       
    73         return 'django_date_trunc("%s", %s)' % (lookup_type.lower(), field_name)
       
    74 
       
    75     def drop_foreignkey_sql(self):
       
    76         return ""
       
    77 
       
    78     def pk_default_value(self):
       
    79         return 'NULL'
       
    80 
       
    81     def quote_name(self, name):
       
    82         if name.startswith('"') and name.endswith('"'):
       
    83             return name # Quoting once is enough.
       
    84         return '"%s"' % name
       
    85 
       
    86     def no_limit_value(self):
       
    87         return -1
       
    88 
       
    89     def sql_flush(self, style, tables, sequences):
       
    90         # NB: The generated SQL below is specific to SQLite
       
    91         # Note: The DELETE FROM... SQL generated below works for SQLite databases
       
    92         # because constraints don't exist
       
    93         sql = ['%s %s %s;' % \
       
    94                 (style.SQL_KEYWORD('DELETE'),
       
    95                  style.SQL_KEYWORD('FROM'),
       
    96                  style.SQL_FIELD(self.quote_name(table))
       
    97                  ) for table in tables]
       
    98         # Note: No requirement for reset of auto-incremented indices (cf. other
       
    99         # sql_flush() implementations). Just return SQL at this point
       
   100         return sql
       
   101 
       
   102     def year_lookup_bounds(self, value):
       
   103         first = '%s-01-01'
       
   104         second = '%s-12-31 23:59:59.999999'
       
   105         return [first % value, second % value]
       
   106 
       
   107     def convert_values(self, value, field):
       
   108         """SQLite returns floats when it should be returning decimals,
       
   109         and gets dates and datetimes wrong.
       
   110         For consistency with other backends, coerce when required.
       
   111         """
       
   112         internal_type = field.get_internal_type()
       
   113         if internal_type == 'DecimalField':
       
   114             return util.typecast_decimal(field.format_number(value))
       
   115         elif internal_type and internal_type.endswith('IntegerField') or internal_type == 'AutoField':
       
   116             return int(value)
       
   117         elif internal_type == 'DateField':
       
   118             return util.typecast_date(value)
       
   119         elif internal_type == 'DateTimeField':
       
   120             return util.typecast_timestamp(value)
       
   121         elif internal_type == 'TimeField':
       
   122             return util.typecast_time(value)
       
   123 
       
   124         # No field, or the field isn't known to be a decimal or integer
       
   125         return value
       
   126 
       
   127 class DatabaseWrapper(BaseDatabaseWrapper):
       
   128 
       
   129     # SQLite requires LIKE statements to include an ESCAPE clause if the value
       
   130     # being escaped has a percent or underscore in it.
       
   131     # See http://www.sqlite.org/lang_expr.html for an explanation.
       
   132     operators = {
       
   133         'exact': '= %s',
       
   134         'iexact': "LIKE %s ESCAPE '\\'",
       
   135         'contains': "LIKE %s ESCAPE '\\'",
       
   136         'icontains': "LIKE %s ESCAPE '\\'",
       
   137         'regex': 'REGEXP %s',
       
   138         'iregex': "REGEXP '(?i)' || %s",
       
   139         'gt': '> %s',
       
   140         'gte': '>= %s',
       
   141         'lt': '< %s',
       
   142         'lte': '<= %s',
       
   143         'startswith': "LIKE %s ESCAPE '\\'",
       
   144         'endswith': "LIKE %s ESCAPE '\\'",
       
   145         'istartswith': "LIKE %s ESCAPE '\\'",
       
   146         'iendswith': "LIKE %s ESCAPE '\\'",
       
   147     }
       
   148 
       
   149     def __init__(self, *args, **kwargs):
       
   150         super(DatabaseWrapper, self).__init__(*args, **kwargs)
       
   151 
       
   152         self.features = DatabaseFeatures()
       
   153         self.ops = DatabaseOperations()
       
   154         self.client = DatabaseClient(self)
       
   155         self.creation = DatabaseCreation(self)
       
   156         self.introspection = DatabaseIntrospection(self)
       
   157         self.validation = BaseDatabaseValidation()
       
   158 
       
   159     def _cursor(self):
       
   160         if self.connection is None:
       
   161             settings_dict = self.settings_dict
       
   162             if not settings_dict['DATABASE_NAME']:
       
   163                 from django.core.exceptions import ImproperlyConfigured
       
   164                 raise ImproperlyConfigured, "Please fill out DATABASE_NAME in the settings module before using the database."
       
   165             kwargs = {
       
   166                 'database': settings_dict['DATABASE_NAME'],
       
   167                 'detect_types': Database.PARSE_DECLTYPES | Database.PARSE_COLNAMES,
       
   168             }
       
   169             kwargs.update(settings_dict['DATABASE_OPTIONS'])
       
   170             self.connection = Database.connect(**kwargs)
       
   171             # Register extract, date_trunc, and regexp functions.
       
   172             self.connection.create_function("django_extract", 2, _sqlite_extract)
       
   173             self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc)
       
   174             self.connection.create_function("regexp", 2, _sqlite_regexp)
       
   175             connection_created.send(sender=self.__class__)
       
   176         return self.connection.cursor(factory=SQLiteCursorWrapper)
       
   177 
       
   178     def close(self):
       
   179         # If database is in memory, closing the connection destroys the
       
   180         # database. To prevent accidental data loss, ignore close requests on
       
   181         # an in-memory db.
       
   182         if self.settings_dict['DATABASE_NAME'] != ":memory:":
       
   183             BaseDatabaseWrapper.close(self)
       
   184 
       
   185 class SQLiteCursorWrapper(Database.Cursor):
       
   186     """
       
   187     Django uses "format" style placeholders, but pysqlite2 uses "qmark" style.
       
   188     This fixes it -- but note that if you want to use a literal "%s" in a query,
       
   189     you'll need to use "%%s".
       
   190     """
       
   191     def execute(self, query, params=()):
       
   192         query = self.convert_query(query, len(params))
       
   193         return Database.Cursor.execute(self, query, params)
       
   194 
       
   195     def executemany(self, query, param_list):
       
   196         try:
       
   197           query = self.convert_query(query, len(param_list[0]))
       
   198           return Database.Cursor.executemany(self, query, param_list)
       
   199         except (IndexError,TypeError):
       
   200           # No parameter list provided
       
   201           return None
       
   202 
       
   203     def convert_query(self, query, num_params):
       
   204         return query % tuple("?" * num_params)
       
   205 
       
   206 def _sqlite_extract(lookup_type, dt):
       
   207     if dt is None:
       
   208         return None
       
   209     try:
       
   210         dt = util.typecast_timestamp(dt)
       
   211     except (ValueError, TypeError):
       
   212         return None
       
   213     if lookup_type == 'week_day':
       
   214         return (dt.isoweekday() % 7) + 1
       
   215     else:
       
   216         return getattr(dt, lookup_type)
       
   217 
       
   218 def _sqlite_date_trunc(lookup_type, dt):
       
   219     try:
       
   220         dt = util.typecast_timestamp(dt)
       
   221     except (ValueError, TypeError):
       
   222         return None
       
   223     if lookup_type == 'year':
       
   224         return "%i-01-01 00:00:00" % dt.year
       
   225     elif lookup_type == 'month':
       
   226         return "%i-%02i-01 00:00:00" % (dt.year, dt.month)
       
   227     elif lookup_type == 'day':
       
   228         return "%i-%02i-%02i 00:00:00" % (dt.year, dt.month, dt.day)
       
   229 
       
   230 def _sqlite_regexp(re_pattern, re_string):
       
   231     import re
       
   232     try:
       
   233         return bool(re.search(re_pattern, re_string))
       
   234     except:
       
   235         return False