Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
22 / 22 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| common_persistence_sql_QueryIterator | |
100.00% |
22 / 22 |
|
100.00% |
7 / 7 |
10 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| rewind | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| current | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| key | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| next | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| valid | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| load | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2002-2020 (original work) 2014 Open Assessment Technologies SA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * Iterator over all triples |
| 24 | * |
| 25 | * @author joel bout <joel@taotesting.com> |
| 26 | */ |
| 27 | class common_persistence_sql_QueryIterator implements Iterator |
| 28 | { |
| 29 | private const CACHE_SIZE = 100; |
| 30 | |
| 31 | /** |
| 32 | * @var common_persistence_SqlPersistence |
| 33 | */ |
| 34 | private $persistence; |
| 35 | /** |
| 36 | * @var string |
| 37 | */ |
| 38 | private $query; |
| 39 | /** |
| 40 | * @var array |
| 41 | */ |
| 42 | private $params; |
| 43 | |
| 44 | /** |
| 45 | * Id of the current instance |
| 46 | * |
| 47 | * @var int |
| 48 | */ |
| 49 | private $currentResult; |
| 50 | |
| 51 | /** |
| 52 | * Return statements of the last query |
| 53 | * |
| 54 | * @var array |
| 55 | */ |
| 56 | private $cache; |
| 57 | /** |
| 58 | * @var array |
| 59 | */ |
| 60 | private $types; |
| 61 | /** |
| 62 | * @var int |
| 63 | */ |
| 64 | private $limit; |
| 65 | |
| 66 | public function __construct( |
| 67 | common_persistence_SqlPersistence $persistence, |
| 68 | string $query, |
| 69 | array $params = [], |
| 70 | array $types = [], |
| 71 | int $limit = self::CACHE_SIZE |
| 72 | ) { |
| 73 | $this->persistence = $persistence; |
| 74 | $this->query = $query; |
| 75 | $this->params = $params; |
| 76 | $this->types = $types; |
| 77 | $this->limit = $limit; |
| 78 | $this->rewind(); |
| 79 | } |
| 80 | |
| 81 | public function rewind() |
| 82 | { |
| 83 | $this->load(0); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @return core_kernel_classes_Triple |
| 88 | */ |
| 89 | public function current() |
| 90 | { |
| 91 | return $this->cache[$this->currentResult]; |
| 92 | } |
| 93 | |
| 94 | public function key() |
| 95 | { |
| 96 | return $this->currentResult; |
| 97 | } |
| 98 | |
| 99 | public function next() |
| 100 | { |
| 101 | if ($this->valid()) { |
| 102 | $last = $this->key(); |
| 103 | $this->currentResult++; |
| 104 | if (!isset($this->cache[$this->currentResult])) { |
| 105 | $this->load($last + 1); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | public function valid() |
| 111 | { |
| 112 | return !empty($this->cache); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Loads the next n results, starting with $offset |
| 117 | * |
| 118 | * @param int $offset |
| 119 | */ |
| 120 | protected function load($offset) |
| 121 | { |
| 122 | $query = $this->persistence->getPlatForm()->limitStatement($this->query, $this->limit, $offset); |
| 123 | $result = $this->persistence->query($query, $this->params, $this->types); |
| 124 | |
| 125 | $this->cache = []; |
| 126 | $pos = $offset; |
| 127 | while ($statement = $result->fetch()) { |
| 128 | $this->cache[$pos++] = $statement; |
| 129 | } |
| 130 | |
| 131 | $this->currentResult = $offset; |
| 132 | } |
| 133 | } |