~ubuntu-branches/ubuntu/quantal/python-django/quantal-security

« back to all changes in this revision

Viewing changes to django/db/backends/sqlite3/base.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2010-05-21 07:52:55 UTC
  • mfrom: (1.3.6 upstream)
  • mto: This revision was merged to the branch mainline in revision 28.
  • Revision ID: james.westby@ubuntu.com-20100521075255-ii78v1dyfmyu3uzx
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
"""
2
2
SQLite3 backend for django.
3
3
 
4
 
Python 2.3 and 2.4 require pysqlite2 (http://pysqlite.org/).
 
4
Python 2.4 requires pysqlite2 (http://pysqlite.org/).
5
5
 
6
6
Python 2.5 and later can use a pysqlite2 module or the sqlite3 module in the
7
7
standard library.
8
8
"""
9
9
 
 
10
import re
 
11
import sys
 
12
 
 
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
27
31
        exc = e1
28
32
    else:
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))
31
35
 
32
 
try:
33
 
    import decimal
34
 
except ImportError:
35
 
    from django.utils import _decimal as decimal # for Python 2.3
36
36
 
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)
69
71
 
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)
74
78
 
75
79
    def drop_foreignkey_sql(self):
76
80
        return ""
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)
158
162
 
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.")
165
169
            kwargs = {
166
 
                'database': settings_dict['DATABASE_NAME'],
 
170
                'database': settings_dict['NAME'],
167
171
                'detect_types': Database.PARSE_DECLTYPES | Database.PARSE_COLNAMES,
168
172
            }
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)
184
188
 
 
189
FORMAT_QMARK_REGEX = re.compile(r'(?![^%])%s')
 
190
 
185
191
class SQLiteCursorWrapper(Database.Cursor):
186
192
    """
187
193
    Django uses "format" style placeholders, but pysqlite2 uses "qmark" style.
189
195
    you'll need to use "%%s".
190
196
    """
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)
 
199
        try:
 
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]
194
205
 
195
206
    def executemany(self, query, param_list):
 
207
        query = self.convert_query(query)
196
208
        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
 
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]
202
214
 
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('%%','%')
205
217
 
206
218
def _sqlite_extract(lookup_type, dt):
207
219
    if dt is None: