Ticket #1533: sqlitebase.patch
File sqlitebase.patch, 2.6 KB (added by , 19 years ago) |
---|
-
base.py
4 4 5 5 from django.db.backends import util 6 6 from pysqlite2 import dbapi2 as Database 7 7 from threading import currentThread 8 from django.utils.synch import RWLock 9 8 10 DatabaseError = Database.DatabaseError 9 11 10 12 Database.register_converter("bool", lambda s: str(s) == '1') … … 24 26 def __init__(self): 25 27 self.connection = None 26 28 self.queries = [] 27 29 self._lock = RWLock() 30 self._active_threads = [] 31 28 32 def cursor(self): 29 33 from django.conf import settings 30 if self.connection is None: 31 self.connection = Database.connect(settings.DATABASE_NAME, detect_types=Database.PARSE_DECLTYPES) 32 # register extract and date_trun functions 33 self.connection.create_function("django_extract", 2, _sqlite_extract) 34 self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc) 34 self._lock.writer_enters() 35 try: 36 if self.connection is None: 37 self.connection = Database.connect(settings.DATABASE_NAME, detect_types=Database.PARSE_DECLTYPES) 38 # register extract and date_trun functions 39 self.connection.create_function("django_extract", 2, _sqlite_extract) 40 self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc) 41 finally: 42 self._lock.writer_leaves() 35 43 cursor = self.connection.cursor(factory=SQLiteCursorWrapper) 36 44 cursor.row_factory = utf8rowFactory 37 45 if settings.DEBUG: … … 43 51 self.connection.commit() 44 52 45 53 def _rollback(self): 46 if self.connection: 47 self.connection.rollback() 48 54 self._lock.reader_enters() 55 try: 56 if self.connection: 57 return self.connection.rollback() 58 finally: 59 self._lock.reader_leaves() 60 49 61 def close(self): 50 if self.connection is not None: 51 self.connection.close() 52 self.connection = None 62 self._lock.writer_enters() 63 try: 64 if currentThread() in self._active_threads: 65 self._active_threads.remove(currentThread()) 66 if not self._active_threads and self.connection is not None: 67 self.connection.close() 68 self.connection = None 69 finally: 70 self._lock.writer_leaves() 53 71 54 72 class SQLiteCursorWrapper(Database.Cursor): 55 73 """