From: Seamus Lee Date: Fri, 27 Oct 2017 09:54:08 +0000 (+1100) Subject: Move supportsFullGroupBy to CRM_Utils_SQL and add a CRM_Utils_SQL::getSqlModes functi... X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=712e729fddb35d5e15d9beaf761a3b438d738f59;p=civicrm-core.git Move supportsFullGroupBy to CRM_Utils_SQL and add a CRM_Utils_SQL::getSqlModes function and set private test variable and update code as needed --- diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 35be41f176..55c28556fe 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -4716,7 +4716,7 @@ civicrm_relationship.is_permission_a_b = 0 $sqlMode = CRM_Core_DAO::singleValueQuery('SELECT @@sql_mode'); //return if ONLY_FULL_GROUP_BY is not enabled. - if (CRM_Core_DAO::supportsFullGroupBy() && !empty($sqlMode) && in_array('ONLY_FULL_GROUP_BY', explode(',', $sqlMode))) { + if (CRM_Utils_SQL::supportsFullGroupBy() && !empty($sqlMode) && in_array('ONLY_FULL_GROUP_BY', explode(',', $sqlMode))) { $regexToExclude = '/(ROUND|AVG|COUNT|GROUP_CONCAT|SUM|MAX|MIN)\(/i'; foreach ($selectClauses as $key => $val) { $aliasArray = preg_split('/ as /i', $val); diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index 4f3bb20633..1077d8cbe0 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -118,10 +118,9 @@ class CRM_Core_DAO extends DB_DataObject { } $factory = new CRM_Contact_DAO_Factory(); CRM_Core_DAO::setFactory($factory); - $currentSqlMode = CRM_Core_DAO::singleValueQuery("SELECT @@GLOBAL.sql_mode"); - $currentModes = explode(',', $currentSqlMode); + $currentModes = CRM_Utils_SQL::getSqlModes(); if (CRM_Utils_Constant::value('CIVICRM_MYSQL_STRICT', CRM_Utils_System::isDevelopment())) { - if (self::supportsFullGroupBy() && !in_array('ONLY_FULL_GROUP_BY', $currentModes)) { + if (CRM_Utils_SQL::supportsFullGroupBy() && !in_array('ONLY_FULL_GROUP_BY', $currentModes)) { $currentModes[] = 'ONLY_FULL_GROUP_BY'; } if (!in_array('STRICT_TRANS_TABLES', $currentModes)) { @@ -410,14 +409,6 @@ class CRM_Core_DAO extends DB_DataObject { } } - /** - * Does this System support the MYSQL mode ONLY_FULL_GROUP_BY - * @return mixed - */ - public static function supportsFullGroupBy() { - return version_compare(CRM_Core_DAO::singleValueQuery('SELECT VERSION()'), '5.7', '>='); - } - /** * Defines the default key as 'id'. * diff --git a/CRM/Utils/SQL.php b/CRM/Utils/SQL.php index 574b3a6cfe..98f437e09a 100644 --- a/CRM/Utils/SQL.php +++ b/CRM/Utils/SQL.php @@ -59,4 +59,21 @@ class CRM_Utils_SQL { return $clauses; } + /** + * Get current sqlModes of the session + * @return array + */ + public static function getSqlModes() { + $sqlModes = explode(',', CRM_Core_DAO::singleValueQuery('SELECT @@sql_mode')); + return $sqlModes; + } + + /** + * Does this System support the MYSQL mode ONLY_FULL_GROUP_BY + * @return mixed + */ + public static function supportsFullGroupBy() { + return version_compare(CRM_Core_DAO::singleValueQuery('SELECT VERSION()'), '5.7', '>='); + } + } diff --git a/tests/phpunit/CRM/Core/DAOTest.php b/tests/phpunit/CRM/Core/DAOTest.php index 49879fd0ff..b569237928 100644 --- a/tests/phpunit/CRM/Core/DAOTest.php +++ b/tests/phpunit/CRM/Core/DAOTest.php @@ -384,11 +384,10 @@ class CRM_Core_DAOTest extends CiviUnitTestCase { * Test that known sql modes are present in session. */ public function testSqlModePresent() { - $currentSqlModes = CRM_Core_DAO::singleValueQuery("SELECT @@sql_mode"); - $sqlModes = explode(',', $currentSqlModes); + $sqlModes = CRM_Utils_SQL::getSqlModes(); // assert we have strict trans $this->assertContains('STRICT_TRANS_TABLES', $sqlModes); - if (CRM_Core_DAO::supportsFullGroupBy()) { + if ($this->_supportFullGroupBy) { $this->assertContains('ONLY_FULL_GROUP_BY', $sqlModes); } } diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 24922f270f..324bb4ac2e 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -64,6 +64,11 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { */ private static $dbInit = FALSE; + /** + * Does the current setup support ONLY_FULL_GROUP_BY mode + */ + protected $_supportFullGroupBy; + /** * Database connection. * @@ -291,6 +296,8 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { // Get and save a connection to the database $this->_dbconn = $this->getConnection(); + $this->_supportFullGroupBy = CRM_Utils_SQL::supportsFullGroupBy(); + // reload database before each test // $this->_populateDB();