server/sources/pyrorql.py
changeset 2625 d6012db7b93e
parent 2593 16d9419a4a79
child 2665 0c6281487f90
equal deleted inserted replaced
2624:c0b5b189190e 2625:d6012db7b93e
    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")
   324                                 del descr[rowindex]
   320                                 del descr[rowindex]
   325                                 break
   321                                 break
   326             results = rows
   322             results = rows
   327         else:
   323         else:
   328             results = []
   324             results = []
   329         if server.DEBUG & server.DBG_RQL:
       
   330             if len(results)>10:
       
   331                 print '-->', results[:10], '...', len(results)
       
   332             else:
       
   333                 print '-->', results
       
   334         return results
   325         return results
   335 
   326 
   336     def _entity_relations_and_kwargs(self, session, entity):
   327     def _entity_relations_and_kwargs(self, session, entity):
   337         relations = []
   328         relations = []
   338         kwargs = {'x': self.eid2extid(entity.eid, session)}
   329         kwargs = {'x': self.eid2extid(entity.eid, session)}