Merge pull request #17213 from eileenmcnaughton/test
[civicrm-core.git] / tests / phpunit / api / v4 / Traits / QueryCounterTrait.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 |
10 +--------------------------------------------------------------------+
11 */
12
13 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 * $Id$
18 *
19 */
20
21
22 namespace api\v4\Traits;
23
24 trait QueryCounterTrait {
25
26 /**
27 * @var int
28 */
29 protected $startCount = 0;
30
31 /**
32 * Start the query counter
33 */
34 protected function beginQueryCount() {
35 $this->startCount = $this->getCurrentGlobalQueryCount();
36 }
37
38 /**
39 * @return int
40 * The number of queries since the counter was started
41 */
42 protected function getQueryCount() {
43 return $this->getCurrentGlobalQueryCount() - $this->startCount;
44 }
45
46 /**
47 * @return int
48 * @throws \Exception
49 */
50 private function getCurrentGlobalQueryCount() {
51 global $_DB_DATAOBJECT;
52
53 if (!$_DB_DATAOBJECT) {
54 throw new \Exception('Database object not set so cannot count queries');
55 }
56
57 return $_DB_DATAOBJECT['RESULTSEQ'] ?? 0;
58 }
59
60 }