From 4ed8961729a43758e865125daa0cc9985358bede Mon Sep 17 00:00:00 2001
From: Yoshinori Okuji <yo@nexedi.com>
Date: Mon, 11 Apr 2005 15:22:44 +0000
Subject: [PATCH] Support limit_expression.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@2859 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 .../ERP5Catalog/sql/erp5_mysql/z_search_results.zsql | 10 ++++++++--
 product/ZSQLCatalog/SQLCatalog.py                    | 12 ++++++++++--
 2 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/product/ERP5Catalog/sql/erp5_mysql/z_search_results.zsql b/product/ERP5Catalog/sql/erp5_mysql/z_search_results.zsql
index 965ec5b29a..34682a9e21 100755
--- a/product/ERP5Catalog/sql/erp5_mysql/z_search_results.zsql
+++ b/product/ERP5Catalog/sql/erp5_mysql/z_search_results.zsql
@@ -1,7 +1,7 @@
 <dtml-comment>
 title:
 connection_id:erp5_sql_connection
-max_rows:500
+max_rows:0
 max_cache:100
 cache_time:0
 class_name:ZSQLBrain
@@ -13,7 +13,8 @@ selection_report
 where_expression
 select_expression
 from_table_list:list
-sort_on</params>
+sort_on
+limit_expression</params>
 SELECT DISTINCT
   <dtml-in getCatalogSearchResultKeys> <dtml-var sequence-item><dtml-if sequence-end> <dtml-else>, </dtml-if> </dtml-in>
 
@@ -37,3 +38,8 @@ WHERE
 ORDER BY
   <dtml-var sort_on>
 </dtml-if>
+<dtml-if limit_expression>
+LIMIT <dtml-var "limit_expression">
+<dtml-else>
+LIMIT 1000
+</dtml-if>
diff --git a/product/ZSQLCatalog/SQLCatalog.py b/product/ZSQLCatalog/SQLCatalog.py
index a0257d1239..b9b189114f 100755
--- a/product/ZSQLCatalog/SQLCatalog.py
+++ b/product/ZSQLCatalog/SQLCatalog.py
@@ -1339,7 +1339,7 @@ class Catalog(Folder, Persistent, Acquisition.Implicit, ExtensionClass.Base):
       # We can now consider that old style query is changed into new style
       for key in kw.keys(): # Do not use kw.items() because this consumes much more memory
         value = kw[key]
-        if key not in ('where_expression', 'sort-on', 'sort_on', 'sort-order', 'sort_order'):
+        if key not in ('where_expression', 'sort-on', 'sort_on', 'sort-order', 'sort_order', 'limit'):
           # Make sure key belongs to schema
           key_is_acceptable = key in acceptable_keys # Only calculate once
           key_is_related = key in related_keys
@@ -1487,11 +1487,18 @@ class Catalog(Folder, Persistent, Acquisition.Implicit, ExtensionClass.Base):
           where_expression = "(%s) AND (%s)" % (kw['where_expression'], join(where_expression, ' AND ') )
       else:
         where_expression = join(where_expression, ' AND ')
+        
+      limit_expression = kw.get('limit', None)
+      if type(limit_expression) in (type(()), type([])):
+        limit_expression = '%s,%s' % (limit_expression[0], limit_expression[1])
+      elif limit_expression is not None:
+        limit_expression = str(limit_expression)
 
     # Use a dictionary at the moment.
     return { 'from_table_list' : from_table_dict.items(),
              'order_by_expression' : sort_on,
-             'where_expression' : where_expression }
+             'where_expression' : where_expression,
+             'limit_expression' : limit_expression }
 
   def queryResults(self, sql_method, REQUEST=None, used=None, src__=0, **kw):
     """ Returns a list of brains from a set of constraints on variables """
@@ -1499,6 +1506,7 @@ class Catalog(Folder, Persistent, Acquisition.Implicit, ExtensionClass.Base):
     kw['where_expression'] = query['where_expression']
     kw['sort_on'] = query['order_by_expression']
     kw['from_table_list'] = query['from_table_list']
+    kw['limit_expression'] = query['limit_expression']
     # Return the result
 
     #LOG('acceptable_keys',0,'acceptable_keys: %s' % str(acceptable_keys))
-- 
2.30.9