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

Source for file mysql.php

Documentation is available at mysql.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. // | Author: Lukas Smith <smith@pooteeweet.org>                           |
  44. // +----------------------------------------------------------------------+
  45. //
  46. // $Id: mysql.php,v 1.182 2007/05/02 22:00:08 quipo Exp $
  47. //
  48.  
  49. /**
  50.  * MDB2 MySQL driver
  51.  *
  52.  * @package MDB2
  53.  * @category Database
  54.  * @author  Lukas Smith <smith@pooteeweet.org>
  55.  */
  56. class MDB2_Driver_mysql extends MDB2_Driver_Common
  57. {
  58.     // {{{ properties
  59.     var $string_quoting = array('start' => "'"'end' => "'"'escape' => '\\''escape_pattern' => '\\');
  60.  
  61.     var $identifier_quoting = array('start' => '`''end' => '`''escape' => '`');
  62.  
  63.     var $sql_comments = array(
  64.         array('start' => '-- ''end' => "\n"'escape' => false),
  65.         array('start' => '#''end' => "\n"'escape' => false),
  66.         array('start' => '/*''end' => '*/''escape' => false),
  67.     );
  68.  
  69.     var $start_transaction = false;
  70.  
  71.     var $varchar_max_length = 255;
  72.     // }}}
  73.     // {{{ constructor
  74.  
  75.     /**
  76.      * Constructor
  77.      */
  78.     function __construct()
  79.     {
  80.         parent::__construct();
  81.  
  82.         $this->phptype 'mysql';
  83.         $this->dbsyntax 'mysql';
  84.  
  85.         $this->supported['sequences''emulated';
  86.         $this->supported['indexes'= true;
  87.         $this->supported['affected_rows'= true;
  88.         $this->supported['transactions'= false;
  89.         $this->supported['savepoints'= false;
  90.         $this->supported['summary_functions'= true;
  91.         $this->supported['order_by_text'= true;
  92.         $this->supported['current_id''emulated';
  93.         $this->supported['limit_queries'= true;
  94.         $this->supported['LOBs'= true;
  95.         $this->supported['replace'= true;
  96.         $this->supported['sub_selects''emulated';
  97.         $this->supported['auto_increment'= true;
  98.         $this->supported['primary_key'= true;
  99.         $this->supported['result_introspection'= true;
  100.         $this->supported['prepared_statements''emulated';
  101.         $this->supported['identifier_quoting'= true;
  102.         $this->supported['pattern_escaping'= true;
  103.         $this->supported['new_link'= true;
  104.  
  105.         $this->options['default_table_type''';
  106.     }
  107.  
  108.     // }}}
  109.     // {{{ errorInfo()
  110.  
  111.     /**
  112.      * This method is used to collect information about an error
  113.      *
  114.      * @param integer $error 
  115.      * @return array 
  116.      * @access public
  117.      */
  118.     function errorInfo($error = null)
  119.     {
  120.         if ($this->connection{
  121.             $native_code @mysql_errno($this->connection);
  122.             $native_msg  @mysql_error($this->connection);
  123.         else {
  124.             $native_code @mysql_errno();
  125.             $native_msg  @mysql_error();
  126.         }
  127.         if (is_null($error)) {
  128.             static $ecode_map;
  129.             if (empty($ecode_map)) {
  130.                 $ecode_map = array(
  131.                     1004 => MDB2_ERROR_CANNOT_CREATE,
  132.                     1005 => MDB2_ERROR_CANNOT_CREATE,
  133.                     1006 => MDB2_ERROR_CANNOT_CREATE,
  134.                     1007 => MDB2_ERROR_ALREADY_EXISTS,
  135.                     1008 => MDB2_ERROR_CANNOT_DROP,
  136.                     1022 => MDB2_ERROR_ALREADY_EXISTS,
  137.                     1044 => MDB2_ERROR_ACCESS_VIOLATION,
  138.                     1046 => MDB2_ERROR_NODBSELECTED,
  139.                     1048 => MDB2_ERROR_CONSTRAINT,
  140.                     1049 => MDB2_ERROR_NOSUCHDB,
  141.                     1050 => MDB2_ERROR_ALREADY_EXISTS,
  142.                     1051 => MDB2_ERROR_NOSUCHTABLE,
  143.                     1054 => MDB2_ERROR_NOSUCHFIELD,
  144.                     1061 => MDB2_ERROR_ALREADY_EXISTS,
  145.                     1062 => MDB2_ERROR_ALREADY_EXISTS,
  146.                     1064 => MDB2_ERROR_SYNTAX,
  147.                     1091 => MDB2_ERROR_NOT_FOUND,
  148.                     1100 => MDB2_ERROR_NOT_LOCKED,
  149.                     1136 => MDB2_ERROR_VALUE_COUNT_ON_ROW,
  150.                     1142 => MDB2_ERROR_ACCESS_VIOLATION,
  151.                     1146 => MDB2_ERROR_NOSUCHTABLE,
  152.                     1216 => MDB2_ERROR_CONSTRAINT,
  153.                     1217 => MDB2_ERROR_CONSTRAINT,
  154.                     1356 => MDB2_ERROR_DIVZERO,
  155.                     1451 => MDB2_ERROR_CONSTRAINT,
  156.                     1452 => MDB2_ERROR_CONSTRAINT,
  157.                 );
  158.             }
  159.             if ($this->options['portability'MDB2_PORTABILITY_ERRORS{
  160.                 $ecode_map[1022= MDB2_ERROR_CONSTRAINT;
  161.                 $ecode_map[1048= MDB2_ERROR_CONSTRAINT_NOT_NULL;
  162.                 $ecode_map[1062= MDB2_ERROR_CONSTRAINT;
  163.             else {
  164.                 // Doing this in case mode changes during runtime.
  165.                 $ecode_map[1022= MDB2_ERROR_ALREADY_EXISTS;
  166.                 $ecode_map[1048= MDB2_ERROR_CONSTRAINT;
  167.                 $ecode_map[1062= MDB2_ERROR_ALREADY_EXISTS;
  168.             }
  169.             if (isset($ecode_map[$native_code])) {
  170.                 $error $ecode_map[$native_code];
  171.             }
  172.         }
  173.         return array($error$native_code$native_msg);
  174.     }
  175.  
  176.     // }}}
  177.     // {{{ escape()
  178.  
  179.     /**
  180.      * Quotes a string so it can be safely used in a query. It will quote
  181.      * the text so it can safely be used within a query.
  182.      *
  183.      * @param   string  the input string to quote
  184.      * @param   bool    escape wildcards
  185.      *
  186.      * @return  string  quoted string
  187.      *
  188.      * @access  public
  189.      */
  190.     function escape($text$escape_wildcards = false)
  191.     {
  192.         if ($escape_wildcards{
  193.             $text $this->escapePattern($text);
  194.         }
  195.         $connection $this->getConnection();
  196.         if (PEAR::isError($connection)) {
  197.             return $connection;
  198.         }
  199.         $text @mysql_real_escape_string($text$connection);
  200.         return $text;
  201.     }
  202.  
  203.     // }}}
  204.     // {{{
  205.  
  206.     /**
  207.      * Start a transaction or set a savepoint.
  208.      *
  209.      * @param   string  name of a savepoint to set
  210.      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
  211.      *
  212.      * @access  public
  213.      */
  214.     function beginTransaction($savepoint = null)
  215.     {
  216.         $this->debug('Starting transaction/savepoint'__FUNCTION__array('is_manip' => true'savepoint' => $savepoint));
  217.         $this->_getServerCapabilities();
  218.         if (!is_null($savepoint)) {
  219.             if (!$this->supports('savepoints')) {
  220.                 return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  221.                     'savepoints are not supported'__FUNCTION__);
  222.             }
  223.             if (!$this->in_transaction{
  224.                 return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  225.                     'savepoint cannot be released when changes are auto committed'__FUNCTION__);
  226.             }
  227.             $query 'SAVEPOINT '.$savepoint;
  228.             return $this->_doQuery($querytrue);
  229.         elseif ($this->in_transaction{
  230.             return MDB2_OK;  //nothing to do
  231.         }
  232.         if (!$this->destructor_registered && $this->opened_persistent{
  233.             $this->destructor_registered = true;
  234.             register_shutdown_function('MDB2_closeOpenTransactions');
  235.         }
  236.         $query $this->start_transaction ? 'START TRANSACTION' 'SET AUTOCOMMIT = 1';
  237.         $result =$this->_doQuery($querytrue);
  238.         if (PEAR::isError($result)) {
  239.             return $result;
  240.         }
  241.         $this->in_transaction = true;
  242.         return MDB2_OK;
  243.     }
  244.  
  245.     // }}}
  246.     // {{{ commit()
  247.  
  248.     /**
  249.      * Commit the database changes done during a transaction that is in
  250.      * progress or release a savepoint. This function may only be called when
  251.      * auto-committing is disabled, otherwise it will fail. Therefore, a new
  252.      * transaction is implicitly started after committing the pending changes.
  253.      *
  254.      * @param   string  name of a savepoint to release
  255.      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
  256.      *
  257.      * @access  public
  258.      */
  259.     function commit($savepoint = null)
  260.     {
  261.         $this->debug('Committing transaction/savepoint'__FUNCTION__array('is_manip' => true'savepoint' => $savepoint));
  262.         if (!$this->in_transaction{
  263.             return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  264.                 'commit/release savepoint cannot be done changes are auto committed'__FUNCTION__);
  265.         }
  266.         if (!is_null($savepoint)) {
  267.             if (!$this->supports('savepoints')) {
  268.                 return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  269.                     'savepoints are not supported'__FUNCTION__);
  270.             }
  271.             $server_info $this->getServerVersion();
  272.             if (version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch']'5.0.3''<')) {
  273.                 return MDB2_OK;
  274.             }
  275.             $query 'RELEASE SAVEPOINT '.$savepoint;
  276.             return $this->_doQuery($querytrue);
  277.         }
  278.  
  279.         if (!$this->supports('transactions')) {
  280.             return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  281.                 'transactions are not supported'__FUNCTION__);
  282.         }
  283.  
  284.         $result =$this->_doQuery('COMMIT'true);
  285.         if (PEAR::isError($result)) {
  286.             return $result;
  287.         }
  288.         if (!$this->start_transaction{
  289.             $query 'SET AUTOCOMMIT = 0';
  290.             $result =$this->_doQuery($querytrue);
  291.             if (PEAR::isError($result)) {
  292.                 return $result;
  293.             }
  294.         }
  295.         $this->in_transaction = false;
  296.         return MDB2_OK;
  297.     }
  298.  
  299.     // }}}
  300.     // {{{ rollback()
  301.  
  302.     /**
  303.      * Cancel any database changes done during a transaction or since a specific
  304.      * savepoint that is in progress. This function may only be called when
  305.      * auto-committing is disabled, otherwise it will fail. Therefore, a new
  306.      * transaction is implicitly started after canceling the pending changes.
  307.      *
  308.      * @param   string  name of a savepoint to rollback to
  309.      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
  310.      *
  311.      * @access  public
  312.      */
  313.     function rollback($savepoint = null)
  314.     {
  315.         $this->debug('Rolling back transaction/savepoint'__FUNCTION__array('is_manip' => true'savepoint' => $savepoint));
  316.         if (!$this->in_transaction{
  317.             return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  318.                 'rollback cannot be done changes are auto committed'__FUNCTION__);
  319.         }
  320.         if (!is_null($savepoint)) {
  321.             if (!$this->supports('savepoints')) {
  322.                 return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  323.                     'savepoints are not supported'__FUNCTION__);
  324.             }
  325.             $query 'ROLLBACK TO SAVEPOINT '.$savepoint;
  326.             return $this->_doQuery($querytrue);
  327.         }
  328.  
  329.         $query 'ROLLBACK';
  330.         $result =$this->_doQuery($querytrue);
  331.         if (PEAR::isError($result)) {
  332.             return $result;
  333.         }
  334.         if (!$this->start_transaction{
  335.             $query 'SET AUTOCOMMIT = 0';
  336.             $result =$this->_doQuery($querytrue);
  337.             if (PEAR::isError($result)) {
  338.                 return $result;
  339.             }
  340.         }
  341.         $this->in_transaction = false;
  342.         return MDB2_OK;
  343.     }
  344.  
  345.     // }}}
  346.     // {{{ function setTransactionIsolation()
  347.  
  348.     /**
  349.      * Set the transacton isolation level.
  350.      *
  351.      * @param   string  standard isolation level
  352.      *                   READ UNCOMMITTED (allows dirty reads)
  353.      *                   READ COMMITTED (prevents dirty reads)
  354.      *                   REPEATABLE READ (prevents nonrepeatable reads)
  355.      *                   SERIALIZABLE (prevents phantom reads)
  356.      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
  357.      *
  358.      * @access  public
  359.      * @since   2.1.1
  360.      */
  361.     function setTransactionIsolation($isolation)
  362.     {
  363.         $this->debug('Setting transaction isolation level'__FUNCTION__array('is_manip' => true));
  364.         if (!$this->supports('transactions')) {
  365.             return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  366.                 'transactions are not supported'__FUNCTION__);
  367.         }
  368.         switch ($isolation{
  369.         case 'READ UNCOMMITTED':
  370.         case 'READ COMMITTED':
  371.         case 'REPEATABLE READ':
  372.         case 'SERIALIZABLE':
  373.             break;
  374.         default:
  375.             return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  376.                 'isolation level is not supported: '.$isolation__FUNCTION__);
  377.         }
  378.  
  379.         $query = "SET SESSION TRANSACTION ISOLATION LEVEL $isolation";
  380.         return $this->_doQuery($querytrue);
  381.     }
  382.  
  383.     // }}}
  384.     // {{{ connect()
  385.  
  386.     /**
  387.      * Connect to the database
  388.      *
  389.      * @return true on success, MDB2 Error Object on failure
  390.      */
  391.     function connect()
  392.     {
  393.         if (is_resource($this->connection)) {
  394.             if (count(array_diff($this->connected_dsn$this->dsn)) == 0
  395.                 && $this->opened_persistent == $this->options['persistent']
  396.                 && $this->connected_database_name == $this->database_name
  397.             {
  398.                 return MDB2_OK;
  399.             }
  400.             $this->disconnect(false);
  401.         }
  402.  
  403.         if (!PEAR::loadExtension($this->phptype)) {
  404.             return $this->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  405.                 'extension '.$this->phptype.' is not compiled into PHP'__FUNCTION__);
  406.         }
  407.  
  408.         $params = array();
  409.         if ($this->dsn['protocol'&& $this->dsn['protocol'== 'unix'{
  410.             $params[0':' $this->dsn['socket'];
  411.         else {
  412.             $params[0$this->dsn['hostspec'$this->dsn['hostspec']
  413.                          : 'localhost';
  414.             if ($this->dsn['port']{
  415.                 $params[0].= ':' $this->dsn['port'];
  416.             }
  417.         }
  418.         $params[$this->dsn['username'$this->dsn['username': null;
  419.         $params[$this->dsn['password'$this->dsn['password': null;
  420.         if (!$this->options['persistent']{
  421.             if (isset($this->dsn['new_link'])
  422.                 && ($this->dsn['new_link'== 'true' || $this->dsn['new_link'=== true)
  423.             {
  424.                 $params[= true;
  425.             else {
  426.                 $params[= false;
  427.             }
  428.         }
  429.         if (version_compare(phpversion()'4.3.0''>=')) {
  430.             $params[= isset($this->dsn['client_flags'])
  431.                 ? $this->dsn['client_flags': null;
  432.         }
  433.         $connect_function $this->options['persistent''mysql_pconnect' 'mysql_connect';
  434.  
  435.         $connection @call_user_func_array($connect_function$params);
  436.         if (!$connection{
  437.             if (($err @mysql_error()) != ''{
  438.                 return $this->raiseError(MDB2_ERROR_CONNECT_FAILEDnullnull,
  439.                     $err__FUNCTION__);
  440.             else {
  441.                 return $this->raiseError(MDB2_ERROR_CONNECT_FAILEDnullnull,
  442.                     'unable to establish a connection'__FUNCTION__);
  443.             }
  444.         }
  445.  
  446.         if (!empty($this->dsn['charset'])) {
  447.             $result $this->setCharset($this->dsn['charset']$connection);
  448.             if (PEAR::isError($result)) {
  449.                 return $result;
  450.             }
  451.         }
  452.  
  453.         $this->connection $connection;
  454.         $this->connected_dsn $this->dsn;
  455.         $this->connected_database_name '';
  456.         $this->opened_persistent $this->options['persistent'];
  457.         $this->dbsyntax $this->dsn['dbsyntax'$this->dsn['dbsyntax'$this->phptype;
  458.  
  459.         if ($this->database_name{
  460.             if ($this->database_name != $this->connected_database_name{
  461.                 if (!@mysql_select_db($this->database_name$connection)) {
  462.                     $err $this->raiseError(nullnullnull,
  463.                         'Could not select the database: '.$this->database_name__FUNCTION__);
  464.                     return $err;
  465.                 }
  466.                 $this->connected_database_name $this->database_name;
  467.             }
  468.         }
  469.  
  470.         $this->supported['transactions'$this->options['use_transactions'];
  471.         if ($this->options['default_table_type']{
  472.             switch (strtoupper($this->options['default_table_type'])) {
  473.             case 'BLACKHOLE':
  474.             case 'MEMORY':
  475.             case 'ARCHIVE':
  476.             case 'CSV':
  477.             case 'HEAP':
  478.             case 'ISAM':
  479.             case 'MERGE':
  480.             case 'MRG_ISAM':
  481.             case 'ISAM':
  482.             case 'MRG_MYISAM':
  483.             case 'MYISAM':
  484.                 $this->supported['transactions'= false;
  485.                 $this->warnings[$this->options['default_table_type'.
  486.                     ' is not a supported default table type';
  487.                 break;
  488.             }
  489.         }
  490.         
  491.         $this->_getServerCapabilities();
  492.  
  493.         return MDB2_OK;
  494.     }
  495.  
  496.     // }}}
  497.     // {{{ setCharset()
  498.  
  499.     /**
  500.      * Set the charset on the current connection
  501.      *
  502.      * @param string    charset
  503.      * @param resource  connection handle
  504.      *
  505.      * @return true on success, MDB2 Error Object on failure
  506.      */
  507.     function setCharset($charset$connection = null)
  508.     {
  509.         if (is_null($connection)) {
  510.             $connection $this->getConnection();
  511.             if (PEAR::isError($connection)) {
  512.                 return $connection;
  513.             }
  514.         }
  515.         $query "SET NAMES '".mysql_real_escape_string($charset$connection)."'";
  516.         return $this->_doQuery($querytrue$connection);
  517.     }
  518.  
  519.     // }}}
  520.     // {{{ disconnect()
  521.  
  522.     /**
  523.      * Log out and disconnect from the database.
  524.      *
  525.      * @param  boolean $force if the disconnect should be forced even if the
  526.      *                         connection is opened persistently
  527.      * @return mixed true on success, false if not connected and error
  528.      *                 object on error
  529.      * @access public
  530.      */
  531.     function disconnect($force = true)
  532.     {
  533.         if (is_resource($this->connection)) {
  534.             if ($this->in_transaction{
  535.                 $dsn $this->dsn;
  536.                 $database_name $this->database_name;
  537.                 $persistent $this->options['persistent'];
  538.                 $this->dsn $this->connected_dsn;
  539.                 $this->database_name $this->connected_database_name;
  540.                 $this->options['persistent'$this->opened_persistent;
  541.                 $this->rollback();
  542.                 $this->dsn $dsn;
  543.                 $this->database_name $database_name;
  544.                 $this->options['persistent'$persistent;
  545.             }
  546.  
  547.             if (!$this->opened_persistent || $force{
  548.                 @mysql_close($this->connection);
  549.             }
  550.         }
  551.         return parent::disconnect($force);
  552.     }
  553.  
  554.     // }}}
  555.     // {{{ _doQuery()
  556.  
  557.     /**
  558.      * Execute a query
  559.      * @param string $query  query
  560.      * @param boolean $is_manip  if the query is a manipulation query
  561.      * @param resource $connection 
  562.      * @param string $database_name 
  563.      * @return result or error object
  564.      * @access protected
  565.      */
  566.     function &_doQuery($query$is_manip = false$connection = null$database_name = null)
  567.     {
  568.         $this->last_query $query;
  569.         $result $this->debug($query'query'array('is_manip' => $is_manip'when' => 'pre'));
  570.         if ($result{
  571.             if (PEAR::isError($result)) {
  572.                 return $result;
  573.             }
  574.             $query $result;
  575.         }
  576.         if ($this->options['disable_query']{
  577.             $result $is_manip ? 0 : null;
  578.             return $result;
  579.         }
  580.  
  581.         if (is_null($connection)) {
  582.             $connection $this->getConnection();
  583.             if (PEAR::isError($connection)) {
  584.                 return $connection;
  585.             }
  586.         }
  587.         if (is_null($database_name)) {
  588.             $database_name $this->database_name;
  589.         }
  590.  
  591.         if ($database_name{
  592.             if ($database_name != $this->connected_database_name{
  593.                 if (!@mysql_select_db($database_name$connection)) {
  594.                     $err $this->raiseError(nullnullnull,
  595.                         'Could not select the database: '.$database_name__FUNCTION__);
  596.                     return $err;
  597.                 }
  598.                 $this->connected_database_name $database_name;
  599.             }
  600.         }
  601.  
  602.         $function $this->options['result_buffering']
  603.             ? 'mysql_query' 'mysql_unbuffered_query';
  604.         $result @$function($query$connection);
  605.         if (!$result{
  606.             $err =$this->raiseError(nullnullnull,
  607.                 'Could not execute statement'__FUNCTION__);
  608.             return $err;
  609.         }
  610.  
  611.         $this->debug($query'query'array('is_manip' => $is_manip'when' => 'post''result' => $result));
  612.         return $result;
  613.     }
  614.  
  615.     // }}}
  616.     // {{{ _affectedRows()
  617.  
  618.     /**
  619.      * Returns the number of rows affected
  620.      *
  621.      * @param resource $result 
  622.      * @param resource $connection 
  623.      * @return mixed MDB2 Error Object or the number of rows affected
  624.      * @access private
  625.      */
  626.     function _affectedRows($connection$result = null)
  627.     {
  628.         if (is_null($connection)) {
  629.             $connection $this->getConnection();
  630.             if (PEAR::isError($connection)) {
  631.                 return $connection;
  632.             }
  633.         }
  634.         return @mysql_affected_rows($connection);
  635.     }
  636.  
  637.     // }}}
  638.     // {{{ _modifyQuery()
  639.  
  640.     /**
  641.      * Changes a query string for various DBMS specific reasons
  642.      *
  643.      * @param string $query  query to modify
  644.      * @param boolean $is_manip  if it is a DML query
  645.      * @param integer $limit  limit the number of rows
  646.      * @param integer $offset  start reading from given offset
  647.      * @return string modified query
  648.      * @access protected
  649.      */
  650.     function _modifyQuery($query$is_manip$limit$offset)
  651.     {
  652.         if ($this->options['portability'MDB2_PORTABILITY_DELETE_COUNT{
  653.             // "DELETE FROM table" gives 0 affected rows in MySQL.
  654.             // This little hack lets you know how many rows were deleted.
  655.             if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i'$query)) {
  656.                 $query preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/',
  657.                                       'DELETE FROM \1 WHERE 1=1'$query);
  658.             }
  659.         }
  660.         if ($limit > 0
  661.             && !preg_match('/LIMIT\s*\d(?:\s*(?:,|OFFSET)\s*\d+)?(?:[^\)]*)?$/i'$query)
  662.         {
  663.             $query rtrim($query);
  664.             if (substr($query-1== ';'{
  665.                 $query substr($query0-1);
  666.             }
  667.  
  668.             // LIMIT doesn't always come last in the query
  669.             // @see http://dev.mysql.com/doc/refman/5.0/en/select.html
  670.             $after '';
  671.             if (preg_match('/(\s+INTO\s+(?:OUT|DUMP)FILE\s.*)$/ims'$query$matches)) {
  672.                 $after $matches[0];
  673.                 $query preg_replace('/(\s+INTO\s+(?:OUT|DUMP)FILE\s.*)$/ims'''$query);
  674.             elseif (preg_match('/(\s+FOR\s+UPDATE\s*)$/i'$query$matches)) {
  675.                $after $matches[0];
  676.                $query preg_replace('/(\s+FOR\s+UPDATE\s*)$/im'''$query);
  677.             elseif (preg_match('/(\s+LOCK\s+IN\s+SHARE\s+MODE\s*)$/im'$query$matches)) {
  678.                $after $matches[0];
  679.                $query preg_replace('/(\s+LOCK\s+IN\s+SHARE\s+MODE\s*)$/im'''$query);
  680.             }
  681.  
  682.             if ($is_manip{
  683.                 return $query . " LIMIT $limit" . $after;
  684.             else {
  685.                 return $query . " LIMIT $offset$limit" . $after;
  686.             }
  687.         }
  688.         return $query;
  689.     }
  690.  
  691.     // }}}
  692.     // {{{ getServerVersion()
  693.  
  694.     /**
  695.      * return version information about the server
  696.      *
  697.      * @param bool   $native  determines if the raw version string should be returned
  698.      * @return mixed array/string with version information or MDB2 error object
  699.      * @access public
  700.      */
  701.     function getServerVersion($native = false)
  702.     {
  703.         $connection $this->getConnection();
  704.         if (PEAR::isError($connection)) {
  705.             return $connection;
  706.         }
  707.         if ($this->connected_server_info{
  708.             $server_info $this->connected_server_info;
  709.         else {
  710.             $server_info @mysql_get_server_info($connection);
  711.         }
  712.         if (!$server_info{
  713.             return $this->raiseError(nullnullnull,
  714.                 'Could not get server information'__FUNCTION__);
  715.         }
  716.         // cache server_info
  717.         $this->connected_server_info $server_info;
  718.         if (!$native{
  719.             $tmp explode('.'$server_info3);
  720.             if (isset($tmp[2]&& strpos($tmp[2]'-')) {
  721.                 $tmp2 explode('-'@$tmp[2]2);
  722.             else {
  723.                 $tmp2[0= isset($tmp[2]$tmp[2: null;
  724.                 $tmp2[1= null;
  725.             }
  726.             $server_info = array(
  727.                 'major' => isset($tmp[0]$tmp[0: null,
  728.                 'minor' => isset($tmp[1]$tmp[1: null,
  729.                 'patch' => $tmp2[0],
  730.                 'extra' => $tmp2[1],
  731.                 'native' => $server_info,
  732.             );
  733.         }
  734.         return $server_info;
  735.     }
  736.  
  737.     // }}}
  738.     // {{{ _getServerCapabilities()
  739.  
  740.     /**
  741.      * Fetch some information about the server capabilities
  742.      * (transactions, subselects, prepared statements, etc).
  743.      *
  744.      * @access private
  745.      */
  746.     function _getServerCapabilities()
  747.     {
  748.         static $already_checked = false;
  749.         if (!$already_checked{
  750.             $already_checked = true;
  751.  
  752.             //set defaults
  753.             $this->supported['sub_selects''emulated';
  754.             $this->supported['prepared_statements''emulated';
  755.             $this->start_transaction = false;
  756.             $this->varchar_max_length = 255;
  757.             
  758.             $server_info $this->getServerVersion();
  759.             if (is_array($server_info)) {
  760.                 if (!version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch']'4.1.0''<')) {
  761.                     $this->supported['sub_selects'= true;
  762.                     $this->supported['prepared_statements'= true;
  763.                 }
  764.  
  765.                 if (!version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch']'4.0.14''<')
  766.                     || !version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch']'4.1.1''<')
  767.                 {
  768.                     $this->supported['savepoints'= true;
  769.                 }
  770.  
  771.                 if (!version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch']'4.0.11''<')) {
  772.                     $this->start_transaction = true;
  773.                 }
  774.  
  775.                 if (!version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch']'5.0.3''<')) {
  776.                     $this->varchar_max_length = 65532;
  777.                 }
  778.             }
  779.         }
  780.     }
  781.  
  782.     // }}}
  783.     // {{{ function _skipUserDefinedVariable($query, $position)
  784.  
  785.     /**
  786. /**
  787.      * Utility method, used by prepare() to avoid misinterpreting MySQL user
  788.      * defined variables (SELECT @x:=5) for placeholders.
  789.      * Check if the placeholder is a false positive, i.e. if it is an user defined
  790.      * variable instead. If so, skip it and advance the position, otherwise
  791.      * return the current position, which is valid
  792.      *
  793.      * @param string $query 
  794.      * @param integer $position current string cursor position
  795.      * @return integer $new_position
  796.      * @access protected
  797.      */
  798.     function _skipUserDefinedVariable($query$position)
  799.     {
  800.         $found = strpos(strrev(substr($query0$position))'@');
  801.         if ($found === false{
  802.             return $position;
  803.         }
  804.         $pos strlen($querystrlen(substr($query$position)) $found - 1;
  805.         $substring substr($query$pos$position $pos + 2);
  806.         if (preg_match('/^@\w+:=$/'$substring)) {
  807.             return $position + 1; //found an user defined variable: skip it
  808.         }
  809.         return $position;
  810.     }
  811.  
  812.     // }}}
  813.     // {{{ prepare()
  814.  
  815.     /**
  816.      * Prepares a query for multiple execution with execute().
  817.      * With some database backends, this is emulated.
  818.      * prepare() requires a generic query as string like
  819.      * 'INSERT INTO numbers VALUES(?,?)' or
  820.      * 'INSERT INTO numbers VALUES(:foo,:bar)'.
  821.      * The ? and :[a-zA-Z] and  are placeholders which can be set using
  822.      * bindParam() and the query can be send off using the execute() method.
  823.      *
  824.      * @param string $query the query to prepare
  825.      * @param mixed   $types  array that contains the types of the placeholders
  826.      * @param mixed   $result_types  array that contains the types of the columns in
  827.      *                         the result set or MDB2_PREPARE_RESULT, if set to
  828.      *                         MDB2_PREPARE_MANIP the query is handled as a manipulation query
  829.      * @param mixed   $lobs   key (field) value (parameter) pair for all lob placeholders
  830.      * @return mixed resource handle for the prepared query on success, a MDB2
  831.      *         error on failure
  832.      * @access public
  833.      * @see bindParam, execute
  834.      */
  835.     function &prepare($query$types = null$result_types = null$lobs = array())
  836.     {
  837.         if ($this->options['emulate_prepared']
  838.             || $this->supported['prepared_statements'!== true
  839.         {
  840.             $obj =parent::prepare($query$types$result_types$lobs);
  841.             return $obj;
  842.         }
  843.         $is_manip ($result_types === MDB2_PREPARE_MANIP);
  844.         $offset $this->offset;
  845.         $limit $this->limit;
  846.         $this->offset $this->limit = 0;
  847.         $query $this->_modifyQuery($query$is_manip$limit$offset);
  848.         $result $this->debug($query__FUNCTION__array('is_manip' => $is_manip'when' => 'pre'));
  849.         if ($result{
  850.             if (PEAR::isError($result)) {
  851.                 return $result;
  852.             }
  853.             $query $result;
  854.         }
  855.         $placeholder_type_guess $placeholder_type = null;
  856.         $question '?';
  857.         $colon ':';
  858.         $positions = array();
  859.         $position = 0;
  860.         while ($position strlen($query)) {
  861.             $q_position strpos($query$question$position);
  862.             $c_position strpos($query$colon$position);
  863.             if ($q_position && $c_position{
  864.                 $p_position min($q_position$c_position);
  865.             elseif ($q_position{
  866.                 $p_position $q_position;
  867.             elseif ($c_position{
  868.                 $p_position $c_position;
  869.             else {
  870.                 break;
  871.             }
  872.             if (is_null($placeholder_type)) {
  873.                 $placeholder_type_guess $query[$p_position];
  874.             }
  875.             
  876.             $new_pos $this->_skipDelimitedStrings($query$position$p_position);
  877.             if (PEAR::isError($new_pos)) {
  878.                 return $new_pos;
  879.             }
  880.             if ($new_pos != $position{
  881.                 $position $new_pos;
  882.                 continue; //evaluate again starting from the new position
  883.             }
  884.             
  885.             if ($query[$position== $placeholder_type_guess{
  886.                 if (is_null($placeholder_type)) {
  887.                     $placeholder_type $query[$p_position];
  888.                     $question $colon $placeholder_type;
  889.                 }
  890.                 if ($placeholder_type == ':'{
  891.                     //make sure this is not part of an user defined variable
  892.                     $new_pos $this->_skipUserDefinedVariable($query$position);
  893.                     if ($new_pos != $position{
  894.                         $position $new_pos;
  895.                         continue; //evaluate again starting from the new position
  896.                     }
  897.                     $parameter preg_replace('/^.{'.($position+1).'}([a-z0-9_]+).*$/si''\\1'$query);
  898.                     if ($parameter === ''{
  899.                         $err =$this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  900.                             'named parameter with an empty name'__FUNCTION__);
  901.                         return $err;
  902.                     }
  903.                     $positions[$p_position$parameter;
  904.                     $query substr_replace($query'?'$positionstrlen($parameter)+1);
  905.                 else {
  906.                     $positions[$p_positioncount($positions);
  907.                 }
  908.                 $position $p_position + 1;
  909.             else {
  910.                 $position $p_position;
  911.             }
  912.         }
  913.         $connection $this->getConnection();
  914.         if (PEAR::isError($connection)) {
  915.             return $connection;
  916.         }
  917.         $statement_name sprintf($this->options['statement_format']$this->phptypemd5(time(rand()));
  918.         $query = "PREPARE $statement_name FROM ".$this->quote($query'text');
  919.         $statement =$this->_doQuery($querytrue$connection);
  920.         if (PEAR::isError($statement)) {
  921.             return $statement;
  922.         }
  923.  
  924.         $class_name 'MDB2_Statement_'.$this->phptype;
  925.         $obj =new $class_name($this$statement_name$positions$query$types$result_types$is_manip$limit$offset);
  926.         $this->debug($query__FUNCTION__array('is_manip' => $is_manip'when' => 'post''result' => $obj));
  927.         return $obj;
  928.     }
  929.  
  930.     // }}}
  931.     // {{{ replace()
  932.  
  933.     /**
  934.      * Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT
  935.      * query, except that if there is already a row in the table with the same
  936.      * key field values, the REPLACE query just updates its values instead of
  937.      * inserting a new row.
  938.      *
  939.      * The REPLACE type of query does not make part of the SQL standards. Since
  940.      * practically only MySQL implements it natively, this type of query is
  941.      * emulated through this method for other DBMS using standard types of
  942.      * queries inside a transaction to assure the atomicity of the operation.
  943.      *
  944.      * @access public
  945.      *
  946.      * @param string $table name of the table on which the REPLACE query will
  947.      *   be executed.
  948.      * @param array $fields associative array that describes the fields and the
  949.      *   values that will be inserted or updated in the specified table. The
  950.      *   indexes of the array are the names of all the fields of the table. The
  951.      *   values of the array are also associative arrays that describe the
  952.      *   values and other properties of the table fields.
  953.      *
  954.      *   Here follows a list of field properties that need to be specified:
  955.      *
  956.      *     value:
  957.      *           Value to be assigned to the specified field. This value may be
  958.      *           of specified in database independent type format as this
  959.      *           function can perform the necessary datatype conversions.
  960.      *
  961.      *     Default:
  962.      *           this property is required unless the Null property
  963.      *           is set to 1.
  964.      *
  965.      *     type
  966.      *           Name of the type of the field. Currently, all types Metabase
  967.      *           are supported except for clob and blob.
  968.      *
  969.      *     Default: no type conversion
  970.      *
  971.      *     null
  972.      *           Boolean property that indicates that the value for this field
  973.      *           should be set to null.
  974.      *
  975.      *           The default value for fields missing in INSERT queries may be
  976.      *           specified the definition of a table. Often, the default value
  977.      *           is already null, but since the REPLACE may be emulated using
  978.      *           an UPDATE query, make sure that all fields of the table are
  979.      *           listed in this function argument array.
  980.      *
  981.      *     Default: 0
  982.      *
  983.      *     key
  984.      *           Boolean property that indicates that this field should be
  985.      *           handled as a primary key or at least as part of the compound
  986.      *           unique index of the table that will determine the row that will
  987.      *           updated if it exists or inserted a new row otherwise.
  988.      *
  989.      *           This function will fail if no key field is specified or if the
  990.      *           value of a key field is set to null because fields that are
  991.      *           part of unique index they may not be null.
  992.      *
  993.      *     Default: 0
  994.      *
  995.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  996.      */
  997.     function replace($table$fields)
  998.     {
  999.         $count count($fields);
  1000.         $query $values '';
  1001.         $keys $colnum = 0;
  1002.         for (reset($fields)$colnum $countnext($fields)$colnum++{
  1003.             $name key($fields);
  1004.             if ($colnum > 0{
  1005.                 $query .= ',';
  1006.                 $values.= ',';
  1007.             }
  1008.             $query.= $name;
  1009.             if (isset($fields[$name]['null']&& $fields[$name]['null']{
  1010.                 $value 'NULL';
  1011.             else {
  1012.                 $type = isset($fields[$name]['type']$fields[$name]['type': null;
  1013.                 $value $this->quote($fields[$name]['value']$type);
  1014.             }
  1015.             $values.= $value;
  1016.             if (isset($fields[$name]['key']&& $fields[$name]['key']{
  1017.                 if ($value === 'NULL'{
  1018.                     return $this->raiseError(MDB2_ERROR_CANNOT_REPLACEnullnull,
  1019.                         'key value '.$name.' may not be NULL'__FUNCTION__);
  1020.                 }
  1021.                 $keys++;
  1022.             }
  1023.         }
  1024.         if ($keys == 0{
  1025.             return $this->raiseError(MDB2_ERROR_CANNOT_REPLACEnullnull,
  1026.                 'not specified which fields are keys'__FUNCTION__);
  1027.         }
  1028.  
  1029.         $connection $this->getConnection();
  1030.         if (PEAR::isError($connection)) {
  1031.             return $connection;
  1032.         }
  1033.  
  1034.         $query = "REPLACE INTO $table ($query) VALUES ($values)";
  1035.         $result =$this->_doQuery($querytrue$connection);
  1036.         if (PEAR::isError($result)) {
  1037.             return $result;
  1038.         }
  1039.         return $this->_affectedRows($connection$result);
  1040.     }
  1041.  
  1042.     // }}}
  1043.     // {{{ nextID()
  1044.  
  1045.     /**
  1046.      * Returns the next free id of a sequence
  1047.      *
  1048.      * @param string $seq_name name of the sequence
  1049.      * @param boolean $ondemand when true the sequence is
  1050.      *                           automatic created, if it
  1051.      *                           not exists
  1052.      *
  1053.      * @return mixed MDB2 Error Object or id
  1054.      * @access public
  1055.      */
  1056.     function nextID($seq_name$ondemand = true)
  1057.     {
  1058.         $sequence_name $this->quoteIdentifier($this->getSequenceName($seq_name)true);
  1059.         $seqcol_name $this->quoteIdentifier($this->options['seqcol_name']true);
  1060.         $query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (NULL)";
  1061.         $this->expectError(MDB2_ERROR_NOSUCHTABLE);
  1062.         $result =$this->_doQuery($querytrue);
  1063.         $this->popExpect();
  1064.         if (PEAR::isError($result)) {
  1065.             if ($ondemand && $result->getCode(== MDB2_ERROR_NOSUCHTABLE{
  1066.                 $this->loadModule('Manager'nulltrue);
  1067.                 $result $this->manager->createSequence($seq_name);
  1068.                 if (PEAR::isError($result)) {
  1069.                     return $this->raiseError($resultnullnull,
  1070.                         'on demand sequence '.$seq_name.' could not be created'__FUNCTION__);
  1071.                 else {
  1072.                     return $this->nextID($seq_namefalse);
  1073.                 }
  1074.             }
  1075.             return $result;
  1076.         }
  1077.         $value $this->lastInsertID();
  1078.         if (is_numeric($value)) {
  1079.             $query = "DELETE FROM $sequence_name WHERE $seqcol_name < $value";
  1080.             $result =$this->_doQuery($querytrue);
  1081.             if (PEAR::isError($result)) {
  1082.                 $this->warnings['nextID: could not delete previous sequence table values from '.$seq_name;
  1083.             }
  1084.         }
  1085.         return $value;
  1086.     }
  1087.  
  1088.     // }}}
  1089.     // {{{ lastInsertID()
  1090.  
  1091.     /**
  1092.      * Returns the autoincrement ID if supported or $id or fetches the current
  1093.      * ID in a sequence called: $table.(empty($field) ? '' : '_'.$field)
  1094.      *
  1095.      * @param string $table name of the table into which a new row was inserted
  1096.      * @param string $field name of the field into which a new row was inserted
  1097.      * @return mixed MDB2 Error Object or id
  1098.      * @access public
  1099.      */
  1100.     function lastInsertID($table = null$field = null)
  1101.     {
  1102.         // not using mysql_insert_id() due to http://pear.php.net/bugs/bug.php?id=8051
  1103.         return $this->queryOne('SELECT LAST_INSERT_ID()''integer');
  1104.     }
  1105.  
  1106.     // }}}
  1107.     // {{{ currID()
  1108.  
  1109.     /**
  1110.      * Returns the current id of a sequence
  1111.      *
  1112.      * @param string $seq_name name of the sequence
  1113.      * @return mixed MDB2 Error Object or id
  1114.      * @access public
  1115.      */
  1116.     function currID($seq_name)
  1117.     {
  1118.         $sequence_name $this->quoteIdentifier($this->getSequenceName($seq_name)true);
  1119.         $seqcol_name $this->quoteIdentifier($this->options['seqcol_name']true);
  1120.         $query = "SELECT MAX($seqcol_name) FROM $sequence_name";
  1121.         return $this->queryOne($query'integer');
  1122.     }
  1123. }
  1124.  
  1125. /**
  1126.  * MDB2 MySQL result driver
  1127.  *
  1128.  * @package MDB2
  1129.  * @category Database
  1130.  * @author  Lukas Smith <smith@pooteeweet.org>
  1131.  */
  1132. class MDB2_Result_mysql extends MDB2_Result_Common
  1133. {
  1134.     // }}}
  1135.     // {{{ fetchRow()
  1136.  
  1137.     /**
  1138.      * Fetch a row and insert the data into an existing array.
  1139.      *
  1140.      * @param int       $fetchmode  how the array data should be indexed
  1141.      * @param int    $rownum    number of the row where the data can be found
  1142.      * @return int data array on success, a MDB2 error on failure
  1143.      * @access public
  1144.      */
  1145.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  1146.     {
  1147.         if (!is_null($rownum)) {
  1148.             $seek $this->seek($rownum);
  1149.             if (PEAR::isError($seek)) {
  1150.                 return $seek;
  1151.             }
  1152.         }
  1153.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  1154.             $fetchmode $this->db->fetchmode;
  1155.         }
  1156.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  1157.             $row @mysql_fetch_assoc($this->result);
  1158.             if (is_array($row)
  1159.                 && $this->db->options['portability'MDB2_PORTABILITY_FIX_CASE
  1160.             {
  1161.                 $row array_change_key_case($row$this->db->options['field_case']);
  1162.             }
  1163.         else {
  1164.            $row @mysql_fetch_row($this->result);
  1165.         }
  1166.  
  1167.         if (!$row{
  1168.             if ($this->result === false{
  1169.                 $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1170.                     'resultset has already been freed'__FUNCTION__);
  1171.                 return $err;
  1172.             }
  1173.             $null = null;
  1174.             return $null;
  1175.         }
  1176.         $mode $this->db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL;
  1177.         if ($mode{
  1178.             $this->db->_fixResultArrayValues($row$mode);
  1179.         }
  1180.         if (!empty($this->types)) {
  1181.             $row $this->db->datatype->convertResultRow($this->types$rowfalse);
  1182.         }
  1183.         if (!empty($this->values)) {
  1184.             $this->_assignBindColumns($row);
  1185.         }
  1186.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  1187.             $object_class $this->db->options['fetch_class'];
  1188.             if ($object_class == 'stdClass'{
  1189.                 $row = (object) $row;
  1190.             else {
  1191.                 $row &new $object_class($row);
  1192.             }
  1193.         }
  1194.         ++$this->rownum;
  1195.         return $row;
  1196.     }
  1197.  
  1198.     // }}}
  1199.     // {{{ _getColumnNames()
  1200.  
  1201.     /**
  1202.      * Retrieve the names of columns returned by the DBMS in a query result.
  1203.      *
  1204.      * @return  mixed   Array variable that holds the names of columns as keys
  1205.      *                   or an MDB2 error on failure.
  1206.      *                   Some DBMS may not return any columns when the result set
  1207.      *                   does not contain any rows.
  1208.      * @access private
  1209.      */
  1210.     function _getColumnNames()
  1211.     {
  1212.         $columns = array();
  1213.         $numcols $this->numCols();
  1214.         if (PEAR::isError($numcols)) {
  1215.             return $numcols;
  1216.         }
  1217.         for ($column = 0; $column $numcols$column++{
  1218.             $column_name @mysql_field_name($this->result$column);
  1219.             $columns[$column_name$column;
  1220.         }
  1221.         if ($this->db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  1222.             $columns array_change_key_case($columns$this->db->options['field_case']);
  1223.         }
  1224.         return $columns;
  1225.     }
  1226.  
  1227.     // }}}
  1228.     // {{{ numCols()
  1229.  
  1230.     /**
  1231.      * Count the number of columns returned by the DBMS in a query result.
  1232.      *
  1233.      * @return mixed integer value with the number of columns, a MDB2 error
  1234.      *                        on failure
  1235.      * @access public
  1236.      */
  1237.     function numCols()
  1238.     {
  1239.         $cols @mysql_num_fields($this->result);
  1240.         if (is_null($cols)) {
  1241.             if ($this->result === false{
  1242.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1243.                     'resultset has already been freed'__FUNCTION__);
  1244.             elseif (is_null($this->result)) {
  1245.                 return count($this->types);
  1246.             }
  1247.             return $this->db->raiseError(nullnullnull,
  1248.                 'Could not get column count'__FUNCTION__);
  1249.         }
  1250.         return $cols;
  1251.     }
  1252.  
  1253.     // }}}
  1254.     // {{{ free()
  1255.  
  1256.     /**
  1257.      * Free the internal resources associated with result.
  1258.      *
  1259.      * @return boolean true on success, false if result is invalid
  1260.      * @access public
  1261.      */
  1262.     function free()
  1263.     {
  1264.         if (is_resource($this->result&& $this->db->connection{
  1265.             $free @mysql_free_result($this->result);
  1266.             if ($free === false{
  1267.                 return $this->db->raiseError(nullnullnull,
  1268.                     'Could not free result'__FUNCTION__);
  1269.             }
  1270.         }
  1271.         $this->result = false;
  1272.         return MDB2_OK;
  1273.     }
  1274. }
  1275.  
  1276. /**
  1277.  * MDB2 MySQL buffered result driver
  1278.  *
  1279.  * @package MDB2
  1280.  * @category Database
  1281.  * @author  Lukas Smith <smith@pooteeweet.org>
  1282.  */
  1283. {
  1284.     // }}}
  1285.     // {{{ seek()
  1286.  
  1287.     /**
  1288.      * Seek to a specific row in a result set
  1289.      *
  1290.      * @param int    $rownum    number of the row where the data can be found
  1291.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1292.      * @access public
  1293.      */
  1294.     function seek($rownum = 0)
  1295.     {
  1296.         if ($this->rownum != ($rownum - 1&& !@mysql_data_seek($this->result$rownum)) {
  1297.             if ($this->result === false{
  1298.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1299.                     'resultset has already been freed'__FUNCTION__);
  1300.             elseif (is_null($this->result)) {
  1301.                 return MDB2_OK;
  1302.             }
  1303.             return $this->db->raiseError(MDB2_ERROR_INVALIDnullnull,
  1304.                 'tried to seek to an invalid row number ('.$rownum.')'__FUNCTION__);
  1305.         }
  1306.         $this->rownum $rownum - 1;
  1307.         return MDB2_OK;
  1308.     }
  1309.  
  1310.     // }}}
  1311.     // {{{ valid()
  1312.  
  1313.     /**
  1314.      * Check if the end of the result set has been reached
  1315.      *
  1316.      * @return mixed true or false on sucess, a MDB2 error on failure
  1317.      * @access public
  1318.      */
  1319.     function valid()
  1320.     {
  1321.         $numrows $this->numRows();
  1322.         if (PEAR::isError($numrows)) {
  1323.             return $numrows;
  1324.         }
  1325.         return $this->rownum ($numrows - 1);
  1326.     }
  1327.  
  1328.     // }}}
  1329.     // {{{ numRows()
  1330.  
  1331.     /**
  1332.      * Returns the number of rows in a result object
  1333.      *
  1334.      * @return mixed MDB2 Error Object or the number of rows
  1335.      * @access public
  1336.      */
  1337.     function numRows()
  1338.     {
  1339.         $rows @mysql_num_rows($this->result);
  1340.         if (is_null($rows)) {
  1341.             if ($this->result === false{
  1342.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1343.                     'resultset has already been freed'__FUNCTION__);
  1344.             elseif (is_null($this->result)) {
  1345.                 return 0;
  1346.             }
  1347.             return $this->db->raiseError(nullnullnull,
  1348.                 'Could not get row count'__FUNCTION__);
  1349.         }
  1350.         return $rows;
  1351.     }
  1352. }
  1353.  
  1354. /**
  1355.  * MDB2 MySQL statement driver
  1356.  *
  1357.  * @package MDB2
  1358.  * @category Database
  1359.  * @author  Lukas Smith <smith@pooteeweet.org>
  1360.  */
  1361. class MDB2_Statement_mysql extends MDB2_Statement_Common
  1362. {
  1363.     // {{{ _execute()
  1364.  
  1365.     /**
  1366.      * Execute a prepared query statement helper method.
  1367.      *
  1368.      * @param mixed $result_class string which specifies which result class to use
  1369.      * @param mixed $result_wrap_class string which specifies which class to wrap results in
  1370.      * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
  1371.      * @access private
  1372.      */
  1373.     function &_execute($result_class = true$result_wrap_class = false)
  1374.     {
  1375.         if (is_null($this->statement)) {
  1376.             $result =parent::_execute($result_class$result_wrap_class);
  1377.             return $result;
  1378.         }
  1379.         $this->db->last_query = $this->query;
  1380.         $this->db->debug($this->query'execute'array('is_manip' => $this->is_manip'when' => 'pre''parameters' => $this->values));
  1381.         if ($this->db->getOption('disable_query')) {
  1382.             $result $this->is_manip ? 0 : null;
  1383.             return $result;
  1384.         }
  1385.  
  1386.         $connection $this->db->getConnection();
  1387.         if (PEAR::isError($connection)) {
  1388.             return $connection;
  1389.         }
  1390.  
  1391.         $query 'EXECUTE '.$this->statement;
  1392.         if (!empty($this->positions)) {
  1393.             $parameters = array();
  1394.             foreach ($this->positions as $parameter{
  1395.                 if (!array_key_exists($parameter$this->values)) {
  1396.                     return $this->db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  1397.                         'Unable to bind to missing placeholder: '.$parameter__FUNCTION__);
  1398.                 }
  1399.                 $value $this->values[$parameter];
  1400.                 $type array_key_exists($parameter$this->types$this->types[$parameter: null;
  1401.                 if (is_resource($value|| $type == 'clob' || $type == 'blob'{
  1402.                     if (!is_resource($value&& preg_match('/^(\w+:\/\/)(.*)$/'$value$match)) {
  1403.                         if ($match[1== 'file://'{
  1404.                             $value $match[2];
  1405.                         }
  1406.                         $value @fopen($value'r');
  1407.                         $close = true;
  1408.                     }
  1409.                     if (is_resource($value)) {
  1410.                         $data '';
  1411.                         while (!@feof($value)) {
  1412.                             $data.= @fread($value$this->db->options['lob_buffer_length']);
  1413.                         }
  1414.                         if ($close{
  1415.                             @fclose($value);
  1416.                         }
  1417.                         $value $data;
  1418.                     }
  1419.                 }
  1420.                 $quoted $this->db->quote($value$type);
  1421.                 if (PEAR::isError($quoted)) {
  1422.                     return $quoted;
  1423.                 }
  1424.                 $param_query 'SET @'.$parameter.' = '.$quoted;
  1425.                 $result $this->db->_doQuery($param_querytrue$connection);
  1426.                 if (PEAR::isError($result)) {
  1427.                     return $result;
  1428.                 }
  1429.             }
  1430.             $query.= ' USING @'.implode(', @'array_values($this->positions));
  1431.         }
  1432.  
  1433.         $result $this->db->_doQuery($query$this->is_manip$connection);
  1434.         if (PEAR::isError($result)) {
  1435.             return $result;
  1436.         }
  1437.  
  1438.         if ($this->is_manip{
  1439.             $affected_rows $this->db->_affectedRows($connection$result);
  1440.             return $affected_rows;
  1441.         }
  1442.  
  1443.         $result =$this->db->_wrapResult($result$this->result_types,
  1444.             $result_class$result_wrap_class$this->limit$this->offset);
  1445.         $this->db->debug($this->query'execute'array('is_manip' => $this->is_manip'when' => 'post''result' => $result));
  1446.         return $result;
  1447.     }
  1448.  
  1449.     // }}}
  1450.     // {{{ free()
  1451.  
  1452.     /**
  1453.      * Release resources allocated for the specified prepared query.
  1454.      *
  1455.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1456.      * @access public
  1457.      */
  1458.     function free()
  1459.     {
  1460.         if (is_null($this->positions)) {
  1461.             return $this->db->raiseError(MDB2_ERRORnullnull,
  1462.                 'Prepared statement has already been freed'__FUNCTION__);
  1463.         }
  1464.         $result = MDB2_OK;
  1465.  
  1466.         if (!is_null($this->statement)) {
  1467.             $connection $this->db->getConnection();
  1468.             if (PEAR::isError($connection)) {
  1469.                 return $connection;
  1470.             }
  1471.             $query 'DEALLOCATE PREPARE '.$this->statement;
  1472.             $result $this->db->_doQuery($querytrue$connection);
  1473.         }
  1474.  
  1475.         parent::free();
  1476.         return $result;
  1477.     }
  1478. }
  1479. ?>

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