/
BufferedStatement.php
175 lines (151 loc) · 3.97 KB
/
BufferedStatement.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Database\Statement;
/**
* A statement decorator that implements buffered results.
*
* This statement decorator will save fetched results in memory, allowing
* the iterator to be rewound and reused.
*/
class BufferedStatement extends StatementDecorator
{
/**
* Records count
*
* @var int
*/
protected $_count = 0;
/**
* Array of results
*
* @var array
*/
protected $_records = [];
/**
* If true, all rows were fetched
*
* @var bool
*/
protected $_allFetched = true;
/**
* Current record pointer
*
* @var int
*/
protected $_counter = 0;
/**
* Constructor
*
* @param \Cake\Database\StatementInterface|null $statement Statement implementation such as PDOStatement
* @param \Cake\Database\Driver|null $driver Driver instance
*/
public function __construct($statement = null, $driver = null)
{
parent::__construct($statement, $driver);
$this->_reset();
}
/**
* Execute the statement and return the results.
*
* @param array|null $params list of values to be bound to query
* @return bool true on success, false otherwise
*/
public function execute($params = null)
{
$this->_reset();
return parent::execute($params);
}
/**
* {@inheritDoc}
*
* @param string $type The type to fetch.
* @return array|false
*/
public function fetch($type = parent::FETCH_TYPE_NUM)
{
if ($this->_allFetched) {
$row = ($this->_counter < $this->_count) ? $this->_records[$this->_counter++] : false;
$row = ($row && $type === static::FETCH_TYPE_NUM) ? array_values($row) : $row;
return $row;
}
$record = parent::fetch($type);
if ($record === false) {
$this->_allFetched = true;
$this->_counter = $this->_count + 1;
$this->_statement->closeCursor();
return false;
}
$this->_count++;
return $this->_records[] = $record;
}
/**
* {@inheritdoc}
*/
public function fetchAssoc()
{
$result = $this->fetch(static::FETCH_TYPE_ASSOC);
return $result ? $result : [];
}
/**
* {@inheritDoc}
*
* @param string $type The type to fetch.
* @return array
*/
public function fetchAll($type = parent::FETCH_TYPE_NUM)
{
if ($this->_allFetched) {
return $this->_records;
}
$this->_records = parent::fetchAll($type);
$this->_count = count($this->_records);
$this->_allFetched = true;
$this->_statement->closeCursor();
return $this->_records;
}
/**
* {@inheritDoc}
*/
public function rowCount()
{
if (!$this->_allFetched) {
$counter = $this->_counter;
while ($this->fetch(static::FETCH_TYPE_ASSOC)) {
}
$this->_counter = $counter;
}
return $this->_count;
}
/**
* Rewind the _counter property
*
* @return void
*/
public function rewind()
{
$this->_counter = 0;
}
/**
* Reset all properties
*
* @return void
*/
protected function _reset()
{
$this->_count = $this->_counter = 0;
$this->_records = [];
$this->_allFetched = false;
}
}