Revision 1916
Added by Aaron Marcuse-Kubitza over 12 years ago
lib/sql.py | ||
---|---|---|
152 | 152 |
or isinstance(self.result, Exception)): |
153 | 153 |
|
154 | 154 |
assert self.query_lookup != None |
155 |
self.query_results[self.query_lookup] = util.dict_subset(
|
|
156 |
dicts.AttrsDictView(self), |
|
157 |
['query', 'result', 'rowcount', 'description']) |
|
155 |
self.query_results[self.query_lookup] = self.CacheCursor(
|
|
156 |
util.dict_subset(dicts.AttrsDictView(self),
|
|
157 |
['query', 'result', 'rowcount', 'description']))
|
|
158 | 158 |
|
159 | 159 |
def _is_insert(self): return self.query.upper().find('INSERT') >= 0 |
160 |
|
|
161 |
class CacheCursor: |
|
162 |
def __init__(self, cached_result): self.__dict__ = cached_result |
|
163 | 160 |
|
164 |
def execute(self): |
|
165 |
if isinstance(self.result, Exception): raise self.result |
|
166 |
# otherwise, result is a rows list |
|
167 |
self.iter = iter(self.result) |
|
168 |
|
|
169 |
def fetchone(self): |
|
170 |
try: return self.iter.next() |
|
171 |
except StopIteration: return None |
|
161 |
class CacheCursor: |
|
162 |
def __init__(self, cached_result): self.__dict__ = cached_result |
|
163 |
|
|
164 |
def execute(self): |
|
165 |
if isinstance(self.result, Exception): raise self.result |
|
166 |
# otherwise, result is a rows list |
|
167 |
self.iter = iter(self.result) |
|
168 |
|
|
169 |
def fetchone(self): |
|
170 |
try: return self.iter.next() |
|
171 |
except StopIteration: return None |
|
172 | 172 |
|
173 | 173 |
def run_query(self, query, params=None, cacheable=False): |
174 | 174 |
query_lookup = _query_lookup(query, params) |
... | ... | |
176 | 176 |
try: |
177 | 177 |
try: |
178 | 178 |
if not cacheable: raise KeyError |
179 |
cached_result = self.query_results[query_lookup]
|
|
179 |
cur = self.query_results[query_lookup]
|
|
180 | 180 |
used_cache = True |
181 | 181 |
except KeyError: |
182 | 182 |
cur = self.DbCursor(self, cacheable) |
... | ... | |
184 | 184 |
except Exception, e: |
185 | 185 |
_add_cursor_info(e, cur) |
186 | 186 |
raise |
187 |
else: |
|
188 |
cur = self.CacheCursor(cached_result) |
|
189 |
cur.execute() |
|
187 |
else: cur.execute() |
|
190 | 188 |
finally: |
191 | 189 |
if self.log_debug != log_debug_none: # only compute msg if needed |
192 | 190 |
if used_cache: cache_status = 'Cache hit' |
Also available in: Unified diff
sql.py: DbConn: Cache the constructed CacheCursor itself, rather than the dict that's used to create it