21 from rql.utils import rqlvar_maker |
21 from rql.utils import rqlvar_maker |
22 |
22 |
23 from cubicweb import dbapi, server |
23 from cubicweb import dbapi, server |
24 from cubicweb import BadConnectionId, UnknownEid, ConnectionError |
24 from cubicweb import BadConnectionId, UnknownEid, ConnectionError |
25 from cubicweb.cwconfig import register_persistent_options |
25 from cubicweb.cwconfig import register_persistent_options |
26 from cubicweb.server.sources import AbstractSource, ConnectionWrapper, TimedCache |
26 from cubicweb.server.sources import (AbstractSource, ConnectionWrapper, |
|
27 TimedCache, dbg_st_search, dbg_results) |
27 |
28 |
28 class ReplaceByInOperator(Exception): |
29 class ReplaceByInOperator(Exception): |
29 def __init__(self, eids): |
30 def __init__(self, eids): |
30 self.eids = eids |
31 self.eids = eids |
31 |
32 |
251 # try to reconnect |
252 # try to reconnect |
252 return self.get_connection() |
253 return self.get_connection() |
253 |
254 |
254 def syntax_tree_search(self, session, union, args=None, cachekey=None, |
255 def syntax_tree_search(self, session, union, args=None, cachekey=None, |
255 varmap=None): |
256 varmap=None): |
256 #assert not varmap, (varmap, union) |
257 assert dbg_st_search(self.uri, union, varmap, args, cachekey) |
257 rqlkey = union.as_string(kwargs=args) |
258 rqlkey = union.as_string(kwargs=args) |
258 try: |
259 try: |
259 results = self._query_cache[rqlkey] |
260 results = self._query_cache[rqlkey] |
260 except KeyError: |
261 except KeyError: |
261 results = self._syntax_tree_search(session, union, args) |
262 results = self._syntax_tree_search(session, union, args) |
262 self._query_cache[rqlkey] = results |
263 self._query_cache[rqlkey] = results |
|
264 assert dbg_results(results) |
263 return results |
265 return results |
264 |
266 |
265 def _syntax_tree_search(self, session, union, args): |
267 def _syntax_tree_search(self, session, union, args): |
266 """return result from this source for a rql query (actually from a rql |
268 """return result from this source for a rql query (actually from a rql |
267 syntax tree and a solution dictionary mapping each used variable to a |
269 syntax tree and a solution dictionary mapping each used variable to a |
268 possible type). If cachekey is given, the query necessary to fetch the |
270 possible type). If cachekey is given, the query necessary to fetch the |
269 results (but not the results themselves) may be cached using this key. |
271 results (but not the results themselves) may be cached using this key. |
270 """ |
272 """ |
271 if not args is None: |
273 if not args is None: |
272 args = args.copy() |
274 args = args.copy() |
273 if server.DEBUG & server.DBG_RQL: |
|
274 print 'RQL FOR PYRO SOURCE %s: %s', self.uri, union.as_string() |
|
275 if args: |
|
276 print 'ARGS', args |
|
277 if server.DEBUG & server.DBG_MORE: |
|
278 print 'SOLUTIONS', ','.join(str(s.solutions) for s in union.children) |
|
279 # get cached cursor anyway |
275 # get cached cursor anyway |
280 cu = session.pool[self.uri] |
276 cu = session.pool[self.uri] |
281 if cu is None: |
277 if cu is None: |
282 # this is a ConnectionWrapper instance |
278 # this is a ConnectionWrapper instance |
283 msg = session._("can't connect to source %s, some data may be missing") |
279 msg = session._("can't connect to source %s, some data may be missing") |
285 return [] |
281 return [] |
286 try: |
282 try: |
287 rql, cachekey = RQL2RQL(self).generate(session, union, args) |
283 rql, cachekey = RQL2RQL(self).generate(session, union, args) |
288 except UnknownEid, ex: |
284 except UnknownEid, ex: |
289 if server.DEBUG: |
285 if server.DEBUG: |
290 print 'unknown eid', ex, 'no results' |
286 print ' unknown eid', ex, 'no results' |
291 return [] |
287 return [] |
292 if server.DEBUG & server.DBG_RQL: |
288 if server.DEBUG & server.DBG_RQL: |
293 print 'TRANSLATED RQL', rql |
289 print ' translated rql', rql |
294 try: |
290 try: |
295 rset = cu.execute(rql, args, cachekey) |
291 rset = cu.execute(rql, args, cachekey) |
296 except Exception, ex: |
292 except Exception, ex: |
297 self.exception(str(ex)) |
293 self.exception(str(ex)) |
298 msg = session._("error while querying source %s, some data may be missing") |
294 msg = session._("error while querying source %s, some data may be missing") |