| 1 |
<?php |
| 2 |
|
| 3 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class BatchRowIterator implements RecursiveIterator { |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
protected $db; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
protected $table; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
protected $primaryKey; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
protected $batchSize; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
protected $conditions = []; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
protected $joinConditions = []; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
protected $fetchColumns; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
protected $orderBy; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
private $current = []; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
private $key = -1; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
protected $options = []; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
protected $caller; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public function __construct( IReadableDatabase $db, $table, $primaryKey, $batchSize ) { |
| 105 |
if ( $batchSize < 1 ) { |
| 106 |
throw new InvalidArgumentException( 'Batch size must be at least 1 row.' ); |
| 107 |
} |
| 108 |
$this->db = $db; |
| 109 |
$this->table = $table; |
| 110 |
$this->primaryKey = (array)$primaryKey; |
| 111 |
$this->fetchColumns = $this->primaryKey; |
| 112 |
$this->orderBy = implode( ' ASC,', $this->primaryKey ) . ' ASC'; |
| 113 |
$this->batchSize = $batchSize; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public function addConditions( array $conditions ) { |
| 121 |
$this->conditions = array_merge( $this->conditions, $conditions ); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public function addOptions( array $options ) { |
| 129 |
$this->options = array_merge( $this->options, $options ); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public function addJoinConditions( array $conditions ) { |
| 137 |
$this->joinConditions = array_merge( $this->joinConditions, $conditions ); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public function setFetchColumns( array $columns ) { |
| 145 |
|
| 146 |
if ( count( $columns ) === 1 && reset( $columns ) === '*' ) { |
| 147 |
$this->fetchColumns = $columns; |
| 148 |
} else { |
| 149 |
$this->fetchColumns = array_unique( array_merge( |
| 150 |
$this->primaryKey, |
| 151 |
$columns |
| 152 |
) ); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public function setCaller( $caller ) { |
| 165 |
$this->caller = $caller; |
| 166 |
|
| 167 |
return $this; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public function extractPrimaryKeys( $row ) { |
| 177 |
$pk = []; |
| 178 |
foreach ( $this->primaryKey as $alias => $column ) { |
| 179 |
$name = is_numeric( $alias ) ? $column : $alias; |
| 180 |
$pk[$name] = $row->{$name}; |
| 181 |
} |
| 182 |
return $pk; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
public function current(): array { |
| 189 |
return $this->current; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public function key(): int { |
| 196 |
return $this->key; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public function rewind(): void { |
| 203 |
$this->key = -1; |
| 204 |
$this->current = []; |
| 205 |
$this->next(); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public function valid(): bool { |
| 212 |
return (bool)$this->current; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public function hasChildren(): bool { |
| 219 |
return $this->current && count( $this->current ); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
public function getChildren(): ?RecursiveIterator { |
| 226 |
return new NotRecursiveIterator( new ArrayIterator( $this->current ) ); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
public function next(): void { |
| 233 |
$caller = __METHOD__; |
| 234 |
if ( (string)$this->caller !== '' ) { |
| 235 |
$caller .= " (for {$this->caller})"; |
| 236 |
} |
| 237 |
|
| 238 |
$res = $this->db->newSelectQueryBuilder() |
| 239 |
->tables( is_array( $this->table ) ? $this->table : [ $this->table ] ) |
| 240 |
->fields( $this->fetchColumns ) |
| 241 |
->where( $this->buildConditions() ) |
| 242 |
->caller( $caller ) |
| 243 |
->limit( $this->batchSize ) |
| 244 |
->orderBy( $this->orderBy ) |
| 245 |
->options( $this->options ) |
| 246 |
->joinConds( $this->joinConditions ) |
| 247 |
->fetchResultSet(); |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
$this->current = iterator_to_array( $res ); |
| 253 |
$this->key++; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
protected function buildConditions() { |
| 265 |
if ( !$this->current ) { |
| 266 |
return $this->conditions; |
| 267 |
} |
| 268 |
|
| 269 |
$maxRow = end( $this->current ); |
| 270 |
$maximumValues = []; |
| 271 |
foreach ( $this->primaryKey as $alias => $column ) { |
| 272 |
$name = is_numeric( $alias ) ? $column : $alias; |
| 273 |
$maximumValues[$column] = $maxRow->$name; |
| 274 |
} |
| 275 |
|
| 276 |
$conditions = $this->conditions; |
| 277 |
$conditions[] = $this->db->buildComparison( '>', $maximumValues ); |
| 278 |
|
| 279 |
return $conditions; |
| 280 |
} |
| 281 |
} |