cubicweb/server/sources/rql2sql.py
branch3.26
changeset 12296 23bc9f385645
parent 12233 62918203b9fc
child 12567 26744ad37953
equal deleted inserted replaced
12293:e36f2c862d5c 12296:23bc9f385645
   277                 for vref in term.iget_nodes(VariableRef):
   277                 for vref in term.iget_nodes(VariableRef):
   278                     if not any(vref.is_equivalent(g) for g in groups):
   278                     if not any(vref.is_equivalent(g) for g in groups):
   279                         groups.append(vref)
   279                         groups.append(vref)
   280 
   280 
   281 
   281 
       
   282 def is_in_aggregat(node):
       
   283     while node:
       
   284         node = node.parent
       
   285         if isinstance(node, Function) and node.descr().aggregat:
       
   286             return True
       
   287     return False
       
   288 
       
   289 
   282 def fix_selection_and_group(rqlst, needwrap, selectsortterms,
   290 def fix_selection_and_group(rqlst, needwrap, selectsortterms,
   283                             sorts, groups, having):
   291                             sorts, groups, having):
   284     if selectsortterms and sorts:
   292     if selectsortterms and sorts:
   285         sort_term_selection(sorts, rqlst, not needwrap and groups)
   293         sort_term_selection(sorts, rqlst, not needwrap and groups)
   286     groupvrefs = [vref for term in groups for vref in term.iget_nodes(VariableRef)]
   294     groupvrefs = [vref for term in groups for vref in term.iget_nodes(VariableRef)]
   287     if sorts and groups:
   295     if sorts and groups:
   288         # when a query is grouped, ensure sort terms are grouped as well
   296         # when a query is grouped, ensure sort terms are grouped as well
   289         for sortterm in sorts:
   297         for sortterm in sorts:
   290             term = sortterm.term
   298             term = sortterm.term
   291             if not (isinstance(term, Constant) or
   299             for vref in term.iget_nodes(VariableRef):
   292                     (isinstance(term, Function) and
   300                 if not (any(vref.is_equivalent(group) for group in groupvrefs)
   293                      get_func_descr(term.name).aggregat)):
   301                         or is_in_aggregat(vref)):
   294                 for vref in term.iget_nodes(VariableRef):
   302                     groups.append(vref)
   295                     if not any(vref.is_equivalent(group) for group in groupvrefs):
   303                     groupvrefs.append(vref)
   296                         groups.append(vref)
       
   297                         groupvrefs.append(vref)
       
   298     if needwrap and (groups or having):
   304     if needwrap and (groups or having):
   299         selectedidx = set(vref.name for term in rqlst.selection
   305         selectedidx = set(vref.name for term in rqlst.selection
   300                           for vref in term.get_nodes(VariableRef))
   306                           for vref in term.get_nodes(VariableRef))
   301         if groups:
   307         if groups:
   302             for vref in groupvrefs:
   308             for vref in groupvrefs: