3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 * - MySQL imposes a 64 char limit.
19 * - All temp tables start with "civicrm_tmp".
20 * - Durable temp tables: "civicrm_tmp_d_{12}_{32}"
21 * - Ephemeral temp tables: "civicrm_tmp_e_{12}_{32}"
24 * - Begin by calling `CRM_Utils_SQL_TempTable::build()`.
25 * - Optionally, describe the table with `setDurable()`, `setCategory()`, `setId()`.
26 * - Finally, call `getName()` or `createWithQuery()` or `createWithColumns()`.
28 * Example 1: Just create a table name. You'll be responsible for CREATE/DROP actions.
30 * $name = CRM_Utils_SQL_TempTable::build()->getName();
31 * $name = CRM_Utils_SQL_TempTable::build()->setDurable()->getName();
32 * $name = CRM_Utils_SQL_TempTable::build()->setCategory('contactstats')->setId($contact['id'])->getName();
34 * Example 2: Create a temp table using the results of a SELECT query.
36 * $tmpTbl = CRM_Utils_SQL_TempTable::build()->createWithQuery('SELECT id, display_name FROM civicrm_contact');
37 * $tmpTbl = CRM_Utils_SQL_TempTable::build()->createWithQuery(CRM_Utils_SQL_Select::from('civicrm_contact')->select('display_name'));
39 * Example 3: Create an empty temp table with list of columns.
41 * $tmpTbl = CRM_Utils_SQL_TempTable::build()->setDurable()->createWithColumns('id int(10, name varchar(64)');
43 * Example 4: Drop a table that you previously created.
47 * Example 5: Auto-drop a temp table when $tmpTbl falls out of scope
49 * $tmpTbl->setAutodrop();
52 class CRM_Utils_SQL_TempTable
{
54 const UTF8
= 'DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci';
55 const CATEGORY_LENGTH
= 12;
56 const CATEGORY_REGEXP
= ';^[a-zA-Z0-9]+$;';
57 // MAX{64} - CATEGORY_LENGTH{12} - CONST_LENGHTH{15} = 37
59 const ID_REGEXP
= ';^[a-zA-Z0-9_]+$;';
60 const INNODB
= 'ENGINE=InnoDB';
61 const MEMORY
= 'ENGINE=MEMORY';
84 * @return CRM_Utils_SQL_TempTable
86 public static function build() {
87 $t = new CRM_Utils_SQL_TempTable();
89 $t->id
= md5(uniqid('', TRUE));
90 // The constant CIVICRM_TEMP_FORCE_DURABLE is for local debugging.
91 $t->durable
= CRM_Utils_Constant
::value('CIVICRM_TEMP_FORCE_DURABLE', FALSE);
98 public function __destruct() {
99 if ($this->autodrop
) {
105 * Determine the full table name.
108 * Ex: 'civicrm_tmp_d_foo_abcd1234abcd1234'
110 public function getName() {
111 $parts = ['civicrm', 'tmp'];
112 $parts[] = ($this->durable ?
'd' : 'e');
113 $parts[] = $this->category ?
$this->category
: 'dflt';
114 $parts[] = $this->id ?
$this->id
: 'dflt';
115 return implode('_', $parts);
119 * Create the table using results from a SELECT query.
121 * @param string|CRM_Utils_SQL_Select $selectQuery
122 * @return CRM_Utils_SQL_TempTable
124 public function createWithQuery($selectQuery) {
125 $sql = sprintf('%s %s %s AS %s',
126 $this->toSQL('CREATE'),
127 $this->memory ? self
::MEMORY
: self
::INNODB
,
128 $this->utf8 ? self
::UTF8
: '',
129 ($selectQuery instanceof CRM_Utils_SQL_Select ?
$selectQuery->toSQL() : $selectQuery)
131 CRM_Core_DAO
::executeQuery($sql, [], TRUE, NULL, TRUE, FALSE);
132 $this->createSql
= $sql;
137 * Create the empty table.
139 * @parma string $columns
140 * SQL column listing.
141 * Ex: 'id int(10), name varchar(64)'.
142 * @return CRM_Utils_SQL_TempTable
144 public function createWithColumns($columns) {
145 $sql = sprintf('%s (%s) %s %s',
146 $this->toSQL('CREATE'),
148 $this->memory ? self
::MEMORY
: self
::INNODB
,
149 $this->utf8 ? self
::UTF8
: ''
151 CRM_Core_DAO
::executeQuery($sql, [], TRUE, NULL, TRUE, FALSE);
152 $this->createSql
= $sql;
159 * @return CRM_Utils_SQL_TempTable
161 public function drop() {
162 $sql = $this->toSQL('DROP', 'IF EXISTS');
163 CRM_Core_DAO
::executeQuery($sql, [], TRUE, NULL, TRUE, FALSE);
168 * @param string $action
169 * Ex: 'CREATE', 'DROP'
170 * @param string|NULL $ifne
171 * Ex: 'IF EXISTS', 'IF NOT EXISTS'.
173 * Ex: 'CREATE TEMPORARY TABLE `civicrm_tmp_e_foo_abcd1234`'
174 * Ex: 'CREATE TABLE IF NOT EXISTS `civicrm_tmp_d_foo_abcd1234`'
176 private function toSQL($action, $ifne = NULL) {
179 if (!$this->durable
) {
180 $parts[] = 'TEMPORARY';
186 $parts[] = '`' . $this->getName() . '`';
187 return implode(' ', $parts);
191 * @return string|NULL
193 public function getCategory() {
194 return $this->category
;
198 * @return string|NULL
200 public function getId() {
205 * @return string|NULL
207 public function getCreateSql() {
208 return $this->createSql
;
214 public function isAutodrop() {
215 return $this->autodrop
;
221 public function isDurable() {
222 return $this->durable
;
228 public function isMemory() {
229 return $this->memory
;
235 public function isUtf8() {
240 * @param bool $autodrop
241 * @return CRM_Utils_SQL_TempTable
243 public function setAutodrop($autodrop = TRUE) {
244 $this->autodrop
= $autodrop;
249 * @param string|NULL $category
251 * @return CRM_Utils_SQL_TempTable
253 public function setCategory($category) {
254 if ($category && !preg_match(self
::CATEGORY_REGEXP
, $category) ||
strlen($category) > self
::CATEGORY_LENGTH
) {
255 throw new \
RuntimeException("Malformed temp table category");
257 $this->category
= $category;
262 * Set whether the table should be durable.
264 * Durable tables are not TEMPORARY in the mysql sense.
266 * @param bool $durable
268 * @return CRM_Utils_SQL_TempTable
270 public function setDurable($durable = TRUE) {
271 $this->durable
= $durable;
280 * @return CRM_Utils_SQL_TempTable
282 public function setId($id) {
283 if ($id && !preg_match(self
::ID_REGEXP
, $id) ||
strlen($id) > self
::ID_LENGTH
) {
284 throw new \
RuntimeException("Malformed temp table id");
291 * Set table engine to MEMORY.
297 public function setMemory($value = TRUE) {
298 $this->memory
= $value;
303 * Set table collation to UTF8.
305 * @deprecated This method is deprecated as tables should be assumed to have
306 * UTF-8 as the default character set and collation; some other character set
307 * or collation may be specified in the column definition.
313 public function setUtf8($value = TRUE) {
314 $this->utf8
= $value;