#343322: should generate IS NULL in sql w/ None values in substitution stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 19 May 2009 13:08:51 +0200
branchstable
changeset 1862 94dc8ccd320b
parent 1861 21fae1546ac4
child 1867 b2fe73a55899
#343322: should generate IS NULL in sql w/ None values in substitution
server/sources/rql2sql.py
server/test/unittest_rql2sql.py
--- a/server/sources/rql2sql.py	Tue May 19 11:18:05 2009 +0200
+++ b/server/sources/rql2sql.py	Tue May 19 13:08:51 2009 +0200
@@ -901,6 +901,7 @@
     def visit_comparison(self, cmp, contextrels=None):
         """generate SQL for a comparaison"""
         if len(cmp.children) == 2:
+            # XXX occurs ?
             lhs, rhs = cmp.children
         else:
             lhs = None
@@ -911,6 +912,11 @@
                 operator = ' LIKE '
             else:
                 operator = ' %s ' % operator
+        elif (operator == '=' and isinstance(rhs, Constant)
+              and rhs.eval(self._args) is None):
+            if lhs is None:
+                return ' IS NULL'
+            return '%s IS NULL' % lhs.accept(self, contextrels)
         elif isinstance(rhs, Function) and rhs.name == 'IN':
             assert operator == '='
             operator = ' '
--- a/server/test/unittest_rql2sql.py	Tue May 19 11:18:05 2009 +0200
+++ b/server/test/unittest_rql2sql.py	Tue May 19 13:08:51 2009 +0200
@@ -1,3 +1,4 @@
+
 """unit tests for module cubicweb.server.sources.rql2sql"""
 
 import sys
@@ -1211,6 +1212,14 @@
 WHERE rel_in_group0.eid_from=T00.x AND rel_in_group0.eid_to=G.cw_eid''',
                     varmap={'X': 'T00.x', 'X.login': 'T00.l'})
 
+    def test_is_null_transform(self):
+        union = self._prepare('Any X WHERE X login %(login)s')
+        r, args = self.o.generate(union, {'login': None})
+        self.assertLinesEquals((r % args).strip(),
+                               '''SELECT X.cw_eid
+FROM cw_CWUser AS X
+WHERE X.cw_login IS NULL''')
+
     def test_parser_parse(self):
         for t in self._parse(PARSER):
             yield t