1 | <?php |
||
26 | class BatchRowIterator implements RecursiveIterator { |
||
27 | |||
28 | /** |
||
29 | * @var IDatabase $db The database to read from |
||
30 | */ |
||
31 | protected $db; |
||
32 | |||
33 | /** |
||
34 | * @var string|array $table The name or names of the table to read from |
||
35 | */ |
||
36 | protected $table; |
||
37 | |||
38 | /** |
||
39 | * @var array $primaryKey The name of the primary key(s) |
||
40 | */ |
||
41 | protected $primaryKey; |
||
42 | |||
43 | /** |
||
44 | * @var integer $batchSize The number of rows to fetch per iteration |
||
45 | */ |
||
46 | protected $batchSize; |
||
47 | |||
48 | /** |
||
49 | * @var array $conditions Array of strings containing SQL conditions |
||
50 | * to add to the query |
||
51 | */ |
||
52 | protected $conditions = []; |
||
53 | |||
54 | /** |
||
55 | * @var array $joinConditions |
||
56 | */ |
||
57 | protected $joinConditions = []; |
||
58 | |||
59 | /** |
||
60 | * @var array $fetchColumns List of column names to select from the |
||
61 | * table suitable for use with IDatabase::select() |
||
62 | */ |
||
63 | protected $fetchColumns; |
||
64 | |||
65 | /** |
||
66 | * @var string $orderBy SQL Order by condition generated from $this->primaryKey |
||
67 | */ |
||
68 | protected $orderBy; |
||
69 | |||
70 | /** |
||
71 | * @var array $current The current iterator value |
||
72 | */ |
||
73 | private $current = []; |
||
74 | |||
75 | /** |
||
76 | * @var integer key 0-indexed number of pages fetched since self::reset() |
||
77 | */ |
||
78 | private $key; |
||
79 | |||
80 | /** |
||
81 | * @var array Additional query options |
||
82 | */ |
||
83 | protected $options = []; |
||
84 | |||
85 | /** |
||
86 | * @param IDatabase $db The database to read from |
||
87 | * @param string|array $table The name or names of the table to read from |
||
88 | * @param string|array $primaryKey The name or names of the primary key columns |
||
89 | * @param integer $batchSize The number of rows to fetch per iteration |
||
90 | * @throws InvalidArgumentException |
||
91 | */ |
||
92 | public function __construct( IDatabase $db, $table, $primaryKey, $batchSize ) { |
||
103 | |||
104 | /** |
||
105 | * @param array $conditions Query conditions suitable for use with |
||
106 | * IDatabase::select |
||
107 | */ |
||
108 | public function addConditions( array $conditions ) { |
||
111 | |||
112 | /** |
||
113 | * @param array $options Query options suitable for use with |
||
114 | * IDatabase::select |
||
115 | */ |
||
116 | public function addOptions( array $options ) { |
||
119 | |||
120 | /** |
||
121 | * @param array $conditions Query join conditions suitable for use |
||
122 | * with IDatabase::select |
||
123 | */ |
||
124 | public function addJoinConditions( array $conditions ) { |
||
127 | |||
128 | /** |
||
129 | * @param array $columns List of column names to select from the |
||
130 | * table suitable for use with IDatabase::select() |
||
131 | */ |
||
132 | public function setFetchColumns( array $columns ) { |
||
143 | |||
144 | /** |
||
145 | * Extracts the primary key(s) from a database row. |
||
146 | * |
||
147 | * @param stdClass $row An individual database row from this iterator |
||
148 | * @return array Map of primary key column to value within the row |
||
149 | */ |
||
150 | public function extractPrimaryKeys( $row ) { |
||
158 | |||
159 | /** |
||
160 | * @return array The most recently fetched set of rows from the database |
||
161 | */ |
||
162 | public function current() { |
||
165 | |||
166 | /** |
||
167 | * @return integer 0-indexed count of the page number fetched |
||
168 | */ |
||
169 | public function key() { |
||
172 | |||
173 | /** |
||
174 | * Reset the iterator to the begining of the table. |
||
175 | */ |
||
176 | public function rewind() { |
||
181 | |||
182 | /** |
||
183 | * @return bool True when the iterator is in a valid state |
||
184 | */ |
||
185 | public function valid() { |
||
188 | |||
189 | /** |
||
190 | * @return bool True when this result set has rows |
||
191 | */ |
||
192 | public function hasChildren() { |
||
195 | |||
196 | /** |
||
197 | * @return RecursiveIterator |
||
198 | */ |
||
199 | public function getChildren() { |
||
202 | |||
203 | /** |
||
204 | * Fetch the next set of rows from the database. |
||
205 | */ |
||
206 | public function next() { |
||
225 | |||
226 | /** |
||
227 | * Uses the primary key list and the maximal result row from the |
||
228 | * previous iteration to build an SQL condition sufficient for |
||
229 | * selecting the next page of results. All except the final key use |
||
230 | * `=` conditions while the final key uses a `>` condition |
||
231 | * |
||
232 | * Example output: |
||
233 | * [ '( foo = 42 AND bar > 7 ) OR ( foo > 42 )' ] |
||
234 | * |
||
235 | * @return array The SQL conditions necessary to select the next set |
||
236 | * of rows in the batched query |
||
237 | */ |
||
238 | protected function buildConditions() { |
||
268 | |||
269 | /** |
||
270 | * Given an array of column names and their maximum value generate |
||
271 | * an SQL condition where all keys except the last match $quotedMaximumValues |
||
272 | * exactly and the last column is greater than the matching value in |
||
273 | * $quotedMaximumValues |
||
274 | * |
||
275 | * @param array $quotedMaximumValues The maximum values quoted with |
||
276 | * $this->db->addQuotes() |
||
277 | * @return string An SQL condition that will select rows where all |
||
278 | * columns match the maximum value exactly except the last column |
||
279 | * which must be greater than the provided maximum value |
||
280 | */ |
||
281 | protected function buildGreaterThanCondition( array $quotedMaximumValues ) { |
||
293 | } |
||
294 |