From: Tim Starling Date: Mon, 9 Jul 2007 00:48:40 +0000 (+0000) Subject: Added iterator interface to ResultWrapper. No support in Oracle yet. Updated document... X-Git-Tag: 1.31.0-rc.0~52198 X-Git-Url: http://git.cyclocoop.org/%22.%28%24lien.?a=commitdiff_plain;h=8c3b4616cdf2bcd9c8cc1264b806c47b2523f163;p=lhc%2Fweb%2Fwiklou.git Added iterator interface to ResultWrapper. No support in Oracle yet. Updated documentation for Database::getLag(). --- diff --git a/includes/Database.php b/includes/Database.php index 293ed055b1..eaffcf62f1 100644 --- a/includes/Database.php +++ b/includes/Database.php @@ -2084,8 +2084,7 @@ class Database { */ function getLag() { $res = $this->query( 'SHOW PROCESSLIST' ); - # Find slave SQL thread. Assumed to be the second one running, which is a bit - # dubious, but unfortunately there's no easy rigorous way + # Find slave SQL thread while ( $row = $this->fetchObject( $res ) ) { /* This should work for most situations - when default db * for thread is not specified, it had no events executed, @@ -2282,8 +2281,8 @@ class DatabaseMysql extends Database { * Result wrapper for grabbing data queried by someone else * @addtogroup Database */ -class ResultWrapper { - var $db, $result; +class ResultWrapper implements Iterator { + var $db, $result, $pos = 0, $currentRow = null; /** * Create a new result object from a result resource and a Database object @@ -2345,16 +2344,41 @@ class ResultWrapper { function seek( $row ) { $this->db->dataSeek( $this->result, $row ); } - - /** - * Reset the cursor to the start of the result set + + /********************* + * Iterator functions + * Note that using these in combination with the non-iterator functions + * above may cause rows to be skipped or repeated. */ + function rewind() { if ($this->numRows()) { $this->db->dataSeek($this->result, 0); } + $this->pos = 0; + $this->currentRow = null; + } + + function current() { + if ( is_null( $this->currentRow ) ) { + $this->next(); + } + return $this->currentRow; + } + + function key() { + return $this->pos; } + function next() { + $this->pos++; + $this->currentRow = $this->fetchObject(); + return $this->currentRow; + } + + function valid() { + return $this->current() !== false; + } }