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

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