| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * Just another collection of static utils functions. |
| 14 | * |
| 15 | * @package CRM |
| 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 17 | */ |
| 18 | class CRM_Utils_SQL { |
| 19 | |
| 20 | /** |
| 21 | * Helper function for adding the permissioned subquery from one entity onto another |
| 22 | * |
| 23 | * @param string $entity |
| 24 | * @param string $joinColumn |
| 25 | * @return array |
| 26 | */ |
| 27 | public static function mergeSubquery($entity, $joinColumn = 'id') { |
| 28 | require_once 'api/v3/utils.php'; |
| 29 | $baoName = _civicrm_api3_get_BAO($entity); |
| 30 | $bao = new $baoName(); |
| 31 | $clauses = $subclauses = []; |
| 32 | foreach ((array) $bao->addSelectWhereClause() as $field => $vals) { |
| 33 | if ($vals && $field == $joinColumn) { |
| 34 | $clauses = array_merge($clauses, (array) $vals); |
| 35 | } |
| 36 | elseif ($vals) { |
| 37 | $subclauses[] = "$field " . implode(" AND $field ", (array) $vals); |
| 38 | } |
| 39 | } |
| 40 | if ($subclauses) { |
| 41 | $clauses[] = "IN (SELECT `$joinColumn` FROM `" . $bao->tableName() . "` WHERE " . implode(' AND ', $subclauses) . ")"; |
| 42 | } |
| 43 | return $clauses; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Get current sqlModes of the session |
| 48 | * @return array |
| 49 | */ |
| 50 | public static function getSqlModes() { |
| 51 | $sqlModes = explode(',', CRM_Core_DAO::singleValueQuery('SELECT @@sql_mode')); |
| 52 | return $sqlModes; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Checks if this system enforce the MYSQL mode ONLY_FULL_GROUP_BY. |
| 57 | * This function should be named supportsAnyValueAndEnforcesFullGroupBY(), |
| 58 | * but should be deprecated instead. |
| 59 | * |
| 60 | * @return mixed |
| 61 | * @deprecated |
| 62 | */ |
| 63 | public static function supportsFullGroupBy() { |
| 64 | // CRM-21455 MariaDB 10.2 does not support ANY_VALUE |
| 65 | $version = self::getDatabaseVersion(); |
| 66 | |
| 67 | if (stripos($version, 'mariadb') !== FALSE) { |
| 68 | return FALSE; |
| 69 | } |
| 70 | |
| 71 | return version_compare($version, '5.7', '>='); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Disable ONLY_FULL_GROUP_BY for MySQL versions lower then 5.7 |
| 76 | * |
| 77 | * @return bool |
| 78 | */ |
| 79 | public static function disableFullGroupByMode() { |
| 80 | $sqlModes = self::getSqlModes(); |
| 81 | |
| 82 | // Disable only_full_group_by mode for lower sql versions. |
| 83 | if (!self::supportsFullGroupBy() || (!empty($sqlModes) && !in_array('ONLY_FULL_GROUP_BY', $sqlModes))) { |
| 84 | if ($key = array_search('ONLY_FULL_GROUP_BY', $sqlModes)) { |
| 85 | unset($sqlModes[$key]); |
| 86 | CRM_Core_DAO::executeQuery("SET SESSION sql_mode = '" . implode(',', $sqlModes) . "'"); |
| 87 | } |
| 88 | return TRUE; |
| 89 | } |
| 90 | |
| 91 | return FALSE; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * CHeck if ONLY_FULL_GROUP_BY is in the global sql_modes |
| 96 | * @return bool |
| 97 | */ |
| 98 | public static function isGroupByModeInDefault() { |
| 99 | $sqlModes = explode(',', CRM_Core_DAO::singleValueQuery('SELECT @@global.sql_mode')); |
| 100 | if (!in_array('ONLY_FULL_GROUP_BY', $sqlModes)) { |
| 101 | return FALSE; |
| 102 | } |
| 103 | return TRUE; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Does the DB version support mutliple locks per |
| 108 | * |
| 109 | * https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_get-lock |
| 110 | * |
| 111 | * This is a conservative measure to introduce the change which we expect to deprecate later. |
| 112 | * |
| 113 | * @todo we only check mariadb & mysql right now but maybe can add percona. |
| 114 | */ |
| 115 | public static function supportsMultipleLocks() { |
| 116 | static $isSupportLocks = NULL; |
| 117 | if (!isset($isSupportLocks)) { |
| 118 | $version = self::getDatabaseVersion(); |
| 119 | if (stripos($version, 'mariadb') !== FALSE) { |
| 120 | $isSupportLocks = version_compare($version, '10.0.2', '>='); |
| 121 | } |
| 122 | else { |
| 123 | $isSupportLocks = version_compare($version, '5.7.5', '>='); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | return $isSupportLocks; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Get the version string for the database. |
| 132 | * |
| 133 | * @return string |
| 134 | */ |
| 135 | public static function getDatabaseVersion() { |
| 136 | return CRM_Core_DAO::singleValueQuery('SELECT VERSION()'); |
| 137 | } |
| 138 | |
| 139 | } |