From 67cae8735f3cf7aa19ca169694374c4861261fcf Mon Sep 17 00:00:00 2001 From: Steve Binkowski Date: Thu, 28 Jan 2016 15:33:22 -0500 Subject: [PATCH] Fix: new unbuffered query wrapper, work-around for no record count --- CRM/Core/DAO.php | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index 76892fb626..4560e5368b 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -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. * -- 2.25.1