From e2d1567aa752428232e55eb27d46de221ed9b11a Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Sun, 29 Apr 2018 12:02:04 +1000 Subject: [PATCH 1/2] Dev/Core#90 Disable Only Full Group By sql mode on specific queries to get tests to pass so we can switch to using MySQL 5.7 for PR Tests --- CRM/Core/DAO.php | 23 ++++++++++++++++++++++ CRM/Export/BAO/Export.php | 7 +++++-- CRM/Mailing/Event/BAO/TrackableURLOpen.php | 4 ++-- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index 565a84ad1a6b..b549a1a822c5 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -164,6 +164,29 @@ public static function getConnection() { return $_DB_DATAOBJECT['CONNECTIONS'][$dao->_database_dsn_md5]; } + /** + * Disables usage of the ONLY_FULL_GROUP_BY Mode if necessary + */ + public static function disableFullGroupByMode() { + $currentModes = CRM_Utils_SQL::getSqlModes(); + if (CRM_Utils_SQL::supportsFullGroupBy() && in_array('ONLY_FULL_GROUP_BY', $currentModes) && CRM_Utils_SQL::isGroupByModeInDefault()) { + $key = array_search('ONLY_FULL_GROUP_BY', $currentModes); + unset($currentModes[$key]); + CRM_Core_DAO::executeQuery("SET SESSION sql_mode = %1", array(1 => array(implode(',', $currentModes), 'String'))); + } + } + + /** + * Enables ONLY_FULL_GROUP_BY sql_mode as necessary.. + */ + public static function enableFullGroupByMode() { + $currentModes = CRM_Utils_SQL::getSqlModes(); + if (CRM_Utils_SQL::supportsFullGroupBy() && !in_array('ONLY_FULL_GROUP_BY', $currentModes) && CRM_Utils_SQL::isGroupByModeInDefault()) { + $currentModes[] = 'ONLY_FULL_GROUP_BY'; + CRM_Core_DAO::executeQuery("SET SESSION sql_mode = %1", array(1 => array(implode(',', $currentModes), 'String'))); + } + } + /** * @param string $fieldName * @param $fieldDef diff --git a/CRM/Export/BAO/Export.php b/CRM/Export/BAO/Export.php index 70a1a14e7358..ccc095f057de 100644 --- a/CRM/Export/BAO/Export.php +++ b/CRM/Export/BAO/Export.php @@ -204,6 +204,9 @@ public static function getGroupBy($exportMode, $queryMode, $returnProperties, $q } if (!empty($groupBy)) { + if (!Civi::settings()->get('searchPrimaryDetailsOnly')) { + CRM_Core_DAO::disableFullGroupByMode(); + } $groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($query->_select, $groupBy); } @@ -1004,10 +1007,11 @@ public static function exportComponents( // delete the export temp table and component table $sql = "DROP TABLE IF EXISTS {$exportTempTable}"; CRM_Core_DAO::executeQuery($sql); - + CRM_Core_DAO::enableFullGroupByMode(); CRM_Utils_System::civiExit(); } else { + CRM_Core_DAO::enableFullGroupByMode(); throw new CRM_Core_Exception(ts('No records to export')); } } @@ -1309,7 +1313,6 @@ public static function writeDetailsToTable($tableName, &$details, &$sqlColumns) INSERT INTO $tableName $sqlColumnString VALUES $sqlValueString "; - CRM_Core_DAO::executeQuery($sql); } diff --git a/CRM/Mailing/Event/BAO/TrackableURLOpen.php b/CRM/Mailing/Event/BAO/TrackableURLOpen.php index 992a57852203..bd75ce5cf70c 100644 --- a/CRM/Mailing/Event/BAO/TrackableURLOpen.php +++ b/CRM/Mailing/Event/BAO/TrackableURLOpen.php @@ -357,9 +357,9 @@ public static function &getRows( //Added "||$rowCount" to avoid displaying all records on first page $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer'); } - + CRM_Core_DAO::disableFullGroupByMode(); $dao->query($query); - + CRM_Core_DAO::enableFullGroupByMode(); $results = array(); while ($dao->fetch()) { From 22e0ffed057b72422add063e564cce56a2efcdbc Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Tue, 1 May 2018 14:20:36 +1000 Subject: [PATCH 2/2] Rename function enableFullGroupByMode to be reenableFullGroupByMode to be more expliict about what the function does --- CRM/Core/DAO.php | 4 ++-- CRM/Export/BAO/Export.php | 4 ++-- CRM/Mailing/Event/BAO/TrackableURLOpen.php | 2 +- CRM/Report/Form.php | 1 + 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index b549a1a822c5..4dbfd0dafce1 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -177,9 +177,9 @@ public static function disableFullGroupByMode() { } /** - * Enables ONLY_FULL_GROUP_BY sql_mode as necessary.. + * Re-enables ONLY_FULL_GROUP_BY sql_mode as necessary.. */ - public static function enableFullGroupByMode() { + public static function reenableFullGroupByMode() { $currentModes = CRM_Utils_SQL::getSqlModes(); if (CRM_Utils_SQL::supportsFullGroupBy() && !in_array('ONLY_FULL_GROUP_BY', $currentModes) && CRM_Utils_SQL::isGroupByModeInDefault()) { $currentModes[] = 'ONLY_FULL_GROUP_BY'; diff --git a/CRM/Export/BAO/Export.php b/CRM/Export/BAO/Export.php index ccc095f057de..3abe80234a4b 100644 --- a/CRM/Export/BAO/Export.php +++ b/CRM/Export/BAO/Export.php @@ -1007,11 +1007,11 @@ public static function exportComponents( // delete the export temp table and component table $sql = "DROP TABLE IF EXISTS {$exportTempTable}"; CRM_Core_DAO::executeQuery($sql); - CRM_Core_DAO::enableFullGroupByMode(); + CRM_Core_DAO::reenableFullGroupByMode(); CRM_Utils_System::civiExit(); } else { - CRM_Core_DAO::enableFullGroupByMode(); + CRM_Core_DAO::reenableFullGroupByMode(); throw new CRM_Core_Exception(ts('No records to export')); } } diff --git a/CRM/Mailing/Event/BAO/TrackableURLOpen.php b/CRM/Mailing/Event/BAO/TrackableURLOpen.php index bd75ce5cf70c..97987f61bd5a 100644 --- a/CRM/Mailing/Event/BAO/TrackableURLOpen.php +++ b/CRM/Mailing/Event/BAO/TrackableURLOpen.php @@ -359,7 +359,7 @@ public static function &getRows( } CRM_Core_DAO::disableFullGroupByMode(); $dao->query($query); - CRM_Core_DAO::enableFullGroupByMode(); + CRM_Core_DAO::reenableFullGroupByMode(); $results = array(); while ($dao->fetch()) { diff --git a/CRM/Report/Form.php b/CRM/Report/Form.php index c8795f440780..6f8d36c8946b 100644 --- a/CRM/Report/Form.php +++ b/CRM/Report/Form.php @@ -2750,6 +2750,7 @@ public function unselectedSectionColumns() { */ public function buildRows($sql, &$rows) { $dao = CRM_Core_DAO::executeQuery($sql); + CRM_Core_DAO::reenableFullGroupByMode(); if (!is_array($rows)) { $rows = array(); }