Commit | Line | Data |
---|---|---|
40c8b829 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 5 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2018 | | |
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 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2018 | |
32 | * | |
33 | * Table naming rules: | |
34 | * - MySQL imposes a 64 char limit. | |
35 | * - All temp tables start with "civicrm_tmp". | |
36 | * - Durable temp tables: "civicrm_tmp_d_{12}_{32}" | |
37 | * - Ephemeral temp tables: "civicrm_tmp_e_{12}_{32}" | |
38 | * | |
0038409f TO |
39 | * To use `TempTable`: |
40 | * - Begin by calling `CRM_Utils_SQL_TempTable::build()`. | |
41 | * - Optionally, describe the table with `setDurable()`, `setCategory()`, `setId()`. | |
42 | * - Finally, call `getName()` or `createWithQuery()` or `createWithColumns()`. | |
43 | * | |
44 | * Example 1: Just create a table name. You'll be responsible for CREATE/DROP actions. | |
40c8b829 TO |
45 | * |
46 | * $name = CRM_Utils_SQL_TempTable::build()->getName(); | |
47 | * $name = CRM_Utils_SQL_TempTable::build()->setDurable()->getName(); | |
0038409f | 48 | * $name = CRM_Utils_SQL_TempTable::build()->setCategory('contactstats')->setId($contact['id'])->getName(); |
40c8b829 TO |
49 | * |
50 | * Example 2: Create a temp table using the results of a SELECT query. | |
51 | * | |
52 | * $tmpTbl = CRM_Utils_SQL_TempTable::build()->createWithQuery('SELECT id, display_name FROM civicrm_contact'); | |
53 | * $tmpTbl = CRM_Utils_SQL_TempTable::build()->createWithQuery(CRM_Utils_SQL_Select::from('civicrm_contact')->select('display_name')); | |
54 | * | |
55 | * Example 3: Create an empty temp table with list of columns. | |
56 | * | |
57 | * $tmpTbl = CRM_Utils_SQL_TempTable::build()->setDurable()->setUtf8()->createWithColumns('id int(10, name varchar(64)'); | |
58 | * | |
59 | * Example 4: Drop a table that you previously created. | |
60 | * | |
61 | * $tmpTbl->drop(); | |
62 | * | |
63 | * Example 5: Auto-drop a temp table when $tmpTbl falls out of scope | |
64 | * | |
65 | * $tmpTbl->setAutodrop(); | |
66 | * | |
67 | */ | |
68 | class CRM_Utils_SQL_TempTable { | |
69 | ||
70 | const UTF8 = 'DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci'; | |
71 | const CATEGORY_LENGTH = 12; | |
72 | const CATEGORY_REGEXP = ';^[a-zA-Z0-9]+$;'; | |
73 | const ID_LENGTH = 37; // MAX{64} - CATEGORY_LENGTH{12} - CONST_LENGHTH{15} = 37 | |
74 | const ID_REGEXP = ';^[a-zA-Z0-9_]+$;'; | |
75 | ||
76 | /** | |
77 | * @var bool | |
78 | */ | |
79 | protected $durable, $utf8; | |
80 | ||
81 | protected $category; | |
82 | ||
83 | protected $id; | |
84 | ||
85 | protected $autodrop; | |
86 | ||
87 | /** | |
88 | * @return CRM_Utils_SQL_TempTable | |
89 | */ | |
90 | public static function build() { | |
91 | $t = new CRM_Utils_SQL_TempTable(); | |
92 | $t->category = NULL; | |
93 | $t->id = md5(uniqid('', TRUE)); | |
94 | // The constant CIVICRM_TEMP_FORCE_DURABLE is for local debugging. | |
95 | $t->durable = CRM_Utils_Constant::value('CIVICRM_TEMP_FORCE_DURABLE', FALSE); | |
96 | // I suspect it would be better to just say utf8=true, but a lot of existing queries don't do the utf8 bit. | |
97 | $t->utf8 = CRM_Utils_Constant::value('CIVICRM_TEMP_FORCE_UTF8', FALSE); | |
98 | $t->autodrop = FALSE; | |
99 | return $t; | |
100 | } | |
101 | ||
102 | public function __destruct() { | |
103 | if ($this->autodrop) { | |
104 | $this->drop(); | |
105 | } | |
106 | } | |
107 | ||
108 | /** | |
109 | * Determine the full table name. | |
110 | * | |
111 | * @return string | |
112 | * Ex: 'civicrm_tmp_d_foo_abcd1234abcd1234' | |
113 | */ | |
114 | public function getName() { | |
115 | $parts = ['civicrm', 'tmp']; | |
116 | $parts[] = ($this->durable ? 'd' : 'e'); | |
117 | $parts[] = $this->category ? $this->category : 'dflt'; | |
118 | $parts[] = $this->id ? $this->id : 'dflt'; | |
119 | return implode('_', $parts); | |
120 | } | |
121 | ||
122 | /** | |
123 | * Create the table using results from a SELECT query. | |
124 | * | |
125 | * @param string|CRM_Utils_SQL_Select $selectQuery | |
126 | * @return CRM_Utils_SQL_TempTable | |
127 | */ | |
128 | public function createWithQuery($selectQuery) { | |
129 | $sql = sprintf('%s %s AS %s', | |
130 | $this->toSQL('CREATE'), | |
131 | $this->utf8 ? self::UTF8 : '', | |
132 | ($selectQuery instanceof CRM_Utils_SQL_Select ? $selectQuery->toSQL() : $selectQuery) | |
133 | ); | |
134 | CRM_Core_DAO::executeQuery($sql, array(), TRUE, NULL, TRUE, FALSE); | |
135 | return $this; | |
136 | } | |
137 | ||
138 | /** | |
139 | * Create the empty table. | |
140 | * | |
141 | * @parma string $columns | |
142 | * SQL column listing. | |
143 | * Ex: 'id int(10), name varchar(64)'. | |
144 | * @return CRM_Utils_SQL_TempTable | |
145 | */ | |
146 | public function createWithColumns($columns) { | |
147 | $sql = sprintf('%s (%s) %s', | |
148 | $this->toSQL('CREATE'), | |
149 | $columns, | |
150 | $this->utf8 ? self::UTF8 : '' | |
151 | ); | |
152 | CRM_Core_DAO::executeQuery($sql, array(), TRUE, NULL, TRUE, FALSE); | |
153 | return $this; | |
154 | } | |
155 | ||
156 | /** | |
157 | * Drop the table. | |
158 | * | |
159 | * @return CRM_Utils_SQL_TempTable | |
160 | */ | |
161 | public function drop() { | |
162 | $sql = $this->toSQL('DROP', 'IF EXISTS'); | |
163 | CRM_Core_DAO::executeQuery($sql, array(), TRUE, NULL, TRUE, FALSE); | |
164 | return $this; | |
165 | } | |
166 | ||
167 | /** | |
168 | * @param string $action | |
169 | * Ex: 'CREATE', 'DROP' | |
170 | * @param string|NULL $ifne | |
171 | * Ex: 'IF EXISTS', 'IF NOT EXISTS'. | |
172 | * @return string | |
173 | * Ex: 'CREATE TEMPORARY TABLE `civicrm_tmp_e_foo_abcd1234`' | |
174 | * Ex: 'CREATE TABLE IF NOT EXISTS `civicrm_tmp_d_foo_abcd1234`' | |
175 | */ | |
176 | private function toSQL($action, $ifne = NULL) { | |
177 | $parts = []; | |
178 | $parts[] = $action; | |
179 | if (!$this->durable) { | |
180 | $parts[] = 'TEMPORARY'; | |
181 | } | |
182 | $parts[] = 'TABLE'; | |
183 | if ($ifne) { | |
184 | $parts[] = $ifne; | |
185 | } | |
186 | $parts[] = '`' . $this->getName() . '`'; | |
187 | return implode(' ', $parts); | |
188 | } | |
189 | ||
190 | /** | |
191 | * @return string|NULL | |
192 | */ | |
193 | public function getCategory() { | |
194 | return $this->category; | |
195 | } | |
196 | ||
197 | /** | |
198 | * @return string|NULL | |
199 | */ | |
200 | public function getId() { | |
201 | return $this->id; | |
202 | } | |
203 | ||
204 | /** | |
205 | * @return bool | |
206 | */ | |
207 | public function isAutodrop() { | |
208 | return $this->autodrop; | |
209 | } | |
210 | ||
211 | /** | |
212 | * @return bool | |
213 | */ | |
214 | public function isDurable() { | |
215 | return $this->durable; | |
216 | } | |
217 | ||
218 | /** | |
219 | * @return bool | |
220 | */ | |
221 | public function isUtf8() { | |
222 | return $this->utf8; | |
223 | } | |
224 | ||
225 | /** | |
226 | * @param bool $autodrop | |
227 | * @return CRM_Utils_SQL_TempTable | |
228 | */ | |
229 | public function setAutodrop($autodrop = TRUE) { | |
230 | $this->autodrop = $autodrop; | |
231 | return $this; | |
232 | } | |
233 | ||
234 | /** | |
235 | * @param string|NULL $category | |
f0197a3d | 236 | * |
40c8b829 TO |
237 | * @return CRM_Utils_SQL_TempTable |
238 | */ | |
239 | public function setCategory($category) { | |
240 | if ($category && !preg_match(self::CATEGORY_REGEXP, $category) || strlen($category) > self::CATEGORY_LENGTH) { | |
241 | throw new \RuntimeException("Malformed temp table category"); | |
242 | } | |
243 | $this->category = $category; | |
244 | return $this; | |
245 | } | |
246 | ||
247 | /** | |
f0197a3d | 248 | * Set whether the table should be durable. |
249 | * | |
250 | * Durable tables are not TEMPORARY in the mysql sense. | |
251 | * | |
252 | * @param bool $durable | |
253 | * | |
40c8b829 TO |
254 | * @return CRM_Utils_SQL_TempTable |
255 | */ | |
256 | public function setDurable($durable = TRUE) { | |
257 | $this->durable = $durable; | |
258 | return $this; | |
259 | } | |
260 | ||
261 | /** | |
f0197a3d | 262 | * Setter for id |
263 | * | |
40c8b829 | 264 | * @param mixed $id |
f0197a3d | 265 | * |
40c8b829 TO |
266 | * @return CRM_Utils_SQL_TempTable |
267 | */ | |
268 | public function setId($id) { | |
269 | if ($id && !preg_match(self::ID_REGEXP, $id) || strlen($id) > self::ID_LENGTH) { | |
270 | throw new \RuntimeException("Malformed temp table id"); | |
271 | } | |
272 | $this->id = $id; | |
273 | return $this; | |
274 | } | |
275 | ||
f0197a3d | 276 | /** |
277 | * Set table collation to UTF8. | |
278 | * | |
279 | * This would make sense as a default but cautiousness during phasing in has made it opt-in. | |
280 | * | |
281 | * @param bool $value | |
282 | * | |
283 | * @return $this | |
284 | */ | |
40c8b829 TO |
285 | public function setUtf8($value = TRUE) { |
286 | $this->utf8 = $value; | |
287 | return $this; | |
288 | } | |
289 | ||
290 | } |