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 versions 4 and 5                                                 |
  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.36 2005/04/25 13:09:04 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 protected 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 __construct()
  227.     {
  228.         parent::__construct();
  229.  
  230.         $this->phptype  = 'querysim';
  231.         $this->dbsyntax = 'querysim';
  232.  
  233.         // Most of these are dummies to simulate availability if checked
  234.         $this->supported['sequences'= true;
  235.         $this->supported['indexes'= true;
  236.         $this->supported['affected_rows'= true;
  237.         $this->supported['summary_functions'= true;
  238.         $this->supported['order_by_text'= true;
  239.         $this->supported['current_id'= false;
  240.         $this->supported['limit_queries'= true;// this one is real
  241.         $this->supported['LOBs'= true;
  242.         $this->supported['replace'= true;
  243.         $this->supported['sub_selects'= true;
  244.         $this->supported['transactions'= false;
  245.         $this->supported['auto_increment'= false;
  246.  
  247.         $this->options['columnDelim'',';
  248.         $this->options['dataDelim''|';
  249.         $this->options['eolDelim'"\n";
  250.     }
  251.  
  252.     // }}}
  253.  
  254.     // {{{ connect()
  255.  
  256.     /**
  257.      * Open a file or simulate a successful database connect
  258.      *
  259.      * @access public
  260.      *
  261.      * @return mixed MDB2_OK string on success, a MDB2 error object on failure
  262.      */
  263.     function connect()
  264.     {
  265.         if (is_resource($this->connection)) {
  266.             if ($this->connected_database_name == $this->database_name
  267.                 && ($this->opened_persistent == $this->options['persistent'])
  268.             {
  269.                 return MDB2_OK;
  270.             }
  271.             if ($this->connected_database_name{
  272.                 $this->_close($this->connection);
  273.             }
  274.             $this->disconnect();
  275.         }
  276.  
  277.         $connection = 1;// sim connect
  278.         // if external, check file...
  279.         if ($this->database_name{
  280.             $file $this->database_name;
  281.             if (!file_exists($file)) {
  282.                 return $this->raiseError(MDB2_ERROR_NOT_FOUNDnullnull'file not found');
  283.             }
  284.             if (!is_file($file)) {
  285.                 return $this->raiseError(MDB2_ERROR_INVALIDnullnull'not a file');
  286.             }
  287.             if (!is_readable($file)) {
  288.                 return $this->raiseError(MDB2_ERROR_ACCESS_VIOLATIONnullnull,
  289.                     'could not open file - check permissions');
  290.             }
  291.             // ...and open if persistent
  292.             if ($this->options['persistent']{
  293.                 $connection @fopen($file'r');
  294.             }
  295.         }
  296.         $this->connection = $connection;
  297.         $this->connected_database_name = $this->database_name;
  298.         $this->opened_persistent = $this->options['persistent'];
  299.         $this->dbsyntax = $this->dsn['dbsyntax'$this->dsn['dbsyntax'$this->phptype;
  300.  
  301.         return MDB2_OK;
  302.     }
  303.     // }}}
  304.  
  305.     // {{{ disconnect()
  306.  
  307.     /**
  308.      * Log out and disconnect from the database.
  309.      *
  310.      * @return mixed true on success, false if not connected and error
  311.      *                 object on error
  312.      * @access public
  313.      */
  314.     function disconnect()
  315.     {
  316.         if (is_resource($this->connection)) {
  317.             if (($this->opened_persistent&& (is_resource($this->connection))) {
  318.                 if (!@fclose($this->connection)) {
  319.                     return $this->raiseError();
  320.                 }
  321.             }
  322.             $this->connection = 0;
  323.         }
  324.         return MDB2_OK;
  325.     }
  326.     // }}}
  327.  
  328.     // {{{ _doQuery()
  329.  
  330.     /**
  331.      * Execute a query
  332.      * @param string $query  query
  333.      * @param boolean $isManip  if the query is a manipulation query
  334.      * @param resource $connection 
  335.      * @param string $database_name 
  336.      * @return result or error object
  337.      * @access protected
  338.      */
  339.     function _doQuery($query$isManip = false$connection = null$database_name = null)
  340.     {
  341.         if ($isManip{
  342.             return $this->raiseError(MDB2_ERROR_UNSUPPORTED);
  343.         }
  344.  
  345.         $this->last_query = $query;
  346.         $this->debug($query'query');
  347.         if ($this->options['disable_query']{
  348.             return null;
  349.         }
  350.  
  351.         $result $this->_buildResult($query);
  352.         if (PEAR::isError($result)) {
  353.             return $result;
  354.         }
  355.  
  356.         if ($limit > 0{
  357.             $result[1array_slice($result[1]$offset-1$limit);
  358.         }
  359.         return $result;
  360.     }
  361.     // }}}
  362.  
  363.     // {{{ _readFile()
  364.  
  365.     /**
  366.      * Read an external file
  367.      *
  368.      * @param string filepath/filename
  369.      *
  370.      * @access protected
  371.      *
  372.      * @return string the contents of a file
  373.      */
  374.     function _readFile()
  375.     {
  376.         $buffer '';
  377.         if ($this->opened_persistent{
  378.             while (!feof($this->connection)) {
  379.                 $buffer .= fgets($this->connection1024);
  380.             }
  381.         else {
  382.             $this->connection = @fopen($this->connected_database_name'r');
  383.             while (!feof($this->connection)) {
  384.                 $buffer .= fgets($this->connection1024);
  385.             }
  386.             $this->connection = @fclose($this->connection);
  387.         }
  388.         return $buffer;
  389.     }
  390.     // }}}
  391.  
  392.     // {{{ _buildResult()
  393.  
  394.     /**
  395.      * Convert QuerySim text into an array
  396.      *
  397.      * @param string Text of simulated query
  398.      *
  399.      * @access protected
  400.      *
  401.      * @return multi-dimensional array containing the column names and data
  402.      *                                  from the QuerySim
  403.      */
  404.     function _buildResult($query)
  405.     {
  406.         $eolDelim    $this->options['eolDelim'];
  407.         $columnDelim $this->options['columnDelim'];
  408.         $dataDelim   $this->options['dataDelim'];
  409.  
  410.         $columnNames = array();
  411.         $data        = array();
  412.  
  413.         if ($columnDelim == $eolDelim{
  414.             return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  415.                 'columnDelim and eolDelim must be different');
  416.         elseif ($dataDelim == $eolDelim){
  417.             return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  418.                 'dataDelim and eolDelim must be different');
  419.         }
  420.  
  421.         $query trim($query);
  422.         //tokenize escaped slashes
  423.         $query str_replace('\\\\''[$double-slash$]'$query);
  424.  
  425.         if (!strlen($query)) {
  426.             return $this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  427.                 'empty querysim text');
  428.         }
  429.         $lineData $this->_parseOnDelim($query$eolDelim);
  430.         //kill the empty last row created by final eol char if it exists
  431.         if (!strlen(trim($lineData[count($lineData- 1]))) {
  432.             unset($lineData[count($lineData- 1]);
  433.         }
  434.         //populate columnNames array
  435.         $thisLine each($lineData);
  436.         $columnNames $this->_parseOnDelim($thisLine[1]$columnDelim);
  437.         if ((in_array(''$columnNames)) || (in_array('NULL'$columnNames))) {
  438.             return $this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  439.                 'all column names must be defined');
  440.         }
  441.         //replace double-slash tokens with single-slash
  442.         $columnNames str_replace('[$double-slash$]''\\'$columnNames);
  443.         $columnCount count($columnNames);
  444.         $rowNum = 0;
  445.         //loop through data lines
  446.         if (count($lineData> 1{
  447.             while ($thisLine each($lineData)) {
  448.                 $thisData $this->_parseOnDelim($thisLine[1]$dataDelim);
  449.                 $thisDataCount count($thisData);
  450.                 if ($thisDataCount != $columnCount{
  451.                     $fileLineNo $rowNum + 2;
  452.                     return $this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  453.                         "number of data elements ($thisDataCount) in line $fileLineNo not equal to number of defined columns ($columnCount)");
  454.                 }
  455.                 //loop through data elements in data line
  456.                 foreach ($thisData as $thisElement{
  457.                     if (strtoupper($thisElement== 'NULL'){
  458.                         $thisElement '';
  459.                     }
  460.                     //replace double-slash tokens with single-slash
  461.                     $data[$rowNum][str_replace('[$double-slash$]''\\'$thisElement);
  462.                 }//end foreach
  463.                 ++$rowNum;
  464.             }//end while
  465.         }//end if
  466.         return array($columnNames$data);
  467.     }//end function _buildResult()
  468.     // }}}
  469.  
  470.     // {{{ _parseOnDelim()
  471.  
  472.     /**
  473.      * Split QuerySim string into an array on a delimiter
  474.      *
  475.      * @param string $thisLine Text of simulated query
  476.      * @param string $delim    The delimiter to split on
  477.      *
  478.      * @access protected
  479.      *
  480.      * @return array containing parsed string
  481.      */
  482.     function _parseOnDelim($thisLine$delim)
  483.     {
  484.         $delimQuoted preg_quote($delim'/');
  485.         $thisLine trim($thisLine);
  486.  
  487.         $parsed preg_split('/(?<!\\\\)' .$delimQuoted'/'$thisLine);
  488.         //replaces escaped delimiters
  489.         $parsed preg_replace('/\\\\' .$delimQuoted'/'$delim$parsed);
  490.         if ($delim != $this->options['eolDelim']{
  491.             //replaces escape chars
  492.             $parsed preg_replace('/\\\\/'''$parsed);
  493.         }
  494.         return $parsed;
  495.     }
  496.     // }}}
  497. }
  498.  
  499. class MDB2_Result_querysim extends MDB2_Result_Common
  500. {
  501.     // }}}
  502.     // {{{ fetchRow()
  503.  
  504.     /**
  505.      * Fetch a row and insert the data into an existing array.
  506.      *
  507.      * @param int       $fetchmode  how the array data should be indexed
  508.      * @param int    $rownum    number of the row where the data can be found
  509.      * @return int data array on success, a MDB2 error on failure
  510.      * @access public
  511.      */
  512.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  513.     {
  514.         if (is_null($this->result)) {
  515.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  516.                 'fetchRow: resultset has already been freed');
  517.         }
  518.         if (!is_null($rownum)) {
  519.             $seek $this->seek($rownum);
  520.             if (PEAR::isError($seek)) {
  521.                 return $seek;
  522.             }
  523.         }
  524.         $target_rownum $this->rownum + 1;
  525.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  526.             $fetchmode $this->db->fetchmode;
  527.         }
  528.         if (!isset($this->result[1][$target_rownum])) {
  529.             return null;
  530.         }
  531.         $row $this->result[1][$target_rownum];
  532.         // make row associative
  533.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  534.             $column_names $this->getColumnNames();
  535.             foreach ($column_names as $name => $i{
  536.                 $column_names[$name$row[$i];
  537.             }
  538.             $row $column_names;
  539.         }
  540.         if ($this->db->options['portability'MDB2_PORTABILITY_RTRIM{
  541.             $this->db->_rtrimArrayValues($row);
  542.         }
  543.         if ($this->db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL{
  544.             $this->db->_convertEmptyArrayValuesToNull($row);
  545.         }
  546.         if (isset($this->values)) {
  547.             $this->_assignBindColumns($row);
  548.         }
  549.         if (isset($this->types)) {
  550.             $row $this->db->datatype->convertResultRow($this->types$row);
  551.         }
  552.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  553.             $object_class $this->db->options['fetch_class'];
  554.             if ($object_class == 'stdClass'{
  555.                 $row = (object) $row;
  556.             else {
  557.                 $row &new $object_class($row);
  558.             }
  559.         }
  560.         ++$this->rownum;
  561.         return $row;
  562.     }
  563.  
  564.     // }}}
  565.     // {{{ _getColumnNames()
  566.  
  567.     /**
  568.      * Retrieve the names of columns returned by the DBMS in a query result.
  569.      *
  570.      * @return mixed                an associative array variable
  571.      *                               that will hold the names of columns. The
  572.      *                               indexes of the array are the column names
  573.      *                               mapped to lower case and the values are the
  574.      *                               respective numbers of the columns starting
  575.      *                               from 0. Some DBMS may not return any
  576.      *                               columns when the result set does not
  577.      *                               contain any rows.
  578.      *
  579.      *                               a MDB2 error on failure
  580.      * @access private
  581.      */
  582.     function _getColumnNames()
  583.     {
  584.         if (is_null($this->result)) {
  585.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  586.                 'getColumnNames: resultset has already been freed');
  587.         }
  588.         $columns array_flip($this->result[0]);
  589.         if ($this->db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  590.             $columns array_change_key_case($columnsCASE_LOWER);
  591.         }
  592.         return $columns;
  593.     }
  594.  
  595.     // }}}
  596.     // {{{ numCols()
  597.  
  598.     /**
  599.      * Count the number of columns returned by the DBMS in a query result.
  600.      *
  601.      * @access public
  602.      * @return mixed integer value with the number of columns, a MDB2 error
  603.      *                        on failure
  604.      */
  605.     function numCols()
  606.     {
  607.         if (is_null($this->result)) {
  608.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  609.                 'numCols: resultset has already been freed');
  610.         }
  611.         $cols count($this->result[0]);
  612.         return $cols;
  613.     }
  614. }
  615.  
  616. class MDB2_BufferedResult_querysim extends MDB2_Result_querysim
  617. {
  618.     // {{{ seek()
  619.  
  620.     /**
  621.     * seek to a specific row in a result set
  622.     *
  623.     * @param int    $rownum    number of the row where the data can be found
  624.     * @return mixed MDB2_OK on success, a MDB2 error on failure
  625.     * @access public
  626.     */
  627.     function seek($rownum = 0)
  628.     {
  629.         if (is_null($this->result)) {
  630.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  631.                 'seek: resultset has already been freed');
  632.         }
  633.         $this->rownum $rownum - 1;
  634.         return MDB2_OK;
  635.     }
  636.  
  637.     // }}}
  638.     // {{{ valid()
  639.  
  640.     /**
  641.     * check if the end of the result set has been reached
  642.     *
  643.     * @return mixed true or false on sucess, a MDB2 error on failure
  644.     * @access public
  645.     */
  646.     function valid()
  647.     {
  648.         $numrows $this->numRows();
  649.         if (PEAR::isError($numrows)) {
  650.             return $numrows;
  651.         }
  652.         return $this->rownum ($numrows - 1);
  653.     }
  654.  
  655.     // }}}
  656.     // {{{ numRows()
  657.  
  658.     /**
  659.     * returns the number of rows in a result object
  660.     *
  661.     * @return mixed MDB2 Error Object or the number of rows
  662.     * @access public
  663.     */
  664.     function numRows()
  665.     {
  666.         if (is_null($this->result)) {
  667.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  668.                 'numRows: resultset has already been freed');
  669.         }
  670.         $rows count($this->result[1]);
  671.         return $rows;
  672.     }
  673. }
  674.  
  675.  
  676. class MDB2_Statement_querysim extends MDB2_Statement_Common
  677. {
  678.  
  679. }
  680.  
  681. ?>

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