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