server/sources/rql2sql.py
changeset 1251 af40e615dc89
parent 1124 5548b608b7d9
child 1263 01152fffd593
child 1382 618f6aee8d52
--- a/server/sources/rql2sql.py	Mon Apr 06 16:11:38 2009 +0200
+++ b/server/sources/rql2sql.py	Mon Apr 06 16:18:46 2009 +0200
@@ -38,6 +38,7 @@
                        Variable, ColumnAlias, Relation, SubQuery, Exists)
 
 from cubicweb import server
+from cubicweb.server.sqlutils import SQL_PREFIX
 from cubicweb.server.utils import cleanup_solutions
 
 def _new_var(select, varname): 
@@ -765,7 +766,7 @@
             if '%s.%s' % (lhs.name, attr) in self._varmap:
                 lhssql = self._varmap['%s.%s' % (lhs.name, attr)]
             else:
-                lhssql = '%s.%s' % (self._var_table(lhs.variable), attr)
+                lhssql = '%s.%s%s' % (self._var_table(lhs.variable), SQL_PREFIX, attr)
             if not rhsvar is None:
                 t2 = self._var_table(rhsvar)
                 if t2 is None:
@@ -806,7 +807,8 @@
             if rhstable:
                 assert rhstable is not None, rhsvar
                 join += ' %s OUTER JOIN %s ON (%s.%s=%s)' % (
-                    outertype, self._state.tables[rhstable][1], rid, restrattr, rhssql)
+                    outertype, self._state.tables[rhstable][1], rid, restrattr,
+                    rhssql)
                 toreplace.append(rhstable)
         self.replace_tables_by_outer_join(join, maintable, *toreplace)
         return ''
@@ -846,7 +848,10 @@
             try:
                 lhssql = self._varmap['%s.%s' % (lhs.name, relation.r_type)]
             except KeyError:
-                lhssql = '%s.%s' % (table, relation.r_type)
+                if relation.r_type == 'eid':
+                    lhssql = lhs.variable._q_sql
+                else:
+                    lhssql = '%s.%s%s' % (table, SQL_PREFIX, relation.r_type)
         try:
             if relation._q_needcast == 'TODAY':
                 sql = 'DATE(%s)%s' % (lhssql, rhssql)
@@ -993,7 +998,7 @@
             principal = variable.stinfo['principal']
             if principal is None:
                 vtablename = variable.name
-                self.add_table('entities AS %s' % variable.name, vtablename)
+                self.add_table('entities AS %s' % vtablename, vtablename)
                 sql = '%s.eid' % vtablename
                 if variable.stinfo['typerels']:
                     # add additional restriction on entities.type column
@@ -1058,8 +1063,8 @@
             if self.schema.eschema(etype).is_final():
                 raise BadRQLQuery(var.stmt.root)
             table = var.name
-            sql = '%s.eid' % table
-            self.add_table('%s AS %s' % (etype, table), table, scope=scope)
+            sql = '%s.%seid' % (table, SQL_PREFIX)
+            self.add_table('%s%s AS %s' % (SQL_PREFIX, etype, table), table, scope=scope)
         return sql, table
     
     def _inlined_var_sql(self, var, rtype):
@@ -1068,7 +1073,7 @@
             scope = var.sqlscope is var.stmt and 0 or -1
             self.add_table(sql.split('.', 1)[0], scope=scope)
         except KeyError:
-            sql = '%s.%s' % (self._var_table(var), rtype)
+            sql = '%s.%s%s' % (self._var_table(var), SQL_PREFIX, rtype)
             #self._state.done.add(var.name)
         return sql
         
@@ -1091,7 +1096,7 @@
             sql = self._varmap['%s.%s' % (linkedvar.name, rel.r_type)]
         except KeyError:
             linkedvar.accept(self)            
-            sql = '%s.%s' % (linkedvar._q_sqltable, rel.r_type)
+            sql = '%s.%s%s' % (linkedvar._q_sqltable, SQL_PREFIX, rel.r_type)
         return sql
 
     # tables handling #########################################################