Move supportsFullGroupBy to CRM_Utils_SQL and add a CRM_Utils_SQL::getSqlModes functi...
authorSeamus Lee <seamuslee001@gmail.com>
Fri, 27 Oct 2017 09:54:08 +0000 (20:54 +1100)
committerSeamus Lee <seamuslee001@gmail.com>
Fri, 27 Oct 2017 09:54:38 +0000 (20:54 +1100)
CRM/Contact/BAO/Query.php
CRM/Core/DAO.php
CRM/Utils/SQL.php
tests/phpunit/CRM/Core/DAOTest.php
tests/phpunit/CiviTest/CiviUnitTestCase.php

index 35be41f1761e040e57182b2a71cbfb22d57f982e..55c28556fe78369952dab0db06d3f49e001485ba 100644 (file)
@@ -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);
index 4f3bb2063303862d7241841ab4176c07db095424..1077d8cbe0da8d4045390723d409da5a58b44091 100644 (file)
@@ -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'.
    *
index 574b3a6cfecaba54593e8271a1bf04b648e36198..98f437e09a3e6bf35d27cc4fe2ce7af0956040cb 100644 (file)
@@ -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', '>=');
+  }
+
 }
index 49879fd0ff10a0e38ace68b9b45bd78a0c190f86..b569237928febed099f4512e875ab3321ee5565f 100644 (file)
@@ -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);
     }
   }
index 24922f270fd2694d57ca502c800898ce4ca1dc27..324bb4ac2ebde36f03463cef41312b3f1fb12381 100644 (file)
@@ -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();