Project

General

Profile

« Previous | Next » 

Revision 1927

sql.py: DbConn: For non-cacheable queries, use a plain cursor() instead of a DbCursor to avoid the overhead of saving the result and wrapping the cursor

View differences:

sql.py
108 108
        return self.__db
109 109
    
110 110
    class DbCursor(Proxy):
111
        def __init__(self, outer, cache_results):
111
        def __init__(self, outer):
112 112
            Proxy.__init__(self, outer.db.cursor())
113
            if cache_results: self.query_results = outer.query_results
114
            else: self.query_results = None
113
            self.query_results = outer.query_results
115 114
            self.query_lookup = None
116 115
            self.result = []
117 116
        
......
149 148
        class CacheCursor:
150 149
            def __init__(self, cached_result): self.__dict__ = cached_result
151 150
            
152
            def execute(self):
151
            def execute(self, *args, **kw_args):
153 152
                if isinstance(self.result, Exception): raise self.result
154 153
                # otherwise, result is a rows list
155 154
                self.iter = iter(self.result)
......
159 158
                except StopIteration: return None
160 159
    
161 160
    def run_query(self, query, params=None, cacheable=False):
162
        query_lookup = _query_lookup(query, params)
163 161
        used_cache = False
164 162
        try:
165
            try:
166
                if not cacheable: raise KeyError
167
                cur = self.query_results[query_lookup]
168
                used_cache = True
169
            except KeyError:
170
                cur = self.DbCursor(self, cacheable)
171
                try: cur.execute(query, params)
172
                except Exception, e:
173
                    _add_cursor_info(e, cur)
174
                    raise
175
            else: cur.execute()
163
            # Get cursor
164
            if cacheable:
165
                query_lookup = _query_lookup(query, params)
166
                try:
167
                    cur = self.query_results[query_lookup]
168
                    used_cache = True
169
                except KeyError: cur = self.DbCursor(self)
170
            else: cur = self.db.cursor()
171
            
172
            # Run query
173
            try: cur.execute(query, params)
174
            except Exception, e:
175
                _add_cursor_info(e, cur)
176
                raise
176 177
        finally:
177 178
            if self.log_debug != log_debug_none: # only compute msg if needed
178 179
                if used_cache: cache_status = 'Cache hit'
179 180
                elif cacheable: cache_status = 'Cache miss'
180 181
                else: cache_status = 'Non-cacheable'
181
                self.log_debug(cache_status+': '+strings.one_line(cur.query))
182
                self.log_debug(cache_status+': '
183
                    +strings.one_line(get_cur_query(cur)))
182 184
        
183 185
        return cur
184 186
    

Also available in: Unified diff