Commit | Line | Data |
---|---|---|
d1d3c04a CW |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
d1d3c04a | 5 | | | |
bc77d7c0 TO |
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 | | |
d1d3c04a CW |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
13 | * Just another collection of static utils functions. | |
14 | * | |
15 | * @package CRM | |
ca5cec67 | 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
d1d3c04a CW |
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') { | |
4f0e32e0 | 28 | require_once 'api/v3/utils.php'; |
d1d3c04a CW |
29 | $baoName = _civicrm_api3_get_BAO($entity); |
30 | $bao = new $baoName(); | |
be2fb01f | 31 | $clauses = $subclauses = []; |
d1d3c04a CW |
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 | ||
712e729f SL |
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 | /** | |
2c40ef6b ML |
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 | * | |
712e729f | 60 | * @return mixed |
2c40ef6b | 61 | * @deprecated |
712e729f SL |
62 | */ |
63 | public static function supportsFullGroupBy() { | |
2c40ef6b | 64 | // CRM-21455 MariaDB 10.2 does not support ANY_VALUE |
79ee443c | 65 | $version = self::getDatabaseVersion(); |
2c40ef6b | 66 | |
6454ff54 | 67 | if (stripos($version, 'mariadb') !== FALSE) { |
2c40ef6b ML |
68 | return FALSE; |
69 | } | |
70 | ||
71 | return version_compare($version, '5.7', '>='); | |
712e729f SL |
72 | } |
73 | ||
b07c960a | 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 | ||
a2ed62b3 SL |
94 | /** |
95 | * CHeck if ONLY_FULL_GROUP_BY is in the global sql_modes | |
96 | * @return bool | |
97 | */ | |
98 | public static function isGroupByModeInDefault() { | |
a2ed62b3 SL |
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 | ||
79ee443c | 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 | * | |
79ee443c | 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() { | |
79ee443c | 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 | ||
d1d3c04a | 139 | } |