Commit | Line | Data |
---|---|---|
d1d3c04a CW |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
d1d3c04a | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
d1d3c04a CW |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * Just another collection of static utils functions. | |
30 | * | |
31 | * @package CRM | |
6b83d5bd | 32 | * @copyright CiviCRM LLC (c) 2004-2019 |
d1d3c04a CW |
33 | */ |
34 | class CRM_Utils_SQL { | |
35 | ||
36 | /** | |
37 | * Helper function for adding the permissioned subquery from one entity onto another | |
38 | * | |
39 | * @param string $entity | |
40 | * @param string $joinColumn | |
41 | * @return array | |
42 | */ | |
43 | public static function mergeSubquery($entity, $joinColumn = 'id') { | |
4f0e32e0 | 44 | require_once 'api/v3/utils.php'; |
d1d3c04a CW |
45 | $baoName = _civicrm_api3_get_BAO($entity); |
46 | $bao = new $baoName(); | |
be2fb01f | 47 | $clauses = $subclauses = []; |
d1d3c04a CW |
48 | foreach ((array) $bao->addSelectWhereClause() as $field => $vals) { |
49 | if ($vals && $field == $joinColumn) { | |
50 | $clauses = array_merge($clauses, (array) $vals); | |
51 | } | |
52 | elseif ($vals) { | |
53 | $subclauses[] = "$field " . implode(" AND $field ", (array) $vals); | |
54 | } | |
55 | } | |
56 | if ($subclauses) { | |
57 | $clauses[] = "IN (SELECT `$joinColumn` FROM `" . $bao->tableName() . "` WHERE " . implode(' AND ', $subclauses) . ")"; | |
58 | } | |
59 | return $clauses; | |
60 | } | |
61 | ||
712e729f SL |
62 | /** |
63 | * Get current sqlModes of the session | |
64 | * @return array | |
65 | */ | |
66 | public static function getSqlModes() { | |
67 | $sqlModes = explode(',', CRM_Core_DAO::singleValueQuery('SELECT @@sql_mode')); | |
68 | return $sqlModes; | |
69 | } | |
70 | ||
71 | /** | |
2c40ef6b ML |
72 | * Checks if this system enforce the MYSQL mode ONLY_FULL_GROUP_BY. |
73 | * This function should be named supportsAnyValueAndEnforcesFullGroupBY(), | |
74 | * but should be deprecated instead. | |
75 | * | |
712e729f | 76 | * @return mixed |
2c40ef6b | 77 | * @deprecated |
712e729f SL |
78 | */ |
79 | public static function supportsFullGroupBy() { | |
2c40ef6b | 80 | // CRM-21455 MariaDB 10.2 does not support ANY_VALUE |
79ee443c | 81 | $version = self::getDatabaseVersion(); |
2c40ef6b | 82 | |
6454ff54 | 83 | if (stripos($version, 'mariadb') !== FALSE) { |
2c40ef6b ML |
84 | return FALSE; |
85 | } | |
86 | ||
87 | return version_compare($version, '5.7', '>='); | |
712e729f SL |
88 | } |
89 | ||
b07c960a | 90 | /** |
91 | * Disable ONLY_FULL_GROUP_BY for MySQL versions lower then 5.7 | |
92 | * | |
93 | * @return bool | |
94 | */ | |
95 | public static function disableFullGroupByMode() { | |
96 | $sqlModes = self::getSqlModes(); | |
97 | ||
98 | // Disable only_full_group_by mode for lower sql versions. | |
99 | if (!self::supportsFullGroupBy() || (!empty($sqlModes) && !in_array('ONLY_FULL_GROUP_BY', $sqlModes))) { | |
100 | if ($key = array_search('ONLY_FULL_GROUP_BY', $sqlModes)) { | |
101 | unset($sqlModes[$key]); | |
102 | CRM_Core_DAO::executeQuery("SET SESSION sql_mode = '" . implode(',', $sqlModes) . "'"); | |
103 | } | |
104 | return TRUE; | |
105 | } | |
106 | ||
107 | return FALSE; | |
108 | } | |
109 | ||
a2ed62b3 SL |
110 | /** |
111 | * CHeck if ONLY_FULL_GROUP_BY is in the global sql_modes | |
112 | * @return bool | |
113 | */ | |
114 | public static function isGroupByModeInDefault() { | |
a2ed62b3 SL |
115 | $sqlModes = explode(',', CRM_Core_DAO::singleValueQuery('SELECT @@global.sql_mode')); |
116 | if (!in_array('ONLY_FULL_GROUP_BY', $sqlModes)) { | |
117 | return FALSE; | |
118 | } | |
119 | return TRUE; | |
120 | } | |
121 | ||
828cd10c SL |
122 | /** |
123 | * Is the Database set up to handle acceents. | |
fae75b58 SL |
124 | * @warning This function was introduced in attempt to determine the reason why the test getInternationalStrings was failing on ubu1604 but passing on ubu1204-5 |
125 | * This function should not be used as the basis of further work as the reasoning is not perfact and is giving false failures. | |
828cd10c SL |
126 | * @return bool |
127 | */ | |
128 | public static function supportStorageOfAccents() { | |
129 | $charSetDB = CRM_Core_DAO::executeQuery("SHOW VARIABLES LIKE 'character_set_database'")->fetchAll(); | |
130 | $charSet = $charSetDB[0]['Value']; | |
131 | if ($charSet == 'utf8') { | |
132 | return TRUE; | |
133 | } | |
134 | return FALSE; | |
135 | } | |
136 | ||
79ee443c | 137 | /** |
138 | * Does the DB version support mutliple locks per | |
139 | * | |
140 | * https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_get-lock | |
141 | * | |
142 | * As an interim measure we ALSO require CIVICRM_SUPPORT_MULTIPLE_LOCKS to be defined. | |
143 | * | |
144 | * This is a conservative measure to introduce the change which we expect to deprecate later. | |
145 | * | |
146 | * @todo we only check mariadb & mysql right now but maybe can add percona. | |
147 | */ | |
148 | public static function supportsMultipleLocks() { | |
149 | if (!defined('CIVICRM_SUPPORT_MULTIPLE_LOCKS')) { | |
150 | return FALSE; | |
151 | } | |
152 | static $isSupportLocks = NULL; | |
153 | if (!isset($isSupportLocks)) { | |
154 | $version = self::getDatabaseVersion(); | |
155 | if (stripos($version, 'mariadb') !== FALSE) { | |
156 | $isSupportLocks = version_compare($version, '10.0.2', '>='); | |
157 | } | |
158 | else { | |
159 | $isSupportLocks = version_compare($version, '5.7.5', '>='); | |
160 | } | |
161 | } | |
162 | ||
163 | return $isSupportLocks; | |
164 | } | |
165 | ||
166 | /** | |
167 | * Get the version string for the database. | |
168 | * | |
169 | * @return string | |
170 | */ | |
171 | public static function getDatabaseVersion() { | |
172 | return CRM_Core_DAO::singleValueQuery('SELECT VERSION()'); | |
173 | } | |
174 | ||
d1d3c04a | 175 | } |