2
2
SQLite3 backend for django.
4
Python 2.3 and 2.4 require pysqlite2 (http://pysqlite.org/).
4
Python 2.4 requires pysqlite2 (http://pysqlite.org/).
6
6
Python 2.5 and later can use a pysqlite2 module or the sqlite3 module in the
13
from django.db import utils
10
14
from django.db.backends import *
11
15
from django.db.backends.signals import connection_created
12
16
from django.db.backends.sqlite3.client import DatabaseClient
29
33
module = 'either pysqlite2 or sqlite3 modules (tried in that order)'
30
raise ImproperlyConfigured, "Error loading %s: %s" % (module, exc)
34
raise ImproperlyConfigured("Error loading %s: %s" % (module, exc))
35
from django.utils import _decimal as decimal # for Python 2.3
37
37
DatabaseError = Database.DatabaseError
38
38
IntegrityError = Database.IntegrityError
64
64
class DatabaseOperations(BaseDatabaseOperations):
65
65
def date_extract_sql(self, lookup_type, field_name):
66
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)
67
# function django_extract that's registered in connect(). Note that
68
# single quotes are used because this is a string (and could otherwise
69
# cause a collision with a field name).
70
return "django_extract('%s', %s)" % (lookup_type.lower(), field_name)
70
72
def date_trunc_sql(self, lookup_type, field_name):
71
73
# 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
# function django_date_trunc that's registered in connect(). Note that
75
# single quotes are used because this is a string (and could otherwise
76
# cause a collision with a field name).
77
return "django_date_trunc('%s', %s)" % (lookup_type.lower(), field_name)
75
79
def drop_foreignkey_sql(self):
154
158
self.client = DatabaseClient(self)
155
159
self.creation = DatabaseCreation(self)
156
160
self.introspection = DatabaseIntrospection(self)
157
self.validation = BaseDatabaseValidation()
161
self.validation = BaseDatabaseValidation(self)
159
163
def _cursor(self):
160
164
if self.connection is None:
161
165
settings_dict = self.settings_dict
162
if not settings_dict['DATABASE_NAME']:
166
if not settings_dict['NAME']:
163
167
from django.core.exceptions import ImproperlyConfigured
164
raise ImproperlyConfigured, "Please fill out DATABASE_NAME in the settings module before using the database."
168
raise ImproperlyConfigured("Please fill out the database NAME in the settings module before using the database.")
166
'database': settings_dict['DATABASE_NAME'],
170
'database': settings_dict['NAME'],
167
171
'detect_types': Database.PARSE_DECLTYPES | Database.PARSE_COLNAMES,
169
kwargs.update(settings_dict['DATABASE_OPTIONS'])
173
kwargs.update(settings_dict['OPTIONS'])
170
174
self.connection = Database.connect(**kwargs)
171
175
# Register extract, date_trunc, and regexp functions.
172
176
self.connection.create_function("django_extract", 2, _sqlite_extract)
179
183
# If database is in memory, closing the connection destroys the
180
184
# database. To prevent accidental data loss, ignore close requests on
181
185
# an in-memory db.
182
if self.settings_dict['DATABASE_NAME'] != ":memory:":
186
if self.settings_dict['NAME'] != ":memory:":
183
187
BaseDatabaseWrapper.close(self)
189
FORMAT_QMARK_REGEX = re.compile(r'(?![^%])%s')
185
191
class SQLiteCursorWrapper(Database.Cursor):
187
193
Django uses "format" style placeholders, but pysqlite2 uses "qmark" style.
189
195
you'll need to use "%%s".
191
197
def execute(self, query, params=()):
192
query = self.convert_query(query, len(params))
193
return Database.Cursor.execute(self, query, params)
198
query = self.convert_query(query)
200
return Database.Cursor.execute(self, query, params)
201
except Database.IntegrityError, e:
202
raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]
203
except Database.DatabaseError, e:
204
raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2]
195
206
def executemany(self, query, param_list):
207
query = self.convert_query(query)
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
209
return Database.Cursor.executemany(self, query, param_list)
210
except Database.IntegrityError, e:
211
raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]
212
except Database.DatabaseError, e:
213
raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2]
203
def convert_query(self, query, num_params):
204
return query % tuple("?" * num_params)
215
def convert_query(self, query):
216
return FORMAT_QMARK_REGEX.sub('?', query).replace('%%','%')
206
218
def _sqlite_extract(lookup_type, dt):