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 4                                                        |
  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.4 2004/04/23 17:06:01 lsmith Exp $
  46.  
  47. /**
  48.  * @package  MDB2
  49.  * @category Database
  50.  * @author   Lukas Smith <smith@backendmedia.com>
  51.  */
  52.  
  53. class MDB2_Iterator extends MDB2_Result implements Iterator
  54. {
  55.     private $result;
  56.     private $row;
  57.     private $buffer;
  58.  
  59.     // }}}
  60.     // {{{ constructor
  61.  
  62.     /**
  63.      * Constructor
  64.      */
  65.     function __constructor(&$result)
  66.     {
  67.         $this->result =$result;
  68.     }
  69.  
  70.     // }}}
  71.     // {{{ seek()
  72.  
  73.     /**
  74.     * seek forward to a specific row in a result set
  75.     *
  76.     * @param int    $rownum    number of the row where the data can be found
  77.     * @return mixed MDB2_OK on success, a MDB2 error on failure
  78.     * @access public
  79.     */
  80.     function seek($rownum = 0)
  81.     {
  82.         if ($this->result->rownum == $rownum{
  83.             return true;
  84.         }
  85.         if ($this->result->rownum < $rownum{
  86.             return false;
  87.         }
  88.         $this->row = null;
  89.         $this->buffer = null;
  90.         return $this->result->seek($rownum);
  91.     }
  92.  
  93.     // }}}
  94.     // {{{ next()
  95.  
  96.     /**
  97.     * Fetch next row of data
  98.     *
  99.     * @return mixed data array on success, a MDB2 error on failure
  100.     * @access public
  101.     */
  102.     function next()
  103.     {
  104.         if ($this->buffer{
  105.             $this->row $this->buffer;
  106.             $this->buffer = null;
  107.             return true;
  108.         }
  109.         $row $this->result->fetchRow();
  110.         if (MDB2::isError($row)) {
  111.             $this->row = null;
  112.             return false;
  113.         }
  114.         $this->row $row;
  115.         return true;
  116.     }
  117.  
  118.     // }}}
  119.     // {{{ current()
  120.  
  121.     /**
  122.      * return a row of data
  123.      *
  124.      * @return mixed data array on success, a MDB2 error on failure
  125.      * @access public
  126.     */
  127.     function current()
  128.     {
  129.         return $this->row;
  130.     }
  131.  
  132.     // }}}
  133.     // {{{ valid()
  134.  
  135.     /**
  136.     * check if the end of the result set has been reached
  137.     *
  138.     * @return mixed true or false on sucess, a MDB2 error on failure
  139.     * @access public
  140.     */
  141.     function valid()
  142.     {
  143.         return $this->result->valid();
  144.     }
  145.  
  146.     // }}}
  147.     // {{{ free()
  148.  
  149.     /**
  150.      * Free the internal resources associated with result.
  151.      *
  152.      * @return boolean true on success, false if result is invalid
  153.      * @access public
  154.      */
  155.     function free()
  156.     {
  157.         return $this->result->free();
  158.     }
  159.  
  160.     // }}}
  161.     // {{{ destructor
  162.  
  163.     /**
  164.      * Destructor
  165.      */
  166.     function __destructor()
  167.     {
  168.         $this->free();
  169.     }
  170. }
  171.  
  172. class MDB2_BufferedIterator extends MDB2_Iterator implements BufferedIterator
  173. {
  174.     // }}}
  175.     // {{{ seek()
  176.  
  177.     /**
  178.     * seek to a specific row in a result set
  179.     *
  180.     * @param int    $rownum    number of the row where the data can be found
  181.     * @return mixed MDB2_OK on success, a MDB2 error on failure
  182.     * @access public
  183.     */
  184.     function seek($rownum = 0)
  185.     {
  186.         $this->row = null;
  187.         return $this->result->seek($rownum);
  188.     }
  189.  
  190.     // }}}
  191.     // {{{ valid()
  192.  
  193.     /**
  194.     * check if the end of the result set has been reached
  195.     *
  196.     * @return mixed true or false on sucess, a MDB2 error on failure
  197.     * @access public
  198.     */
  199.     function valid()
  200.     {
  201.         return $this->result->valid();
  202.     }
  203.  
  204.     // }}}
  205.     // {{{ next()
  206.  
  207.     /**
  208.     * Fetch next row of data
  209.     *
  210.     * @return mixed data array on success, a MDB2 error on failure
  211.     * @access public
  212.     */
  213.     function next()
  214.     {
  215.         $row $this->result->fetchRow();
  216.         if (MDB2::isError($row)) {
  217.             $this->row = null;
  218.             return false;
  219.         }
  220.         $this->row $row;
  221.         return true;
  222.     }
  223.  
  224.     // }}}
  225.     // {{{ size()
  226.  
  227.     /**
  228.      * returns the number of rows in a result object
  229.      *
  230.      * @return mixed MDB2 Error Object or the number of rows
  231.      * @access public
  232.      */
  233.     function size()
  234.     {
  235.         return $this->result->numRows();
  236.     }
  237.  
  238.     // }}}
  239.     // {{{ hasPrev()
  240.  
  241.     /**
  242.     * check if there is a previous row
  243.     *
  244.     * @return mixed true or false on sucess, a MDB2 error on failure
  245.     * @access public
  246.     */
  247.     function hasPrev()
  248.     {
  249.         return $this->result->rownum > - 1;
  250.     }
  251.  
  252.     // }}}
  253.     // {{{ rewind()
  254.  
  255.     /**
  256.     * seek to the first row in a result set
  257.     *
  258.     * @return mixed MDB2_OK on success, a MDB2 error on failure
  259.     * @access public
  260.     */
  261.     function rewind()
  262.     {
  263.         return $this->seek(0);
  264.     }
  265.  
  266.     // }}}
  267.     // {{{ prev()
  268.  
  269.     /**
  270.     * move internal row point to the previous row
  271.     * Fetch and return a row of data
  272.     *
  273.     * @return mixed data array on success, a MDB2 error on failure
  274.     * @access public
  275.     */
  276.     function prev()
  277.     {
  278.         if ($this->hasPrev()) {
  279.             $this->seek($this->result->rownum - 1);
  280.         else {
  281.             return false;
  282.         }
  283.         return $this->next();
  284.     }
  285. }
  286.  
  287. ?>

Documentation generated on Mon, 11 Mar 2019 10:15:47 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.