Ticket #3446: t3446-EagerCursor-r8488.diff
| File t3446-EagerCursor-r8488.diff, 2.8 KB (added by cboos, 2 years ago) |
|---|
-
trac/db/sqlite_backend.py
62 62 return self._rollback_on_error(sqlite.Cursor.executemany, sql, 63 63 args or []) 64 64 65 # EagerCursor taken from the example in pysqlite's repository: 66 # 67 # http://oss.itsystementwicklung.de/hg/pysqlite/raw-file/0a726720f540/misc/eager.py 68 # 69 # Only change is to subclass it from PyFormatCursor instead of 70 # sqlite.Cursor. 71 72 class EagerCursor(PyFormatCursor): 73 def __init__(self, con): 74 PyFormatCursor.__init__(self, con) 75 self.rows = [] 76 self.pos = 0 77 78 def execute(self, *args): 79 PyFormatCursor.execute(self, *args) 80 self.rows = PyFormatCursor.fetchall(self) 81 self.pos = 0 82 83 def fetchone(self): 84 try: 85 row = self.rows[self.pos] 86 self.pos += 1 87 return row 88 except IndexError: 89 return None 90 91 def fetchmany(self, num=None): 92 if num is None: 93 num = self.arraysize 94 95 result = self.rows[self.pos:self.pos+num] 96 self.pos += num 97 return result 98 99 def fetchall(self): 100 result = self.rows[self.pos:] 101 self.pos = len(self.rows) 102 return result 103 65 104 elif have_pysqlite == 1: 66 105 _ver = sqlite._sqlite.sqlite_version_info() 67 106 sqlite_version = _ver[0] * 10000 + _ver[1] * 100 + _ver[2] … … 160 199 class SQLiteConnection(ConnectionWrapper): 161 200 """Connection wrapper for SQLite.""" 162 201 163 __slots__ = ['_active_cursors' ]202 __slots__ = ['_active_cursors', '_eager'] 164 203 poolable = have_pysqlite and os.name == 'nt' and sqlite_version >= 30301 165 204 166 205 def __init__(self, path, log=None, params={}): … … 181 220 if have_pysqlite == 2: 182 221 self._active_cursors = weakref.WeakKeyDictionary() 183 222 timeout = int(params.get('timeout', 10.0)) 223 self._eager = params.get('cursor', 'eager') == 'eager' 224 # eager is default, can be turned off by specifying ?cursor= 225 print '*'*88,self._eager, repr(params) 184 226 if isinstance(path, unicode): # needed with 2.4.0 185 227 path = path.encode('utf-8') 186 228 cnx = sqlite.connect(path, detect_types=sqlite.PARSE_DECLTYPES, … … 194 236 195 237 if have_pysqlite == 2: 196 238 def cursor(self): 197 cursor = self.cnx.cursor( PyFormatCursor)239 cursor = self.cnx.cursor((PyFormatCursor, EagerCursor)[self._eager]) 198 240 self._active_cursors[cursor] = True 199 241 cursor.cnx = self 200 242 return cursor
