server/rqlannotation.py
branchstable
changeset 3815 50b87f759b5d
parent 3694 33dbb1da1db9
child 4212 ab6573088b4a
equal deleted inserted replaced
3814:a4659adf4eee 3815:50b87f759b5d
     8 """
     8 """
     9 __docformat__ = "restructuredtext en"
     9 __docformat__ = "restructuredtext en"
    10 
    10 
    11 from logilab.common.compat import any
    11 from logilab.common.compat import any
    12 
    12 
       
    13 from rql import BadRQLQuery
    13 from rql.nodes import Relation, VariableRef, Constant, Variable, Or
    14 from rql.nodes import Relation, VariableRef, Constant, Variable, Or
    14 from rql.utils import common_parent
    15 from rql.utils import common_parent
    15 
    16 
    16 def _annotate_select(annotator, rqlst):
    17 def _annotate_select(annotator, rqlst):
    17     for subquery in rqlst.with_:
    18     for subquery in rqlst.with_:
   175 
   176 
   176 def _select_main_var(relations):
   177 def _select_main_var(relations):
   177     """given a list of rqlst relations, select one which will be used as main
   178     """given a list of rqlst relations, select one which will be used as main
   178     relation for the rhs variable
   179     relation for the rhs variable
   179     """
   180     """
   180     for rel in relations:
   181     principal = None
       
   182     # sort for test predictability
       
   183     for rel in sorted(relations, key=lambda x: (x.children[0].name, x.r_type)):
       
   184         # only equality relation with a variable as rhs may be principal
       
   185         if rel.operator() not in ('=', 'IS') \
       
   186                or not isinstance(rel.children[1].children[0], VariableRef):
       
   187             continue
   181         if rel.sqlscope is rel.stmt:
   188         if rel.sqlscope is rel.stmt:
   182             return rel
   189             return rel
   183         principal = rel
   190         principal = rel
       
   191     if principal is None:
       
   192         print iter(relations).next().root
       
   193         raise BadRQLQuery('unable to find principal in %s' % ', '.join(
       
   194             r.as_string() for r in relations))
   184     return principal
   195     return principal
   185 
   196 
   186 
   197 
   187 def set_qdata(getrschema, union, noinvariant):
   198 def set_qdata(getrschema, union, noinvariant):
   188     """recursive function to set querier data on variables in the syntax tree
   199     """recursive function to set querier data on variables in the syntax tree