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

Source for file querysim.php

Documentation is available at querysim.php

  1. <?php
  2. // vim: set et ts=4 sw=4 fdm=marker:
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1998-2004 Manuel Lemos, Tomas V.V.Cox,                 |
  7. // | Stig. S. Bakken, Lukas Smith                                         |
  8. // | All rights reserved.                                                 |
  9. // +----------------------------------------------------------------------+
  10. // | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB  |
  11. // | API as well as database abstraction for PHP applications.            |
  12. // | This LICENSE is in the BSD license style.                            |
  13. // |                                                                      |
  14. // | Redistribution and use in source and binary forms, with or without   |
  15. // | modification, are permitted provided that the following conditions   |
  16. // | are met:                                                             |
  17. // |                                                                      |
  18. // | Redistributions of source code must retain the above copyright       |
  19. // | notice, this list of conditions and the following disclaimer.        |
  20. // |                                                                      |
  21. // | Redistributions in binary form must reproduce the above copyright    |
  22. // | notice, this list of conditions and the following disclaimer in the  |
  23. // | documentation and/or other materials provided with the distribution. |
  24. // |                                                                      |
  25. // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken,    |
  26. // | Lukas Smith nor the names of his contributors may be used to endorse |
  27. // | or promote products derived from this software without specific prior|
  28. // | written permission.                                                  |
  29. // |                                                                      |
  30. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  |
  31. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT    |
  32. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    |
  33. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE      |
  34. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,          |
  35. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  36. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  37. // |  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED  |
  38. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT          |
  39. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  40. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE          |
  41. // | POSSIBILITY OF SUCH DAMAGE.                                          |
  42. // +----------------------------------------------------------------------+
  43. // | Original QuerySim Concept & ColdFusion Author: Hal Helms             |
  44. // | <hal.helms@teamallaire.com>                                          |
  45. // | Bert Dawson <bdawson@redbanner.com>                                  |
  46. // +----------------------------------------------------------------------+
  47. // | Original PHP Author: Alan Richmond <arichmond@bigfoot.com>           |
  48. // | David Huyck <b@bombusbee.com>                                        |
  49. // +----------------------------------------------------------------------+
  50. // | Special note concerning code documentation:                          |
  51. // | QuerySim was originally created for use during development of        |
  52. // | applications built using the Fusebox framework. (www.fusebox.org)    |
  53. // | Fusebox uses an XML style of documentation called Fusedoc. (Which    |
  54. // | is admittedly not well suited to documenting classes and functions.  |
  55. // | This short-coming is being addressed by the Fusebox community.) PEAR |
  56. // | uses a Javadoc style of documentation called PHPDoc. (www.phpdoc.de) |
  57. // | Since this class extension spans two groups of users, it is asked    |
  58. // | that the members of each respect the documentation standard of the   |
  59. // | other.  So it is a further requirement that both documentation       |
  60. // | standards be included and maintained. If assistance is required      |
  61. // | please contact Alan Richmond.                                        |
  62. // +----------------------------------------------------------------------+
  63. //
  64. // $Id: querysim.php,v 1.9 2004/04/23 17:06:46 lsmith Exp $
  65. //
  66.  
  67. /*
  68. <fusedoc fuse="querysim.php" language="PHP">
  69.     <responsibilities>
  70.         I take information and turn it into a recordset that can be accessed
  71.         through the PEAR MDB2 API.  Based on Hal Helms' QuerySim.cfm ColdFusion
  72.         custom tag available at halhelms.com.
  73.     </responsibilities>
  74.     <properties>
  75.         <property name="API" value="PEAR MDB2" />
  76.         <property name="version" value="0.2.1" />
  77.         <property name="status" value="beta" />
  78.         <history author="Hal Helms" email="hal.helms@teamallaire.com" type="Create" />
  79.         <history author="Bert Dawson" email="bdawson@redbanner.com" type="Update">
  80.             Extensive revision that is backwardly compatible but eliminates the
  81.             need for a separate .sim file.
  82.         </history>
  83.         <history author="Alan Richmond" email="arichmond@bigfoot.com" type="Create" date="10-July-2002">
  84.             Rewrote in PHP as an extention to the PEAR DB API.
  85.             Functions supported:
  86.                 connect, disconnect, query, fetchRow, freeResult,
  87.                 numCols, numRows, getSpecialQuery
  88.             David Huyck (bombusbee.com) added ability to escape special
  89.                 characters (i.e., delimiters) using a '\'.
  90.             Extended PEAR DB options[] for adding incoming parameters.  Added
  91.                 options:  columnDelim, dataDelim, eolDelim
  92.         </history>
  93.         <history author="David Huyck" email="b@bombusbee.com" type="Update" date="19-July-2002">
  94.             Added the ability to set the QuerySim options at runtime.
  95.             Default options are:
  96.                 'columnDelim' => ',',            // Commas split the column names
  97.                 'dataDelim'   => '|',            // Pipes split the data fields
  98.                 'eolDelim'    => chr(13).chr(10) // Carriage returns split the
  99.                                                  // lines of data
  100.             Affected functions are:
  101.                 DB_querysim():          set the default options when the
  102.                                         constructor method is called
  103.                 _parseQuerySim($query): altered the parsing of lines, column
  104.                                         names, and data fields
  105.                 _empty2null:            altered the way this function is called
  106.                                         to simplify calling it
  107.         </history>
  108.         <history author="Alan Richmond" email="arichmond@bigfoot.com" type="Update" date="24-July-2002">
  109.             Added error catching for malformed QuerySim text.
  110.             Bug fix _empty2null():  altered version was returning unmodified
  111.                                     lineData.
  112.             Cleanup:
  113.                 PEAR compliant formatting, finished PHPDocs and added 'out' to
  114.                 Fusedoc 'io'.
  115.                 Broke up _parseQuerySim() into _buildResult() and _parseOnDelim()
  116.                 to containerize duplicate parse code.
  117.         </history>
  118.         <history author="David Huyck" email="b@bombusbee.com" type="Update" date="25-July-2002">
  119.             Edited the _buildResult() and _parseOnDelim() functions to improve
  120.             reliability of special character escaping.
  121.             Re-introduced a custom setOption() method to throw an error when a
  122.             person tries to set one of the delimiters to '\'.
  123.         </history>
  124.         <history author="Alan Richmond" email="arichmond@bigfoot.com" type="Update" date="27-July-2002">
  125.             Added '/' delimiter param to preg_quote() in _empty2null() and
  126.             _parseOnDelim() so '/' can be used as a delimiter.
  127.             Added error check for columnDelim == eolDelim or dataDelim == eolDelim.
  128.             Renamed some variables for consistancy.
  129.         </history>
  130.         <history author="Alan Richmond" email="arichmond@bigfoot.com" type="Update" date="30-July-2002">
  131.             Removed private function _empty2null().  Turns out preg_split()
  132.             deals with empty elemants by making them zero length strings, just
  133.             what they ended up being anyway.  This should speed things up a little.
  134.             Affected functions:
  135.                 _parseOnDelim()     perform trim on line here, instead of in
  136.                                     _empty2null().
  137.                 _buildResult()      remove call to _empty2null().
  138.                 _empty2null()       removed function.
  139.         </history>
  140.         <history author="Alan Richmond" email="arichmond@bigfoot.com" type="Update" date="1-Jan-2003">
  141.             Ported to PEAR MDB2.
  142.             Methods supported:
  143.                 connect, query, getColumnNames, numCols, valid, fetch,
  144.                 numRows, free, fetchRow, nextResult, setLimit
  145.                 (inherited).
  146.         </history>
  147.         <history
  148.             Removed array_change_key_case() work around for <4.2.0 in
  149.             getColumnNames(), found it already done in MDB2/Common.php.
  150.         </history>
  151.         <history author="Alan Richmond" email="arichmond@bigfoot.com" type="Update" date="3-Feb-2003">
  152.             Changed default eolDelim to a *nix file eol, since we're trimming
  153.             the result anyway, it makes no difference for Windows.  Now only
  154.             Mac file eols should need to be set (and other kinds of chars).
  155.         </history>
  156.         <note author="Alan Richmond">
  157.             Got WAY too long.  See querysim_readme.txt for instructions and some
  158.             examples.
  159.             io section only documents elements of DB_result that DB_querysim uses,
  160.             adds or changes; see MDB2 and MDB2_Driver_Common for more info.
  161.             io section uses some elements that are not Fusedoc 2.0 compliant:
  162.             object and resource.
  163.         </note>
  164.     </properties>
  165.     <io>
  166.         <in>
  167.             <file path="MDB2/Common.php" action="require_once" />
  168.         </in>
  169.         <out>
  170.             <object name="MDB2_querysim" extends="MDB2_Driver_Common" instantiatedby="MDB2::connect()">
  171.                 <resource type="file" name="connection" oncondition="source is external file" scope="class" />
  172.                 <string name="phptype" default="querysim" />
  173.                 <string name="dbsyntax" default="querysim" />
  174.                 <array name="supported" comments="most of these don't actually do anything, they are enabled to simulate the option being available if checked">
  175.                     <boolean name="sequences" default="true" />
  176.                     <boolean name="indexes" default="true" />
  177.                     <boolean name="affected_rows" default="true" />
  178.                     <boolean name="summary_functions" default="true" />
  179.                     <boolean name="order_by_text" default="true" />
  180.                     <boolean name="current_id" default="true" />
  181.                     <boolean name="limit_querys" default="true" comments="this one is functional" />
  182.                     <boolean name="LOBs" default="true" />
  183.                     <boolean name="replace" default="true" />
  184.                     <boolean name="sub_selects" default="true" />
  185.                     <boolean name="transactions" default="true" />
  186.                 </array>
  187.                 <string name="last_query" comments="last value passed in with query()" />
  188.                 <array name="options" comments="these can be changed at run time">
  189.                     <string name="columnDelim" default="," />
  190.                     <string name="dataDelim" default="|" />
  191.                     <string name="eolDelim" default="chr(13).chr(10)" />
  192.                 </array>
  193.             </object>
  194.             <array name="result" comments="the simulated record set returned by ::query()">
  195.                 <array comments="columns">
  196.                     <string comments="column name" />
  197.                 </array>
  198.                 <array comments="data">
  199.                     <array comments="row">
  200.                         <string comments="data element" />
  201.                     </array>
  202.                 </array>
  203.             </array>
  204.         </out>
  205.     </io>
  206. </fusedoc>
  207. */
  208.  
  209. /**
  210.  * MDB2 QuerySim driver
  211.  *
  212.  * @package MDB2
  213.  * @category Database
  214.  * @author  Alan Richmond <arichmond@bigfoot.com>
  215.  */
  216. {
  217.     // {{{ properties
  218.     var $escape_quotes = "\\";
  219.     // }}}
  220.  
  221.     // {{{ constructor
  222.  
  223.     /**
  224.     * Constructor
  225.     */
  226.     function MDB2_Driver_querysim()
  227.     {
  228.         $this->MDB2_Driver_Common();
  229.         $this->phptype  'querysim';
  230.         $this->dbsyntax 'querysim';
  231.  
  232.         // Most of these are dummies to simulate availability if checked
  233.         $this->supported['sequences'= true;
  234.         $this->supported['indexes'= true;
  235.         $this->supported['affected_rows'= true;
  236.         $this->supported['summary_functions'= true;
  237.         $this->supported['order_by_text'= true;
  238.         $this->supported['current_id'= true;
  239.         $this->supported['limit_queries'= true;// this one is real
  240.         $this->supported['LOBs'= true;
  241.         $this->supported['replace'= true;
  242.         $this->supported['sub_selects'= true;
  243.         $this->supported['transactions'= true;
  244.  
  245.         $this->options['columnDelim'',';
  246.         $this->options['dataDelim''|';
  247.         $this->options['eolDelim'"\n";
  248.     }
  249.     // }}}
  250.  
  251.     // {{{ connect()
  252.  
  253.     /**
  254.      * Open a file or simulate a successful database connect
  255.      *
  256.      * @access public
  257.      *
  258.      * @return mixed MDB2_OK string on success, a MDB2 error object on failure
  259.      */
  260.     function connect()
  261.     {
  262.         if ($this->connection != 0{
  263.             if (!strcmp($this->connected_database_name$this->database_name)
  264.                 && ($this->opened_persistent == $this->options['persistent']))
  265.             {
  266.                 return MDB2_OK;
  267.             }
  268.             if ($this->connected_database_name{
  269.                 $this->_close($this->connection);
  270.             }
  271.             $this->connection = 0;
  272.         }
  273.  
  274.         $connection = 1;// sim connect
  275.         // if external, check file...
  276.         if ($this->database_name{
  277.             $file $this->database_name;
  278.             if (!file_exists($file)) {
  279.                 return $this->raiseError(MDB2_ERROR_NOT_FOUNDnullnull'file not found');
  280.             }
  281.             if (!is_file($file)) {
  282.                 return $this->raiseError(MDB2_ERROR_INVALIDnullnull'not a file');
  283.             }
  284.             if (!is_readable($file)) {
  285.                 return $this->raiseError(MDB2_ERROR_ACCESS_VIOLATIONnullnull,
  286.                     'could not open file - check permissions');
  287.             }
  288.             // ...and open if persistent
  289.             if ($this->options['persistent']{
  290.                 $connection @fopen($file'r');
  291.             }
  292.         }
  293.         $this->connection $connection;
  294.         $this->connected_database_name $this->database_name;
  295.         $this->opened_persistent $this->options['persistent'];
  296.         return MDB2_OK;
  297.     }
  298.     // }}}
  299.  
  300.     // {{{ _close()
  301.  
  302.     /**
  303.      * Close a file or simulate a successful database disconnect
  304.      *
  305.      * @access public
  306.      *
  307.      * @return bool true on success, false if file closed.
  308.      *               Always true if simulated.
  309.      */
  310.     function _close()
  311.     {
  312.         if ($this->connection != 0{
  313.             if (($this->opened_persistent&& (is_resource($this->connection))) {
  314.                 $ret @fclose($this->connection);
  315.             }
  316.             $this->connection = 0;
  317.             unset($GLOBALS['_MDB2_databases'][$this->db_index]);
  318.  
  319.             if (!isset($ret|| !$ret{
  320.                 $this->raiseError();
  321.             }
  322.         }
  323.         return MDB2_OK;
  324.     }
  325.     // }}}
  326.  
  327.     // {{{ query()
  328.  
  329.     /**
  330.      * Get QuerySim text from appropriate source and return
  331.      * the parsed text.
  332.      *
  333.      * @param string The QuerySim text
  334.      * @param mixed   $types  array that contains the types of the columns in
  335.      *                         the result set
  336.      *
  337.      * @param mixed $result_class string which specifies which result class to use
  338.      * @param mixed $result_wrap_class string which specifies which class to wrap results in
  339.      *
  340.      * @return mixed Simulated result set as a multidimentional
  341.      *  array if valid QuerySim text was passed in.  A MDB2 error
  342.      *  is returned on failure.
  343.      *
  344.      * @access public
  345.      */
  346.     function &query($query$types = null$result_class = false$result_wrap_class = false)
  347.     {
  348.         $ismanip = false;
  349.         $offset $this->row_offset;
  350.         $limit $this->row_limit;
  351.         $this->row_offset $this->row_limit = 0;
  352.         if ($this->database_name{
  353.             // this looks wrong
  354.             $query $this->_readFile();
  355.         }
  356.         $this->last_query = $query;
  357.         $this->debug($query'query');
  358.  
  359.         $connected $this->connect();
  360.         if (MDB2::isError($connected)) {
  361.             return $connected;
  362.         }
  363.  
  364.         if ($result $this->_buildResult($query)) {
  365.             if ($limit > 0{
  366.                 $result[1array_slice($result[1]$offset-1$limit);
  367.             }
  368.             if (!$result_class{
  369.                 $result_class $this->options['result_buffering']
  370.                     ? $this->options['buffered_result_class'$this->options['result_class'];
  371.             }
  372.             $class_name sprintf($result_class$this->phptype);
  373.             $result =new $class_name($this$result);
  374.             if ($types{
  375.                 $err $result->setResultTypes($types);
  376.                 if (MDB2::isError($err)) {
  377.                     $result->free();
  378.                     return $err;
  379.                 }
  380.             }
  381.             if (!$result_wrap_class{
  382.                 $result_wrap_class $this->options['result_wrap_class'];
  383.             }
  384.             if ($result_wrap_class{
  385.                 $result =new $result_wrap_class($result);
  386.             }
  387.             return $result;
  388.         }
  389.         $error =$this->raiseError();
  390.         return $error;
  391.     }
  392.  
  393.     // }}}
  394.  
  395.     // {{{ _readFile()
  396.  
  397.     /**
  398.      * Read an external file
  399.      *
  400.      * @param string filepath/filename
  401.      *
  402.      * @access private
  403.      *
  404.      * @return string the contents of a file
  405.      */
  406.     function _readFile()
  407.     {
  408.         $buffer '';
  409.         if ($this->opened_persistent{
  410.             while (!feof($this->connection)) {
  411.                 $buffer .= fgets($this->connection1024);
  412.             }
  413.         else {
  414.             $this->connection @fopen($this->connected_database_name'r');
  415.             while (!feof($this->connection)) {
  416.                 $buffer .= fgets($this->connection1024);
  417.             }
  418.             $this->connection @fclose($this->connection);
  419.         }
  420.         return $buffer;
  421.     }
  422.     // }}}
  423.  
  424.     // {{{ _buildResult()
  425.  
  426.     /**
  427.      * Convert QuerySim text into an array
  428.      *
  429.      * @param string Text of simulated query
  430.      *
  431.      * @access private
  432.      *
  433.      * @return multi-dimensional array containing the column names and data
  434.      *                                  from the QuerySim
  435.      */
  436.     function _buildResult($query)
  437.     {
  438.         $eolDelim    $this->options['eolDelim'];
  439.         $columnDelim $this->options['columnDelim'];
  440.         $dataDelim   $this->options['dataDelim'];
  441.  
  442.         $columnNames = array();
  443.         $data        = array();
  444.  
  445.         if ($columnDelim == $eolDelim{
  446.             return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  447.                 'columnDelim and eolDelim must be different');
  448.         elseif ($dataDelim == $eolDelim){
  449.             return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  450.                 'dataDelim and eolDelim must be different');
  451.         }
  452.  
  453.         $query trim($query);
  454.         //tokenize escaped slashes
  455.         $query str_replace('\\\\''[$double-slash$]'$query);
  456.  
  457.         if (!strlen($query)) {
  458.             return $this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  459.                 'empty querysim text');
  460.         }
  461.         $lineData $this->_parseOnDelim($query$eolDelim);
  462.         //kill the empty last row created by final eol char if it exists
  463.         if (!strlen(trim($lineData[count($lineData- 1]))) {
  464.             unset($lineData[count($lineData- 1]);
  465.         }
  466.         //populate columnNames array
  467.         $thisLine each($lineData);
  468.         $columnNames $this->_parseOnDelim($thisLine[1]$columnDelim);
  469.         if ((in_array(''$columnNames)) || (in_array('NULL'$columnNames))) {
  470.             return $this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  471.                 'all column names must be defined');
  472.         }
  473.         //replace double-slash tokens with single-slash
  474.         $columnNames str_replace('[$double-slash$]''\\'$columnNames);
  475.         $columnCount count($columnNames);
  476.         $rowNum = 0;
  477.         //loop through data lines
  478.         if (count($lineData> 1{
  479.             while ($thisLine each($lineData)) {
  480.                 $thisData $this->_parseOnDelim($thisLine[1]$dataDelim);
  481.                 $thisDataCount count($thisData);
  482.                 if ($thisDataCount != $columnCount{
  483.                     $fileLineNo $rowNum + 2;
  484.                     return $this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  485.                         "number of data elements ($thisDataCount) in line $fileLineNo not equal to number of defined columns ($columnCount)");
  486.                 }
  487.                 //loop through data elements in data line
  488.                 foreach ($thisData as $thisElement{
  489.                     if (strtoupper($thisElement== 'NULL'){
  490.                         $thisElement '';
  491.                     }
  492.                     //replace double-slash tokens with single-slash
  493.                     $data[$rowNum][str_replace('[$double-slash$]''\\'$thisElement);
  494.                 }//end foreach
  495.                 ++$rowNum;
  496.             }//end while
  497.         }//end if
  498.         return array($columnNames$data);
  499.     }//end function _buildResult()
  500.     // }}}
  501.  
  502.     // {{{ _parseOnDelim()
  503.  
  504.     /**
  505.      * Split QuerySim string into an array on a delimiter
  506.      *
  507.      * @param string $thisLine Text of simulated query
  508.      * @param string $delim    The delimiter to split on
  509.      *
  510.      * @access private
  511.      *
  512.      * @return array containing parsed string
  513.      */
  514.     function _parseOnDelim($thisLine$delim)
  515.     {
  516.         $delimQuoted preg_quote($delim'/');
  517.         $thisLine trim($thisLine);
  518.  
  519.         $parsed preg_split('/(?<!\\\\)' .$delimQuoted'/'$thisLine);
  520.         //replaces escaped delimiters
  521.         $parsed preg_replace('/\\\\' .$delimQuoted'/'$delim$parsed);
  522.         if ($delim != $this->options['eolDelim']{
  523.             //replaces escape chars
  524.             $parsed preg_replace('/\\\\/'''$parsed);
  525.         }
  526.         return $parsed;
  527.     }
  528.     // }}}
  529. }
  530.  
  531. class MDB2_Result_querysim extends MDB2_Result_Common
  532. {
  533.     // }}}
  534.     // {{{ constructor
  535.  
  536.     /**
  537.      * Constructor
  538.      */
  539.     function MDB2_Result_querysim(&$mdb&$result)
  540.     {
  541.         parent::MDB2_Result_Common($mdb$result);
  542.     }
  543.  
  544.     // }}}
  545.     // {{{ fetch()
  546.  
  547.     /**
  548.     * fetch value from a result set
  549.     *
  550.     * @param int    $rownum    number of the row where the data can be found
  551.     * @param int    $colnum    field number where the data can be found
  552.     * @return mixed string on success, a MDB2 error on failure
  553.     * @access public
  554.     */
  555.     function fetch($rownum = 0$colnum = 0)
  556.     {
  557.         if (!isset($this->result[1][$rownum][$colnum])) {
  558.             if (is_null($this->result)) {
  559.                 return $this->mdb->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  560.                     'fetch: resultset has already been freed');
  561.             }
  562.             return null;
  563.         }
  564.         $value $this->result[1][$rownum][$colnum];
  565.         if (isset($this->types[$colnum])) {
  566.             $value $this->mdb->datatype->convertResult($value$this->types[$colnum]);
  567.         }
  568.         if ($this->mdb->options['portability'MDB2_PORTABILITY_RTRIM{
  569.             $value rtrim($value);
  570.         }
  571.         if ($value === ''
  572.             && $this->mdb->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL
  573.         {
  574.             $value = null;
  575.         }
  576.         return $value;
  577.     }
  578.  
  579.     // }}}
  580.     // {{{ fetchRow()
  581.  
  582.     /**
  583.      * Fetch a row and insert the data into an existing array.
  584.      *
  585.      * @param int       $fetchmode  how the array data should be indexed
  586.      * @return int data array on success, a MDB2 error on failure
  587.      * @access public
  588.      */
  589.     function fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT)
  590.     {
  591.         if (is_null($this->result)) {
  592.             return $this->mdb->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  593.                 'fetchRow: resultset has already been freed');
  594.         }
  595.         $target_rownum $this->rownum + 1;
  596.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  597.             $fetchmode $this->mdb->fetchmode;
  598.         }
  599.         if (!isset($this->result[1][$target_rownum])) {
  600.             return null;
  601.         }
  602.         $row $this->result[1][$target_rownum];
  603.         // make row associative
  604.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  605.             $column_names $this->getColumnNames();
  606.             foreach ($column_names as $name => $i{
  607.                 $column_names[$name$row[$i];
  608.             }
  609.             $row $column_names;
  610.         }
  611.         if (isset($this->types)) {
  612.             $row $this->mdb->datatype->convertResultRow($this->types$row);
  613.         }
  614.         if ($this->mdb->options['portability'MDB2_PORTABILITY_RTRIM{
  615.             $this->mdb->_rtrimArrayValues($row);
  616.         }
  617.         if ($this->mdb->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL{
  618.             $this->mdb->_convertEmptyArrayValuesToNull($row);
  619.         }
  620.         ++$this->rownum;
  621.         return $row;
  622.     }
  623.  
  624.     // }}}
  625.     // {{{ getColumnNames()
  626.  
  627.     /**
  628.      * Retrieve the names of columns returned by the DBMS in a query result.
  629.      *
  630.      * @return mixed                an associative array variable
  631.      *                               that will hold the names of columns. The
  632.      *                               indexes of the array are the column names
  633.      *                               mapped to lower case and the values are the
  634.      *                               respective numbers of the columns starting
  635.      *                               from 0. Some DBMS may not return any
  636.      *                               columns when the result set does not
  637.      *                               contain any rows.
  638.      *
  639.      *                               a MDB2 error on failure
  640.      * @access public
  641.      */
  642.     function getColumnNames()
  643.     {
  644.         if (is_null($this->result)) {
  645.             return $this->mdb->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  646.                 'getColumnNames: resultset has already been freed');
  647.         }
  648.         $columns array_flip($this->result[0]);
  649.         if ($this->mdb->options['portability'MDB2_PORTABILITY_LOWERCASE{
  650.             $columns array_change_key_case($columnsCASE_LOWER);
  651.         }
  652.         return $columns;
  653.     }
  654.  
  655.     // }}}
  656.     // {{{ numCols()
  657.  
  658.     /**
  659.      * Count the number of columns returned by the DBMS in a query result.
  660.      *
  661.      * @access public
  662.      * @return mixed integer value with the number of columns, a MDB2 error
  663.      *                        on failure
  664.      */
  665.     function numCols()
  666.     {
  667.         if (is_null($this->result)) {
  668.             return $this->mdb->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  669.                 'numCols: resultset has already been freed');
  670.         }
  671.         $cols count($this->result[0]);
  672.         return $cols;
  673.     }
  674. }
  675.  
  676. class MDB2_BufferedResult_querysim extends MDB2_Result_querysim
  677. {
  678.     // }}}
  679.     // {{{ constructor
  680.  
  681.     /**
  682.      * Constructor
  683.      */
  684.     function MDB2_BufferedResult_querysim(&$mdb&$result)
  685.     {
  686.         parent::MDB2_Result_querysim($mdb$result);
  687.     }
  688.  
  689.     // }}}
  690.     // {{{ seek()
  691.  
  692.     /**
  693.     * seek to a specific row in a result set
  694.     *
  695.     * @param int    $rownum    number of the row where the data can be found
  696.     * @return mixed MDB2_OK on success, a MDB2 error on failure
  697.     * @access public
  698.     */
  699.     function seek($rownum = 0)
  700.     {
  701.         if (is_null($this->result)) {
  702.             return $this->mdb->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  703.                 'seek: resultset has already been freed');
  704.         }
  705.         $this->rownum $rownum - 1;
  706.         return MDB2_OK;
  707.     }
  708.  
  709.     // }}}
  710.     // {{{ valid()
  711.  
  712.     /**
  713.     * check if the end of the result set has been reached
  714.     *
  715.     * @return mixed true or false on sucess, a MDB2 error on failure
  716.     * @access public
  717.     */
  718.     function valid()
  719.     {
  720.         $numrows $this->numRows();
  721.         if (MDB2::isError($numrows)) {
  722.             return $numrows;
  723.         }
  724.         return $this->rownum ($numrows - 1);
  725.     }
  726.  
  727.     // }}}
  728.     // {{{ numRows()
  729.  
  730.     /**
  731.     * returns the number of rows in a result object
  732.     *
  733.     * @return mixed MDB2 Error Object or the number of rows
  734.     * @access public
  735.     */
  736.     function numRows()
  737.     {
  738.         if (is_null($this->result)) {
  739.             return $this->mdb->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  740.                 'numRows: resultset has already been freed');
  741.         }
  742.         $rows count($this->result[1]);
  743.         return $rows;
  744.     }
  745. }
  746.  
  747. ?>

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