PHP Classes

File: DATA/MySQL5/TableIterator.php

Recommend this page to a friend!
  Classes of Martin Alterisio   DATA   DATA/MySQL5/TableIterator.php   Download  
File: DATA/MySQL5/TableIterator.php
Role: Class source
Content type: text/plain
Description: Table rows iterator.
Class: DATA
Access data stored in MySQL tables like arrays
Author: By
Last change:
Date: 16 years ago
Size: 3,142 bytes
 

Contents

Class file image Download
<?php
/**
 * @package DATA_MySQL5
 */

/**
 * Table rows iterator.
 *
 * Row access isn't read only on iteration, which means rows can be modified
 * on a foreach loop, but this modifications won't be observed on the iteration
 * itself, since the iteration results are cached.
 */
class DATA_MySQL5_TableIterator implements Iterator {
   
/**
     * Table name.
     * @var string
     * @access private
     */
   
private $table;
   
/**
     * Stored query.
     * @var resource
     * @access private
     */
   
private $query;
   
/**
     * Last fetched row data.
     * @var array
     * @access private
     */
   
private $currentRecord;
   
/**
     * Last fetched row number.
     * @var int
     * @access private
     */
   
private $currentRecordNumber;
   
   
/**
     * Default constructor.
     * @access protected
     * @param string $table The table name.
     */
   
public function __construct($table) {
       
$this->table = $table;
       
$this->query = null;
       
$this->currentRecord = null;
       
$this->currentRecordNumber = 0;
    }
   
   
/**
     * Destructor.
     * Frees the stored query.
     */
   
public function __destruct() {
        if (
$this->query !== null) {
           
DATA_MySQL5_Access::freeResult($this->query);
        }
    }
   
   
/**
     * Lazily runs the stored query for this iteration.
     * Ran before starting any other function call that requires the stored query.
     * @access private
     */
   
private function lazyRunQuery() {
        if (
$this->query === null) {
           
$this->query = DATA_MySQL5_Access::query("
                SELECT *
                  FROM `
{$this->table}`
            "
);
           
$this->currentRecord = DATA_MySQL5_Access::fetchAssoc($this->query);
        }
    }
   
   
/**
     * Returns current row in iteration.
     * @return DATA_MySQL5_Row The current row.
     */
   
public function current() {
       
$this->lazyRunQuery();
        return new
DATA_MySQL5_Row($this->table, $this->currentRecord);
    }
   
   
/**
     * Returns current row number.
     * @return int The current row number.
     */
   
public function key() {
        return
$this->currentRecordNumber;
    }
   
   
/**
     * Moves the iteration to the next row.
     */
   
public function next() {
       
$this->lazyRunQuery();
       
$this->currentRecord = DATA_MySQL5_Access::fetchAssoc($this->query);
       
$this->currentRecordNumber++;
    }
   
   
/**
     * Moves the iteration to the first row.
     */
   
public function rewind() {
       
$this->lazyRunQuery();
       
DATA_MySQL5_Access::dataSeek($this->query, 0);
       
$this->currentRecord = DATA_MySQL5_Access::fetchAssoc($this->query);
       
$this->currentRecordNumber = 0;
    }
   
   
/**
     * Indicates if there are still rows in the iteration.
     * @return bool True if there are still rows remaining.
     */
   
public function valid() {
       
$this->lazyRunQuery();
        return
$this->currentRecord !== false;
    }
}
?>