Edgewall Software

Ticket #3446: t3446-EagerCursor-r8488.diff

File t3446-EagerCursor-r8488.diff, 2.8 KB (added by cboos, 2 years ago)

use the EagerCursor (see comment:27)

  • trac/db/sqlite_backend.py

     
    6262            return self._rollback_on_error(sqlite.Cursor.executemany, sql, 
    6363                                           args or []) 
    6464 
     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 
    65104elif have_pysqlite == 1: 
    66105    _ver = sqlite._sqlite.sqlite_version_info() 
    67106    sqlite_version = _ver[0] * 10000 + _ver[1] * 100 + _ver[2] 
     
    160199class SQLiteConnection(ConnectionWrapper): 
    161200    """Connection wrapper for SQLite.""" 
    162201 
    163     __slots__ = ['_active_cursors'] 
     202    __slots__ = ['_active_cursors', '_eager'] 
    164203    poolable = have_pysqlite and os.name == 'nt' and sqlite_version >= 30301 
    165204 
    166205    def __init__(self, path, log=None, params={}): 
     
    181220        if have_pysqlite == 2: 
    182221            self._active_cursors = weakref.WeakKeyDictionary() 
    183222            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) 
    184226            if isinstance(path, unicode): # needed with 2.4.0 
    185227                path = path.encode('utf-8') 
    186228            cnx = sqlite.connect(path, detect_types=sqlite.PARSE_DECLTYPES, 
     
    194236 
    195237    if have_pysqlite == 2: 
    196238        def cursor(self): 
    197             cursor = self.cnx.cursor(PyFormatCursor) 
     239            cursor = self.cnx.cursor((PyFormatCursor, EagerCursor)[self._eager]) 
    198240            self._active_cursors[cursor] = True 
    199241            cursor.cnx = self 
    200242            return cursor