From 84cb7d107586417b3b7cc2bf3cc7568cf0aa0fd4 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Sun, 29 Apr 2018 12:02:04 +1000 Subject: [PATCH] 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 cab84ffeb0ee..294c1e4f7215 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 c1d6843e74f7..9b1523e1ad68 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 dcc1b9a6d810..ba933218e1cb 100644 --- a/CRM/Mailing/Event/BAO/TrackableURLOpen.php +++ b/CRM/Mailing/Event/BAO/TrackableURLOpen.php @@ -364,9 +364,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()) {