MediaWiki  master
BatchRowIterator Class Reference

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

Inheritance diagram for BatchRowIterator:
Collaboration diagram for BatchRowIterator:

Public Member Functions

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

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. More...
 
 buildGreaterThanCondition (array $quotedMaximumValues)
 Given an array of column names and their maximum value generate an SQL condition where all keys except the last match $quotedMaximumValues exactly and the last column is greater than the matching value in $quotedMaximumValues. More...
 

Protected Attributes

int $batchSize
 The number of rows to fetch per iteration. More...
 
 $caller
 For debugging which method is using this class. More...
 
array $conditions = []
 Array of strings containing SQL conditions to add to the query. More...
 
IDatabase $db
 The database to read from. More...
 
array $fetchColumns
 List of column names to select from the table suitable for use with IDatabase::select() More...
 
array $joinConditions = []
 
array $options = []
 Additional query options. More...
 
string $orderBy
 SQL Order by condition generated from $this->primaryKey. More...
 
array $primaryKey
 The name of the primary key(s) More...
 
string array $table
 The name or names of the table to read from. More...
 

Private Attributes

array $current = []
 The current iterator value. More...
 
int $key
 0-indexed number of pages fetched since self::reset() More...
 

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.

@newable

Definition at line 33 of file BatchRowIterator.php.

Constructor & Destructor Documentation

◆ __construct()

BatchRowIterator::__construct ( IDatabase  $db,
  $table,
  $primaryKey,
  $batchSize 
)

Stable to call.

Parameters
IDatabase$dbThe database to read from
string | array$tableThe name or names of the table to read from
string | array$primaryKeyThe name or names of the primary key columns
int$batchSizeThe number of rows to fetch per iteration
Exceptions
InvalidArgumentException

Definition at line 105 of file BatchRowIterator.php.

References $batchSize, $db, $primaryKey, and $table.

Member Function Documentation

◆ addConditions()

BatchRowIterator::addConditions ( array  $conditions)
Parameters
array$conditionsQuery conditions suitable for use with IDatabase::select

Definition at line 121 of file BatchRowIterator.php.

References $conditions.

Referenced by CategoryChangesAsRdf\addTimestampConditions().

◆ addJoinConditions()

BatchRowIterator::addJoinConditions ( array  $conditions)
Parameters
array$conditionsQuery join conditions suitable for use with IDatabase::select

Definition at line 137 of file BatchRowIterator.php.

References $conditions.

◆ addOptions()

BatchRowIterator::addOptions ( array  $options)
Parameters
array$optionsQuery options suitable for use with IDatabase::select

Definition at line 129 of file BatchRowIterator.php.

References $options.

Referenced by CategoryChangesAsRdf\addIndex().

◆ buildConditions()

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.

All except the final key use = conditions while the final key uses a > condition

Example output: [ '( foo = 42 AND bar > 7 ) OR ( foo > 42 )' ]

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

Definition at line 270 of file BatchRowIterator.php.

◆ buildGreaterThanCondition()

BatchRowIterator::buildGreaterThanCondition ( array  $quotedMaximumValues)
protected

Given an array of column names and their maximum value generate an SQL condition where all keys except the last match $quotedMaximumValues exactly and the last column is greater than the matching value in $quotedMaximumValues.

Parameters
array$quotedMaximumValuesThe maximum values quoted with $this->db->addQuotes()
Returns
string An SQL condition that will select rows where all columns match the maximum value exactly except the last column which must be greater than the provided maximum value

Definition at line 313 of file BatchRowIterator.php.

References $keys.

◆ current()

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

Definition at line 189 of file BatchRowIterator.php.

◆ extractPrimaryKeys()

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 177 of file BatchRowIterator.php.

◆ getChildren()

BatchRowIterator::getChildren ( )
Returns
RecursiveIterator

Definition at line 226 of file BatchRowIterator.php.

◆ hasChildren()

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

Definition at line 219 of file BatchRowIterator.php.

◆ key()

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

Definition at line 196 of file BatchRowIterator.php.

◆ next()

BatchRowIterator::next ( )

Fetch the next set of rows from the database.

Definition at line 233 of file BatchRowIterator.php.

References $res.

◆ rewind()

BatchRowIterator::rewind ( )

Reset the iterator to the begining of the table.

Definition at line 203 of file BatchRowIterator.php.

◆ setCaller()

BatchRowIterator::setCaller (   $caller)

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

Only used in debugging output.

Since
1.36
Parameters
string$caller
Returns
self

Definition at line 165 of file BatchRowIterator.php.

References $caller.

◆ setFetchColumns()

BatchRowIterator::setFetchColumns ( array  $columns)
Parameters
array$columnsList of column names to select from the table suitable for use with IDatabase::select()

Definition at line 145 of file BatchRowIterator.php.

◆ valid()

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

Definition at line 212 of file BatchRowIterator.php.

Member Data Documentation

◆ $batchSize

int BatchRowIterator::$batchSize
protected

The number of rows to fetch per iteration.

Definition at line 53 of file BatchRowIterator.php.

Referenced by __construct().

◆ $caller

BatchRowIterator::$caller
protected

For debugging which method is using this class.

Definition at line 94 of file BatchRowIterator.php.

Referenced by setCaller().

◆ $conditions

array BatchRowIterator::$conditions = []
protected

Array of strings containing SQL conditions to add to the query.

Definition at line 58 of file BatchRowIterator.php.

Referenced by addConditions(), and addJoinConditions().

◆ $current

array BatchRowIterator::$current = []
private

The current iterator value.

Definition at line 79 of file BatchRowIterator.php.

◆ $db

IDatabase BatchRowIterator::$db
protected

The database to read from.

Definition at line 38 of file BatchRowIterator.php.

Referenced by __construct().

◆ $fetchColumns

array BatchRowIterator::$fetchColumns
protected

List of column names to select from the table suitable for use with IDatabase::select()

Definition at line 69 of file BatchRowIterator.php.

◆ $joinConditions

array BatchRowIterator::$joinConditions = []
protected

Definition at line 63 of file BatchRowIterator.php.

◆ $key

int BatchRowIterator::$key
private

0-indexed number of pages fetched since self::reset()

Definition at line 84 of file BatchRowIterator.php.

◆ $options

array BatchRowIterator::$options = []
protected

Additional query options.

Definition at line 89 of file BatchRowIterator.php.

Referenced by addOptions().

◆ $orderBy

string BatchRowIterator::$orderBy
protected

SQL Order by condition generated from $this->primaryKey.

Definition at line 74 of file BatchRowIterator.php.

◆ $primaryKey

array BatchRowIterator::$primaryKey
protected

The name of the primary key(s)

Definition at line 48 of file BatchRowIterator.php.

Referenced by __construct().

◆ $table

string array BatchRowIterator::$table
protected

The name or names of the table to read from.

Definition at line 43 of file BatchRowIterator.php.

Referenced by __construct().


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