From 646ef35ad22a903f3ea510ffaa20690855b7cbe1 Mon Sep 17 00:00:00 2001
From: Kazuhiko Shiozaki <kazuhiko@nexedi.com>
Date: Wed, 22 Jun 2016 15:07:37 +0200
Subject: [PATCH] SQLCatalog: add getOptimizerSwitchKeyList().

---
 ...z_select_optimizer_switch.catalog_keys.xml |  5 ++
 .../z_select_optimizer_switch.sql             |  1 +
 .../z_select_optimizer_switch.xml             | 53 +++++++++++++++++++
 .../bt/template_catalog_method_id_list        |  1 +
 product/ZSQLCatalog/SQLCatalog.py             | 30 +++++++++++
 5 files changed, 90 insertions(+)
 create mode 100644 product/ERP5/bootstrap/erp5_mysql_innodb_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/z_select_optimizer_switch.catalog_keys.xml
 create mode 100644 product/ERP5/bootstrap/erp5_mysql_innodb_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/z_select_optimizer_switch.sql
 create mode 100644 product/ERP5/bootstrap/erp5_mysql_innodb_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/z_select_optimizer_switch.xml

diff --git a/product/ERP5/bootstrap/erp5_mysql_innodb_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/z_select_optimizer_switch.catalog_keys.xml b/product/ERP5/bootstrap/erp5_mysql_innodb_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/z_select_optimizer_switch.catalog_keys.xml
new file mode 100644
index 0000000000..da699b5e07
--- /dev/null
+++ b/product/ERP5/bootstrap/erp5_mysql_innodb_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/z_select_optimizer_switch.catalog_keys.xml
@@ -0,0 +1,5 @@
+<catalog_method>
+ <item key="sql_optimizer_switch" type="int">
+  <value>1</value>
+ </item>
+</catalog_method>
diff --git a/product/ERP5/bootstrap/erp5_mysql_innodb_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/z_select_optimizer_switch.sql b/product/ERP5/bootstrap/erp5_mysql_innodb_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/z_select_optimizer_switch.sql
new file mode 100644
index 0000000000..045060ab07
--- /dev/null
+++ b/product/ERP5/bootstrap/erp5_mysql_innodb_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/z_select_optimizer_switch.sql
@@ -0,0 +1 @@
+SELECT @@optimizer_switch
\ No newline at end of file
diff --git a/product/ERP5/bootstrap/erp5_mysql_innodb_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/z_select_optimizer_switch.xml b/product/ERP5/bootstrap/erp5_mysql_innodb_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/z_select_optimizer_switch.xml
new file mode 100644
index 0000000000..3b14fd6e57
--- /dev/null
+++ b/product/ERP5/bootstrap/erp5_mysql_innodb_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/z_select_optimizer_switch.xml
@@ -0,0 +1,53 @@
+<?xml version="1.0"?>
+<ZopeData>
+  <record id="1" aka="AAAAAAAAAAE=">
+    <pickle>
+      <global name="SQL" module="Products.ZSQLMethods.SQL"/>
+    </pickle>
+    <pickle>
+      <dictionary>
+        <item>
+            <key> <string>_col</string> </key>
+            <value>
+              <list>
+                <dictionary>
+                  <item>
+                      <key> <string>name</string> </key>
+                      <value> <string>@@optimizer_switch</string> </value>
+                  </item>
+                  <item>
+                      <key> <string>null</string> </key>
+                      <value> <int>1</int> </value>
+                  </item>
+                  <item>
+                      <key> <string>type</string> </key>
+                      <value> <string>t</string> </value>
+                  </item>
+                  <item>
+                      <key> <string>width</string> </key>
+                      <value> <int>610</int> </value>
+                  </item>
+                </dictionary>
+              </list>
+            </value>
+        </item>
+        <item>
+            <key> <string>arguments_src</string> </key>
+            <value> <string></string> </value>
+        </item>
+        <item>
+            <key> <string>connection_id</string> </key>
+            <value> <string>erp5_sql_connection</string> </value>
+        </item>
+        <item>
+            <key> <string>id</string> </key>
+            <value> <string>z_select_optimizer_switch</string> </value>
+        </item>
+        <item>
+            <key> <string>title</string> </key>
+            <value> <string></string> </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+</ZopeData>
diff --git a/product/ERP5/bootstrap/erp5_mysql_innodb_catalog/bt/template_catalog_method_id_list b/product/ERP5/bootstrap/erp5_mysql_innodb_catalog/bt/template_catalog_method_id_list
index 00e5a30fc0..c30e334644 100644
--- a/product/ERP5/bootstrap/erp5_mysql_innodb_catalog/bt/template_catalog_method_id_list
+++ b/product/ERP5/bootstrap/erp5_mysql_innodb_catalog/bt/template_catalog_method_id_list
@@ -130,6 +130,7 @@ erp5_mysql_innodb/z_related_uid_or_parent_uid
 erp5_mysql_innodb/z_reserve_uid
 erp5_mysql_innodb/z_search_results
 erp5_mysql_innodb/z_search_security
+erp5_mysql_innodb/z_select_optimizer_switch
 erp5_mysql_innodb/z_show_category_columns
 erp5_mysql_innodb/z_show_columns
 erp5_mysql_innodb/z_show_index
diff --git a/product/ZSQLCatalog/SQLCatalog.py b/product/ZSQLCatalog/SQLCatalog.py
index f798c5737a..4368f5f238 100644
--- a/product/ZSQLCatalog/SQLCatalog.py
+++ b/product/ZSQLCatalog/SQLCatalog.py
@@ -474,6 +474,11 @@ class Catalog(Folder,
       'type'    : 'selection',
       'select_variable' : 'getCatalogMethodIds',
       'mode'    : 'w' },
+    { 'id'      : 'sql_optimizer_switch',
+      'description': 'Method to get optimizer_switch value',
+      'type'    : 'selection',
+      'select_variable' : 'getCatalogMethodIds',
+      'mode'    : 'w' },
     { 'id'      : 'sql_catalog_tables',
       'description' : 'Method to get the main catalog tables',
       'type'    : 'selection',
@@ -599,6 +604,7 @@ class Catalog(Folder,
   sql_count_results = ''
   sql_getitem_by_path = ''
   sql_getitem_by_uid = ''
+  sql_optimizer_switch = ''
   sql_catalog_tables = ''
   sql_search_tables = ()
   sql_catalog_schema = ''
@@ -2899,6 +2905,30 @@ class Catalog(Folder,
             }
         return getEngine().getContext(data)
 
+  def _getOptimizerSwitch(self):
+    method_name = self.sql_optimizer_switch
+    try:
+      method = getattr(self, method_name)
+    except AttributeError:
+      pass
+    else:
+      try:
+        return method()[0][0]
+      except (ConflictError, DatabaseError):
+        raise
+      except Exception:
+        pass
+
+    LOG('SQLCatalog', WARNING, 'getTableIds failed with the method %s'
+        % method_name, error=sys.exc_info())
+    return ''
+
+  security.declarePublic('getOptimizerSwitchKeyList')
+  @transactional_cache_decorator('SQLCatalog.getOptimizerSwitchKeyList')
+  def getOptimizerSwitchKeyList(self):
+    return [pair.split('=', 1)[0] for pair in \
+              self._getOptimizerSwitch().split(',')]
+
 InitializeClass(Catalog)
 
 class CatalogError(Exception): pass
-- 
2.30.9