Refactored CRM-20428. Refactored sourceSQLFile by creating a separate function to...
authorJohn Kirk <accounts@civifirst.com>
Sat, 22 Apr 2017 22:46:03 +0000 (22:46 +0000)
committerJohn Kirk <accounts@civifirst.com>
Sat, 22 Apr 2017 22:46:03 +0000 (22:46 +0000)
CRM/Upgrade/Form.php
CRM/Utils/File.php

index fed0ea65b437330e7fbcd3a4a0d75b2600b345ba..d49776861cf04ff58087e71cced676e64f3f9c6f 100644 (file)
@@ -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() {
index c69d9c88d0ef447028afe36636b1ecc18844e34f..f377b742cd17a616bc920a49a69542824718fac7 100644 (file)
@@ -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