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

Source for file Result.php

Documentation is available at Result.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP Version 4                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2003 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.02 of the PHP license,      |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/2_02.txt.                                 |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Author: Lorenzo Alberton <l.alberton at quipo.it>                    |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: Result.php,v 1.6 2004/04/27 23:49:14 quipo Exp $
  19. //
  20. // This is just a port of DB_QueryTool, originally written by
  21. // Wolfram Kriesing and Paolo Panto, vision:produktion <wk@visionp.de>
  22. // All the praises go to them :)
  23. //
  24.  
  25. /**
  26.  * this result actually contains the 'data' itself, the number of rows
  27.  * returned and some additional info
  28.  * using ZE2 you can also get retreive data from the result doing the following:
  29.  * <MDB_QueryTool_Common-instance>->getAll()->getCount()
  30.  * or
  31.  * <MDB_QueryTool_Common-instance>->getAll()->getData()
  32.  *
  33.  *
  34.  * @package    MDB_QueryTool
  35.  * @version    2004/04/28
  36.  * @access     public
  37.  * @author     Lorenzo Alberton
  38.  */
  39. {
  40.     // {{{ class vars
  41.  
  42.     /**
  43.      * @var array 
  44.      */
  45.     var $_data = array();
  46.  
  47.     /**
  48.      * @var array 
  49.      */
  50.     var $_dataKeys = array();
  51.  
  52.     /**
  53.      * @var integer 
  54.      */
  55.     var $_count = 0;
  56.  
  57.     /**
  58.      * the counter for the methods getFirst, getNext
  59.      * @var array 
  60.      */
  61.     var $_counter = null;
  62.  
  63.     // }}}
  64.     // {{{ MDB_QueryTool_Result()
  65.  
  66.     /**
  67.      * create a new instance of result with the data returned by the query
  68.      *
  69.      * @param      array   the data returned by the result
  70.      * @access     public
  71.      */
  72.     function MDB_QueryTool_Result($data)
  73.     {
  74.         if (!count($data)) {
  75.             $this->_count = 0;
  76.         else {
  77.             list($firstElement$data;
  78.             if (is_array($firstElement)) // is the array a collection of rows?
  79.                 $this->_count sizeof($data);
  80.             else {
  81.                 if (sizeof($data> 0{
  82.                     $this->_count = 1;
  83.                 else {
  84.                     $this->_count = 0;
  85.                 }
  86.             }
  87.         }
  88.         $this->_data $data;
  89.     }
  90.  
  91.     // }}}
  92.     // {{{ numRows
  93.  
  94.     /**
  95.      * return the number of rows returned. This is an alias for getCount().
  96.      *
  97.      * @access    public
  98.      * @return    integer 
  99.      */
  100.     function numRows()
  101.     {
  102.         return $this->_count;
  103.     }
  104.  
  105.     // }}}
  106.     // {{{ getCount()
  107.  
  108.     /**
  109.      * return the number of rows returned
  110.      *
  111.      * @access     public
  112.      * @return integer the number of rows returned
  113.      */
  114.     function getCount()
  115.     {
  116.         return $this->_count;
  117.     }
  118.  
  119.     // }}}
  120.     // {{{ getData()
  121.  
  122.     /**
  123.      * get all the data returned
  124.      *
  125.      * @param  string $key 
  126.      * @return mixed array or PEAR_Error
  127.      * @access public
  128.      */
  129.     function getData($key=null)
  130.     {
  131.         if (is_null($key)) {
  132.             return $this->_data;
  133.         }
  134.         if ($this->_data[$key]{
  135.             return $this->_data[$key];
  136.         }
  137.         return new PEAR_Error("there is no element with the key '$key'!");
  138.     }
  139.  
  140.     // }}}
  141.     // {{{ getFirst()
  142.  
  143.     /**
  144.      * get the first result set
  145.      * we are not using next, current, and reset, since those ignore keys
  146.      * which are empty or 0
  147.      *
  148.      * @return mixed 
  149.      * @access public
  150.      */
  151.     function getFirst()
  152.     {
  153.         if ($this->getCount(> 0{
  154.             $this->_dataKeys array_keys($this->_data);
  155.             $this->_counter = 0;
  156.             return $this->_data[$this->_dataKeys[$this->_counter]];
  157.         }
  158.         return new PEAR_Error('there are no elements!');
  159.     }
  160.  
  161.     // }}}
  162.     // {{{ getNext()
  163.  
  164.     /**
  165.      * Get next result set. If getFirst() has never been called before,
  166.      * it calls that method.
  167.      *
  168.      * @return mixed 
  169.      * @access public
  170.      */
  171.     function getNext()
  172.     {
  173.         if (!$this->initDone()) {
  174.             return $this->getFirst();
  175.         }
  176.         if ($this->hasMore()) {
  177.             $this->_counter++;
  178.             return $this->_data[$this->_dataKeys[$this->_counter]];
  179.         }
  180.         return new PEAR_Error('there are no more elements!');
  181.     }
  182.  
  183.     // }}}
  184.     // {{{ hasMore()
  185.  
  186.     /**
  187.      * check if there are other rows
  188.      *
  189.      * @return boolean 
  190.      * @access public
  191.      */
  192.     function hasMore()
  193.     {
  194.         if ($this->_counter+1 < $this->getCount()) {
  195.             return true;
  196.         }
  197.         return false;
  198.     }
  199.  
  200.     // }}}
  201.     // {{{ fetchRow
  202.  
  203.     /**
  204.      * This function emulates PEAR_MDB fetchRow() method.
  205.      * With this method, MDB_QueryTool can transparently replace PEAR::MDB
  206.      *
  207.      * @todo implement fetchmode support?
  208.      * @access    public
  209.      * @return    void 
  210.      */
  211.     function fetchRow()
  212.     {
  213.         if ($this->hasMore()) {
  214.             $arr $this->getNext();
  215.             if (!PEAR::isError($arr)) {
  216.                 return $arr;
  217.             }
  218.         }
  219.         return false;
  220.     }
  221.  
  222.     // }}}
  223.     // {{{ initDone
  224.  
  225.     /**
  226.      * Helper method. Check if $this->_dataKeys has been initialized
  227.      *
  228.      * @return boolean 
  229.      * @access private
  230.      */
  231.     function initDone()
  232.     {
  233.         return (
  234.             isset($this->_dataKeys&&
  235.             is_array($this->_dataKeys&&
  236.             count($this->_dataKeys)
  237.         );
  238.     }
  239.  
  240.     // }}}
  241.  
  242.     #TODO
  243.     #function getPrevious()
  244.     #function getLast()
  245.  
  246. }
  247. ?>

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