107 throw new InvalidArgumentException(
'Batch size must be at least 1 row.' );
113 $this->orderBy = implode(
' ASC,', $this->primaryKey ) .
' ASC';
122 $this->conditions = array_merge( $this->conditions,
$conditions );
130 $this->options = array_merge( $this->options,
$options );
138 $this->joinConditions = array_merge( $this->joinConditions,
$conditions );
147 if ( count( $columns ) === 1 && reset( $columns ) ===
'*' ) {
148 $this->fetchColumns = $columns;
150 $this->fetchColumns = array_unique( array_merge(
179 foreach ( $this->primaryKey as $alias => $column ) {
180 $name = is_numeric( $alias ) ? $column : $alias;
181 $pk[$name] = $row->{$name};
190 return $this->current;
213 return (
bool)$this->current;
220 return $this->current && count( $this->current );
234 $caller = __METHOD__;
235 if ( (
string)$this->caller !==
'' ) {
236 $caller .=
" (for {$this->caller})";
239 $res = $this->db->select(
242 $this->buildConditions(),
245 'LIMIT' => $this->batchSize,
246 'ORDER BY' => $this->orderBy,
248 $this->joinConditions
254 $this->current = iterator_to_array(
$res );
271 if ( !$this->current ) {
272 return $this->conditions;
275 $maxRow = end( $this->current );
277 foreach ( $this->primaryKey as $alias => $column ) {
278 $name = is_numeric( $alias ) ? $column : $alias;
279 $maximumValues[$column] = $this->db->addQuotes( $maxRow->{$name} );
290 while ( $maximumValues ) {
291 $pkConditions[] = $this->buildGreaterThanCondition( $maximumValues );
292 array_pop( $maximumValues );
295 $conditions = $this->conditions;
296 $conditions[] = sprintf(
'( %s )', implode(
' ) OR ( ', $pkConditions ) );
314 $keys = array_keys( $quotedMaximumValues );
315 $lastColumn = end(
$keys );
316 $lastValue = array_pop( $quotedMaximumValues );
318 foreach ( $quotedMaximumValues as $column => $value ) {
319 $conditions[] =
"$column = $value";
321 $conditions[] =
"$lastColumn > $lastValue";
323 return implode(
' AND ', $conditions );
Allows iterating a large number of rows in batches transparently.
next()
Fetch the next set of rows from the database.
addConditions(array $conditions)
array $primaryKey
The name of the primary key(s)
rewind()
Reset the iterator to the beginning of the table.
string array $table
The name or names of the table to read from.
array $options
Additional query options.
string null $caller
For debugging which method is using this class.
IDatabase $db
The database to read from.
addJoinConditions(array $conditions)
array $current
The current iterator value.
setFetchColumns(array $columns)
string $orderBy
SQL Order by condition generated from $this->primaryKey.
setCaller( $caller)
Use ->setCaller( METHOD ) to indicate which code is using this class.
extractPrimaryKeys( $row)
Extracts the primary key(s) from a database row.
addOptions(array $options)
array $conditions
Array of strings containing SQL conditions to add to the query.
array $fetchColumns
List of column names to select from the table suitable for use with IDatabase::select()
int $batchSize
The number of rows to fetch per iteration.
int $key
0-indexed number of pages fetched since self::reset()
buildGreaterThanCondition(array $quotedMaximumValues)
Given an array of column names and their maximum value generate an SQL condition where all keys excep...
buildConditions()
Uses the primary key list and the maximal result row from the previous iteration to build an SQL cond...
__construct(IDatabase $db, $table, $primaryKey, $batchSize)