From: John Kirk Date: Sat, 22 Apr 2017 22:46:03 +0000 (+0000) Subject: Refactored CRM-20428. Refactored sourceSQLFile by creating a separate function to... X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=c0e4c31d33e2bc7f7b53891f5b6481b719bed1d8;p=civicrm-core.git Refactored CRM-20428. Refactored sourceSQLFile by creating a separate function to run the SQL piece. This means that sourceSQLFile no longer has an 'isQueryString' variable, which was used to pass an SQL query in the filename parameter. --- diff --git a/CRM/Upgrade/Form.php b/CRM/Upgrade/Form.php index fed0ea65b4..d49776861c 100644 --- a/CRM/Upgrade/Form.php +++ b/CRM/Upgrade/Form.php @@ -190,10 +190,16 @@ class CRM_Upgrade_Form extends CRM_Core_Form { * @param bool $isQueryString */ public function source($fileName, $isQueryString = FALSE) { - - CRM_Utils_File::sourceSQLFile($this->_config->dsn, - $fileName, NULL, $isQueryString - ); + if ($isQueryString) { + CRM_Utils_File::runSqlQuery($this->_config->dsn, + $fileName, NULL + ); + } + else { + CRM_Utils_File::sourceSQLFile($this->_config->dsn, + $fileName, NULL + ); + } } public function preProcess() { diff --git a/CRM/Utils/File.php b/CRM/Utils/File.php index c69d9c88d0..f377b742cd 100644 --- a/CRM/Utils/File.php +++ b/CRM/Utils/File.php @@ -313,19 +313,29 @@ class CRM_Utils_File { * Use NULL to load the default/active connection from CRM_Core_DAO. * Otherwise, give a full DSN string. * @param string $fileName - * @param null $prefix - * @param bool $isQueryString + * @param string $prefix * @param bool $dieOnErrors */ - public static function sourceSQLFile($dsn, $fileName, $prefix = NULL, $isQueryString = FALSE, $dieOnErrors = TRUE) { - if (!$isQueryString) { - if (FALSE === file_get_contents($fileName)) { - // Our file cannot be found. - // Using 'die' here breaks this on extension upgrade. - throw new CRM_Exception('Could not find the SQL file.'); - } + public static function sourceSQLFile($dsn, $fileName, $prefix = NULL, $dieOnErrors = TRUE) { + if (FALSE === file_get_contents($fileName)) { + // Our file cannot be found. + // Using 'die' here breaks this on extension upgrade. + throw new CRM_Exception('Could not find the SQL file.'); } + self::runSqlQuery($dsn, file_get_contents($fileName), $prefix, $dieOnErrors); + } + + /** + * + * @param string|NULL $dsn + * @param string $queryString + * @param string $prefix + * @param bool $dieOnErrors + */ + public static function runSqlQuery($dsn, $queryString, $prefix = NULL, $dieOnErrors = TRUE) { + $string = $prefix . $queryString; + if ($dsn === NULL) { $db = CRM_Core_DAO::getConnection(); } @@ -344,14 +354,6 @@ class CRM_Utils_File { $transactionId = CRM_Utils_Type::escape(CRM_Utils_Request::id(), 'String'); $db->query('SET @uniqueID = ' . "'$transactionId'"); - if (!$isQueryString) { - $string = $prefix . file_get_contents($fileName); - } - else { - // use filename as query string - $string = $prefix . $fileName; - } - // get rid of comments starting with # and -- $string = self::stripComments($string); @@ -373,6 +375,7 @@ class CRM_Utils_File { } } } + /** * * Strips comment from a possibly multiline SQL string