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

Source for file mysqli.php

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

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