<?php
+
+use Wikimedia\Rdbms\IDatabase;
+
/**
* Allows iterating a large number of rows in batches transparently.
* By default when iterated over returns the full query result as an
protected $primaryKey;
/**
- * @var integer $batchSize The number of rows to fetch per iteration
+ * @var int $batchSize The number of rows to fetch per iteration
*/
protected $batchSize;
private $current = [];
/**
- * @var integer key 0-indexed number of pages fetched since self::reset()
+ * @var int key 0-indexed number of pages fetched since self::reset()
*/
private $key;
+ /**
+ * @var array Additional query options
+ */
+ protected $options = [];
+
/**
* @param IDatabase $db The database to read from
- * @param string|array $table The name or names of the table to read from
+ * @param string|array $table The name or names of the table to read from
* @param string|array $primaryKey The name or names of the primary key columns
- * @param integer $batchSize The number of rows to fetch per iteration
- * @throws MWException
+ * @param int $batchSize The number of rows to fetch per iteration
+ * @throws InvalidArgumentException
*/
public function __construct( IDatabase $db, $table, $primaryKey, $batchSize ) {
if ( $batchSize < 1 ) {
- throw new MWException( 'Batch size must be at least 1 row.' );
+ throw new InvalidArgumentException( 'Batch size must be at least 1 row.' );
}
$this->db = $db;
$this->table = $table;
}
/**
- * @param array $condition Query conditions suitable for use with
+ * @param array $conditions Query conditions suitable for use with
* IDatabase::select
*/
public function addConditions( array $conditions ) {
}
/**
- * @param array $condition Query join conditions suitable for use
+ * @param array $options Query options suitable for use with
+ * IDatabase::select
+ */
+ public function addOptions( array $options ) {
+ $this->options = array_merge( $this->options, $options );
+ }
+
+ /**
+ * @param array $conditions Query join conditions suitable for use
* with IDatabase::select
*/
public function addJoinConditions( array $conditions ) {
}
/**
- * @return integer 0-indexed count of the page number fetched
+ * @return int 0-indexed count of the page number fetched
*/
public function key() {
return $this->key;
[
'LIMIT' => $this->batchSize,
'ORDER BY' => $this->orderBy,
- ],
+ ] + $this->options,
$this->joinConditions
);
* `=` conditions while the final key uses a `>` condition
*
* Example output:
- * [ '( foo = 42 AND bar > 7 ) OR ( foo > 42 )' ]
+ * [ '( foo = 42 AND bar > 7 ) OR ( foo > 42 )' ]
*
* @return array The SQL conditions necessary to select the next set
* of rows in the batched query