fix #694445: related entity generates weird RQL which in turn generates weird SQL which fails on SQL Server stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 18 Feb 2010 12:52:34 +0100
branchstable
changeset 4630 528dee042927
parent 4629 1eeef3cbf528
child 4631 457160bc8cfe
fix #694445: related entity generates weird RQL which in turn generates weird SQL which fails on SQL Server quick fix to reuse modification_date if already retreived by the rql query.
entity.py
test/unittest_entity.py
--- a/entity.py	Thu Feb 18 12:13:36 2010 +0100
+++ b/entity.py	Thu Feb 18 12:52:34 2010 +0100
@@ -638,8 +638,17 @@
                 rql = '%s WHERE %s' % (rql.split(' ORDERBY ', 1)[0],
                                        rql.split(' WHERE ', 1)[1])
         elif not ' ORDERBY ' in rql:
-            args = tuple(rql.split(' WHERE ', 1))
-            rql = '%s ORDERBY Z DESC WHERE X modification_date Z, %s' % args
+            args = rql.split(' WHERE ', 1)
+            # if modification_date already retreived, we should use it instead
+            # of adding another variable for sort. This should be be problematic
+            # but it's actually with sqlserver, see ticket #694445
+            if 'X modification_date ' in args[1]:
+                var = args[1].split('X modification_date ', 1)[1].split(',', 1)[0]
+                args.insert(1, var.strip())
+                rql = '%s ORDERBY %s DESC WHERE %s' % tuple(args)
+            else:
+                rql = '%s ORDERBY Z DESC WHERE X modification_date Z, %s' % \
+                      tuple(args)
         return rql
 
     # generic vocabulary methods ##############################################
--- a/test/unittest_entity.py	Thu Feb 18 12:13:36 2010 +0100
+++ b/test/unittest_entity.py	Thu Feb 18 12:52:34 2010 +0100
@@ -175,7 +175,7 @@
             Personne.fetch_attrs = pfetch_attrs
             Societe.fetch_attrs = sfetch_attrs
 
-    def test_related_rql(self):
+    def test_related_rql_base(self):
         Personne = self.vreg['etypes'].etype_class('Personne')
         Note = self.vreg['etypes'].etype_class('Note')
         self.failUnless(issubclass(self.vreg['etypes'].etype_class('SubNote'), Note))
@@ -202,6 +202,14 @@
                           'WHERE E eid %(x)s, E tags X, X is IN (Personne), X nom AA, '
                           'X modification_date AB')
 
+    def test_related_rql_ambigous_cant_use_fetch_order(self):
+        tag = self.vreg['etypes'].etype_class('Tag')(self.request())
+        for ttype in self.schema['tags'].objects():
+            self.vreg['etypes'].etype_class(ttype).fetch_attrs = ('modification_date',)
+        self.assertEquals(tag.related_rql('tags', 'subject'),
+                          'Any X,AA ORDERBY AA DESC '
+                          'WHERE E eid %(x)s, E tags X, X modification_date AA')
+
     def test_unrelated_rql_security_1(self):
         user = self.request().user
         rql = user.unrelated_rql('use_email', 'EmailAddress', 'subject')[0]