From d91db01d45274830939fa7349f503b020f99ae5c Mon Sep 17 00:00:00 2001
From: Nicolas Delaby <nicolas@nexedi.com>
Date: Tue, 1 Jul 2008 09:51:25 +0000
Subject: [PATCH] Extend RawKey to handle None values

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@22143 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ZSQLCatalog/Query/SimpleQuery.py    | 2 ++
 product/ZSQLCatalog/SearchKey/RawKey.py     | 5 ++++-
 product/ZSQLCatalog/tests/testSearchKeys.py | 7 ++++++-
 3 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/product/ZSQLCatalog/Query/SimpleQuery.py b/product/ZSQLCatalog/Query/SimpleQuery.py
index d503266590..20f498ea07 100644
--- a/product/ZSQLCatalog/Query/SimpleQuery.py
+++ b/product/ZSQLCatalog/Query/SimpleQuery.py
@@ -140,6 +140,8 @@ class SimpleQuery(QueryMixin):
       search_key_class = DefaultKey
     elif isinstance(value, float):
       search_key_class = FloatKey
+    elif value is None:
+      return RawKey
     return search_key_class
 
   def _asSQLExpression(self, search_key_class, key, value, format=None, mode=None, range_value=None, stat__=None):
diff --git a/product/ZSQLCatalog/SearchKey/RawKey.py b/product/ZSQLCatalog/SearchKey/RawKey.py
index dd464c6880..064bf7c4b4 100644
--- a/product/ZSQLCatalog/SearchKey/RawKey.py
+++ b/product/ZSQLCatalog/SearchKey/RawKey.py
@@ -39,5 +39,8 @@ class RawKey(SearchKey):
 
   def buildSQLExpression(self, key, value, 
                          format=None, mode=None, range_value=None, stat__=None):
-    where_expression = "%s = '%s'" %(key, value)
+    if value is not None:
+      where_expression = "%s = '%s'" % (key, value)
+    else:
+      where_expression = "%s is NULL" % (key)
     return where_expression, []
diff --git a/product/ZSQLCatalog/tests/testSearchKeys.py b/product/ZSQLCatalog/tests/testSearchKeys.py
index df504f4aa7..e75ff300ad 100644
--- a/product/ZSQLCatalog/tests/testSearchKeys.py
+++ b/product/ZSQLCatalog/tests/testSearchKeys.py
@@ -374,7 +374,12 @@ class TestSearchKeyQuery(unittest.TestCase):
                  '.',
                  "delivery.stock = '.'",
                  [])
-                
+    #None values
+    self.compare(RawKey,
+                 'title',
+                 None,
+                 "title is NULL",
+                 [])
   def test_06FullTextKey(self, quiet=quiet, run=run_all_test):
     """ Check FullTextKey query generation"""
     if not run: return
-- 
2.30.9