MediaWiki REL1_31
ResultWrapper.php
Go to the documentation of this file.
1<?php
2
3namespace Wikimedia\Rdbms;
4
5use stdClass;
6use RuntimeException;
7
24class ResultWrapper implements IResultWrapper {
26 public $result;
27
29 protected $db;
30
32 protected $pos = 0;
34 protected $currentRow = null;
35
45 public function __construct( IDatabase $db = null, $result ) {
46 $this->db = $db;
47 if ( $result instanceof ResultWrapper ) {
48 $this->result = $result->result;
49 } else {
50 $this->result = $result;
51 }
52 }
53
54 public function numRows() {
55 return $this->getDB()->numRows( $this );
56 }
57
58 public function fetchObject() {
59 return $this->getDB()->fetchObject( $this );
60 }
61
62 public function fetchRow() {
63 return $this->getDB()->fetchRow( $this );
64 }
65
66 public function seek( $row ) {
67 $this->getDB()->dataSeek( $this, $row );
68 }
69
70 public function free() {
71 if ( $this->db ) {
72 $this->db->freeResult( $this );
73 $this->db = null;
74 }
75 $this->result = null;
76 }
77
82 private function getDB() {
83 if ( !$this->db ) {
84 throw new RuntimeException( static::class . ' needs a DB handle for iteration.' );
85 }
86
87 return $this->db;
88 }
89
90 function rewind() {
91 if ( $this->numRows() ) {
92 $this->getDB()->dataSeek( $this, 0 );
93 }
94 $this->pos = 0;
95 $this->currentRow = null;
96 }
97
98 function current() {
99 if ( is_null( $this->currentRow ) ) {
100 $this->next();
101 }
102
103 return $this->currentRow;
104 }
105
106 function key() {
107 return $this->pos;
108 }
109
110 function next() {
111 $this->pos++;
112 $this->currentRow = $this->fetchObject();
113
114 return $this->currentRow;
115 }
116
117 function valid() {
118 return $this->current() !== false;
119 }
120}
121
122class_alias( ResultWrapper::class, 'ResultWrapper' );
Result wrapper for grabbing data queried from an IDatabase object.
seek( $row)
Change the position of the cursor in a result object.
__construct(IDatabase $db=null, $result)
Create a row iterator from a result resource and an optional Database object.
free()
Free a result object.
fetchObject()
Fetch the next row from the given result object, in object form.
resource array null $result
Optional underlying result handle for subclass usage.
fetchRow()
Fetch the next row from the given result object, in associative array form.
numRows()
Get the number of rows in a result object.
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38
Result wrapper for grabbing data queried from an IDatabase object.