PHP Classes
Icontem

File: DATA/MySQL5/TableIterator.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Martin Alterisio  >  DATA  >  DATA/MySQL5/TableIterator.php  
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
 

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->query0);
        
$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;
    }
}
?>

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products