DB_QueryTool
[ class tree: DB_QueryTool ] [ index: DB_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:  Wolfram Kriesing, Paolo Panto, vision:produktion <wk@visionp.de>
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: Result.php,v 1.7 2004/04/27 23:36:38 quipo Exp $
  19. //
  20.  
  21. /**
  22.  * this result actually contains the 'data' itself, the number of rows
  23.  * returned and some additional info
  24.  * using ZE2 you can also get retreive data from the result doing the following:
  25.  * <vp_DB_Common-instance>->getAll()->getCount()
  26.  * or
  27.  * <vp_DB_Common-instance>->getAll()->getData()
  28.  *
  29.  *
  30.  * @package    DB_QueryTool
  31.  * @version    2002/07/11
  32.  * @access     public
  33.  * @author     Wolfram Kriesing <wolfram@kriesing.de>
  34.  */
  35. {
  36.     // {{{ class vars
  37.  
  38.     /**
  39.      * @var array 
  40.      */
  41.     var $_data = array();
  42.  
  43.     /**
  44.      * @var array 
  45.      */
  46.     var $_dataKeys = array();
  47.  
  48.     /**
  49.      * @var integer 
  50.      */
  51.     var $_count = 0;
  52.  
  53.     /**
  54.      * the counter for the methods getFirst, getNext
  55.      * @var array 
  56.      */
  57.     var $_counter = null;
  58.  
  59.     // }}}
  60.     // {{{ DB_QueryTool_Result()
  61.  
  62.     /**
  63.      * create a new instance of result with the data returned by the query
  64.      *
  65.      * @version    2002/07/11
  66.      * @access     public
  67.      * @author     Wolfram Kriesing <wolfram@kriesing.de>
  68.      * @param      array   the data returned by the result
  69.      */
  70.     function DB_QueryTool_Result($data)
  71.     {
  72.         if (!count($data)) {
  73.             $this->_count = 0;
  74.         else {
  75.             list($firstElement$data;
  76.             if (is_array($firstElement)) // is the array a collection of rows?
  77.                 $this->_count sizeof($data);
  78.             else {
  79.                 if (sizeof($data> 0{
  80.                     $this->_count = 1;
  81.                 else {
  82.                     $this->_count = 0;
  83.                 }
  84.             }
  85.         }
  86.         $this->_data $data;
  87.     }
  88.  
  89.     // }}}
  90.     // {{{ numRows
  91.  
  92.     /**
  93.      * return the number of rows returned. This is an alias for getCount().
  94.      *
  95.      * @access    public
  96.      * @return    integer 
  97.      */
  98.     function numRows()
  99.     {
  100.         return $this->_count;
  101.     }
  102.  
  103.     // }}}
  104.     // {{{ getCount()
  105.  
  106.     /**
  107.      * return the number of rows returned
  108.      *
  109.      * @version    2002/07/11
  110.      * @access     public
  111.      * @author     Wolfram Kriesing <wolfram@kriesing.de>
  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.      * @version    2002/07/11
  126.      * @access     public
  127.      * @author     Wolfram Kriesing <wolfram@kriesing.de>
  128.      * @param      string $key 
  129.      * @return mixed array or PEAR_Error
  130.      */
  131.     function getData($key=null)
  132.     {
  133.         if (is_null($key)) {
  134.             return $this->_data;
  135.         }
  136.         if ($this->_data[$key]{
  137.             return $this->_data[$key];
  138.         }
  139.         return new PEAR_Error("there is no element with the key '$key'!");
  140.     }
  141.  
  142.     // }}}
  143.     // {{{ getFirst()
  144.  
  145.     /**
  146.      * get the first result set
  147.      * we are not using next, current, and reset, since those ignore keys
  148.      * which are empty or 0
  149.      *
  150.      * @version    2002/07/11
  151.      * @access     public
  152.      * @author     Wolfram Kriesing <wolfram@kriesing.de>
  153.      * @return mixed 
  154.      */
  155.     function getFirst()
  156.     {
  157.         if ($this->getCount(> 0{
  158.             $this->_dataKeys array_keys($this->_data);
  159.             $this->_counter = 0;
  160.             return $this->_data[$this->_dataKeys[$this->_counter]];
  161.         }
  162.         return new PEAR_Error('There are no elements!');
  163.     }
  164.  
  165.     // }}}
  166.     // {{{ getNext()
  167.  
  168.     /**
  169.      * Get next result set. If getFirst() has never been called before,
  170.      * it calls that method.
  171.      * @return mixed 
  172.      * @access public
  173.      */
  174.     function getNext()
  175.     {
  176.         if (!$this->initDone()) {
  177.             return $this->getFirst();
  178.         }
  179.         if ($this->hasMore()) {
  180.             $this->_counter++;
  181.             return $this->_data[$this->_dataKeys[$this->_counter]];
  182.         }
  183.         return new PEAR_Error('there are no more elements!');
  184.     }
  185.  
  186.     // }}}
  187.     // {{{ hasMore()
  188.  
  189.     /**
  190.      * check if there are other rows
  191.      *
  192.      * @return boolean 
  193.      * @access public
  194.      */
  195.     function hasMore()
  196.     {
  197.         if ($this->_counter+1 < $this->getCount()) {
  198.             return true;
  199.         }
  200.         return false;
  201.     }
  202.  
  203.     // }}}
  204.     // {{{ fetchRow
  205.  
  206.     /**
  207.      * This function emulates PEAR::DB fetchRow() method.
  208.      * With this method, DB_QueryTool can transparently replace PEAR_DB
  209.      *
  210.      * @todo implement fetchmode support?
  211.      * @access    public
  212.      * @return    void 
  213.      */
  214.     function fetchRow()
  215.     {
  216.         if ($this->hasMore()) {
  217.             $arr $this->getNext();
  218.             if (!PEAR::isError($arr)) {
  219.                 return $arr;
  220.             }
  221.         }
  222.         return false;
  223.     }
  224.  
  225.     // }}}
  226.     // {{{ initDone
  227.  
  228.     /**
  229.      * Helper method. Check if $this->_dataKeys has been initialized
  230.      *
  231.      * @return boolean 
  232.      * @access private
  233.      */
  234.     function initDone()
  235.     {
  236.         return (
  237.             isset($this->_dataKeys&&
  238.             is_array($this->_dataKeys&&
  239.             count($this->_dataKeys)
  240.         );
  241.     }
  242.  
  243.     // }}}
  244.  
  245.     #TODO
  246.     #function getPrevious()
  247.     #function getLast()
  248.  
  249. }
  250. ?>

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