From 958c3b87c1f2d984761a5553b4beb2b30cbe1491 Mon Sep 17 00:00:00 2001
From: Vincent Pelletier <vincent@nexedi.com>
Date: Tue, 22 Aug 2017 11:26:01 +0900
Subject: [PATCH] SQLCatalog: Stop relying on portal_selections for SQL
 expression generation

---
 .../erp5_mysql_innodb/z_count_results.sql     | 20 -------------------
 .../erp5_mysql_innodb/z_count_results.xml     |  4 +---
 .../erp5_mysql_innodb/z_search_results.sql    | 20 -------------------
 .../erp5_mysql_innodb/z_search_results.xml    |  4 +---
 product/ZSQLCatalog/SQLCatalog.py             | 17 ++++++++++++----
 5 files changed, 15 insertions(+), 50 deletions(-)

diff --git a/product/ERP5/bootstrap/erp5_mysql_innodb_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/z_count_results.sql b/product/ERP5/bootstrap/erp5_mysql_innodb_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/z_count_results.sql
index cf87d176ab..245aaf9cd7 100644
--- a/product/ERP5/bootstrap/erp5_mysql_innodb_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/z_count_results.sql
+++ b/product/ERP5/bootstrap/erp5_mysql_innodb_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/z_count_results.sql
@@ -13,31 +13,11 @@ FROM
   <dtml-else>
     <dtml-in from_table_list> <dtml-var sequence-item> AS <dtml-var sequence-key><dtml-if sequence-end><dtml-else>,</dtml-if></dtml-in>
   </dtml-if>
-  <dtml-if selection_domain>
-    <dtml-let expression="portal_selections.buildSQLJoinExpressionFromDomainSelection(selection_domain, category_table_alias = 'domain_category')">
-      <dtml-if expression> , <dtml-var expression> </dtml-if>
-    </dtml-let>
-  </dtml-if>
-  <dtml-if selection_report>
-    <dtml-let expression="portal_selections.buildSQLJoinExpressionFromDomainSelection(selection_report, category_table_alias = 'report_category')">
-      <dtml-if expression> , <dtml-var expression> </dtml-if>
-    </dtml-let>
-  </dtml-if>
 WHERE
   1 = 1
 <dtml-if where_expression>
   AND <dtml-var where_expression>
 </dtml-if>
-<dtml-if selection_domain>
-  <dtml-let expression="portal_selections.buildSQLExpressionFromDomainSelection(selection_domain, category_table_alias = 'domain_category')">
-    <dtml-if expression> AND <dtml-var expression> </dtml-if>
-  </dtml-let>
-</dtml-if>
-<dtml-if selection_report>
-  <dtml-let expression="portal_selections.buildSQLExpressionFromDomainSelection(selection_report, strict_membership=1, category_table_alias = 'report_category')">
-    <dtml-if expression> AND <dtml-var expression> </dtml-if>
-  </dtml-let>
-</dtml-if>
 <dtml-if sort_on>
 ORDER BY
   <dtml-var sort_on>
diff --git a/product/ERP5/bootstrap/erp5_mysql_innodb_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/z_count_results.xml b/product/ERP5/bootstrap/erp5_mysql_innodb_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/z_count_results.xml
index a96f463e9f..05019ac40d 100644
--- a/product/ERP5/bootstrap/erp5_mysql_innodb_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/z_count_results.xml
+++ b/product/ERP5/bootstrap/erp5_mysql_innodb_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/z_count_results.xml
@@ -14,9 +14,7 @@
         </item>
         <item>
             <key> <string>arguments_src</string> </key>
-            <value> <string>selection_domain\r\n
-selection_report\r\n
-where_expression\r\n
+            <value> <string>where_expression\r\n
 select_expression\r\n
 group_by_expression\r\n
 from_table_list:list\r\n
diff --git a/product/ERP5/bootstrap/erp5_mysql_innodb_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/z_search_results.sql b/product/ERP5/bootstrap/erp5_mysql_innodb_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/z_search_results.sql
index fd3818a687..1080fb26be 100644
--- a/product/ERP5/bootstrap/erp5_mysql_innodb_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/z_search_results.sql
+++ b/product/ERP5/bootstrap/erp5_mysql_innodb_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/z_search_results.sql
@@ -8,32 +8,12 @@ FROM
   <dtml-else>
     <dtml-in from_table_list> <dtml-var sequence-item> AS <dtml-var sequence-key><dtml-if sequence-end><dtml-else>,</dtml-if></dtml-in>
   </dtml-if>
-  <dtml-if selection_domain>
-    <dtml-let expression="portal_selections.buildSQLJoinExpressionFromDomainSelection(selection_domain, category_table_alias = 'domain_category')">
-      <dtml-if expression> , <dtml-var expression> </dtml-if>
-    </dtml-let>
-  </dtml-if>
-  <dtml-if selection_report>
-    <dtml-let expression="portal_selections.buildSQLJoinExpressionFromDomainSelection(selection_report, category_table_alias = 'report_category')">
-      <dtml-if expression> , <dtml-var expression> </dtml-if>
-    </dtml-let>
-  </dtml-if>
 
 WHERE 
   1 = 1 
 <dtml-if where_expression>
   AND <dtml-var where_expression>
 </dtml-if>
-<dtml-if selection_domain>
-  <dtml-let expression="portal_selections.buildSQLExpressionFromDomainSelection(selection_domain, category_table_alias = 'domain_category')">
-    <dtml-if expression> AND <dtml-var expression> </dtml-if>
-  </dtml-let>
-</dtml-if>
-<dtml-if selection_report>
-  <dtml-let expression="portal_selections.buildSQLExpressionFromDomainSelection(selection_report, strict_membership=1, category_table_alias = 'report_category')">
-    <dtml-if expression> AND <dtml-var expression> </dtml-if>
-  </dtml-let>
-</dtml-if>
 <dtml-if group_by_expression>
 GROUP BY
   <dtml-var group_by_expression>
diff --git a/product/ERP5/bootstrap/erp5_mysql_innodb_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/z_search_results.xml b/product/ERP5/bootstrap/erp5_mysql_innodb_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/z_search_results.xml
index b2453276c2..e508f440a8 100644
--- a/product/ERP5/bootstrap/erp5_mysql_innodb_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/z_search_results.xml
+++ b/product/ERP5/bootstrap/erp5_mysql_innodb_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/z_search_results.xml
@@ -14,9 +14,7 @@
         </item>
         <item>
             <key> <string>arguments_src</string> </key>
-            <value> <string>selection_domain\r\n
-selection_report\r\n
-where_expression\r\n
+            <value> <string>where_expression\r\n
 select_expression\r\n
 group_by_expression\r\n
 from_table_list:list\r\n
diff --git a/product/ZSQLCatalog/SQLCatalog.py b/product/ZSQLCatalog/SQLCatalog.py
index 539a975fad..114603f6ae 100644
--- a/product/ZSQLCatalog/SQLCatalog.py
+++ b/product/ZSQLCatalog/SQLCatalog.py
@@ -143,6 +143,10 @@ else:
 UID_BUFFER_SIZE = 300
 OBJECT_LIST_SIZE = 300 # XXX 300 is arbitrary value of catalog object list
 MAX_PATH_LEN = 255
+DOMAIN_STRICT_MEMBERSHIP_DICT = {
+  'selection_domain': False,
+  'selection_report': True,
+}
 
 manage_addSQLCatalogForm = DTMLFile('dtml/addSQLCatalog',globals())
 
@@ -2229,6 +2233,13 @@ class Catalog(Folder,
     empty_value_dict = {}
     for key, value in kw.iteritems():
       result = None
+      if key in DOMAIN_STRICT_MEMBERSHIP_DICT:
+        if value is None:
+          continue
+        value = self.getPortalObject().portal_selections.asDomainQuery(
+          value,
+          strict_membership=DOMAIN_STRICT_MEMBERSHIP_DICT[key],
+        )
       if isinstance(value, dict_type_list):
         # Cast dict-ish types into plain dicts.
         value = dict(value)
@@ -2547,8 +2558,6 @@ class Catalog(Folder,
         REQUEST=None,
         src__=0,
         build_sql_query_method=None,
-        selection_domain=None,
-        selection_report=None,
         # XXX should get zsql_brain from ZSQLMethod class itself
         zsql_brain=None,
         implicit_join=False,
@@ -2564,8 +2573,8 @@ class Catalog(Folder,
     return sql_method(
       src__=src__,
       zsql_brain=zsql_brain,
-      selection_domain=selection_domain,
-      selection_report=selection_report,
+      selection_domain=None, # BBB
+      selection_report=None, # BBB
       where_expression=query['where_expression'],
       select_expression=query['select_expression'],
       group_by_expression=query['group_by_expression'],
-- 
2.30.9