Project

General

Profile

« Previous | Next » 

Revision 2139

sql.py: DbConn: Added with_savepoint(). with_savepoint(): Use new DbConn.with_savepoint().

View differences:

lib/sql.py
137 137
        
138 138
        self.__db = None
139 139
        self.query_results = {}
140
        self._savepoint = 0
140 141
    
141 142
    def __getattr__(self, name):
142 143
        if name == '__dict__': raise Exception('getting __dict__')
......
255 256
    
256 257
    def is_cached(self, query, params=None):
257 258
        return _query_lookup(query, params) in self.query_results
259
    
260
    def with_savepoint(self, func):
261
        savepoint = 'savepoint_'+str(self._savepoint)
262
        self.run_query('SAVEPOINT '+savepoint)
263
        self._savepoint += 1
264
        try: 
265
            try: return_val = func()
266
            finally:
267
                self._savepoint -= 1
268
                assert self._savepoint >= 0
269
        except:
270
            self.run_query('ROLLBACK TO SAVEPOINT '+savepoint)
271
            raise
272
        else:
273
            self.run_query('RELEASE SAVEPOINT '+savepoint)
274
            return return_val
258 275

  
259 276
connect = DbConn
260 277

  
......
272 289

  
273 290
##### Recoverable querying
274 291

  
275
def with_savepoint(db, func):
276
    savepoint = 'savepoint_'+str(rand.rand_int()) # must be unique
277
    run_raw_query(db, 'SAVEPOINT '+savepoint)
278
    try: return_val = func()
279
    except:
280
        run_raw_query(db, 'ROLLBACK TO SAVEPOINT '+savepoint)
281
        raise
282
    else:
283
        run_raw_query(db, 'RELEASE SAVEPOINT '+savepoint)
284
        return return_val
292
def with_savepoint(db, func): return db.with_savepoint(func)
285 293

  
286 294
def run_query(db, query, params=None, recover=None, cacheable=False):
287 295
    if recover == None: recover = False

Also available in: Unified diff