=== trac/db/sqlite_backend.py ================================================================== --- trac/db/sqlite_backend.py (revision 6466) +++ trac/db/sqlite_backend.py (local) @@ -113,7 +113,7 @@ return _to_sql(table) -class SQLiteConnection(ConnectionWrapper): +class BaseSQLiteConnection(ConnectionWrapper): """Connection wrapper for SQLite.""" __slots__ = ['_active_cursors'] @@ -121,7 +121,6 @@ def __init__(self, path, params={}): assert have_pysqlite > 0 - self.cnx = None if path != ':memory:': if not os.access(path, os.F_OK): raise TracError, 'Database "%s" not found.' % path @@ -135,46 +134,55 @@ 'directory it is located in.' \ % (getuser(), path) - if have_pysqlite == 2: - self._active_cursors = weakref.WeakKeyDictionary() - timeout = int(params.get('timeout', 10.0)) - # Convert unicode to UTF-8 bytestrings. This is case-sensitive, so - # we need two converters - sqlite.register_converter('text', str) - sqlite.register_converter('TEXT', str) + ConnectionWrapper.__init__(self, self._init_cnx(path, params)) - cnx = sqlite.connect(path, detect_types=sqlite.PARSE_DECLTYPES, - timeout=timeout) - else: - timeout = int(params.get('timeout', 10000)) - cnx = sqlite.connect(path, timeout=timeout) - ConnectionWrapper.__init__(self, cnx) - - if have_pysqlite == 2: - def cursor(self): - cursor = self.cnx.cursor(PyFormatCursor) - self._active_cursors[cursor] = True - cursor.cnx = self - return cursor - - def rollback(self): - for cursor in self._active_cursors.keys(): - cursor.close() - self.cnx.rollback() - - else: - def cursor(self): - return self.cnx.cursor() - def cast(self, column, type): return column def like(self): return 'LIKE' - if have_pysqlite == 2: - def get_last_id(self, cursor, table, column='id'): - return cursor.lastrowid - else: - def get_last_id(self, cursor, table, column='id'): - return self.cnx.db.sqlite_last_insert_rowid() + +class SQLite2Connection(BaseSQLiteConnection): + def _init_cnx(self, path, params): + self._active_cursors = weakref.WeakKeyDictionary() + timeout = int(params.get('timeout', 10.0)) + # Convert unicode to UTF-8 bytestrings. This is case-sensitive, so + # we need two converters + sqlite.register_converter('text', str) + sqlite.register_converter('TEXT', str) + + return sqlite.connect(path, detect_types=sqlite.PARSE_DECLTYPES, + timeout=timeout) + + def cursor(self): + cursor = self.cnx.cursor(PyFormatCursor) + self._active_cursors[cursor] = True + cursor.cnx = self + return cursor + + def rollback(self): + for cursor in self._active_cursors.keys(): + cursor.close() + self.cnx.rollback() + + def get_last_id(self, cursor, table, column='id'): + return cursor.lastrowid + + +class SQLite1Connection(BaseSQLiteConnection): + def _init_cnx(self, path, params): + timeout = int(params.get('timeout', 10000)) + return sqlite.connect(path, timeout=timeout) + + def cursor(self): + return self.cnx.cursor() + + def get_last_id(self, cursor, table, column='id'): + return self.cnx.db.sqlite_last_insert_rowid() + + +if have_pysqlite == 2: + SQLiteConnection = SQLite2Connection +else: + SQLiteConnection = SQLite1Connection