From 4ed867e010ac81f61a6c124134dfd497d620b11c Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Mon, 18 Apr 2016 19:27:45 -0700 Subject: [PATCH] CRM-18212 - CRM_Core_DAO - Move rebuild/drop/create triggers to another class --- CRM/Core/DAO.php | 130 ++----------------------- Civi/Core/Container.php | 5 + Civi/Core/SqlTriggers.php | 193 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 204 insertions(+), 124 deletions(-) create mode 100644 Civi/Core/SqlTriggers.php diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index c124c0957d..1b01d7455f 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -1952,25 +1952,12 @@ SELECT contact_id * @param string $tableName * the specific table requiring a rebuild; or NULL to rebuild all tables. * @param bool $force + * @deprecated * * @see CRM-9716 */ public static function triggerRebuild($tableName = NULL, $force = FALSE) { - $info = array(); - - $logging = new CRM_Logging_Schema(); - $logging->triggerInfo($info, $tableName, $force); - - CRM_Core_I18n_Schema::triggerInfo($info, $tableName); - CRM_Contact_BAO_Contact::triggerInfo($info, $tableName); - - CRM_Utils_Hook::triggerInfo($info, $tableName); - - // drop all existing triggers on all tables - $logging->dropTriggers($tableName); - - // now create the set of new triggers - self::createTriggers($info, $tableName); + Civi::service('sql_triggers')->rebuild($tableName, $force); } /** @@ -1995,15 +1982,10 @@ SELECT contact_id * * @param string $tableName * the specific table requiring a rebuild; or NULL to rebuild all tables. + * @deprecated */ public static function dropTriggers($tableName = NULL) { - $info = array(); - - $logging = new CRM_Logging_Schema(); - $logging->triggerInfo($info, $tableName); - - // drop all existing triggers on all tables - $logging->dropTriggers($tableName); + Civi::service('sql_triggers')->dropTriggers($tableName); } /** @@ -2011,110 +1993,10 @@ SELECT contact_id * per hook_civicrm_triggerInfo. * @param string $onlyTableName * the specific table requiring a rebuild; or NULL to rebuild all tables. + * @deprecated */ public static function createTriggers(&$info, $onlyTableName = NULL) { - // Validate info array, should probably raise errors? - if (is_array($info) == FALSE) { - return; - } - - $triggers = array(); - - // now enumerate the tables and the events and collect the same set in a different format - foreach ($info as $value) { - - // clean the incoming data, skip malformed entries - // TODO: malformed entries should raise errors or get logged. - if (isset($value['table']) == FALSE || - isset($value['event']) == FALSE || - isset($value['when']) == FALSE || - isset($value['sql']) == FALSE - ) { - continue; - } - - if (is_string($value['table']) == TRUE) { - $tables = array($value['table']); - } - else { - $tables = $value['table']; - } - - if (is_string($value['event']) == TRUE) { - $events = array(strtolower($value['event'])); - } - else { - $events = array_map('strtolower', $value['event']); - } - - $whenName = strtolower($value['when']); - - foreach ($tables as $tableName) { - if (!isset($triggers[$tableName])) { - $triggers[$tableName] = array(); - } - - foreach ($events as $eventName) { - $template_params = array('{tableName}', '{eventName}'); - $template_values = array($tableName, $eventName); - - $sql = str_replace($template_params, - $template_values, - $value['sql'] - ); - $variables = str_replace($template_params, - $template_values, - CRM_Utils_Array::value('variables', $value) - ); - - if (!isset($triggers[$tableName][$eventName])) { - $triggers[$tableName][$eventName] = array(); - } - - if (!isset($triggers[$tableName][$eventName][$whenName])) { - // We're leaving out cursors, conditions, and handlers for now - // they are kind of dangerous in this context anyway - // better off putting them in stored procedures - $triggers[$tableName][$eventName][$whenName] = array( - 'variables' => array(), - 'sql' => array(), - ); - } - - if ($variables) { - $triggers[$tableName][$eventName][$whenName]['variables'][] = $variables; - } - - $triggers[$tableName][$eventName][$whenName]['sql'][] = $sql; - } - } - } - - // now spit out the sql - foreach ($triggers as $tableName => $tables) { - if ($onlyTableName != NULL && $onlyTableName != $tableName) { - continue; - } - foreach ($tables as $eventName => $events) { - foreach ($events as $whenName => $parts) { - $varString = implode("\n", $parts['variables']); - $sqlString = implode("\n", $parts['sql']); - $validName = CRM_Core_DAO::shortenSQLName($tableName, 48, TRUE); - $triggerName = "{$validName}_{$whenName}_{$eventName}"; - $triggerSQL = "CREATE TRIGGER $triggerName $whenName $eventName ON $tableName FOR EACH ROW BEGIN $varString $sqlString END"; - - CRM_Core_DAO::executeQuery("DROP TRIGGER IF EXISTS $triggerName"); - CRM_Core_DAO::executeQuery( - $triggerSQL, - array(), - TRUE, - NULL, - FALSE, - FALSE - ); - } - } - } + Civi::service('sql_triggers')->createTriggers($info, $onlyTableName); } /** diff --git a/Civi/Core/Container.php b/Civi/Core/Container.php index ce9d947956..cad5ff23ed 100644 --- a/Civi/Core/Container.php +++ b/Civi/Core/Container.php @@ -167,6 +167,11 @@ class Container { ))->setFactoryClass('CRM_Utils_Cache')->setFactoryMethod('create'); } + $container->setDefinition('sql_triggers', new Definition( + 'Civi\Core\SqlTriggers', + array() + )); + $container->setDefinition('pear_mail', new Definition('Mail')) ->setFactoryClass('CRM_Utils_Mail')->setFactoryMethod('createMailer'); diff --git a/Civi/Core/SqlTriggers.php b/Civi/Core/SqlTriggers.php new file mode 100644 index 0000000000..b794127b64 --- /dev/null +++ b/Civi/Core/SqlTriggers.php @@ -0,0 +1,193 @@ +triggerInfo($info, $tableName, $force); + + \CRM_Core_I18n_Schema::triggerInfo($info, $tableName); + \CRM_Contact_BAO_Contact::triggerInfo($info, $tableName); + + \CRM_Utils_Hook::triggerInfo($info, $tableName); + + // drop all existing triggers on all tables + $logging->dropTriggers($tableName); + + // now create the set of new triggers + $this->createTriggers($info, $tableName); + } + + /** + * @param array $info + * per hook_civicrm_triggerInfo. + * @param string $onlyTableName + * the specific table requiring a rebuild; or NULL to rebuild all tables. + */ + public function createTriggers(&$info, $onlyTableName = NULL) { + // Validate info array, should probably raise errors? + if (is_array($info) == FALSE) { + return; + } + + $triggers = array(); + + // now enumerate the tables and the events and collect the same set in a different format + foreach ($info as $value) { + + // clean the incoming data, skip malformed entries + // TODO: malformed entries should raise errors or get logged. + if (isset($value['table']) == FALSE || + isset($value['event']) == FALSE || + isset($value['when']) == FALSE || + isset($value['sql']) == FALSE + ) { + continue; + } + + if (is_string($value['table']) == TRUE) { + $tables = array($value['table']); + } + else { + $tables = $value['table']; + } + + if (is_string($value['event']) == TRUE) { + $events = array(strtolower($value['event'])); + } + else { + $events = array_map('strtolower', $value['event']); + } + + $whenName = strtolower($value['when']); + + foreach ($tables as $tableName) { + if (!isset($triggers[$tableName])) { + $triggers[$tableName] = array(); + } + + foreach ($events as $eventName) { + $template_params = array('{tableName}', '{eventName}'); + $template_values = array($tableName, $eventName); + + $sql = str_replace($template_params, + $template_values, + $value['sql'] + ); + $variables = str_replace($template_params, + $template_values, + \CRM_Utils_Array::value('variables', $value) + ); + + if (!isset($triggers[$tableName][$eventName])) { + $triggers[$tableName][$eventName] = array(); + } + + if (!isset($triggers[$tableName][$eventName][$whenName])) { + // We're leaving out cursors, conditions, and handlers for now + // they are kind of dangerous in this context anyway + // better off putting them in stored procedures + $triggers[$tableName][$eventName][$whenName] = array( + 'variables' => array(), + 'sql' => array(), + ); + } + + if ($variables) { + $triggers[$tableName][$eventName][$whenName]['variables'][] = $variables; + } + + $triggers[$tableName][$eventName][$whenName]['sql'][] = $sql; + } + } + } + + // now spit out the sql + foreach ($triggers as $tableName => $tables) { + if ($onlyTableName != NULL && $onlyTableName != $tableName) { + continue; + } + foreach ($tables as $eventName => $events) { + foreach ($events as $whenName => $parts) { + $varString = implode("\n", $parts['variables']); + $sqlString = implode("\n", $parts['sql']); + $validName = \CRM_Core_DAO::shortenSQLName($tableName, 48, TRUE); + $triggerName = "{$validName}_{$whenName}_{$eventName}"; + $triggerSQL = "CREATE TRIGGER $triggerName $whenName $eventName ON $tableName FOR EACH ROW BEGIN $varString $sqlString END"; + + \CRM_Core_DAO::executeQuery("DROP TRIGGER IF EXISTS $triggerName"); + \CRM_Core_DAO::executeQuery( + $triggerSQL, + array(), + TRUE, + NULL, + FALSE, + FALSE + ); + } + } + } + } + + /** + * Wrapper function to drop triggers. + * + * @param string $tableName + * the specific table requiring a rebuild; or NULL to rebuild all tables. + */ + public function dropTriggers($tableName = NULL) { + $info = array(); + + $logging = new \CRM_Logging_Schema(); + $logging->triggerInfo($info, $tableName); + + // drop all existing triggers on all tables + $logging->dropTriggers($tableName); + } + +} -- 2.25.1