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-2006 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 327310 2012-08-27 15:16:18Z danielc $
  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.     // }}}
  219.     // {{{ constructor
  220.  
  221.     /**
  222.      * Constructor
  223.      */
  224.     function __construct()
  225.     {
  226.         parent::__construct();
  227.  
  228.         $this->phptype  'querysim';
  229.         $this->dbsyntax 'querysim';
  230.  
  231.         // Most of these are dummies to simulate availability if checked
  232.         $this->supported['sequences'= false;
  233.         $this->supported['indexes'= false;
  234.         $this->supported['affected_rows'= false;
  235.         $this->supported['summary_functions'= false;
  236.         $this->supported['order_by_text'= false;
  237.         $this->supported['current_id'= false;
  238.         $this->supported['limit_queries'= true;// this one is real
  239.         $this->supported['LOBs'= true;
  240.         $this->supported['replace'= false;
  241.         $this->supported['sub_selects'= false;
  242.         $this->supported['transactions'= false;
  243.         $this->supported['savepoints'= false;
  244.         $this->supported['auto_increment'= false;
  245.         $this->supported['primary_key'= false;
  246.         $this->supported['result_introspection'= false; // not implemented
  247.         $this->supported['prepared_statements'= false;
  248.         $this->supported['identifier_quoting'= false;
  249.         $this->supported['pattern_escaping'= false;
  250.         $this->supported['new_link'= false;
  251.  
  252.         $this->options['columnDelim'',';
  253.         $this->options['dataDelim''|';
  254.         $this->options['eolDelim'"\n";
  255.     }
  256.  
  257.     // }}}
  258.     // {{{ connect()
  259.  
  260.     /**
  261.      * Open a file or simulate a successful database connect
  262.      *
  263.      * @access public
  264.      *
  265.      * @return mixed MDB2_OK string on success, a MDB2 error object on failure
  266.      */
  267.     function connect()
  268.     {
  269.         if (is_resource($this->connection)) {
  270.             if ($this->connected_database_name == $this->database_name
  271.                 && ($this->opened_persistent == $this->options['persistent'])
  272.             {
  273.                 return MDB2_OK;
  274.             }
  275.             if ($this->connected_database_name{
  276.                 $this->_close($this->connection);
  277.             }
  278.             $this->disconnect();
  279.         }
  280.  
  281.         $connection = 1;// sim connect
  282.         // if external, check file...
  283.         if ($this->database_name{
  284.             $file $this->database_name;
  285.             if (!file_exists($file)) {
  286.                 return $this->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  287.                     'file not found'__FUNCTION__);
  288.             }
  289.             if (!is_file($file)) {
  290.                 return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  291.                     'not a file'__FUNCTION__);
  292.             }
  293.             if (!is_readable($file)) {
  294.                 return $this->raiseError(MDB2_ERROR_ACCESS_VIOLATIONnullnull,
  295.                     'could not open file - check permissions'__FUNCTION__);
  296.             }
  297.             // ...and open if persistent
  298.             if ($this->options['persistent']{
  299.                 $connection @fopen($file'r');
  300.             }
  301.         }
  302.  
  303.         if (!empty($this->dsn['charset'])) {
  304.             $result $this->setCharset($this->dsn['charset']$connection);
  305.             if (MDB2::isError($result)) {
  306.                 return $result;
  307.             }
  308.         }
  309.  
  310.         $this->connection $connection;
  311.         $this->connected_database_name $this->database_name;
  312.         $this->opened_persistent $this->options['persistent'];
  313.         $this->dbsyntax $this->dsn['dbsyntax'$this->dsn['dbsyntax'$this->phptype;
  314.  
  315.         return MDB2_OK;
  316.     }
  317.  
  318.     // }}}
  319.     // {{{ disconnect()
  320.  
  321.     /**
  322.      * Log out and disconnect from the database.
  323.      *
  324.      * @param  boolean $force if the disconnect should be forced even if the
  325.      *                         connection is opened persistently
  326.      * @return mixed true on success, false if not connected and error
  327.      *                 object on error
  328.      * @access public
  329.      */
  330.     function disconnect($force = true)
  331.     {
  332.         if (is_resource($this->connection)) {
  333.             if ($this->opened_persistent{
  334.                 @fclose($this->connection);
  335.             }
  336.         }
  337.         return parent::disconnect($force);
  338.     }
  339.  
  340.     // }}}
  341.     // {{{ _doQuery()
  342.  
  343.     /**
  344.      * Execute a query
  345.      * @param string $query  query
  346.      * @param boolean $is_manip  if the query is a manipulation query
  347.      * @param resource $connection 
  348.      * @param string $database_name 
  349.      * @return result or error object
  350.      * @access protected
  351.      */
  352.     function &_doQuery($query$is_manip = false$connection = null$database_name = null)
  353.     {
  354.         if ($this->database_name{
  355.             $query $this->_readFile();
  356.         }
  357.         $this->last_query $query;
  358.         $result $this->debug($query'query'array('is_manip' => $is_manip'when' => 'pre'));
  359.         if ($result{
  360.             if (MDB2::isError($result)) {
  361.                 return $result;
  362.             }
  363.             $query $result;
  364.         }
  365.         if ($is_manip{
  366.             $err =$this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  367.                 'Manipulation statements are not supported'__FUNCTION__);
  368.             return $err;
  369.         }
  370.         if ($this->options['disable_query']{
  371.             $result $is_manip ? 0 : null;
  372.             return $result;
  373.         }
  374.  
  375.         $result $this->_buildResult($query);
  376.         if (MDB2::isError($result)) {
  377.             return $result;
  378.         }
  379.  
  380.         if ($this->next_limit > 0{
  381.             $result[1array_slice($result[1]$this->next_offset$this->next_limit);
  382.         }
  383.  
  384.         $this->debug($query'query'array('is_manip' => $is_manip'when' => 'post''result' => $result));
  385.         return $result;
  386.     }
  387.  
  388.     // }}}
  389.     // {{{ _modifyQuery()
  390.  
  391.     /**
  392.      * Changes a query string for various DBMS specific reasons
  393.      *
  394.      * @param string $query  query to modify
  395.      * @param boolean $is_manip  if it is a DML query
  396.      * @param integer $limit  limit the number of rows
  397.      * @param integer $offset  start reading from given offset
  398.      * @return string modified query
  399.      * @access protected
  400.      */
  401.     function _modifyQuery($query$is_manip$limit$offset)
  402.     {
  403.         $this->next_limit $limit;
  404.         $this->next_offset $offset;
  405.         return $query;
  406.     }
  407.  
  408.     // }}}
  409.     // {{{ _readFile()
  410.  
  411.     /**
  412.      * Read an external file
  413.      *
  414.      * @param string filepath/filename
  415.      *
  416.      * @access protected
  417.      *
  418.      * @return string the contents of a file
  419.      */
  420.     function _readFile()
  421.     {
  422.         $buffer '';
  423.         if ($this->opened_persistent{
  424.             $connection $this->getConnection();
  425.             if (MDB2::isError($connection)) {
  426.                 return $connection;
  427.             }
  428.             while (!feof($connection)) {
  429.                 $buffer.= fgets($connection1024);
  430.             }
  431.             rewind($connection);
  432.         else {
  433.             $connection @fopen($this->connected_database_name'r');
  434.             while (!feof($connection)) {
  435.                 $buffer.= fgets($connection1024);
  436.             }
  437.             @fclose($connection);
  438.         }
  439.         return $buffer;
  440.     }
  441.  
  442.     // }}}
  443.     // {{{ _buildResult()
  444.  
  445.     /**
  446.      * Convert QuerySim text into an array
  447.      *
  448.      * @param string Text of simulated query
  449.      *
  450.      * @access protected
  451.      *
  452.      * @return multi-dimensional array containing the column names and data
  453.      *                                  from the QuerySim
  454.      */
  455.     function _buildResult($query)
  456.     {
  457.         $eolDelim    $this->options['eolDelim'];
  458.         $columnDelim $this->options['columnDelim'];
  459.         $dataDelim   $this->options['dataDelim'];
  460.  
  461.         $columnNames = array();
  462.         $data        = array();
  463.  
  464.         if ($columnDelim == $eolDelim{
  465.             return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  466.                 'columnDelim and eolDelim must be different'__FUNCTION__);
  467.         elseif ($dataDelim == $eolDelim){
  468.             return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  469.                 'dataDelim and eolDelim must be different'__FUNCTION__);
  470.         }
  471.  
  472.         $query trim($query);
  473.         //tokenize escaped slashes
  474.         $query str_replace('\\\\''[$double-slash$]'$query);
  475.  
  476.         if (!strlen($query)) {
  477.             return $this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  478.                 'empty querysim text'__FUNCTION__);
  479.         }
  480.         $lineData $this->_parseOnDelim($query$eolDelim);
  481.         //kill the empty last row created by final eol char if it exists
  482.         if (!strlen(trim($lineData[count($lineData- 1]))) {
  483.             unset($lineData[count($lineData- 1]);
  484.         }
  485.         //populate columnNames array
  486.         $thisLine each($lineData);
  487.         $columnNames $this->_parseOnDelim($thisLine[1]$columnDelim);
  488.         if ((in_array(''$columnNames)) || (in_array('NULL'$columnNames))) {
  489.             return $this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  490.                 'all column names must be defined'__FUNCTION__);
  491.         }
  492.         //replace double-slash tokens with single-slash
  493.         $columnNames str_replace('[$double-slash$]''\\'$columnNames);
  494.         $columnCount count($columnNames);
  495.         $rowNum = 0;
  496.         //loop through data lines
  497.         if (count($lineData> 1{
  498.             while ($thisLine each($lineData)) {
  499.                 $thisData $this->_parseOnDelim($thisLine[1]$dataDelim);
  500.                 $thisDataCount count($thisData);
  501.                 if ($thisDataCount != $columnCount{
  502.                     $fileLineNo $rowNum + 2;
  503.                     return $this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  504.                         "number of data elements ($thisDataCount) in line $fileLineNo not equal to number of defined columns ($columnCount)"__FUNCTION__);
  505.                 }
  506.                 //loop through data elements in data line
  507.                 foreach ($thisData as $thisElement{
  508.                     if (strtoupper($thisElement== 'NULL'){
  509.                         $thisElement '';
  510.                     }
  511.                     //replace double-slash tokens with single-slash
  512.                     $data[$rowNum][str_replace('[$double-slash$]''\\'$thisElement);
  513.                 }//end foreach
  514.                 ++$rowNum;
  515.             }//end while
  516.         }//end if
  517.         return array($columnNames$data);
  518.     }
  519.  
  520.     // }}}
  521.     // {{{ _parseOnDelim()
  522.  
  523.     /**
  524.      * Split QuerySim string into an array on a delimiter
  525.      *
  526.      * @param string $thisLine Text of simulated query
  527.      * @param string $delim    The delimiter to split on
  528.      *
  529.      * @access protected
  530.      *
  531.      * @return array containing parsed string
  532.      */
  533.     function _parseOnDelim($thisLine$delim)
  534.     {
  535.         $delimQuoted preg_quote($delim'/');
  536.         $thisLine trim($thisLine);
  537.  
  538.         $parsed preg_split('/(?<!\\\\)' .$delimQuoted'/'$thisLine);
  539.         //replaces escaped delimiters
  540.         $parsed preg_replace('/\\\\' .$delimQuoted'/'$delim$parsed);
  541.         if ($delim != $this->options['eolDelim']{
  542.             //replaces escape chars
  543.             $parsed preg_replace('/\\\\/'''$parsed);
  544.         }
  545.         return $parsed;
  546.     }
  547.  
  548.     // }}}
  549.     // {{{ &exec()
  550.  
  551.     /**
  552.      * Execute a manipulation query to the database and return any the affected rows
  553.      *
  554.      * @param string $query the SQL query
  555.      * @return mixed affected rows on success, a MDB2 error on failure
  556.      * @access public
  557.      */
  558.     function &exec($query)
  559.     {
  560.         return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  561.             'Querysim only supports reading data'__FUNCTION__);
  562.     }
  563.  
  564.     // }}}
  565.     // {{{ getServerVersion()
  566.  
  567.     /**
  568.      * return version information about the server
  569.      *
  570.      * @param bool   $native  determines if the raw version string should be returned
  571.      * @return mixed array/string with version information or MDB2 error object
  572.      * @access public
  573.      */
  574.     function getServerVersion($native = false)
  575.     {
  576.         $server_info '0.7.0';
  577.         // cache server_info
  578.         $this->connected_server_info $server_info;
  579.         if (!$native{
  580.             $tmp explode('.'$server_info3);
  581.             if (!isset($tmp[2]|| !preg_match('/(\d+)(.*)/'$tmp[2]$tmp2)) {
  582.                 $tmp2[0= isset($tmp[2]$tmp[2: null;
  583.                 $tmp2[1= null;
  584.             }
  585.             $server_info = array(
  586.                 'major' => isset($tmp[0]$tmp[0: null,
  587.                 'minor' => isset($tmp[1]$tmp[1: null,
  588.                 'patch' => isset($tmp2[0]$tmp2[0: null,
  589.                 'extra' => isset($tmp2[1]$tmp2[1: null,
  590.                 'native' => $server_info,
  591.             );
  592.         }
  593.         return $server_info;
  594.     }
  595. }
  596.  
  597. /**
  598.  * MDB2 QuerySim result driver
  599.  *
  600.  * @package MDB2
  601.  * @category Database
  602.  * @author  Alan Richmond <arichmond@bigfoot.com>
  603.  */
  604. class MDB2_Result_querysim extends MDB2_Result_Common
  605. {
  606.     // }}}
  607.     // {{{ fetchRow()
  608.  
  609.     /**
  610.      * Fetch a row and insert the data into an existing array.
  611.      *
  612.      * @param int       $fetchmode  how the array data should be indexed
  613.      * @param int    $rownum    number of the row where the data can be found
  614.      * @return int data array on success, a MDB2 error on failure
  615.      * @access public
  616.      */
  617.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  618.     {
  619.         if (false === $this->result{
  620.             $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  621.                 'resultset has already been freed'__FUNCTION__);
  622.             return $err;
  623.         }
  624.         if (null === $this->result{
  625.             return null;
  626.         }
  627.         if (null !== $rownum{
  628.             $seek $this->seek($rownum);
  629.             if (MDB2::isError($seek)) {
  630.                 return $seek;
  631.             }
  632.         }
  633.         $target_rownum $this->rownum + 1;
  634.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  635.             $fetchmode $this->db->fetchmode;
  636.         }
  637.         if (!isset($this->result[1][$target_rownum])) {
  638.             $null = null;
  639.             return $null;
  640.         }
  641.         $row $this->result[1][$target_rownum];
  642.         // make row associative
  643.         if (   $fetchmode == MDB2_FETCHMODE_ASSOC
  644.             || $fetchmode == MDB2_FETCHMODE_OBJECT
  645.         {
  646.             $column_names $this->getColumnNames();
  647.             foreach ($column_names as $name => $i{
  648.                 $column_names[$name$row[$i];
  649.             }
  650.             $row $column_names;
  651.         }
  652.         $mode $this->db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL;
  653.         $rtrim = false;
  654.         if ($this->db->options['portability'MDB2_PORTABILITY_RTRIM{
  655.             if (empty($this->types)) {
  656.                 $mode += MDB2_PORTABILITY_RTRIM;
  657.             else {
  658.                 $rtrim = true;
  659.             }
  660.         }
  661.         if ($mode{
  662.             $this->db->_fixResultArrayValues($row$mode);
  663.         }
  664.         if (   (   $fetchmode != MDB2_FETCHMODE_ASSOC
  665.                 && $fetchmode != MDB2_FETCHMODE_OBJECT)
  666.             && !empty($this->types)
  667.         {
  668.             $row $this->db->datatype->convertResultRow($this->types$row$rtrim);
  669.         elseif (($fetchmode == MDB2_FETCHMODE_ASSOC
  670.                 || $detchmode == MDB2_FETCHMODE_OBJECT)
  671.             && !empty($this->types_assoc)
  672.         {
  673.             $row $this->db->datatype->convertResultRow($this->types_assoc$row$rtrim);
  674.         }
  675.         if (!empty($this->values)) {
  676.             $this->_assignBindColumns($row);
  677.         }
  678.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  679.             $object_class $this->db->options['fetch_class'];
  680.             if ($object_class == 'stdClass'{
  681.                 $row = (object) $row;
  682.             else {
  683.                 $row = new $object_class($row);
  684.             }
  685.         }
  686.         ++$this->rownum;
  687.         return $row;
  688.     }
  689.  
  690.     // }}}
  691.     // {{{ _getColumnNames()
  692.  
  693.     /**
  694.      * Retrieve the names of columns returned by the DBMS in a query result.
  695.      *
  696.      * @return  mixed   Array variable that holds the names of columns as keys
  697.      *                   or an MDB2 error on failure.
  698.      *                   Some DBMS may not return any columns when the result set
  699.      *                   does not contain any rows.
  700.      * @access private
  701.      */
  702.     function _getColumnNames()
  703.     {
  704.         if (false === $this->result{
  705.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  706.                 'resultset has already been freed'__FUNCTION__);
  707.         }
  708.         if (null === $this->result{
  709.             return array();
  710.         }
  711.         $columns array_flip($this->result[0]);
  712.         if ($this->db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  713.             $columns array_change_key_case($columns$this->db->options['field_case']);
  714.         }
  715.         return $columns;
  716.     }
  717.  
  718.     // }}}
  719.     // {{{ numCols()
  720.  
  721.     /**
  722.      * Count the number of columns returned by the DBMS in a query result.
  723.      *
  724.      * @access public
  725.      * @return mixed integer value with the number of columns, a MDB2 error
  726.      *                        on failure
  727.      */
  728.     function numCols()
  729.     {
  730.         if (false === $this->result{
  731.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  732.                 'resultset has already been freed'__FUNCTION__);
  733.         }
  734.         if (null === $this->result{
  735.             return count($this->types);
  736.         }
  737.         $cols count($this->result[0]);
  738.         return $cols;
  739.     }
  740. }
  741.  
  742. /**
  743.  * MDB2 QuerySim buffered result driver
  744.  *
  745.  * @package MDB2
  746.  * @category Database
  747.  * @author  Alan Richmond <arichmond@bigfoot.com>
  748.  */
  749. {
  750.     // }}}
  751.     // {{{ seek()
  752.  
  753.     /**
  754.      * Seek to a specific row in a result set
  755.      *
  756.      * @param int    $rownum    number of the row where the data can be found
  757.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  758.      * @access public
  759.      */
  760.     function seek($rownum = 0)
  761.     {
  762.         if (false === $this->result{
  763.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  764.                 'resultset has already been freed'__FUNCTION__);
  765.         }
  766.         $this->rownum $rownum - 1;
  767.         return MDB2_OK;
  768.     }
  769.  
  770.     // }}}
  771.     // {{{ valid()
  772.  
  773.     /**
  774.      * Check if the end of the result set has been reached
  775.      *
  776.      * @return mixed true or false on sucess, a MDB2 error on failure
  777.      * @access public
  778.      */
  779.     function valid()
  780.     {
  781.         $numrows $this->numRows();
  782.         if (MDB2::isError($numrows)) {
  783.             return $numrows;
  784.         }
  785.         return $this->rownum ($numrows - 1);
  786.     }
  787.  
  788.     // }}}
  789.     // {{{ numRows()
  790.  
  791.     /**
  792.      * Returns the number of rows in a result object
  793.      *
  794.      * @return mixed MDB2 Error Object or the number of rows
  795.      * @access public
  796.      */
  797.     function numRows()
  798.     {
  799.         if (false === $this->result{
  800.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  801.                 'resultset has already been freed'__FUNCTION__);
  802.         }
  803.         if (null === $this->result{
  804.             return 0;
  805.         }
  806.         $rows count($this->result[1]);
  807.         return $rows;
  808.     }
  809. }
  810.  
  811. /**
  812.  * MDB2 QuerySim statement driver
  813.  *
  814.  * @package MDB2
  815.  * @category Database
  816.  * @author  Alan Richmond <arichmond@bigfoot.com>
  817.  */
  818. class MDB2_Statement_querysim extends MDB2_Statement_Common
  819. {
  820.  
  821. }
  822.  
  823. ?>

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