18 """RQL rewriting utilities : insert rql expression snippets into rql syntax |
18 """RQL rewriting utilities : insert rql expression snippets into rql syntax |
19 tree. |
19 tree. |
20 |
20 |
21 This is used for instance for read security checking in the repository. |
21 This is used for instance for read security checking in the repository. |
22 """ |
22 """ |
23 |
|
24 from six import text_type, string_types |
|
25 |
23 |
26 from rql import nodes as n, stmts, TypeResolverException |
24 from rql import nodes as n, stmts, TypeResolverException |
27 from rql.utils import common_parent |
25 from rql.utils import common_parent |
28 |
26 |
29 from yams import BadSchemaDefinition |
27 from yams import BadSchemaDefinition |
638 # generate an identifier for the substitution |
636 # generate an identifier for the substitution |
639 argname = subselect.allocate_varname() |
637 argname = subselect.allocate_varname() |
640 while argname in self.kwargs: |
638 while argname in self.kwargs: |
641 argname = subselect.allocate_varname() |
639 argname = subselect.allocate_varname() |
642 subselect.add_constant_restriction(subselect.get_variable(self.u_varname), |
640 subselect.add_constant_restriction(subselect.get_variable(self.u_varname), |
643 'eid', text_type(argname), 'Substitute') |
641 'eid', argname, 'Substitute') |
644 self.kwargs[argname] = self.session.user.eid |
642 self.kwargs[argname] = self.session.user.eid |
645 add_types_restriction(self.schema, subselect, subselect, |
643 add_types_restriction(self.schema, subselect, subselect, |
646 solutions=self.solutions) |
644 solutions=self.solutions) |
647 myunion = stmts.Union() |
645 myunion = stmts.Union() |
648 myunion.append(subselect) |
646 myunion.append(subselect) |
793 while argname in self.kwargs: |
791 while argname in self.kwargs: |
794 argname = stmt.allocate_varname() |
792 argname = stmt.allocate_varname() |
795 # insert "U eid %(u)s" |
793 # insert "U eid %(u)s" |
796 stmt.add_constant_restriction( |
794 stmt.add_constant_restriction( |
797 stmt.get_variable(self.u_varname), |
795 stmt.get_variable(self.u_varname), |
798 'eid', text_type(argname), 'Substitute') |
796 'eid', argname, 'Substitute') |
799 self.kwargs[argname] = self.session.user.eid |
797 self.kwargs[argname] = self.session.user.eid |
800 return self.u_varname |
798 return self.u_varname |
801 key = (self.current_expr, self.varmap, vname) |
799 key = (self.current_expr, self.varmap, vname) |
802 try: |
800 try: |
803 return self.rewritten[key] |
801 return self.rewritten[key] |
915 vi = self.varinfos[index] |
913 vi = self.varinfos[index] |
916 if vi.get('const') is not None: |
914 if vi.get('const') is not None: |
917 return n.Constant(vi['const'], 'Int') |
915 return n.Constant(vi['const'], 'Int') |
918 return n.VariableRef(stmt.get_variable(selectvar)) |
916 return n.VariableRef(stmt.get_variable(selectvar)) |
919 vname_or_term = self._get_varname_or_term(node.name) |
917 vname_or_term = self._get_varname_or_term(node.name) |
920 if isinstance(vname_or_term, string_types): |
918 if isinstance(vname_or_term, str): |
921 return n.VariableRef(stmt.get_variable(vname_or_term)) |
919 return n.VariableRef(stmt.get_variable(vname_or_term)) |
922 # shared term |
920 # shared term |
923 return vname_or_term.copy(stmt) |
921 return vname_or_term.copy(stmt) |
924 |
922 |
925 def current_statement(self): |
923 def current_statement(self): |