cubicweb/server/querier.py
changeset 12886 fa565be41647
parent 12885 194e9ae964ed
child 12887 11953d585a55
equal deleted inserted replaced
12885:194e9ae964ed 12886:fa565be41647
   158         # original rql syntax tree
   158         # original rql syntax tree
   159         self.rqlst = rqlst
   159         self.rqlst = rqlst
   160         self.args = args or {}
   160         self.args = args or {}
   161         # cnx executing the query
   161         # cnx executing the query
   162         self.cnx = cnx
   162         self.cnx = cnx
   163         # quick reference to the system source
       
   164         self.syssource = cnx.repo.system_source
       
   165         # execution steps
   163         # execution steps
   166         self.steps = []
   164         self.steps = []
   167         # various resource accesors
       
   168         self.querier = querier
   165         self.querier = querier
   169         self.schema = querier.schema
       
   170         self.rqlhelper = cnx.vreg.rqlhelper
       
   171         # tracing token for debugging
   166         # tracing token for debugging
   172         self.rql_query_tracing_token = None
   167         self.rql_query_tracing_token = None
   173 
   168 
   174     def add_step(self, step):
   169     def add_step(self, step):
   175         """add a step to the plan"""
   170         """add a step to the plan"""
   176         self.steps.append(step)
   171         self.steps.append(step)
   177 
   172 
   178     def sqlexec(self, sql, args=None):
   173     def sqlexec(self, sql, args=None):
   179         return self.syssource.sqlexec(self.cnx, sql, args)
   174         return self.cnx.repo.system_source.sqlexec(self.cnx, sql, args)
   180 
   175 
   181     def execute(self):
   176     def execute(self):
   182         """execute a plan and return resulting rows"""
   177         """execute a plan and return resulting rows"""
   183         for step in self.steps:
   178         for step in self.steps:
   184             step.rql_query_tracing_token = self.rql_query_tracing_token
   179             step.rql_query_tracing_token = self.rql_query_tracing_token
   213                 if key is not None:
   208                 if key is not None:
   214                     self.cnx.transaction_data[key] = (union, self.args)
   209                     self.cnx.transaction_data[key] = (union, self.args)
   215         else:
   210         else:
   216             noinvariant = ()
   211             noinvariant = ()
   217         if cached is None:
   212         if cached is None:
   218             self.rqlhelper.simplify(union)
   213             self.cnx.vreg.rqlhelper.simplify(union)
   219             self.querier.rqlannotator.annotate(union)
   214             self.querier.rqlannotator.annotate(union)
   220             set_qdata(self.schema.rschema, union, noinvariant)
   215             set_qdata(self.querier.schema.rschema, union, noinvariant)
   221         if union.has_text_query:
   216         if union.has_text_query:
   222             self.cache_key = None
   217             self.cache_key = None
   223 
   218 
   224     def _insert_security(self, union):
   219     def _insert_security(self, union):
   225         noinvariant = set()
   220         noinvariant = set()
   311         for mainvarname, rschema, newvarname in insertedvars:
   306         for mainvarname, rschema, newvarname in insertedvars:
   312             nvartype = str(rschema.objects(solutions[0][mainvarname])[0])
   307             nvartype = str(rschema.objects(solutions[0][mainvarname])[0])
   313             for sol in solutions:
   308             for sol in solutions:
   314                 sol[newvarname] = nvartype
   309                 sol[newvarname] = nvartype
   315         select.clean_solutions(solutions)
   310         select.clean_solutions(solutions)
   316         add_types_restriction(self.schema, select)
   311         add_types_restriction(self.querier.schema, select)
   317         self.rqlhelper.annotate(rqlst)
   312         self.cnx.vreg.rqlhelper.annotate(rqlst)
   318         self.preprocess(rqlst, security=False)
   313         self.preprocess(rqlst, security=False)
   319         return rqlst
   314         return rqlst
   320 
   315 
   321 
   316 
   322 class InsertPlan(ExecutionPlan):
   317 class InsertPlan(ExecutionPlan):
   342         self.e_defs[-1].append(edef)
   337         self.e_defs[-1].append(edef)
   343 
   338 
   344     def add_relation_def(self, rdef):
   339     def add_relation_def(self, rdef):
   345         """add an relation definition to build"""
   340         """add an relation definition to build"""
   346         edef, rtype, value = rdef
   341         edef, rtype, value = rdef
   347         if self.schema[rtype].rule:
   342         if self.querier.schema[rtype].rule:
   348             raise QueryError("'%s' is a computed relation" % rtype)
   343             raise QueryError("'%s' is a computed relation" % rtype)
   349         self.r_defs.add(rdef)
   344         self.r_defs.add(rdef)
   350         if not isinstance(edef, int):
   345         if not isinstance(edef, int):
   351             self._r_subj_index.setdefault(edef, []).append(rdef)
   346             self._r_subj_index.setdefault(edef, []).append(rdef)
   352         if not isinstance(value, int):
   347         if not isinstance(value, int):