Project

General

Profile

« Previous | Next » 

Revision 2664

sql.py: DbConn.run_query(): Log query before running if no debug_msg_ref specified. Documented debug_msg_ref param.

View differences:

lib/sql.py
280 280
        '''
281 281
        @param log_ignore_excs The log_level will be increased by 2 if the query
282 282
            throws one of these exceptions.
283
        @param debug_msg_ref If specified, the log message will be returned in
284
            this instead of being output. This allows you to filter log messages
285
            depending on the result of the query.
283 286
        '''
284 287
        assert query != None
285 288
        
286 289
        if not self.caching: cacheable = False
287 290
        used_cache = False
291
        
292
        def log_msg(query):
293
            if used_cache: cache_status = 'cache hit'
294
            elif cacheable: cache_status = 'cache miss'
295
            else: cache_status = 'non-cacheable'
296
            return 'DB query: '+cache_status+':\n'+strings.as_code(query, 'SQL')
297
        
288 298
        try:
289 299
            # Get cursor
290 300
            if cacheable:
......
295 305
                except KeyError: cur = self.DbCursor(self)
296 306
            else: cur = self.db.cursor()
297 307
            
308
            # Log query
309
            if self.debug and debug_msg_ref == None: # log before running
310
                self.log_debug(log_msg(query), log_level)
311
            
298 312
            # Run query
299 313
            cur.execute(query, params)
300 314
        finally:
301
            if self.debug and debug_msg_ref != None:# only compute msg if needed
302
                if used_cache: cache_status = 'cache hit'
303
                elif cacheable: cache_status = 'cache miss'
304
                else: cache_status = 'non-cacheable'
305
                query_code = strings.as_code(str(get_cur_query(cur, query,
306
                    params)), 'SQL')
307
                debug_msg_ref[0] = 'DB query: '+cache_status+':\n'+query_code
315
            if self.debug and debug_msg_ref != None: # return after running
316
                debug_msg_ref[0] = log_msg(str(get_cur_query(cur, query,
317
                    params)))
308 318
        
309 319
        return cur
310 320
    

Also available in: Unified diff