MediaWiki master
MediaWiki\Utils\BatchRowIterator Class Reference

Allows iterating a large number of rows in batches transparently. More...

Inherits RecursiveIterator.

Collaboration diagram for MediaWiki\Utils\BatchRowIterator:

Public Member Functions

 __construct (IReadableDatabase $db, $sqb, $primaryKey, $batchSize)
 
 addConditions (array $conditions)
 
 addJoinConditions (array $conditions)
 
 addOptions (array $options)
 
 current ()
 
 extractPrimaryKeys ( $row)
 Extracts the primary key(s) from a database row.
 
 getChildren ()
 
 hasChildren ()
 
 key ()
 
 next ()
 Fetch the next set of rows from the database.
 
 rewind ()
 Reset the iterator to the beginning of the table.
 
 setCaller ( $caller)
 Use ->setCaller( METHOD ) to indicate which code is using this class.
 
 setFetchColumns (array $columns)
 
 valid ()
 

Public Attributes

SelectQueryBuilder $sqb
 Underlying database query builder, may be mutated before iteration begins.
 

Protected Member Functions

 buildConditions ()
 Uses the primary key list and the maximal result row from the previous iteration to build an SQL condition sufficient for selecting the next page of results.
 

Protected Attributes

int $batchSize
 The number of rows to fetch per iteration.
 
IReadableDatabase $db
 
array $primaryKey
 The name of the primary key(s)
 

Detailed Description

Allows iterating a large number of rows in batches transparently.

By default when iterated over returns the full query result as an array of rows. Can be wrapped in RecursiveIteratorIterator to collapse those arrays into a single stream of rows queried in batches.

Stability: newable

Definition at line 26 of file BatchRowIterator.php.

Constructor & Destructor Documentation

◆ __construct()

MediaWiki\Utils\BatchRowIterator::__construct ( IReadableDatabase $db,
$sqb,
$primaryKey,
$batchSize )
Stability: stable
to call
Parameters
IReadableDatabase$db
SelectQueryBuilder | string | array$sqbThe query to split into batches (or table name/names)
string | array$primaryKeyThe name or names of the primary key columns
int$batchSizeThe number of rows to fetch per iteration

Definition at line 63 of file BatchRowIterator.php.

References MediaWiki\Utils\BatchRowIterator\$batchSize, MediaWiki\Utils\BatchRowIterator\$db, MediaWiki\Utils\BatchRowIterator\$primaryKey, MediaWiki\Utils\BatchRowIterator\$sqb, and Wikimedia\Rdbms\IReadableDatabase\newSelectQueryBuilder().

Member Function Documentation

◆ addConditions()

MediaWiki\Utils\BatchRowIterator::addConditions ( array $conditions)
Deprecated
since 1.44 Use the SelectQueryBuilder object directly
Parameters
array$conditionsQuery conditions suitable for use with IDatabase::select

Definition at line 86 of file BatchRowIterator.php.

◆ addJoinConditions()

MediaWiki\Utils\BatchRowIterator::addJoinConditions ( array $conditions)
Deprecated
since 1.44 Use the SelectQueryBuilder object directly
Parameters
array$conditionsQuery join conditions suitable for use with IDatabase::select

Definition at line 104 of file BatchRowIterator.php.

◆ addOptions()

MediaWiki\Utils\BatchRowIterator::addOptions ( array $options)
Deprecated
since 1.44 Use the SelectQueryBuilder object directly
Parameters
array$optionsQuery options suitable for use with IDatabase::select

Definition at line 95 of file BatchRowIterator.php.

◆ buildConditions()

MediaWiki\Utils\BatchRowIterator::buildConditions ( )
protected

Uses the primary key list and the maximal result row from the previous iteration to build an SQL condition sufficient for selecting the next page of results.

Returns
array The SQL conditions necessary to select the next set of rows in the batched query

Definition at line 222 of file BatchRowIterator.php.

◆ current()

MediaWiki\Utils\BatchRowIterator::current ( )
Returns
array The most recently fetched set of rows from the database

Definition at line 159 of file BatchRowIterator.php.

◆ extractPrimaryKeys()

MediaWiki\Utils\BatchRowIterator::extractPrimaryKeys ( $row)

Extracts the primary key(s) from a database row.

Parameters
\\stdClass$rowAn individual database row from this iterator
Returns
array Map of primary key column to value within the row

Definition at line 147 of file BatchRowIterator.php.

◆ getChildren()

MediaWiki\Utils\BatchRowIterator::getChildren ( )

Definition at line 193 of file BatchRowIterator.php.

◆ hasChildren()

MediaWiki\Utils\BatchRowIterator::hasChildren ( )
Returns
bool True when this result set has rows

Definition at line 189 of file BatchRowIterator.php.

◆ key()

MediaWiki\Utils\BatchRowIterator::key ( )
Returns
int 0-indexed count of the page number fetched

Definition at line 166 of file BatchRowIterator.php.

◆ next()

MediaWiki\Utils\BatchRowIterator::next ( )

Fetch the next set of rows from the database.

Definition at line 200 of file BatchRowIterator.php.

◆ rewind()

MediaWiki\Utils\BatchRowIterator::rewind ( )

Reset the iterator to the beginning of the table.

Definition at line 173 of file BatchRowIterator.php.

◆ setCaller()

MediaWiki\Utils\BatchRowIterator::setCaller ( $caller)

Use ->setCaller( METHOD ) to indicate which code is using this class.

Only used in debugging output.

Since
1.36
Deprecated
since 1.44 Use the SelectQueryBuilder object directly
Parameters
string$caller
Returns
self

Definition at line 135 of file BatchRowIterator.php.

◆ setFetchColumns()

MediaWiki\Utils\BatchRowIterator::setFetchColumns ( array $columns)
Deprecated
since 1.44 Use the SelectQueryBuilder object directly
Parameters
array$columnsList of column names to select from the table suitable for use with IDatabase::select()

Definition at line 113 of file BatchRowIterator.php.

◆ valid()

MediaWiki\Utils\BatchRowIterator::valid ( )
Returns
bool True when the iterator is in a valid state

Definition at line 182 of file BatchRowIterator.php.

Member Data Documentation

◆ $batchSize

int MediaWiki\Utils\BatchRowIterator::$batchSize
protected

The number of rows to fetch per iteration.

Definition at line 38 of file BatchRowIterator.php.

Referenced by MediaWiki\Utils\BatchRowIterator\__construct().

◆ $db

IReadableDatabase MediaWiki\Utils\BatchRowIterator::$db
protected

Definition at line 28 of file BatchRowIterator.php.

Referenced by MediaWiki\Utils\BatchRowIterator\__construct().

◆ $primaryKey

array MediaWiki\Utils\BatchRowIterator::$primaryKey
protected

The name of the primary key(s)

Definition at line 33 of file BatchRowIterator.php.

Referenced by MediaWiki\Utils\BatchRowIterator\__construct().

◆ $sqb

SelectQueryBuilder MediaWiki\Utils\BatchRowIterator::$sqb

Underlying database query builder, may be mutated before iteration begins.

Definition at line 53 of file BatchRowIterator.php.

Referenced by MediaWiki\Utils\BatchRowIterator\__construct().


The documentation for this class was generated from the following file: