MDB2
[ class tree: MDB2 ] [ index: MDB2 ] [ all elements ]

Source for file Iterator.php

Documentation is available at Iterator.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP version 5                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2004 Manuel Lemos, Tomas V.V.Cox,                 |
  6. // | Stig. S. Bakken, Lukas Smith                                         |
  7. // | All rights reserved.                                                 |
  8. // +----------------------------------------------------------------------+
  9. // | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB  |
  10. // | API as well as database abstraction for PHP applications.            |
  11. // | This LICENSE is in the BSD license style.                            |
  12. // |                                                                      |
  13. // | Redistribution and use in source and binary forms, with or without   |
  14. // | modification, are permitted provided that the following conditions   |
  15. // | are met:                                                             |
  16. // |                                                                      |
  17. // | Redistributions of source code must retain the above copyright       |
  18. // | notice, this list of conditions and the following disclaimer.        |
  19. // |                                                                      |
  20. // | Redistributions in binary form must reproduce the above copyright    |
  21. // | notice, this list of conditions and the following disclaimer in the  |
  22. // | documentation and/or other materials provided with the distribution. |
  23. // |                                                                      |
  24. // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken,    |
  25. // | Lukas Smith nor the names of his contributors may be used to endorse |
  26. // | or promote products derived from this software without specific prior|
  27. // | written permission.                                                  |
  28. // |                                                                      |
  29. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  |
  30. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT    |
  31. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    |
  32. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE      |
  33. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,          |
  34. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  35. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  36. // |  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED  |
  37. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT          |
  38. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  39. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE          |
  40. // | POSSIBILITY OF SUCH DAMAGE.                                          |
  41. // +----------------------------------------------------------------------+
  42. // | Author: Lukas Smith <smith@backendmedia.com>                         |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id: Iterator.php,v 1.12 2005/04/07 15:54:41 lsmith Exp $
  46.  
  47. /**
  48.  * @package  MDB2
  49.  * @category Database
  50.  * @author   Lukas Smith <smith@backendmedia.com>
  51.  */
  52. class MDB2_Iterator implements Iterator
  53. {
  54.     protected $result;
  55.     protected $row;
  56.  
  57.     // {{{ constructor
  58.  
  59.     /**
  60.      * Constructor
  61.      */
  62.     public function __construct($result)
  63.     {
  64.         $this->result = $result;
  65.     }
  66.  
  67.     // }}}
  68.     // {{{ seek()
  69.  
  70.     /**
  71.     * seek forward to a specific row in a result set
  72.     *
  73.     * @param int    $rownum    number of the row where the data can be found
  74.     * @return void 
  75.     * @access public
  76.     */
  77.     public function seek($rownum)
  78.     {
  79.         $this->row = null;
  80.         if ($this->result{
  81.             $this->result->seek($rownum);
  82.         }
  83.     }
  84.  
  85.     // }}}
  86.     // {{{ next()
  87.  
  88.     /**
  89.     * Fetch next row of data
  90.     *
  91.     * @return void 
  92.     * @access public
  93.     */
  94.     public function next()
  95.     {
  96.         $this->row = null;
  97.     }
  98.  
  99.     // }}}
  100.     // {{{ current()
  101.  
  102.     /**
  103.      * return a row of data
  104.      *
  105.     * @return void 
  106.     * @access public
  107.     */
  108.     public function current()
  109.     {
  110.         if (is_null($this->row)) {
  111.             $row $this->result->fetchRow();
  112.             if (PEAR::isError($row)) {
  113.                 $row = false;
  114.             }
  115.             $this->row = $row;
  116.         }
  117.         return $this->row;
  118.     }
  119.  
  120.     // }}}
  121.     // {{{ valid()
  122.  
  123.     /**
  124.     * check if the end of the result set has been reached
  125.     *
  126.     * @return mixed true or false on sucess, a MDB2 error on failure
  127.     * @access public
  128.     */
  129.     public function valid()
  130.     {
  131.         return (bool)$this->current();
  132.     }
  133.  
  134.     // }}}
  135.     // {{{ free()
  136.  
  137.     /**
  138.      * Free the internal resources associated with result.
  139.      *
  140.      * @return boolean true on success, false if result is invalid
  141.      * @access public
  142.      */
  143.     public function free()
  144.     {
  145.         if ($this->result{
  146.             return $this->result->free();
  147.         }
  148.         $this->result = null;
  149.         $this->row = null;
  150.         return false;
  151.     }
  152.  
  153.     // }}}
  154.     // {{{ key()
  155.  
  156.     /**
  157.     * nothing, but Iterator wants to implement this.
  158.     *
  159.     * @return void 
  160.     * @access public
  161.     */
  162.     public function key()
  163.     {
  164.         if ($this->result{
  165.             return $this->result->getRowCounter();
  166.         }
  167.         return false;
  168.     }
  169.  
  170.     // }}}
  171.     // {{{ rewind()
  172.  
  173.     /**
  174.     * seek to the first row in a result set
  175.     *
  176.     * @return void 
  177.     * @access public
  178.     */
  179.     public function rewind()
  180.     {
  181.     }
  182.  
  183.     // }}}
  184.     // {{{ destructor
  185.  
  186.     /**
  187.      * Destructor
  188.      */
  189.     public function __destruct()
  190.     {
  191.         $this->free();
  192.     }
  193. }
  194.  
  195. class MDB2_BufferedIterator extends MDB2_Iterator implements SeekableIterator
  196. {
  197.     // }}}
  198.     // {{{ valid()
  199.  
  200.     /**
  201.     * check if the end of the result set has been reached
  202.     *
  203.     * @return mixed true or false on sucess, a MDB2 error on failure
  204.     * @access public
  205.     */
  206.     public function valid()
  207.     {
  208.         if ($this->result{
  209.             return $this->result->valid();
  210.         }
  211.         return false;
  212.     }
  213.  
  214.     // }}}
  215.     // {{{count()
  216.  
  217.     /**
  218.      * returns the number of rows in a result object
  219.      *
  220.      * @return mixed MDB2 Error Object or the number of rows
  221.      * @access public
  222.      */
  223.     public function count()
  224.     {
  225.         if ($this->result{
  226.             return $this->result->numRows();
  227.         }
  228.         return false;
  229.     }
  230.  
  231.     // }}}
  232.     // {{{ hasPrev()
  233.  
  234.     /**
  235.     * check if there is a previous row
  236.     *
  237.     * @return mixed true or false on sucess, a MDB2 error on failure
  238.     * @access public
  239.     */
  240.     public function hasPrev()
  241.     {
  242.         if ($this->result{
  243.             return $this->result->getRowCounter(> 0;
  244.         }
  245.         return false;
  246.     }
  247.  
  248.     // }}}
  249.     // {{{ rewind()
  250.  
  251.     /**
  252.     * seek to the first row in a result set
  253.     *
  254.     * @return mixed MDB2_OK on success, a MDB2 error on failure
  255.     * @access public
  256.     */
  257.     public function rewind()
  258.     {
  259.         $this->seek(0);
  260.     }
  261.  
  262.     // }}}
  263.     // {{{ prev()
  264.  
  265.     /**
  266.     * move internal row point to the previous row
  267.     * Fetch and return a row of data
  268.     *
  269.     * @return void 
  270.     * @access public
  271.     */
  272.     public function prev()
  273.     {
  274.         if ($this->hasPrev()) {
  275.             $this->seek($this->result->getRowCounter(- 1);
  276.         else {
  277.             return false;
  278.         }
  279.         return $this->next();
  280.     }
  281. }
  282.  
  283. ?>

Documentation generated on Mon, 11 Mar 2019 14:20:18 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.