Ticket #1533: sqlitebase_02.patch
File sqlitebase_02.patch, 2.7 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 or not currentThread() in self._active_threads: 37 connection = Database.connect(settings.DATABASE_NAME, detect_types=Database.PARSE_DECLTYPES) 38 # register extract and date_trun functions 39 connection.create_function("django_extract", 2, _sqlite_extract) 40 connection.create_function("django_date_trunc", 2, _sqlite_date_trunc) 41 self._active_threads[currentThread()] = connection 42 self.connection = self._active_threads[currentThread()] 43 finally: 44 self._lock.writer_leaves() 35 45 cursor = self.connection.cursor(factory=SQLiteCursorWrapper) 36 46 cursor.row_factory = utf8rowFactory 37 47 if settings.DEBUG: … … 43 53 self.connection.commit() 44 54 45 55 def _rollback(self): 46 if self.connection: 47 self.connection.rollback() 48 56 self._lock.reader_enters() 57 try: 58 if self.connection: 59 return self.connection.rollback() 60 finally: 61 self._lock.reader_leaves() 62 49 63 def close(self): 50 if self.connection is not None: 51 self.connection.close() 52 self.connection = None 64 self._lock.writer_enters() 65 try: 66 if currentThread() in self._active_threads: 67 self._active_threads.pop(currentThread()) 68 if not self._active_threads and self.connection is not None: 69 self.connection.close() 70 self.connection = None 71 finally: 72 self._lock.writer_leaves() 53 73 54 74 class SQLiteCursorWrapper(Database.Cursor): 55 75 """