3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2016
35 * This file contains functions for creating and altering CiviCRM-tables structure.
38 * 'name' => TABLE_NAME,
39 * 'attributes' => ATTRIBUTES,
42 * 'name' => FIELD_NAME,
43 * // can be field, index, constraint
44 * 'type' => FIELD_SQL_TYPE,
45 * 'class' => FIELD_CLASS_TYPE,
46 * 'primary' => BOOLEAN,
47 * 'required' => BOOLEAN,
48 * 'searchable' => TRUE,
49 * 'fk_table_name' => FOREIGN_KEY_TABLE_NAME,
50 * 'fk_field_name' => FOREIGN_KEY_FIELD_NAME,
51 * 'comment' => COMMENT,
52 * 'default' => DEFAULT, )
56 class CRM_Core_BAO_SchemaHandler
{
59 * Create a CiviCRM-table
61 * @param array $params
64 * TRUE if successfully created, FALSE otherwise
67 public static function createTable(&$params) {
68 $sql = self
::buildTableSQL($params);
69 // do not i18n-rewrite
70 $dao = CRM_Core_DAO
::executeQuery($sql, array(), TRUE, NULL, FALSE, FALSE);
73 $config = CRM_Core_Config
::singleton();
74 if ($config->logging
) {
76 $logging = new CRM_Logging_Schema();
77 $logging->fixSchemaDifferencesFor($params['name'], NULL, FALSE);
80 // always do a trigger rebuild for this table
81 CRM_Core_DAO
::triggerRebuild($params['name']);
87 * @param array $params
91 public static function buildTableSQL(&$params) {
92 $sql = "CREATE TABLE {$params['name']} (";
93 if (isset($params['fields']) &&
94 is_array($params['fields'])
98 foreach ($params['fields'] as $field) {
99 $sql .= self
::buildFieldSQL($field, $separator, $prefix);
102 foreach ($params['fields'] as $field) {
103 $sql .= self
::buildPrimaryKeySQL($field, $separator, $prefix);
105 foreach ($params['fields'] as $field) {
106 $sql .= self
::buildSearchIndexSQL($field, $separator, $prefix);
108 if (isset($params['indexes'])) {
109 foreach ($params['indexes'] as $index) {
110 $sql .= self
::buildIndexSQL($index, $separator, $prefix);
113 foreach ($params['fields'] as $field) {
114 $sql .= self
::buildForeignKeySQL($field, $separator, $prefix, $params['name']);
117 $sql .= "\n) {$params['attributes']};";
122 * @param array $params
128 public static function buildFieldSQL(&$params, $separator, $prefix) {
131 $sql .= str_repeat(' ', 8);
133 $sql .= "`{$params['name']}` {$params['type']}";
135 if (!empty($params['required'])) {
139 if (!empty($params['attributes'])) {
140 $sql .= " {$params['attributes']}";
143 if (!empty($params['default']) &&
144 $params['type'] != 'text'
146 $sql .= " DEFAULT {$params['default']}";
149 if (!empty($params['comment'])) {
150 $sql .= " COMMENT '{$params['comment']}'";
157 * @param array $params
161 * @return NULL|string
163 public static function buildPrimaryKeySQL(&$params, $separator, $prefix) {
165 if (!empty($params['primary'])) {
167 $sql .= str_repeat(' ', 8);
169 $sql .= "PRIMARY KEY ( {$params['name']} )";
175 * @param array $params
178 * @param bool $indexExist
180 * @return NULL|string
182 public static function buildSearchIndexSQL(&$params, $separator, $prefix, $indexExist = FALSE) {
186 if ($params['type'] == 'text') {
190 //create index only for searchable fields during ADD,
191 //create index only if field is become searchable during MODIFY,
192 //drop index only if field is no more searchable and index was exist.
193 if (!empty($params['searchable']) && !$indexExist) {
195 $sql .= str_repeat(' ', 8);
197 $sql .= "INDEX_{$params['name']} ( {$params['name']} )";
199 elseif (empty($params['searchable']) && $indexExist) {
201 $sql .= str_repeat(' ', 8);
202 $sql .= "DROP INDEX INDEX_{$params['name']}";
208 * @param array $params
214 public static function buildIndexSQL(&$params, $separator, $prefix) {
217 $sql .= str_repeat(' ', 8);
218 if ($params['unique']) {
219 $sql .= 'UNIQUE INDEX';
220 $indexName = 'unique';
224 $indexName = 'index';
228 foreach ($params as $name => $value) {
229 if (substr($name, 0, 11) == 'field_name_') {
230 $indexName .= "_{$value}";
231 $indexFields .= " $value,";
234 $indexFields = substr($indexFields, 0, -1);
236 $sql .= " $indexName ( $indexFields )";
241 * @param string $tableName
242 * @param string $fkTableName
246 public static function changeFKConstraint($tableName, $fkTableName) {
247 $fkName = "{$tableName}_entity_id";
248 if (strlen($fkName) >= 48) {
249 $fkName = substr($fkName, 0, 32) . "_" . substr(md5($fkName), 0, 16);
252 ALTER TABLE {$tableName}
253 DROP FOREIGN KEY `FK_{$fkName}`;";
255 $dao = CRM_Core_DAO
::executeQuery($dropFKSql);
259 ALTER TABLE {$tableName}
260 ADD CONSTRAINT `FK_{$fkName}` FOREIGN KEY (`entity_id`) REFERENCES {$fkTableName} (`id`) ON DELETE CASCADE;";
261 // CRM-7007: do not i18n-rewrite this query
262 $dao = CRM_Core_DAO
::executeQuery($addFKSql, array(), TRUE, NULL, FALSE, FALSE);
269 * @param array $params
272 * @param string $tableName
274 * @return NULL|string
276 public static function buildForeignKeySQL(&$params, $separator, $prefix, $tableName) {
278 if (!empty($params['fk_table_name']) && !empty($params['fk_field_name'])) {
280 $sql .= str_repeat(' ', 8);
282 $fkName = "{$tableName}_{$params['name']}";
283 if (strlen($fkName) >= 48) {
284 $fkName = substr($fkName, 0, 32) . "_" . substr(md5($fkName), 0, 16);
287 $sql .= "CONSTRAINT FK_$fkName FOREIGN KEY ( `{$params['name']}` ) REFERENCES {$params['fk_table_name']} ( {$params['fk_field_name']} ) ";
288 $sql .= CRM_Utils_Array
::value('fk_attributes', $params);
294 * @param array $params
295 * @param bool $indexExist
296 * @param bool $triggerRebuild
300 public static function alterFieldSQL(&$params, $indexExist = FALSE, $triggerRebuild = TRUE) {
301 $sql = str_repeat(' ', 8);
302 $sql .= "ALTER TABLE {$params['table_name']}";
304 // lets suppress the required flag, since that can cause sql issue
305 $params['required'] = FALSE;
307 switch ($params['operation']) {
311 $sql .= self
::buildFieldSQL($params, $separator, "ADD COLUMN ");
313 $sql .= self
::buildPrimaryKeySQL($params, $separator, "ADD PRIMARY KEY ");
314 $sql .= self
::buildSearchIndexSQL($params, $separator, "ADD INDEX ");
315 $sql .= self
::buildForeignKeySQL($params, $separator, "ADD ", $params['table_name']);
321 $sql .= self
::buildFieldSQL($params, $separator, $prefix);
323 $sql .= self
::buildSearchIndexSQL($params, $separator, "ADD INDEX ", $indexExist);
327 $sql .= " DROP COLUMN `{$params['name']}`";
328 if (!empty($params['primary'])) {
329 $sql .= ", DROP PRIMARY KEY";
331 if (!empty($params['fk_table_name'])) {
332 $sql .= ", DROP FOREIGN KEY FK_{$params['fkName']}";
337 // CRM-7007: do not i18n-rewrite this query
338 $dao = CRM_Core_DAO
::executeQuery($sql, array(), TRUE, NULL, FALSE, FALSE);
341 $config = CRM_Core_Config
::singleton();
342 if ($config->logging
) {
343 // CRM-16717 not sure why this was originally limited to add.
344 // For example custom tables can have field length changes - which need to flow through to logging.
345 // Are there any modifies we DON'T was to call this function for (& shouldn't it be clever enough to cope?)
346 if ($params['operation'] == 'add' ||
$params['operation'] == 'modify') {
347 $logging = new CRM_Logging_Schema();
348 $logging->fixSchemaDifferencesFor($params['table_name'], array(trim($prefix) => array($params['name'])), FALSE);
352 if ($triggerRebuild) {
353 CRM_Core_DAO
::triggerRebuild($params['table_name']);
360 * Delete a CiviCRM-table
362 * @param string $tableName
363 * Name of the table to be created.
365 public static function dropTable($tableName) {
366 $sql = "DROP TABLE $tableName";
367 $dao = CRM_Core_DAO
::executeQuery($sql);
371 * @param string $tableName
372 * @param string $columnName
374 public static function dropColumn($tableName, $columnName) {
375 if (self
::checkIfFieldExists($tableName, $columnName)) {
376 $sql = "ALTER TABLE $tableName DROP COLUMN $columnName";
377 CRM_Core_DAO
::executeQuery($sql);
382 * @param string $tableName
383 * @param bool $dropUnique
385 public static function changeUniqueToIndex($tableName, $dropUnique = TRUE) {
387 $sql = "ALTER TABLE $tableName
388 DROP INDEX `unique_entity_id` ,
389 ADD INDEX `FK_{$tableName}_entity_id` ( `entity_id` )";
392 $sql = " ALTER TABLE $tableName
393 DROP INDEX `FK_{$tableName}_entity_id` ,
394 ADD UNIQUE INDEX `unique_entity_id` ( `entity_id` )";
396 $dao = CRM_Core_DAO
::executeQuery($sql);
403 * Tables to create index for in the format:
404 * array('civicrm_entity_table' => 'entity_id')
406 * array('civicrm_entity_table' => array('entity_id', 'entity_table'))
407 * The latter will create a combined index on the 2 keys (in order).
409 * Side note - when creating combined indexes the one with the most variation
410 * goes first - so entity_table always goes after entity_id.
412 * It probably makes sense to consider more sophisticated options at some point
413 * but at the moment this is only being as enhanced as fast as the test is.
415 * @todo add support for length & multilingual on combined keys.
417 * @param string $createIndexPrefix
418 * @param array $substrLenghts
420 public static function createIndexes($tables, $createIndexPrefix = 'index', $substrLenghts = array()) {
422 $domain = new CRM_Core_DAO_Domain();
424 $locales = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
426 // if we're multilingual, cache the information on internationalised fields
427 static $columns = NULL;
428 if (!CRM_Utils_System
::isNull($locales) and $columns === NULL) {
429 $columns = CRM_Core_I18n_SchemaStructure
::columns();
432 foreach ($tables as $table => $fields) {
433 $query = "SHOW INDEX FROM $table";
434 $dao = CRM_Core_DAO
::executeQuery($query);
436 $currentIndexes = array();
437 while ($dao->fetch()) {
438 $currentIndexes[] = $dao->Key_name
;
441 // now check for all fields if the index exists
442 foreach ($fields as $field) {
443 $fieldName = implode('_', (array) $field);
445 if (is_array($field)) {
446 // No support for these for combined indexes as yet - add a test when you
452 // handle indices over substrings, CRM-6245
453 // $lengthName is appended to index name, $lengthSize is the field size modifier
454 $lengthName = isset($substrLenghts[$table][$fieldName]) ?
"_{$substrLenghts[$table][$fieldName]}" : '';
455 $lengthSize = isset($substrLenghts[$table][$fieldName]) ?
"({$substrLenghts[$table][$fieldName]})" : '';
459 "index_{$fieldName}{$lengthName}",
460 "FK_{$table}_{$fieldName}{$lengthName}",
461 "UI_{$fieldName}{$lengthName}",
462 "{$createIndexPrefix}_{$fieldName}{$lengthName}",
465 // skip to the next $field if one of the above $names exists; handle multilingual for CRM-4126
466 foreach ($names as $name) {
467 $regex = '/^' . preg_quote($name) . '(_[a-z][a-z]_[A-Z][A-Z])?$/';
468 if (preg_grep($regex, $currentIndexes)) {
473 // the index doesn't exist, so create it
474 // if we're multilingual and the field is internationalised, do it for every locale
475 // @todo remove is_array check & add multilingual support for combined indexes and add a test.
476 // Note combined indexes currently using this function are on fields like
477 // entity_id + entity_table which are not multilingual.
478 if (!is_array($field) && !CRM_Utils_System
::isNull($locales) and isset($columns[$table][$fieldName])) {
479 foreach ($locales as $locale) {
480 $queries[] = "CREATE INDEX {$createIndexPrefix}_{$fieldName}{$lengthName}_{$locale} ON {$table} ({$fieldName}_{$locale}{$lengthSize})";
484 $queries[] = "CREATE INDEX {$createIndexPrefix}_{$fieldName}{$lengthName} ON {$table} (" . implode(',', (array) $field) . "{$lengthSize})";
489 // run the queries without i18n-rewriting
490 $dao = new CRM_Core_DAO();
491 foreach ($queries as $query) {
492 $dao->query($query, FALSE);
497 * Drop an index if one by that name exists.
499 * @param string $tableName
500 * @param string $indexName
502 public static function dropIndexIfExists($tableName, $indexName) {
503 if (self
::checkIfIndexExists($tableName, $indexName)) {
504 CRM_Core_DAO
::executeQuery("DROP INDEX $indexName ON $tableName");
509 * @param int $customFieldID
510 * @param string $tableName
511 * @param string $columnName
516 public static function alterFieldLength($customFieldID, $tableName, $columnName, $length) {
517 // first update the custom field tables
519 UPDATE civicrm_custom_field
524 1 => array($length, 'Integer'),
525 2 => array($customFieldID, 'Integer'),
527 CRM_Core_DAO
::executeQuery($sql, $params);
530 SELECT is_required, default_value
531 FROM civicrm_custom_field
534 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
539 if ($dao->is_required
) {
540 $clause = " NOT NULL";
543 if (!empty($dao->default_value
)) {
544 $clause .= " DEFAULT '{$dao->default_value}'";
546 // now modify the column
548 ALTER TABLE {$tableName}
549 MODIFY {$columnName} varchar( $length )
552 CRM_Core_DAO
::executeQuery($sql);
555 CRM_Core_Error
::fatal(ts('Could Not Find Custom Field Details for %1, %2, %3',
566 * Check if the table has an index matching the name.
568 * @param string $tableName
569 * @param array $indexName
571 * @return \CRM_Core_DAO|object
573 public static function checkIfIndexExists($tableName, $indexName) {
574 $result = CRM_Core_DAO
::executeQuery(
575 "SHOW INDEX FROM $tableName WHERE key_name = %1 AND seq_in_index = 1",
576 array(1 => array($indexName, 'String'))
578 if ($result->fetch()) {
585 * Check if the table has a specified column
587 * @param string $tableName
588 * @param string $columnName
590 * @return \CRM_Core_DAO|object
592 public static function checkIfFieldExists($tableName, $columnName) {
593 $result = CRM_Core_DAO
::executeQuery(
594 "SHOW COLUMNS FROM $tableName LIKE %1",
595 array(1 => array($columnName, 'String'))
597 if ($result->fetch()) {
604 * Remove a foreign key from a table if it exists
607 * @param $constraint_name
609 public static function safeRemoveFK($table_name, $constraint_name) {
611 $config = CRM_Core_Config
::singleton();
612 $dbUf = DB
::parseDSN($config->dsn
);
614 SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
615 WHERE TABLE_SCHEMA = %1
617 AND CONSTRAINT_NAME = %3
618 AND CONSTRAINT_TYPE = 'FOREIGN KEY'
621 1 => array($dbUf['database'], 'String'),
622 2 => array($table_name, 'String'),
623 3 => array($constraint_name, 'String'),
625 $dao = CRM_Core_DAO
::executeQuery($query, $params);
628 CRM_Core_DAO
::executeQuery("ALTER TABLE {$table_name} DROP FOREIGN KEY {$constraint_name}", array());