Fix: new unbuffered query wrapper, work-around for no record count
authorSteve Binkowski <binkowsk@nysenate.gov>
Thu, 28 Jan 2016 20:33:22 +0000 (15:33 -0500)
committerSteve Binkowski <binkowsk@nysenate.gov>
Thu, 28 Jan 2016 20:33:22 +0000 (15:33 -0500)
CRM/Core/DAO.php

index 76892fb6267ddcf456c598b44ef220a03cad255e..4560e5368beb89878053fc928e34ea4f9aa643b6 100644 (file)
@@ -1166,6 +1166,64 @@ FROM   civicrm_domain
     $object->delete();
   }
 
+  /**
+   * execute an unbuffered query.  This is a wrapper around new functionality
+   * exposed with CRM-17748.
+   *
+   * @param string $query query to be executed
+   * 
+   * @return Object CRM_Core_DAO object that points to an unbuffered result set
+   * @static
+   * @access public
+   */
+  static function executeUnbufferedQuery(
+    $query,
+    $params        = array(),
+    $abort         = TRUE,
+    $daoName       = NULL,
+    $freeDAO       = FALSE,
+    $i18nRewrite   = TRUE,
+    $trapException = FALSE
+  ) {
+    $queryStr = self::composeQuery($query, $params, $abort);
+    //CRM_Core_Error::debug( 'q', $queryStr );
+    if (!$daoName) {
+      $dao = new CRM_Core_DAO();
+    }
+    else {
+      $dao = new $daoName( );
+    }
+
+    if ($trapException) {
+      CRM_Core_Error::ignoreException();
+    }
+
+    // set the DAO object to use an unbuffered query
+    $dao->setOptions( array('result_buffering'=>0) );
+
+    $result = $dao->query($queryStr, $i18nRewrite);
+
+    if ($trapException) {
+      CRM_Core_Error::setCallback();
+    }
+
+    if (is_a($result, 'DB_Error')) {
+      return $result;
+    }
+
+    // since it is unbuffered, ($dao->N==0) is true.  This blocks the standard fetch() mechanism. 
+    $dao->N = TRUE;
+
+    if ($freeDAO ||
+      preg_match('/^(insert|update|delete|create|drop|replace)/i', $queryStr)
+    ) {
+      // we typically do this for insert/update/delete stataments OR if explicitly asked to
+      // free the dao
+      $dao->free();
+    }
+    return $dao;
+  }
+
   /**
    * Execute a query.
    *