Merge pull request #19936 from jmcclelland/only-include-completed
[civicrm-core.git] / tests / phpunit / api / v4 / Traits / QueryCounterTrait.php
CommitLineData
19b53e5b
C
1<?php
2
380f3545
TO
3/*
4 +--------------------------------------------------------------------+
7d61e75f 5 | Copyright CiviCRM LLC. All rights reserved. |
380f3545 6 | |
7d61e75f
TO
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
380f3545
TO
10 +--------------------------------------------------------------------+
11 */
12
13/**
14 *
15 * @package CRM
ca5cec67 16 * @copyright CiviCRM LLC https://civicrm.org/licensing
380f3545
TO
17 */
18
19
19b53e5b
C
20namespace api\v4\Traits;
21
19b53e5b
C
22trait QueryCounterTrait {
23
24 /**
25 * @var int
26 */
27 protected $startCount = 0;
28
29 /**
30 * Start the query counter
31 */
32 protected function beginQueryCount() {
33 $this->startCount = $this->getCurrentGlobalQueryCount();
34 }
35
36 /**
37 * @return int
38 * The number of queries since the counter was started
39 */
40 protected function getQueryCount() {
41 return $this->getCurrentGlobalQueryCount() - $this->startCount;
42 }
43
44 /**
45 * @return int
46 * @throws \Exception
47 */
48 private function getCurrentGlobalQueryCount() {
49 global $_DB_DATAOBJECT;
50
51 if (!$_DB_DATAOBJECT) {
52 throw new \Exception('Database object not set so cannot count queries');
53 }
54
1b8e3bc8 55 return $_DB_DATAOBJECT['RESULTSEQ'] ?? 0;
19b53e5b
C
56 }
57
58}