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.49 2005/12/16 12:09:42 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. class MDB2_Driver_querysim extends MDB2_Driver_Common
  217. {
  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''emulated';
  235.         $this->supported['indexes'= true;
  236.         $this->supported['affected_rows'= false;
  237.         $this->supported['summary_functions'= false;
  238.         $this->supported['order_by_text'= false;
  239.         $this->supported['current_id''emulated';
  240.         $this->supported['limit_queries'= true;// this one is real
  241.         $this->supported['LOBs'= true;
  242.         $this->supported['replace''emulated';
  243.         $this->supported['sub_selects''emulated';
  244.         $this->supported['transactions'= false;
  245.         $this->supported['auto_increment'= false;
  246.         $this->supported['primary_key'= false;
  247.  
  248.         $this->options['columnDelim'',';
  249.         $this->options['dataDelim''|';
  250.         $this->options['eolDelim'"\n";
  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{
  318.                 @fclose($this->connection);
  319.             }
  320.             $this->connection = 0;
  321.         }
  322.         return MDB2_OK;
  323.     }
  324.  
  325.     // }}}
  326.     // {{{ _doQuery()
  327.  
  328.     /**
  329.      * Execute a query
  330.      * @param string $query  query
  331.      * @param boolean $is_manip  if the query is a manipulation query
  332.      * @param resource $connection 
  333.      * @param string $database_name 
  334.      * @return result or error object
  335.      * @access protected
  336.      */
  337.     function _doQuery($query$is_manip = false$connection = null$database_name = null)
  338.     {
  339.         if ($this->database_name{
  340.             $query $this->_readFile();
  341.         }
  342.         $this->last_query $query;
  343.         $this->debug($query'query');
  344.         if ($is_manip{
  345.             return $this->raiseError(MDB2_ERROR_UNSUPPORTED);
  346.         }
  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 ($this->next_limit > 0{
  357.             $result[1array_slice($result[1]$this->next_offset$this->next_limit);
  358.         }
  359.         return $result;
  360.     }
  361.  
  362.     // }}}
  363.     // {{{ _modifyQuery()
  364.  
  365.     /**
  366.      * Changes a query string for various DBMS specific reasons
  367.      *
  368.      * @param string $query  query to modify
  369.      * @return the new (modified) query
  370.      * @access protected
  371.      */
  372.     function _modifyQuery($query$is_manip$limit$offset)
  373.     {
  374.         $this->next_limit $limit;
  375.         $this->next_offset $offset;
  376.         return $query;
  377.     }
  378.  
  379.     // }}}
  380.     // {{{ _readFile()
  381.  
  382.     /**
  383.      * Read an external file
  384.      *
  385.      * @param string filepath/filename
  386.      *
  387.      * @access protected
  388.      *
  389.      * @return string the contents of a file
  390.      */
  391.     function _readFile()
  392.     {
  393.         $buffer '';
  394.         if ($this->opened_persistent{
  395.             $connection $this->getConnection();
  396.             if (PEAR::isError($connection)) {
  397.                 return $connection;
  398.             }
  399.             while (!feof($connection)) {
  400.                 $buffer.= fgets($connection1024);
  401.             }
  402.             rewind($connection);
  403.         else {
  404.             $connection @fopen($this->connected_database_name'r');
  405.             while (!feof($connection)) {
  406.                 $buffer.= fgets($connection1024);
  407.             }
  408.             @fclose($connection);
  409.         }
  410.         return $buffer;
  411.     }
  412.  
  413.     // }}}
  414.     // {{{ _buildResult()
  415.  
  416.     /**
  417.      * Convert QuerySim text into an array
  418.      *
  419.      * @param string Text of simulated query
  420.      *
  421.      * @access protected
  422.      *
  423.      * @return multi-dimensional array containing the column names and data
  424.      *                                  from the QuerySim
  425.      */
  426.     function _buildResult($query)
  427.     {
  428.         $eolDelim    $this->options['eolDelim'];
  429.         $columnDelim $this->options['columnDelim'];
  430.         $dataDelim   $this->options['dataDelim'];
  431.  
  432.         $columnNames = array();
  433.         $data        = array();
  434.  
  435.         if ($columnDelim == $eolDelim{
  436.             return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  437.                 'columnDelim and eolDelim must be different');
  438.         elseif ($dataDelim == $eolDelim){
  439.             return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  440.                 'dataDelim and eolDelim must be different');
  441.         }
  442.  
  443.         $query trim($query);
  444.         //tokenize escaped slashes
  445.         $query str_replace('\\\\''[$double-slash$]'$query);
  446.  
  447.         if (!strlen($query)) {
  448.             return $this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  449.                 'empty querysim text');
  450.         }
  451.         $lineData $this->_parseOnDelim($query$eolDelim);
  452.         //kill the empty last row created by final eol char if it exists
  453.         if (!strlen(trim($lineData[count($lineData- 1]))) {
  454.             unset($lineData[count($lineData- 1]);
  455.         }
  456.         //populate columnNames array
  457.         $thisLine each($lineData);
  458.         $columnNames $this->_parseOnDelim($thisLine[1]$columnDelim);
  459.         if ((in_array(''$columnNames)) || (in_array('NULL'$columnNames))) {
  460.             return $this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  461.                 'all column names must be defined');
  462.         }
  463.         //replace double-slash tokens with single-slash
  464.         $columnNames str_replace('[$double-slash$]''\\'$columnNames);
  465.         $columnCount count($columnNames);
  466.         $rowNum = 0;
  467.         //loop through data lines
  468.         if (count($lineData> 1{
  469.             while ($thisLine each($lineData)) {
  470.                 $thisData $this->_parseOnDelim($thisLine[1]$dataDelim);
  471.                 $thisDataCount count($thisData);
  472.                 if ($thisDataCount != $columnCount{
  473.                     $fileLineNo $rowNum + 2;
  474.                     return $this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  475.                         "number of data elements ($thisDataCount) in line $fileLineNo not equal to number of defined columns ($columnCount)");
  476.                 }
  477.                 //loop through data elements in data line
  478.                 foreach ($thisData as $thisElement{
  479.                     if (strtoupper($thisElement== 'NULL'){
  480.                         $thisElement '';
  481.                     }
  482.                     //replace double-slash tokens with single-slash
  483.                     $data[$rowNum][str_replace('[$double-slash$]''\\'$thisElement);
  484.                 }//end foreach
  485.                 ++$rowNum;
  486.             }//end while
  487.         }//end if
  488.         return array($columnNames$data);
  489.     }
  490.  
  491.     // }}}
  492.     // {{{ _parseOnDelim()
  493.  
  494.     /**
  495.      * Split QuerySim string into an array on a delimiter
  496.      *
  497.      * @param string $thisLine Text of simulated query
  498.      * @param string $delim    The delimiter to split on
  499.      *
  500.      * @access protected
  501.      *
  502.      * @return array containing parsed string
  503.      */
  504.     function _parseOnDelim($thisLine$delim)
  505.     {
  506.         $delimQuoted preg_quote($delim'/');
  507.         $thisLine trim($thisLine);
  508.  
  509.         $parsed preg_split('/(?<!\\\\)' .$delimQuoted'/'$thisLine);
  510.         //replaces escaped delimiters
  511.         $parsed preg_replace('/\\\\' .$delimQuoted'/'$delim$parsed);
  512.         if ($delim != $this->options['eolDelim']{
  513.             //replaces escape chars
  514.             $parsed preg_replace('/\\\\/'''$parsed);
  515.         }
  516.         return $parsed;
  517.     }
  518.  
  519.     // }}}
  520.     // {{{ exec()
  521.  
  522.     /**
  523.      * Execute a manipulation query to the database and return any the affected rows
  524.      *
  525.      * @param string $query the SQL query
  526.      * @return mixed affected rows on success, a MDB2 error on failure
  527.      * @access public
  528.      */
  529.     function exec($query)
  530.     {
  531.         return $this->raiseError(MDB2_ERROR_UNSUPPORTED);
  532.     }
  533.  
  534.     // }}}
  535.     // {{{ getServerVersion()
  536.  
  537.     /**
  538.      * return version information about the server
  539.      *
  540.      * @param string     $native  determines if the raw version string should be returned
  541.      * @return mixed array with versoin information or row string
  542.      * @access public
  543.      */
  544.     function getServerVersion($native = false)
  545.     {
  546.         $server_info '@package_version@';
  547.         if (!$native{
  548.             $tmp explode('.'$server_info);
  549.             preg_match('/(\d+)(.*)/'@$tmp[2]$tmp2);
  550.             $server_info = array(
  551.                 'major' => @$tmp[0],
  552.                 'minor' => @$tmp[1],
  553.                 'patch' => @$tmp2[1],
  554.                 'extra' => @$tmp2[2],
  555.                 'native' => $server_info,
  556.             );
  557.         }
  558.         return $server_info;
  559.     }
  560. }
  561.  
  562. class MDB2_Result_querysim extends MDB2_Result_Common
  563. {
  564.     // }}}
  565.     // {{{ fetchRow()
  566.  
  567.     /**
  568.      * Fetch a row and insert the data into an existing array.
  569.      *
  570.      * @param int       $fetchmode  how the array data should be indexed
  571.      * @param int    $rownum    number of the row where the data can be found
  572.      * @return int data array on success, a MDB2 error on failure
  573.      * @access public
  574.      */
  575.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  576.     {
  577.         if (is_null($this->result)) {
  578.             $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  579.                 'fetchRow: resultset has already been freed');
  580.             return $err;
  581.         }
  582.         if (!is_null($rownum)) {
  583.             $seek $this->seek($rownum);
  584.             if (PEAR::isError($seek)) {
  585.                 return $seek;
  586.             }
  587.         }
  588.         $target_rownum $this->rownum + 1;
  589.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  590.             $fetchmode $this->db->fetchmode;
  591.         }
  592.         if (!isset($this->result[1][$target_rownum])) {
  593.             $null = null;
  594.             return $null;
  595.         }
  596.         $row $this->result[1][$target_rownum];
  597.         // make row associative
  598.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  599.             $column_names $this->getColumnNames();
  600.             foreach ($column_names as $name => $i{
  601.                 $column_names[$name$row[$i];
  602.             }
  603.             $row $column_names;
  604.         }
  605.         if (($mode ($this->db->options['portability'MDB2_PORTABILITY_RTRIM)
  606.             + ($this->db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL))
  607.         {
  608.             $this->db->_fixResultArrayValues($row$mode);
  609.         }
  610.         if (!empty($this->values)) {
  611.             $this->_assignBindColumns($row);
  612.         }
  613.         if (!empty($this->types)) {
  614.             $row $this->db->datatype->convertResultRow($this->types$row);
  615.         }
  616.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  617.             $object_class $this->db->options['fetch_class'];
  618.             if ($object_class == 'stdClass'{
  619.                 $row = (object) $row;
  620.             else {
  621.                 $row &new $object_class($row);
  622.             }
  623.         }
  624.         ++$this->rownum;
  625.         return $row;
  626.     }
  627.  
  628.     // }}}
  629.     // {{{ _getColumnNames()
  630.  
  631.     /**
  632.      * Retrieve the names of columns returned by the DBMS in a query result.
  633.      *
  634.      * @return mixed                an associative array variable
  635.      *                               that will hold the names of columns. The
  636.      *                               indexes of the array are the column names
  637.      *                               mapped to lower case and the values are the
  638.      *                               respective numbers of the columns starting
  639.      *                               from 0. Some DBMS may not return any
  640.      *                               columns when the result set does not
  641.      *                               contain any rows.
  642.      *
  643.      *                               a MDB2 error on failure
  644.      * @access private
  645.      */
  646.     function _getColumnNames()
  647.     {
  648.         if (is_null($this->result)) {
  649.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  650.                 'getColumnNames: resultset has already been freed');
  651.         }
  652.         $columns array_flip($this->result[0]);
  653.         if ($this->db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  654.             $columns array_change_key_case($columns$this->db->options['field_case']);
  655.         }
  656.         return $columns;
  657.     }
  658.  
  659.     // }}}
  660.     // {{{ numCols()
  661.  
  662.     /**
  663.      * Count the number of columns returned by the DBMS in a query result.
  664.      *
  665.      * @access public
  666.      * @return mixed integer value with the number of columns, a MDB2 error
  667.      *                        on failure
  668.      */
  669.     function numCols()
  670.     {
  671.         if (is_null($this->result)) {
  672.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  673.                 'numCols: resultset has already been freed');
  674.         }
  675.         $cols count($this->result[0]);
  676.         return $cols;
  677.     }
  678. }
  679.  
  680. {
  681.     // }}}
  682.     // {{{ seek()
  683.  
  684.     /**
  685.     * seek to a specific row in a result set
  686.     *
  687.     * @param int    $rownum    number of the row where the data can be found
  688.     * @return mixed MDB2_OK on success, a MDB2 error on failure
  689.     * @access public
  690.     */
  691.     function seek($rownum = 0)
  692.     {
  693.         if (is_null($this->result)) {
  694.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  695.                 'seek: resultset has already been freed');
  696.         }
  697.         $this->rownum $rownum - 1;
  698.         return MDB2_OK;
  699.     }
  700.  
  701.     // }}}
  702.     // {{{ valid()
  703.  
  704.     /**
  705.     * check if the end of the result set has been reached
  706.     *
  707.     * @return mixed true or false on sucess, a MDB2 error on failure
  708.     * @access public
  709.     */
  710.     function valid()
  711.     {
  712.         $numrows $this->numRows();
  713.         if (PEAR::isError($numrows)) {
  714.             return $numrows;
  715.         }
  716.         return $this->rownum ($numrows - 1);
  717.     }
  718.  
  719.     // }}}
  720.     // {{{ numRows()
  721.  
  722.     /**
  723.     * returns the number of rows in a result object
  724.     *
  725.     * @return mixed MDB2 Error Object or the number of rows
  726.     * @access public
  727.     */
  728.     function numRows()
  729.     {
  730.         if (is_null($this->result)) {
  731.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  732.                 'numRows: resultset has already been freed');
  733.         }
  734.         $rows count($this->result[1]);
  735.         return $rows;
  736.     }
  737. }
  738.  
  739. class MDB2_Statement_querysim extends MDB2_Statement_Common
  740. {
  741.  
  742. }
  743.  
  744. ?>

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