Commit | Line | Data |
---|---|---|
40c8b829 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
40c8b829 | 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 | | |
40c8b829 TO |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
40c8b829 TO |
16 | * Table naming rules: |
17 | * - MySQL imposes a 64 char limit. | |
18 | * - All temp tables start with "civicrm_tmp". | |
19 | * - Durable temp tables: "civicrm_tmp_d_{12}_{32}" | |
20 | * - Ephemeral temp tables: "civicrm_tmp_e_{12}_{32}" | |
21 | * | |
0038409f TO |
22 | * To use `TempTable`: |
23 | * - Begin by calling `CRM_Utils_SQL_TempTable::build()`. | |
24 | * - Optionally, describe the table with `setDurable()`, `setCategory()`, `setId()`. | |
25 | * - Finally, call `getName()` or `createWithQuery()` or `createWithColumns()`. | |
26 | * | |
27 | * Example 1: Just create a table name. You'll be responsible for CREATE/DROP actions. | |
40c8b829 TO |
28 | * |
29 | * $name = CRM_Utils_SQL_TempTable::build()->getName(); | |
30 | * $name = CRM_Utils_SQL_TempTable::build()->setDurable()->getName(); | |
0038409f | 31 | * $name = CRM_Utils_SQL_TempTable::build()->setCategory('contactstats')->setId($contact['id'])->getName(); |
40c8b829 TO |
32 | * |
33 | * Example 2: Create a temp table using the results of a SELECT query. | |
34 | * | |
35 | * $tmpTbl = CRM_Utils_SQL_TempTable::build()->createWithQuery('SELECT id, display_name FROM civicrm_contact'); | |
36 | * $tmpTbl = CRM_Utils_SQL_TempTable::build()->createWithQuery(CRM_Utils_SQL_Select::from('civicrm_contact')->select('display_name')); | |
37 | * | |
38 | * Example 3: Create an empty temp table with list of columns. | |
39 | * | |
0033aaaf | 40 | * $tmpTbl = CRM_Utils_SQL_TempTable::build()->setDurable()->createWithColumns('id int(10, name varchar(64)'); |
40c8b829 TO |
41 | * |
42 | * Example 4: Drop a table that you previously created. | |
43 | * | |
44 | * $tmpTbl->drop(); | |
45 | * | |
46 | * Example 5: Auto-drop a temp table when $tmpTbl falls out of scope | |
47 | * | |
48 | * $tmpTbl->setAutodrop(); | |
49 | * | |
50 | */ | |
51 | class CRM_Utils_SQL_TempTable { | |
52 | ||
53 | const UTF8 = 'DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci'; | |
54 | const CATEGORY_LENGTH = 12; | |
55 | const CATEGORY_REGEXP = ';^[a-zA-Z0-9]+$;'; | |
6714d8d2 SL |
56 | // MAX{64} - CATEGORY_LENGTH{12} - CONST_LENGHTH{15} = 37 |
57 | const ID_LENGTH = 37; | |
40c8b829 | 58 | const ID_REGEXP = ';^[a-zA-Z0-9_]+$;'; |
8ccee4bf | 59 | const INNODB = 'ENGINE=InnoDB'; |
60 | const MEMORY = 'ENGINE=MEMORY'; | |
40c8b829 TO |
61 | |
62 | /** | |
63 | * @var bool | |
64 | */ | |
6714d8d2 SL |
65 | protected $durable; |
66 | ||
67 | /** | |
68 | * @var bool | |
69 | */ | |
70 | protected $utf8; | |
40c8b829 TO |
71 | |
72 | protected $category; | |
73 | ||
74 | protected $id; | |
75 | ||
76 | protected $autodrop; | |
77 | ||
8ccee4bf | 78 | protected $memory; |
79 | ||
90e5222f | 80 | protected $createSql; |
81 | ||
40c8b829 TO |
82 | /** |
83 | * @return CRM_Utils_SQL_TempTable | |
84 | */ | |
85 | public static function build() { | |
86 | $t = new CRM_Utils_SQL_TempTable(); | |
87 | $t->category = NULL; | |
88 | $t->id = md5(uniqid('', TRUE)); | |
89 | // The constant CIVICRM_TEMP_FORCE_DURABLE is for local debugging. | |
90 | $t->durable = CRM_Utils_Constant::value('CIVICRM_TEMP_FORCE_DURABLE', FALSE); | |
0033aaaf | 91 | $t->utf8 = TRUE; |
40c8b829 | 92 | $t->autodrop = FALSE; |
8ccee4bf | 93 | $t->memory = FALSE; |
40c8b829 TO |
94 | return $t; |
95 | } | |
96 | ||
97 | public function __destruct() { | |
98 | if ($this->autodrop) { | |
99 | $this->drop(); | |
100 | } | |
101 | } | |
102 | ||
103 | /** | |
104 | * Determine the full table name. | |
105 | * | |
106 | * @return string | |
107 | * Ex: 'civicrm_tmp_d_foo_abcd1234abcd1234' | |
108 | */ | |
109 | public function getName() { | |
110 | $parts = ['civicrm', 'tmp']; | |
111 | $parts[] = ($this->durable ? 'd' : 'e'); | |
112 | $parts[] = $this->category ? $this->category : 'dflt'; | |
113 | $parts[] = $this->id ? $this->id : 'dflt'; | |
114 | return implode('_', $parts); | |
115 | } | |
116 | ||
117 | /** | |
118 | * Create the table using results from a SELECT query. | |
119 | * | |
120 | * @param string|CRM_Utils_SQL_Select $selectQuery | |
121 | * @return CRM_Utils_SQL_TempTable | |
122 | */ | |
123 | public function createWithQuery($selectQuery) { | |
8ccee4bf | 124 | $sql = sprintf('%s %s %s AS %s', |
40c8b829 | 125 | $this->toSQL('CREATE'), |
8ccee4bf | 126 | $this->memory ? self::MEMORY : self::INNODB, |
25374ca7 | 127 | $this->getUtf8String(), |
40c8b829 TO |
128 | ($selectQuery instanceof CRM_Utils_SQL_Select ? $selectQuery->toSQL() : $selectQuery) |
129 | ); | |
be2fb01f | 130 | CRM_Core_DAO::executeQuery($sql, [], TRUE, NULL, TRUE, FALSE); |
90e5222f | 131 | $this->createSql = $sql; |
40c8b829 TO |
132 | return $this; |
133 | } | |
134 | ||
25374ca7 | 135 | /** |
136 | * Get the utf8 string for the table. | |
137 | * | |
138 | * If the db collation is already utf8 by default (either | |
139 | * utf8 or utf84mb) then rely on that. Otherwise set to utf8. | |
140 | * | |
141 | * Respecting the DB collation supports utf8mb4 adopters, which is currently | |
142 | * not the norm in civi installs. | |
143 | * | |
144 | * @return string | |
145 | */ | |
146 | public function getUtf8String() { | |
147 | if (!$this->utf8) { | |
148 | return ''; | |
149 | } | |
150 | $dbUTF = CRM_Core_BAO_SchemaHandler::getDBCollation(); | |
253cc262 | 151 | if (strpos($dbUTF, 'utf8') !== FALSE) { |
25374ca7 | 152 | return ''; |
153 | } | |
154 | return self::UTF8; | |
155 | } | |
156 | ||
40c8b829 TO |
157 | /** |
158 | * Create the empty table. | |
159 | * | |
160 | * @parma string $columns | |
161 | * SQL column listing. | |
162 | * Ex: 'id int(10), name varchar(64)'. | |
163 | * @return CRM_Utils_SQL_TempTable | |
164 | */ | |
165 | public function createWithColumns($columns) { | |
8ccee4bf | 166 | $sql = sprintf('%s (%s) %s %s', |
40c8b829 TO |
167 | $this->toSQL('CREATE'), |
168 | $columns, | |
8ccee4bf | 169 | $this->memory ? self::MEMORY : self::INNODB, |
253cc262 | 170 | $this->getUtf8String() |
40c8b829 | 171 | ); |
be2fb01f | 172 | CRM_Core_DAO::executeQuery($sql, [], TRUE, NULL, TRUE, FALSE); |
90e5222f | 173 | $this->createSql = $sql; |
40c8b829 TO |
174 | return $this; |
175 | } | |
176 | ||
177 | /** | |
178 | * Drop the table. | |
179 | * | |
180 | * @return CRM_Utils_SQL_TempTable | |
181 | */ | |
182 | public function drop() { | |
183 | $sql = $this->toSQL('DROP', 'IF EXISTS'); | |
be2fb01f | 184 | CRM_Core_DAO::executeQuery($sql, [], TRUE, NULL, TRUE, FALSE); |
40c8b829 TO |
185 | return $this; |
186 | } | |
187 | ||
188 | /** | |
189 | * @param string $action | |
190 | * Ex: 'CREATE', 'DROP' | |
191 | * @param string|NULL $ifne | |
192 | * Ex: 'IF EXISTS', 'IF NOT EXISTS'. | |
193 | * @return string | |
194 | * Ex: 'CREATE TEMPORARY TABLE `civicrm_tmp_e_foo_abcd1234`' | |
195 | * Ex: 'CREATE TABLE IF NOT EXISTS `civicrm_tmp_d_foo_abcd1234`' | |
196 | */ | |
197 | private function toSQL($action, $ifne = NULL) { | |
198 | $parts = []; | |
199 | $parts[] = $action; | |
200 | if (!$this->durable) { | |
201 | $parts[] = 'TEMPORARY'; | |
202 | } | |
203 | $parts[] = 'TABLE'; | |
204 | if ($ifne) { | |
205 | $parts[] = $ifne; | |
206 | } | |
207 | $parts[] = '`' . $this->getName() . '`'; | |
208 | return implode(' ', $parts); | |
209 | } | |
210 | ||
211 | /** | |
212 | * @return string|NULL | |
213 | */ | |
214 | public function getCategory() { | |
215 | return $this->category; | |
216 | } | |
217 | ||
218 | /** | |
219 | * @return string|NULL | |
220 | */ | |
221 | public function getId() { | |
222 | return $this->id; | |
223 | } | |
224 | ||
90e5222f | 225 | /** |
226 | * @return string|NULL | |
227 | */ | |
228 | public function getCreateSql() { | |
229 | return $this->createSql; | |
230 | } | |
231 | ||
40c8b829 TO |
232 | /** |
233 | * @return bool | |
234 | */ | |
235 | public function isAutodrop() { | |
236 | return $this->autodrop; | |
237 | } | |
238 | ||
239 | /** | |
240 | * @return bool | |
241 | */ | |
242 | public function isDurable() { | |
243 | return $this->durable; | |
244 | } | |
245 | ||
8ccee4bf | 246 | /** |
247 | * @return bool | |
248 | */ | |
249 | public function isMemory() { | |
250 | return $this->memory; | |
251 | } | |
252 | ||
40c8b829 TO |
253 | /** |
254 | * @return bool | |
255 | */ | |
256 | public function isUtf8() { | |
257 | return $this->utf8; | |
258 | } | |
259 | ||
260 | /** | |
261 | * @param bool $autodrop | |
262 | * @return CRM_Utils_SQL_TempTable | |
263 | */ | |
264 | public function setAutodrop($autodrop = TRUE) { | |
265 | $this->autodrop = $autodrop; | |
266 | return $this; | |
267 | } | |
268 | ||
269 | /** | |
270 | * @param string|NULL $category | |
f0197a3d | 271 | * |
40c8b829 TO |
272 | * @return CRM_Utils_SQL_TempTable |
273 | */ | |
274 | public function setCategory($category) { | |
275 | if ($category && !preg_match(self::CATEGORY_REGEXP, $category) || strlen($category) > self::CATEGORY_LENGTH) { | |
f5c0a346 | 276 | throw new \RuntimeException("Malformed temp table category $category"); |
40c8b829 TO |
277 | } |
278 | $this->category = $category; | |
279 | return $this; | |
280 | } | |
281 | ||
282 | /** | |
f0197a3d | 283 | * Set whether the table should be durable. |
284 | * | |
285 | * Durable tables are not TEMPORARY in the mysql sense. | |
286 | * | |
287 | * @param bool $durable | |
288 | * | |
40c8b829 TO |
289 | * @return CRM_Utils_SQL_TempTable |
290 | */ | |
291 | public function setDurable($durable = TRUE) { | |
292 | $this->durable = $durable; | |
293 | return $this; | |
294 | } | |
295 | ||
296 | /** | |
f0197a3d | 297 | * Setter for id |
298 | * | |
40c8b829 | 299 | * @param mixed $id |
f0197a3d | 300 | * |
40c8b829 TO |
301 | * @return CRM_Utils_SQL_TempTable |
302 | */ | |
303 | public function setId($id) { | |
304 | if ($id && !preg_match(self::ID_REGEXP, $id) || strlen($id) > self::ID_LENGTH) { | |
305 | throw new \RuntimeException("Malformed temp table id"); | |
306 | } | |
307 | $this->id = $id; | |
308 | return $this; | |
309 | } | |
310 | ||
8ccee4bf | 311 | /** |
312 | * Set table engine to MEMORY. | |
313 | * | |
314 | * @param bool $value | |
315 | * | |
316 | * @return $this | |
317 | */ | |
318 | public function setMemory($value = TRUE) { | |
319 | $this->memory = $value; | |
320 | return $this; | |
321 | } | |
322 | ||
f0197a3d | 323 | /** |
324 | * Set table collation to UTF8. | |
325 | * | |
0033aaaf | 326 | * @deprecated This method is deprecated as tables should be assumed to have |
327 | * UTF-8 as the default character set and collation; some other character set | |
328 | * or collation may be specified in the column definition. | |
f0197a3d | 329 | * |
330 | * @param bool $value | |
331 | * | |
332 | * @return $this | |
333 | */ | |
40c8b829 TO |
334 | public function setUtf8($value = TRUE) { |
335 | $this->utf8 = $value; | |
336 | return $this; | |
337 | } | |
338 | ||
339 | } |