cubicweb/server/ssplanner.py
changeset 11237 f32134dd0067
parent 11057 0b59724cb3f2
child 11765 9cb215e833b0
equal deleted inserted replaced
11232:25ec9be5f305 11237:f32134dd0067
   302         return self.build_select_plan(plan, union)
   302         return self.build_select_plan(plan, union)
   303 
   303 
   304 
   304 
   305 # execution steps and helper functions ########################################
   305 # execution steps and helper functions ########################################
   306 
   306 
   307 def varmap_test_repr(varmap, tablesinorder):
       
   308     if varmap is None:
       
   309         return varmap
       
   310     maprepr = {}
       
   311     for var, sql in varmap.items():
       
   312         table, col = sql.split('.')
       
   313         maprepr[var] = '%s.%s' % (tablesinorder[table], col)
       
   314     return maprepr
       
   315 
       
   316 class Step(object):
   307 class Step(object):
   317     """base abstract class for execution step"""
   308     """base abstract class for execution step"""
   318     def __init__(self, plan):
   309     def __init__(self, plan):
   319         self.plan = plan
   310         self.plan = plan
   320         self.children = []
   311         self.children = []
   343 
   334 
   344 class OneFetchStep(Step):
   335 class OneFetchStep(Step):
   345     """step consisting in fetching data from sources and directly returning
   336     """step consisting in fetching data from sources and directly returning
   346     results
   337     results
   347     """
   338     """
   348     def __init__(self, plan, union, inputmap=None):
   339     def __init__(self, plan, union):
   349         Step.__init__(self, plan)
   340         Step.__init__(self, plan)
   350         self.union = union
   341         self.union = union
   351         self.inputmap = inputmap
       
   352 
   342 
   353     def execute(self):
   343     def execute(self):
   354         """call .syntax_tree_search with the given syntax tree on each
   344         """call .syntax_tree_search with the given syntax tree on each
   355         source for each solution
   345         source for each solution
   356         """
   346         """
   357         self.execute_children()
   347         self.execute_children()
   358         cnx = self.plan.cnx
   348         cnx = self.plan.cnx
   359         args = self.plan.args
   349         args = self.plan.args
   360         inputmap = self.inputmap
       
   361         union = self.union
   350         union = self.union
   362         # do we have to use a inputmap from a previous step ? If so disable
   351         if self.plan.cache_key is None:
   363         # cachekey
       
   364         if inputmap or self.plan.cache_key is None:
       
   365             cachekey = None
   352             cachekey = None
   366         # union may have been splited into subqueries, in which case we can't
   353         # union may have been splited into subqueries, in which case we can't
   367         # use plan.cache_key, rebuild a cache key
   354         # use plan.cache_key, rebuild a cache key
   368         elif isinstance(self.plan.cache_key, tuple):
   355         elif isinstance(self.plan.cache_key, tuple):
   369             cachekey = list(self.plan.cache_key)
   356             cachekey = list(self.plan.cache_key)
   371             cachekey = tuple(cachekey)
   358             cachekey = tuple(cachekey)
   372         else:
   359         else:
   373             cachekey = union.as_string()
   360             cachekey = union.as_string()
   374         # get results for query
   361         # get results for query
   375         source = cnx.repo.system_source
   362         source = cnx.repo.system_source
   376         result = source.syntax_tree_search(cnx, union, args, cachekey, inputmap)
   363         result = source.syntax_tree_search(cnx, union, args, cachekey)
   377         #print 'ONEFETCH RESULT %s' % (result)
   364         #print 'ONEFETCH RESULT %s' % (result)
   378         return result
   365         return result
   379 
   366 
   380     def mytest_repr(self):
   367     def mytest_repr(self):
   381         """return a representation of this step suitable for test"""
   368         """return a representation of this step suitable for test"""
   382         try:
       
   383             inputmap = varmap_test_repr(self.inputmap, self.plan.tablesinorder)
       
   384         except AttributeError:
       
   385             inputmap = self.inputmap
       
   386         return (self.__class__.__name__,
   369         return (self.__class__.__name__,
   387                 sorted((r.as_string(kwargs=self.plan.args), r.solutions)
   370                 sorted((r.as_string(kwargs=self.plan.args), r.solutions)
   388                        for r in self.union.children),
   371                        for r in self.union.children))
   389                 inputmap)
       
   390 
   372 
   391 
   373 
   392 # UPDATE/INSERT/DELETE steps ##################################################
   374 # UPDATE/INSERT/DELETE steps ##################################################
   393 
   375 
   394 class InsertRelationsStep(Step):
   376 class InsertRelationsStep(Step):