From a5b96113cd8007fbcaf91df0e8a003e40429ec21 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Wed, 2 Jul 2008 15:09:19 +0000
Subject: [PATCH] handle provided key= to Query constructor even if the query
 value is a list

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@22219 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ZSQLCatalog/Query/SimpleQuery.py     | 35 ++++++++++----------
 product/ZSQLCatalog/tests/testZSQLCatalog.py | 15 +++++++++
 2 files changed, 33 insertions(+), 17 deletions(-)

diff --git a/product/ZSQLCatalog/Query/SimpleQuery.py b/product/ZSQLCatalog/Query/SimpleQuery.py
index 20f498ea07..b28db90061 100644
--- a/product/ZSQLCatalog/Query/SimpleQuery.py
+++ b/product/ZSQLCatalog/Query/SimpleQuery.py
@@ -179,6 +179,24 @@ class SimpleQuery(QueryMixin):
     sql_expressions = {'where_expression': '1', 
                        'select_expression_list': []}
     
+    # try to get search key type by the key definitions passed
+    if search_key_class is None:
+      if search_key == EXACT_MATCH_SEARCH_MODE:
+        search_key_class =  RawKey
+      elif search_key == KEYWORD_SEARCH_MODE or \
+          (key in keyword_search_keys):
+        search_key_class =  KeyWordKey
+      elif search_key == DATETIME_SEARCH_MODE or \
+        (key in datetime_search_keys):
+        search_key_class =  DateTimeKey
+      elif search_key == FULL_TEXT_SEARCH_MODE or \
+        (key in full_text_search_keys):
+        search_key_class =  FullTextKey
+    
+    # get search class based on explicitly passed key type
+    if search_key_class is None:
+      search_key_class = self._getSearchKeyClassByType(type)
+    
     # some use cases where we can just return SQL without grammar staff
     if key is None or (ignore_empty_string and \
                        isinstance(value, basestring) and \
@@ -197,9 +215,6 @@ class SimpleQuery(QueryMixin):
         sql_expressions = {'where_expression':  "%s is NULL" % (key),
                            'select_expression_list': [],}
         return sql_expressions                 
-      # get search class based on explicitly passed key type
-      if search_key_class is None:
-        search_key_class = self._getSearchKeyClassByType(type)   
             
       # we have a list of values and respective operator defined
       if isinstance(value, (tuple, list)):
@@ -259,20 +274,6 @@ class SimpleQuery(QueryMixin):
             # try to guess by type of first_element in list
             search_key_class = self._getSearchKeyClassByValue(value[0])
       
-      # try to get search key type by the key definitions passed
-      if search_key_class is None:
-        if search_key == EXACT_MATCH_SEARCH_MODE:
-          search_key_class =  RawKey
-        elif search_key == KEYWORD_SEARCH_MODE or \
-            (key in keyword_search_keys):
-          search_key_class =  KeyWordKey
-        elif search_key == DATETIME_SEARCH_MODE or \
-          (key in datetime_search_keys):
-          search_key_class =  DateTimeKey
-        elif search_key == FULL_TEXT_SEARCH_MODE or \
-          (key in full_text_search_keys):
-          search_key_class =  FullTextKey
-      
       # get search class based on value of value
       if search_key_class is None:
         search_key_class = self._getSearchKeyClassByValue(value)
diff --git a/product/ZSQLCatalog/tests/testZSQLCatalog.py b/product/ZSQLCatalog/tests/testZSQLCatalog.py
index 84e133bb72..bb0bdfd873 100644
--- a/product/ZSQLCatalog/tests/testZSQLCatalog.py
+++ b/product/ZSQLCatalog/tests/testZSQLCatalog.py
@@ -416,6 +416,21 @@ class TestQuery(unittest.TestCase):
                    select_expression_list=[]),
                 q.asSQLExpression())
 
+  def testListValuesInQuery(self):
+    q = Query(title=('Foo', 'Bar'))
+    self.assertEquals(
+      dict(where_expression="(((((title = 'Foo')))) OR ((((title = 'Bar')))))",
+           select_expression_list=[]),
+        q.asSQLExpression())
+
+  def testListValuesInQueryWithKey(self):
+    q = Query(title=('Foo', 'Bar'), key='Keyword')
+    self.assertEquals(
+      dict(where_expression=
+             "(((((title LIKE '%Foo%')))) OR ((((title LIKE '%Bar%')))))",
+           select_expression_list=[]),
+        q.asSQLExpression())
+
 
 def test_suite():
   suite = unittest.TestSuite()
-- 
2.30.9