server/querier.py
changeset 9467 ad66d7b3fd48
parent 9463 d62e13eba033
child 9469 032825bbacab
equal deleted inserted replaced
9466:c3a5f4507f12 9467:ad66d7b3fd48
   152         self.session = session
   152         self.session = session
   153         # quick reference to the system source
   153         # quick reference to the system source
   154         self.syssource = session.repo.system_source
   154         self.syssource = session.repo.system_source
   155         # execution steps
   155         # execution steps
   156         self.steps = []
   156         self.steps = []
   157         # index of temporary tables created during execution
       
   158         self.temp_tables = {}
       
   159         # various resource accesors
   157         # various resource accesors
   160         self.querier = querier
   158         self.querier = querier
   161         self.schema = querier.schema
   159         self.schema = querier.schema
   162         self.sqlannotate = querier.sqlgen_annotate
   160         self.sqlannotate = querier.sqlgen_annotate
   163         self.rqlhelper = session.vreg.rqlhelper
   161         self.rqlhelper = session.vreg.rqlhelper
   168 
   166 
   169     def add_step(self, step):
   167     def add_step(self, step):
   170         """add a step to the plan"""
   168         """add a step to the plan"""
   171         self.steps.append(step)
   169         self.steps.append(step)
   172 
   170 
   173     def clean(self):
       
   174         """remove temporary tables"""
       
   175         self.syssource.clean_temp_data(self.session, self.temp_tables)
       
   176 
       
   177     def sqlexec(self, sql, args=None):
   171     def sqlexec(self, sql, args=None):
   178         return self.syssource.sqlexec(self.session, sql, args)
   172         return self.syssource.sqlexec(self.session, sql, args)
   179 
   173 
   180     def execute(self):
   174     def execute(self):
   181         """execute a plan and return resulting rows"""
   175         """execute a plan and return resulting rows"""
   182         try:
   176         for step in self.steps:
   183             for step in self.steps:
   177             result = step.execute()
   184                 result = step.execute()
   178         # the latest executed step contains the full query result
   185             # the latest executed step contains the full query result
   179         return result
   186             return result
       
   187         finally:
       
   188             self.clean()
       
   189 
       
   190     def make_temp_table_name(self, table):
       
   191         """
       
   192         return a temp table name according to db backend
       
   193         """
       
   194         return self.syssource.make_temp_table_name(table)
       
   195 
       
   196 
       
   197     def init_temp_table(self, table, selected, sol):
       
   198         """initialize sql schema and variable map for a temporary table which
       
   199         will be used to store result for the given rqlst
       
   200         """
       
   201         try:
       
   202             outputmap, sqlschema, _ = self.temp_tables[table]
       
   203             update_varmap(outputmap, selected, table)
       
   204         except KeyError:
       
   205             sqlschema, outputmap = self.syssource.temp_table_def(selected, sol,
       
   206                                                                  table)
       
   207             self.temp_tables[table] = [outputmap, sqlschema, False]
       
   208         return outputmap
       
   209 
       
   210     def create_temp_table(self, table):
       
   211         """create a temporary table to store result for the given rqlst"""
       
   212         if not self.temp_tables[table][-1]:
       
   213             sqlschema = self.temp_tables[table][1]
       
   214             self.syssource.create_temp_table(self.session, table, sqlschema)
       
   215             self.temp_tables[table][-1] = True
       
   216 
   180 
   217     def preprocess(self, union, security=True):
   181     def preprocess(self, union, security=True):
   218         """insert security when necessary then annotate rql st for sql generation
   182         """insert security when necessary then annotate rql st for sql generation
   219 
   183 
   220         return rqlst to actually execute
   184         return rqlst to actually execute