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

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