cubicweb/server/querier.py
changeset 12931 6eae252361e5
parent 12889 bbf3e56b43fe
--- a/cubicweb/server/querier.py	Thu Mar 12 15:38:51 2020 +0100
+++ b/cubicweb/server/querier.py	Fri Mar 20 14:34:07 2020 +0100
@@ -25,7 +25,8 @@
 
 from rql import RQLSyntaxError, CoercionError
 from rql.stmts import Union
-from rql.nodes import ETYPE_PYOBJ_MAP, etype_from_pyobj, Relation, Exists, Not
+from rql.nodes import ETYPE_PYOBJ_MAP, etype_from_pyobj, Relation, Exists, Not,\
+    VariableRef, Constant
 from yams import BASE_TYPES
 
 from cubicweb import ValidationError, Unauthorized, UnknownEid, QueryError
@@ -579,6 +580,7 @@
 
         # build a description for the results if necessary
         descr = ()
+        variables = None
         if build_descr:
             if rqlst.TYPE == 'select':
                 # sample selection
@@ -588,6 +590,8 @@
                     solution = rqlst.children[0].solutions[0]
                     description = _make_description(selected, args, solution)
                     descr = RepeatList(len(results), tuple(description))
+                    variables = [self._get_projected_name(projected, rqlst.children[0].stinfo)
+                                 for projected in selected]
                 else:
                     # hard, delegate the work :o)
                     descr = manual_build_descr(cnx, rqlst, args, results)
@@ -605,12 +609,24 @@
         emit_to_debug_channel("rql", query_debug_informations)
 
         # return a result set object
-        return ResultSet(results, rql, args, descr)
+        return ResultSet(results, rql, args, descr, variables)
 
     # these are overridden by set_log_methods below
     # only defining here to prevent pylint from complaining
     info = warning = error = critical = exception = debug = lambda msg,*a,**kw: None
 
+    @staticmethod
+    def _get_projected_name(projected, stinfo):
+        if isinstance(projected, VariableRef):
+            return projected.name
+        elif isinstance(projected, Constant):
+            if stinfo['rewritten'] is None:
+                return str(projected)
+            for name, value in stinfo['rewritten'].items():
+                if [projected] == value:
+                    return name
+        return str(projected)
+
 
 class RQLCache(object):