| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Rdbms; |
| 4 |
|
| 5 |
use OutOfBoundsException; |
| 6 |
use stdClass; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
abstract class ResultWrapper implements IResultWrapper { |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
protected $nextPos = 0; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
protected $currentPos = 0; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
protected $currentRow; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
private $fieldNames; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
abstract protected function doNumRows(); |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
abstract protected function doFetchObject(); |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
abstract protected function doFetchRow(); |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
abstract protected function doSeek( $pos ); |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
abstract protected function doFree(); |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
abstract protected function doGetFieldNames(); |
| 92 |
|
| 93 |
public function numRows() { |
| 94 |
return $this->doNumRows(); |
| 95 |
} |
| 96 |
|
| 97 |
public function count(): int { |
| 98 |
return $this->doNumRows(); |
| 99 |
} |
| 100 |
|
| 101 |
public function fetchObject() { |
| 102 |
$this->currentPos = $this->nextPos++; |
| 103 |
$this->currentRow = $this->doFetchObject(); |
| 104 |
return $this->currentRow; |
| 105 |
} |
| 106 |
|
| 107 |
public function fetchRow() { |
| 108 |
$this->currentPos = $this->nextPos++; |
| 109 |
$this->currentRow = $this->doFetchRow(); |
| 110 |
return $this->currentRow; |
| 111 |
} |
| 112 |
|
| 113 |
public function seek( $pos ): void { |
| 114 |
$numRows = $this->numRows(); |
| 115 |
|
| 116 |
$max = $numRows ? $numRows - 1 : 0; |
| 117 |
if ( $pos < 0 || $pos > $max ) { |
| 118 |
throw new OutOfBoundsException( __METHOD__ . ': invalid position' ); |
| 119 |
} |
| 120 |
if ( $numRows ) { |
| 121 |
$this->doSeek( $pos ); |
| 122 |
} |
| 123 |
$this->nextPos = $pos; |
| 124 |
$this->currentPos = $pos; |
| 125 |
$this->currentRow = null; |
| 126 |
} |
| 127 |
|
| 128 |
public function free() { |
| 129 |
$this->doFree(); |
| 130 |
$this->currentRow = false; |
| 131 |
} |
| 132 |
|
| 133 |
public function rewind(): void { |
| 134 |
$this->seek( 0 ); |
| 135 |
} |
| 136 |
|
| 137 |
#[\ReturnTypeWillChange] |
| 138 |
public function current() { |
| 139 |
$this->currentRow ??= $this->fetchObject(); |
| 140 |
|
| 141 |
return $this->currentRow; |
| 142 |
} |
| 143 |
|
| 144 |
public function key(): int { |
| 145 |
return $this->currentPos; |
| 146 |
} |
| 147 |
|
| 148 |
public function next(): void { |
| 149 |
$this->fetchObject(); |
| 150 |
} |
| 151 |
|
| 152 |
public function valid(): bool { |
| 153 |
return $this->currentPos >= 0 |
| 154 |
&& $this->currentPos < $this->numRows(); |
| 155 |
} |
| 156 |
|
| 157 |
public function getFieldNames() { |
| 158 |
$this->fieldNames ??= $this->doGetFieldNames(); |
| 159 |
return $this->fieldNames; |
| 160 |
} |
| 161 |
} |