[rql2sql] fix generated SQL when an attribute relation with a variable is ored with another attribute relation. Closes #2410405
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 09 Jul 2012 12:22:43 +0200
changeset 8457 69ad9a509bc3
parent 8456 c912d82f2166
child 8458 1c5f3c66ec53
[rql2sql] fix generated SQL when an attribute relation with a variable is ored with another attribute relation. Closes #2410405
server/sources/rql2sql.py
server/test/unittest_rql2sql.py
--- a/server/sources/rql2sql.py	Fri Jul 06 14:01:09 2012 +0200
+++ b/server/sources/rql2sql.py	Mon Jul 09 12:22:43 2012 +0200
@@ -1,4 +1,4 @@
-# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
 # This file is part of CubicWeb.
@@ -1284,10 +1284,10 @@
     def _visit_var_attr_relation(self, relation, rhs_vars):
         """visit an attribute relation with variable(s) in the RHS
 
-        attribute variables are used either in the selection or for
-        unification (eg X attr1 A, Y attr2 A). In case of selection,
-        nothing to do here.
+        attribute variables are used either in the selection or for unification
+        (eg X attr1 A, Y attr2 A). In case of selection, nothing to do here.
         """
+        ored = relation.ored()
         for vref in rhs_vars:
             var = vref.variable
             if var.name in self._varmap:
@@ -1298,10 +1298,22 @@
                 principal = 1
             else:
                 principal = var.stinfo.get('principal')
-            if principal is not None and principal is not relation:
+                print relation, principal
+            # we've to return some sql if:
+            # 1. visited relation is ored
+            # 2. variable's principal is not this relation and not 1.
+            if ored or (principal is not None and principal is not relation
+                        and not principal.ored()):
                 # we have to generate unification expression
-                lhssql = self._inlined_var_sql(relation.children[0].variable,
-                                               relation.r_type)
+                if principal is relation:
+                    # take care if ored case and principal is the relation to
+                    # use the right relation in the unification term
+                    _rel = [rel for rel in var.stinfo['rhsrelations']
+                            if not rel is principal][0]
+                else:
+                    _rel = relation
+                lhssql = self._inlined_var_sql(_rel.children[0].variable,
+                                               _rel.r_type)
                 try:
                     self._state.ignore_varmap = True
                     sql = lhssql + relation.children[1].accept(self)
--- a/server/test/unittest_rql2sql.py	Fri Jul 06 14:01:09 2012 +0200
+++ b/server/test/unittest_rql2sql.py	Mon Jul 09 12:22:43 2012 +0200
@@ -1,4 +1,4 @@
-# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
 # This file is part of CubicWeb.
@@ -219,6 +219,11 @@
 
 
 ADVANCED = [
+    ("Societe S WHERE S2 is Societe, S2 nom SN, S nom 'Logilab' OR S nom SN",
+     '''SELECT _S.cw_eid
+FROM cw_Societe AS _S, cw_Societe AS _S2
+WHERE ((_S.cw_nom=Logilab) OR (_S2.cw_nom=_S.cw_nom))'''),
+
     ("Societe S WHERE S nom 'Logilab' OR S nom 'Caesium'",
      '''SELECT _S.cw_eid
 FROM cw_Societe AS _S