server/sources/rql2sql.py
branchstable
changeset 5593 f6c55bec9326
parent 5582 3e133b29a1a4
child 5630 40d7c7e180f1
--- a/server/sources/rql2sql.py	Wed May 26 14:59:05 2010 +0200
+++ b/server/sources/rql2sql.py	Wed May 26 15:45:22 2010 +0200
@@ -1177,13 +1177,26 @@
             pass
         return ''
 
+    def _temp_table_scope(self, select, table):
+        scope = 9999
+        for var, sql in self._varmap.iteritems():
+            if table == sql.split('.', 1)[0]:
+                try:
+                    scope = min(scope, self._state.scopes[select.defined_vars[var].scope])
+                except KeyError:
+                    scope = 0 # XXX
+                if scope == 0:
+                    return 0
+        return 0
+
     def _var_info(self, var):
-        scope = self._state.scopes[var.scope]
         try:
             sql = self._varmap[var.name]
             tablealias = sql.split('.', 1)[0]
+            scope = self._temp_table_scope(var.stmt, tablealias)
             self.add_table(tablealias, scope=scope)
         except KeyError:
+            scope = self._state.scopes[var.scope]
             etype = self._state.solution[var.name]
             # XXX this check should be moved in rql.stcheck
             if self.schema.eschema(etype).final: