Skip to content
Snippets Groups Projects
Commit c5c00947 authored by jaapjansma's avatar jaapjansma
Browse files

revert improved count query

parent ca815fc3
No related branches found
No related tags found
No related merge requests found
......@@ -8,12 +8,10 @@ namespace Civi\DataProcessor\DataFlow;
use Civi\DataProcessor\DataFlow\Sort\SortSpecification;
use Civi\DataProcessor\DataFlow\SqlDataFlow\WhereClauseInterface;
use Civi\DataProcessor\DataFlow\Utils\Aggregator;
use \Civi\DataProcessor\DataSpecification\DataSpecification;
use Civi\DataProcessor\DataSpecification\FieldExistsException;
use Civi\DataProcessor\Exception\DataFlowException;
use Civi\DataProcessor\FieldOutputHandler\OutputHandlerAggregate;
use CRM_Dataprocessor_ExtensionUtil as E;
abstract class SqlDataFlow extends AbstractDataFlow {
......@@ -120,15 +118,26 @@ abstract class SqlDataFlow extends AbstractDataFlow {
try {
$selectAndFrom = $this->getSelectQueryStatement();
$from = $this->getFromStatement();
$where = $this->getWhereStatement();
$groupBy = $this->getGroupByStatement();
$orderBy = $this->getOrderByStatement();
$countName = 'count_'.$this->getName();
$sql = "{$selectAndFrom} {$where} {$groupBy} {$orderBy}";
$countSql = "{$selectAndFrom} {$where} {$groupBy}";
$pos = strpos($countSql, "SELECT ");
if ($pos !== false) {
$countSql = substr_replace($countSql, "SELECT SQL_CALC_FOUND_ROWS ", $pos, strlen("SELECT "));
}
$this->sqlCountStatements[] = $countSql;
\CRM_Core_DAO::executeQuery($countSql, [], true, NULL, false, true);
$countDao = \CRM_Core_DAO::executeQuery("SELECT FOUND_ROWS() as `count`", [], true, NULL, false, true);
$this->count = 0;
if (!is_a($countDao, 'DB_Error') && $countDao) {
while ($countDao->fetch()) {
$this->count = $this->count + $countDao->count;
}
}
$tempTableName = "civicrm_data_processor_temp_".md5($sql);
\CRM_Core_DAO::executeQuery("CREATE TEMPORARY TABLE IF NOT EXISTS `{$tempTableName}` AS ({$sql})");
$sql = "SELECT * FROM `{$tempTableName}`";
// Build Limit and Offset.
$limitStatement = "";
if ($this->offset !== FALSE && $this->limit !== FALSE) {
......@@ -147,17 +156,6 @@ abstract class SqlDataFlow extends AbstractDataFlow {
if (is_a($this->dao, 'DB_Error') || !$this->dao) {
throw new DataFlowException('Error in dataflow: '.$sql);
}
$countSql = "SELECT COUNT(*) AS `count` FROM `{$tempTableName}`";
$this->sqlCountStatements[] = $countSql;
$countDao = \CRM_Core_DAO::executeQuery($countSql, [], true, NULL, false, true);
$this->count = 0;
if (!is_a($countDao, 'DB_Error') && $countDao) {
while ($countDao->fetch()) {
$this->count = $this->count + $countDao->count;
}
}
} catch (\Exception $e) {
throw new DataFlowException(
"Error in DataFlow query.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment