home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / DB / QueryTool / Result.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  7.3 KB  |  276 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * Contains the DB_QueryTool_Result class
  6.  *
  7.  * PHP versions 4 and 5
  8.  *
  9.  * LICENSE: Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. The name of the author may not be used to endorse or promote products
  17.  *    derived from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
  20.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22.  * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
  23.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  *
  30.  * @category  Database
  31.  * @package   DB_QueryTool
  32.  * @author    Wolfram Kriesing <wk@visionp.de>
  33.  * @author    Paolo Panto <wk@visionp.de>
  34.  * @author    Lorenzo Alberton <l dot alberton at quipo dot it>
  35.  * @copyright 2003-2007 Wolfram Kriesing, Paolo Panto, Lorenzo Alberton
  36.  * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  37.  * @version   CVS: $Id: Result.php,v 1.14 2007/11/27 07:45:01 quipo Exp $
  38.  * @link      http://pear.php.net/package/DB_QueryTool
  39.  */
  40.  
  41. /**
  42.  * DB_QueryTool_Result class
  43.  *
  44.  * This result actually contains the 'data' itself, the number of rows
  45.  * returned and some additional info
  46.  * using ZE2 you can also get retrieve data from the result doing the following:
  47.  * <DB_QueryTool_Common-instance>->getAll()->getCount()
  48.  * or
  49.  * <DB_QueryTool_Common-instance>->getAll()->getData()
  50.  *
  51.  * @category  Database
  52.  * @package   DB_QueryTool
  53.  * @author    Wolfram Kriesing <wk@visionp.de>
  54.  * @author    Paolo Panto <wk@visionp.de>
  55.  * @author    Lorenzo Alberton <l dot alberton at quipo dot it>
  56.  * @copyright 2003-2007 Wolfram Kriesing, Paolo Panto, Lorenzo Alberton
  57.  * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  58.  * @link      http://pear.php.net/package/DB_QueryTool
  59.  */
  60. class DB_QueryTool_Result
  61. {
  62.     // {{{ class vars
  63.  
  64.     /**
  65.      * @var array
  66.      */
  67.     var $_data = array();
  68.  
  69.     /**
  70.      * @var array
  71.      */
  72.     var $_dataKeys = array();
  73.  
  74.     /**
  75.      * @var integer
  76.      */
  77.     var $_count = 0;
  78.  
  79.     /**
  80.      * the counter for the methods getFirst, getNext
  81.      * @var array
  82.      */
  83.     var $_counter = null;
  84.  
  85.     // }}}
  86.     // {{{ DB_QueryTool_Result()
  87.  
  88.     /**
  89.      * create a new instance of result with the data returned by the query
  90.      *
  91.      * @param array $data the data returned by the result
  92.      *
  93.      * @version 2002/07/11
  94.      * @author Wolfram Kriesing <wolfram@kriesing.de>
  95.      * @access public
  96.      */
  97.     function DB_QueryTool_Result($data)
  98.     {
  99.         if (!count($data)) {
  100.             $this->_count = 0;
  101.         } else {
  102.             list($firstElement) = $data;
  103.             if (is_array($firstElement)) { // is the array a collection of rows?
  104.                 $this->_count = sizeof($data);
  105.             } else {
  106.                 if (sizeof($data) > 0) {
  107.                     $this->_count = 1;
  108.                 } else {
  109.                     $this->_count = 0;
  110.                 }
  111.             }
  112.         }
  113.         $this->_data = $data;
  114.     }
  115.  
  116.     // }}}
  117.     // {{{ numRows
  118.  
  119.     /**
  120.      * return the number of rows returned. This is an alias for getCount().
  121.      *
  122.      * @return integer
  123.      * @access public
  124.      */
  125.     function numRows()
  126.     {
  127.         return $this->_count;
  128.     }
  129.  
  130.     // }}}
  131.     // {{{ getCount()
  132.  
  133.     /**
  134.      * return the number of rows returned
  135.      *
  136.      * @return integer the number of rows returned
  137.      * @version 2002/07/11
  138.      * @author Wolfram Kriesing <wolfram@kriesing.de>
  139.      * @access public
  140.      */
  141.     function getCount()
  142.     {
  143.         return $this->_count;
  144.     }
  145.  
  146.     // }}}
  147.     // {{{ getData()
  148.  
  149.     /**
  150.      * get all the data returned
  151.      *
  152.      * @param string $key key of the data to retrieve
  153.      *
  154.      * @return array|PEAR_Error
  155.      * @version 2002/07/11
  156.      * @author Wolfram Kriesing <wolfram@kriesing.de>
  157.      * @access public
  158.      */
  159.     function getData($key = null)
  160.     {
  161.         if (is_null($key)) {
  162.             return $this->_data;
  163.         }
  164.         if ($this->_data[$key]) {
  165.             return $this->_data[$key];
  166.         }
  167.         return new PEAR_Error("there is no element with the key '$key'!");
  168.     }
  169.  
  170.     // }}}
  171.     // {{{ getFirst()
  172.  
  173.     /**
  174.      * get the first result set
  175.      * we are not using next, current, and reset, since those ignore keys
  176.      * which are empty or 0
  177.      *
  178.      * @return mixed
  179.      * @version 2002/07/11
  180.      * @author Wolfram Kriesing <wolfram@kriesing.de>
  181.      * @access public
  182.      */
  183.     function getFirst()
  184.     {
  185.         if ($this->getCount() > 0) {
  186.             $this->_dataKeys = array_keys($this->_data);
  187.             $this->_counter  = 0;
  188.             return $this->_data[$this->_dataKeys[$this->_counter]];
  189.         }
  190.         return new PEAR_Error('There are no elements!');
  191.     }
  192.  
  193.     // }}}
  194.     // {{{ getNext()
  195.  
  196.     /**
  197.      * Get next result set. If getFirst() has never been called before,
  198.      * it calls that method.
  199.      *
  200.      * @return mixed
  201.      * @access public
  202.      */
  203.     function getNext()
  204.     {
  205.         if (!$this->initDone()) {
  206.             return $this->getFirst();
  207.         }
  208.         if ($this->hasMore()) {
  209.             $this->_counter++;
  210.             return $this->_data[$this->_dataKeys[$this->_counter]];
  211.         }
  212.         return new PEAR_Error('there are no more elements!');
  213.     }
  214.  
  215.     // }}}
  216.     // {{{ hasMore()
  217.  
  218.     /**
  219.      * check if there are other rows
  220.      *
  221.      * @return boolean
  222.      * @access public
  223.      */
  224.     function hasMore()
  225.     {
  226.         if ($this->_counter+1 < $this->getCount()) {
  227.             return true;
  228.         }
  229.         return false;
  230.     }
  231.  
  232.     // }}}
  233.     // {{{ fetchRow
  234.  
  235.     /**
  236.      * This function emulates PEAR::DB fetchRow() method.
  237.      * With this method, DB_QueryTool can transparently replace PEAR_DB
  238.      *
  239.      * @return void
  240.      * @todo implement fetchmode support?
  241.      * @access public
  242.      */
  243.     function fetchRow()
  244.     {
  245.         $arr = $this->getNext();
  246.         if (!PEAR::isError($arr)) {
  247.             return $arr;
  248.         }
  249.         return false;
  250.     }
  251.  
  252.     // }}}
  253.     // {{{ initDone
  254.  
  255.     /**
  256.      * Helper method. Check if $this->_dataKeys has been initialized
  257.      *
  258.      * @return boolean
  259.      * @access private
  260.      */
  261.     function initDone()
  262.     {
  263.         return (
  264.             isset($this->_dataKeys) &&
  265.             is_array($this->_dataKeys) &&
  266.             count($this->_dataKeys)
  267.         );
  268.     }
  269.  
  270.     // }}}
  271.  
  272.     //@TODO
  273.     //function getPrevious()
  274.     //function getLast()
  275. }
  276. ?>