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.155 2007/03/10 09:58:01 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_errno();
  126.             $native_msg  @mysqli_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(MDB2_ERROR_CONNECT_FAILED,
  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->supported['sub_selects''emulated';
  478.         $this->supported['prepared_statements''emulated';
  479.         $this->start_transaction = false;
  480.         $this->varchar_max_length = 255;
  481.         
  482.         $this->_getServerCapabilities();
  483.  
  484.         return MDB2_OK;
  485.     }
  486.  
  487.     // }}}
  488.     // {{{ setCharset()
  489.  
  490.     /**
  491.      * Set the charset on the current connection
  492.      *
  493.      * @param string    charset
  494.      * @param resource  connection handle
  495.      *
  496.      * @return true on success, MDB2 Error Object on failure
  497.      */
  498.     function setCharset($charset$connection = null)
  499.     {
  500.         if (is_null($connection)) {
  501.             $connection $this->getConnection();
  502.             if (PEAR::isError($connection)) {
  503.                 return $connection;
  504.             }
  505.         }
  506.         $query "SET NAMES '".mysql_real_escape_string($charset$connection)."'";
  507.         return $this->_doQuery($querytrue$connection);
  508.     }
  509.  
  510.     // }}}
  511.     // {{{ disconnect()
  512.  
  513.     /**
  514.      * Log out and disconnect from the database.
  515.      *
  516.      * @param  boolean $force if the disconnect should be forced even if the
  517.      *                         connection is opened persistently
  518.      * @return mixed true on success, false if not connected and error
  519.      *                 object on error
  520.      * @access public
  521.      */
  522.     function disconnect($force = true)
  523.     {
  524.         if (is_object($this->connection)) {
  525.             if ($this->in_transaction{
  526.                 $dsn $this->dsn;
  527.                 $database_name $this->database_name;
  528.                 $persistent $this->options['persistent'];
  529.                 $this->dsn $this->connected_dsn;
  530.                 $this->database_name $this->connected_database_name;
  531.                 $this->options['persistent'$this->opened_persistent;
  532.                 $this->rollback();
  533.                 $this->dsn $dsn;
  534.                 $this->database_name $database_name;
  535.                 $this->options['persistent'$persistent;
  536.             }
  537.  
  538.             if ($force{
  539.                 @mysqli_close($this->connection);
  540.             }
  541.         }
  542.         return parent::disconnect($force);
  543.     }
  544.  
  545.     // }}}
  546.     // {{{ _doQuery()
  547.  
  548.     /**
  549.      * Execute a query
  550.      * @param string $query  query
  551.      * @param boolean $is_manip  if the query is a manipulation query
  552.      * @param resource $connection 
  553.      * @param string $database_name 
  554.      * @return result or error object
  555.      * @access protected
  556.      */
  557.     function &_doQuery($query$is_manip = false$connection = null$database_name = null)
  558.     {
  559.         $this->last_query $query;
  560.         $result $this->debug($query'query'array('is_manip' => $is_manip'when' => 'pre'));
  561.         if ($result{
  562.             if (PEAR::isError($result)) {
  563.                 return $result;
  564.             }
  565.             $query $result;
  566.         }
  567.         if ($this->options['disable_query']{
  568.             $result $is_manip ? 0 : null;
  569.             return $result;
  570.         }
  571.  
  572.         if (is_null($connection)) {
  573.             $connection $this->getConnection();
  574.             if (PEAR::isError($connection)) {
  575.                 return $connection;
  576.             }
  577.         }
  578.         if (is_null($database_name)) {
  579.             $database_name $this->database_name;
  580.         }
  581.  
  582.         if ($database_name{
  583.             if ($database_name != $this->connected_database_name{
  584.                 if (!@mysqli_select_db($connection$database_name)) {
  585.                     $err $this->raiseError(nullnullnull,
  586.                         'Could not select the database: '.$database_name__FUNCTION__);
  587.                     return $err;
  588.                 }
  589.                 $this->connected_database_name $database_name;
  590.             }
  591.         }
  592.  
  593.         if ($this->options['multi_query']{
  594.             $result mysqli_multi_query($connection$query);
  595.         else {
  596.             $resultmode $this->options['result_buffering'? MYSQLI_USE_RESULT : MYSQLI_USE_RESULT;
  597.             $result mysqli_query($connection$query);
  598.         }
  599.  
  600.         if (!$result{
  601.             $err =$this->raiseError(nullnullnull,
  602.                 'Could not execute statement'__FUNCTION__);
  603.             return $err;
  604.         }
  605.  
  606.         if ($this->options['multi_query']{
  607.             if ($this->options['result_buffering']{
  608.                 if (!($result @mysqli_store_result($connection))) {
  609.                     $err =$this->raiseError(nullnullnull,
  610.                         'Could not get the first result from a multi query'__FUNCTION__);
  611.                     return $err;
  612.                 }
  613.             elseif (!($result @mysqli_use_result($connection))) {
  614.                 $err =$this->raiseError(nullnullnull,
  615.                         'Could not get the first result from a multi query'__FUNCTION__);
  616.                 return $err;
  617.             }
  618.         }
  619.  
  620.         $this->debug($query'query'array('is_manip' => $is_manip'when' => 'post''result' => $result));
  621.         return $result;
  622.     }
  623.  
  624.     // }}}
  625.     // {{{ _affectedRows()
  626.  
  627.     /**
  628.      * Returns the number of rows affected
  629.      *
  630.      * @param resource $result 
  631.      * @param resource $connection 
  632.      * @return mixed MDB2 Error Object or the number of rows affected
  633.      * @access private
  634.      */
  635.     function _affectedRows($connection$result = null)
  636.     {
  637.         if (is_null($connection)) {
  638.             $connection $this->getConnection();
  639.             if (PEAR::isError($connection)) {
  640.                 return $connection;
  641.             }
  642.         }
  643.         return @mysqli_affected_rows($connection);
  644.     }
  645.  
  646.     // }}}
  647.     // {{{ _modifyQuery()
  648.  
  649.     /**
  650.      * Changes a query string for various DBMS specific reasons
  651.      *
  652.      * @param string $query  query to modify
  653.      * @param boolean $is_manip  if it is a DML query
  654.      * @param integer $limit  limit the number of rows
  655.      * @param integer $offset  start reading from given offset
  656.      * @return string modified query
  657.      * @access protected
  658.      */
  659.     function _modifyQuery($query$is_manip$limit$offset)
  660.     {
  661.         if ($this->options['portability'MDB2_PORTABILITY_DELETE_COUNT{
  662.             // "DELETE FROM table" gives 0 affected rows in MySQL.
  663.             // This little hack lets you know how many rows were deleted.
  664.             if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i'$query)) {
  665.                 $query preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/',
  666.                                       'DELETE FROM \1 WHERE 1=1'$query);
  667.             }
  668.         }
  669.         if ($limit > 0
  670.             && !preg_match('/LIMIT\s*\d(\s*(,|OFFSET)\s*\d+)?/i'$query)
  671.         {
  672.             $query rtrim($query);
  673.             if (substr($query-1== ';'{
  674.                 $query substr($query0-1);
  675.             }
  676.             if ($is_manip{
  677.                 return $query . " LIMIT $limit";
  678.             else {
  679.                 return $query . " LIMIT $offset$limit";
  680.             }
  681.         }
  682.         return $query;
  683.     }
  684.  
  685.     // }}}
  686.     // {{{ getServerVersion()
  687.  
  688.     /**
  689.      * return version information about the server
  690.      *
  691.      * @param bool   $native  determines if the raw version string should be returned
  692.      * @return mixed array/string with version information or MDB2 error object
  693.      * @access public
  694.      */
  695.     function getServerVersion($native = false)
  696.     {
  697.         $connection $this->getConnection();
  698.         if (PEAR::isError($connection)) {
  699.             return $connection;
  700.         }
  701.         if ($this->connected_server_info{
  702.             $server_info $this->connected_server_info;
  703.         else {
  704.             $server_info @mysqli_get_server_info($connection);
  705.         }
  706.         if (!$server_info{
  707.             return $this->raiseError(nullnullnull,
  708.                 'Could not get server information'__FUNCTION__);
  709.         }
  710.         // cache server_info
  711.         $this->connected_server_info $server_info;
  712.         if (!$native{
  713.             $tmp explode('.'$server_info3);
  714.             if (isset($tmp[2]&& strpos($tmp[2]'-')) {
  715.                 $tmp2 explode('-'@$tmp[2]2);
  716.             else {
  717.                 $tmp2[0= isset($tmp[2]$tmp[2: null;
  718.                 $tmp2[1= null;
  719.             }
  720.             $server_info = array(
  721.                 'major' => isset($tmp[0]$tmp[0: null,
  722.                 'minor' => isset($tmp[1]$tmp[1: null,
  723.                 'patch' => $tmp2[0],
  724.                 'extra' => $tmp2[1],
  725.                 'native' => $server_info,
  726.             );
  727.         }
  728.         return $server_info;
  729.     }
  730.  
  731.     // }}}
  732.     // {{{ _getServerCapabilities()
  733.  
  734.     /**
  735.      * Fetch some information about the server capabilities
  736.      * (transactions, subselects, prepared statements, etc).
  737.      *
  738.      * @access private
  739.      */
  740.     function _getServerCapabilities()
  741.     {
  742.         static $already_checked = false;
  743.         if (!$already_checked{
  744.             $already_checked = true;
  745.             $server_info $this->getServerVersion();
  746.             if (is_array($server_info)) {
  747.                 if (!version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch']'4.1.0''<')) {
  748.                     $this->supported['sub_selects'= true;
  749.                     $this->supported['prepared_statements'= true;
  750.                 }
  751.  
  752.                 if (!version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch']'4.0.14''<')
  753.                     || !version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch']'4.1.1''<')
  754.                 {
  755.                     $this->supported['savepoints'= true;
  756.                 }
  757.  
  758.                 if (!version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch']'4.0.11''<')) {
  759.                     $this->start_transaction = true;
  760.                 }
  761.  
  762.                 if (!version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch']'5.0.3''<')) {
  763.                     $this->varchar_max_length = 65532;
  764.                 }
  765.             }
  766.         }
  767.     }
  768.  
  769.     // }}}
  770.     // {{{ function _skipUserDefinedVariable($query, $position)
  771.  
  772.     /**
  773. /**
  774.      * Utility method, used by prepare() to avoid misinterpreting MySQL user
  775.      * defined variables (SELECT @x:=5) for placeholders.
  776.      * Check if the placeholder is a false positive, i.e. if it is an user defined
  777.      * variable instead. If so, skip it and advance the position, otherwise
  778.      * return the current position, which is valid
  779.      *
  780.      * @param string $query 
  781.      * @param integer $position current string cursor position
  782.      * @return integer $new_position
  783.      * @access protected
  784.      */
  785.     function _skipUserDefinedVariable($query$position)
  786.     {
  787.         $found = strpos(strrev(substr($query0$position))'@');
  788.         if ($found === false{
  789.             return $position;
  790.         }
  791.         $pos strlen($querystrlen(substr($query$position)) $found - 1;
  792.         $substring substr($query$pos$position $pos + 2);
  793.         if (preg_match('/^@\w+:=$/'$substring)) {
  794.             return $position + 1; //found an user defined variable: skip it
  795.         }
  796.         return $position;
  797.     }
  798.  
  799.     // }}}
  800.     // {{{ prepare()
  801.  
  802.     /**
  803.      * Prepares a query for multiple execution with execute().
  804.      * With some database backends, this is emulated.
  805.      * prepare() requires a generic query as string like
  806.      * 'INSERT INTO numbers VALUES(?,?)' or
  807.      * 'INSERT INTO numbers VALUES(:foo,:bar)'.
  808.      * The ? and :[a-zA-Z] and  are placeholders which can be set using
  809.      * bindParam() and the query can be send off using the execute() method.
  810.      *
  811.      * @param string $query the query to prepare
  812.      * @param mixed   $types  array that contains the types of the placeholders
  813.      * @param mixed   $result_types  array that contains the types of the columns in
  814.      *                         the result set or MDB2_PREPARE_RESULT, if set to
  815.      *                         MDB2_PREPARE_MANIP the query is handled as a manipulation query
  816.      * @param mixed   $lobs   key (field) value (parameter) pair for all lob placeholders
  817.      * @return mixed resource handle for the prepared query on success, a MDB2
  818.      *         error on failure
  819.      * @access public
  820.      * @see bindParam, execute
  821.      */
  822.     function &prepare($query$types = null$result_types = null$lobs = array())
  823.     {
  824.         if ($this->options['emulate_prepared']
  825.             || $this->supported['prepared_statements'!== true
  826.         {
  827.             $obj =parent::prepare($query$types$result_types$lobs);
  828.             return $obj;
  829.         }
  830.         $is_manip ($result_types === MDB2_PREPARE_MANIP);
  831.         $offset $this->offset;
  832.         $limit $this->limit;
  833.         $this->offset $this->limit = 0;
  834.         $query $this->_modifyQuery($query$is_manip$limit$offset);
  835.         $result $this->debug($query__FUNCTION__array('is_manip' => $is_manip'when' => 'pre'));
  836.         if ($result{
  837.             if (PEAR::isError($result)) {
  838.                 return $result;
  839.             }
  840.             $query $result;
  841.         }
  842.         $placeholder_type_guess $placeholder_type = null;
  843.         $question '?';
  844.         $colon ':';
  845.         $positions = array();
  846.         $position = 0;
  847.         while ($position strlen($query)) {
  848.             $q_position strpos($query$question$position);
  849.             $c_position strpos($query$colon$position);
  850.             if ($q_position && $c_position{
  851.                 $p_position min($q_position$c_position);
  852.             elseif ($q_position{
  853.                 $p_position $q_position;
  854.             elseif ($c_position{
  855.                 $p_position $c_position;
  856.             else {
  857.                 break;
  858.             }
  859.             if (is_null($placeholder_type)) {
  860.                 $placeholder_type_guess $query[$p_position];
  861.             }
  862.             
  863.             $new_pos $this->_skipDelimitedStrings($query$position$p_position);
  864.             if (PEAR::isError($new_pos)) {
  865.                 return $new_pos;
  866.             }
  867.             if ($new_pos != $position{
  868.                 $position $new_pos;
  869.                 continue; //evaluate again starting from the new position
  870.             }
  871.             
  872.             if ($query[$position== $placeholder_type_guess{
  873.                 if (is_null($placeholder_type)) {
  874.                     $placeholder_type $query[$p_position];
  875.                     $question $colon $placeholder_type;
  876.                 }
  877.                 if ($placeholder_type == ':'{
  878.                     //make sure this is not part of an user defined variable
  879.                     $new_pos $this->_skipUserDefinedVariable($query$position);
  880.                     if ($new_pos != $position{
  881.                         $position $new_pos;
  882.                         continue; //evaluate again starting from the new position
  883.                     }
  884.                     $parameter preg_replace('/^.{'.($position+1).'}([a-z0-9_]+).*$/si''\\1'$query);
  885.                     if ($parameter === ''{
  886.                         $err =$this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  887.                             'named parameter with an empty name'__FUNCTION__);
  888.                         return $err;
  889.                     }
  890.                     $positions[$p_position$parameter;
  891.                     $query substr_replace($query'?'$positionstrlen($parameter)+1);
  892.                 else {
  893.                     $positions[$p_positioncount($positions);
  894.                 }
  895.                 $position $p_position + 1;
  896.             else {
  897.                 $position $p_position;
  898.             }
  899.         }
  900.         $connection $this->getConnection();
  901.         if (PEAR::isError($connection)) {
  902.             return $connection;
  903.         }
  904.  
  905.         if (!$is_manip{
  906.             $statement_name sprintf($this->options['statement_format']$this->phptypemd5(time(rand()));
  907.             $query = "PREPARE $statement_name FROM ".$this->quote($query'text');
  908.  
  909.             $statement =$this->_doQuery($querytrue$connection);
  910.             if (PEAR::isError($statement)) {
  911.                 return $statement;
  912.             }
  913.             $statement $statement_name;
  914.         else {
  915.             $statement @mysqli_prepare($connection$query);
  916.             if (!$statement{
  917.                 $err =$this->raiseError(nullnullnull,
  918.                     'Unable to create prepared statement handle'__FUNCTION__);
  919.                 return $err;
  920.             }
  921.         }
  922.  
  923.         $class_name 'MDB2_Statement_'.$this->phptype;
  924.         $obj =new $class_name($this$statement$positions$query$types$result_types$is_manip$limit$offset);
  925.         $this->debug($query__FUNCTION__array('is_manip' => $is_manip'when' => 'post''result' => $obj));
  926.         return $obj;
  927.     }
  928.  
  929.     // }}}
  930.     // {{{ replace()
  931.  
  932.     /**
  933.      * Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT
  934.      * query, except that if there is already a row in the table with the same
  935.      * key field values, the REPLACE query just updates its values instead of
  936.      * inserting a new row.
  937.      *
  938.      * The REPLACE type of query does not make part of the SQL standards. Since
  939.      * practically only MySQL implements it natively, this type of query is
  940.      * emulated through this method for other DBMS using standard types of
  941.      * queries inside a transaction to assure the atomicity of the operation.
  942.      *
  943.      * @access public
  944.      *
  945.      * @param string $table name of the table on which the REPLACE query will
  946.      *   be executed.
  947.      * @param array $fields associative array that describes the fields and the
  948.      *   values that will be inserted or updated in the specified table. The
  949.      *   indexes of the array are the names of all the fields of the table. The
  950.      *   values of the array are also associative arrays that describe the
  951.      *   values and other properties of the table fields.
  952.      *
  953.      *   Here follows a list of field properties that need to be specified:
  954.      *
  955.      *     value:
  956.      *           Value to be assigned to the specified field. This value may be
  957.      *           of specified in database independent type format as this
  958.      *           function can perform the necessary datatype conversions.
  959.      *
  960.      *     Default:
  961.      *           this property is required unless the Null property
  962.      *           is set to 1.
  963.      *
  964.      *     type
  965.      *           Name of the type of the field. Currently, all types Metabase
  966.      *           are supported except for clob and blob.
  967.      *
  968.      *     Default: no type conversion
  969.      *
  970.      *     null
  971.      *           Boolean property that indicates that the value for this field
  972.      *           should be set to null.
  973.      *
  974.      *           The default value for fields missing in INSERT queries may be
  975.      *           specified the definition of a table. Often, the default value
  976.      *           is already null, but since the REPLACE may be emulated using
  977.      *           an UPDATE query, make sure that all fields of the table are
  978.      *           listed in this function argument array.
  979.      *
  980.      *     Default: 0
  981.      *
  982.      *     key
  983.      *           Boolean property that indicates that this field should be
  984.      *           handled as a primary key or at least as part of the compound
  985.      *           unique index of the table that will determine the row that will
  986.      *           updated if it exists or inserted a new row otherwise.
  987.      *
  988.      *           This function will fail if no key field is specified or if the
  989.      *           value of a key field is set to null because fields that are
  990.      *           part of unique index they may not be null.
  991.      *
  992.      *     Default: 0
  993.      *
  994.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  995.      */
  996.     function replace($table$fields)
  997.     {
  998.         $count count($fields);
  999.         $query $values '';
  1000.         $keys $colnum = 0;
  1001.         for (reset($fields)$colnum $countnext($fields)$colnum++{
  1002.             $name key($fields);
  1003.             if ($colnum > 0{
  1004.                 $query .= ',';
  1005.                 $values.= ',';
  1006.             }
  1007.             $query.= $name;
  1008.             if (isset($fields[$name]['null']&& $fields[$name]['null']{
  1009.                 $value 'NULL';
  1010.             else {
  1011.                 $type = isset($fields[$name]['type']$fields[$name]['type': null;
  1012.                 $value $this->quote($fields[$name]['value']$type);
  1013.             }
  1014.             $values.= $value;
  1015.             if (isset($fields[$name]['key']&& $fields[$name]['key']{
  1016.                 if ($value === 'NULL'{
  1017.                     return $this->raiseError(MDB2_ERROR_CANNOT_REPLACEnullnull,
  1018.                         'key value '.$name.' may not be NULL'__FUNCTION__);
  1019.                 }
  1020.                 $keys++;
  1021.             }
  1022.         }
  1023.         if ($keys == 0{
  1024.             return $this->raiseError(MDB2_ERROR_CANNOT_REPLACEnullnull,
  1025.                 'not specified which fields are keys'__FUNCTION__);
  1026.         }
  1027.  
  1028.         $connection $this->getConnection();
  1029.         if (PEAR::isError($connection)) {
  1030.             return $connection;
  1031.         }
  1032.  
  1033.         $query = "REPLACE INTO $table ($query) VALUES ($values)";
  1034.         $result =$this->_doQuery($querytrue$connection);
  1035.         if (PEAR::isError($result)) {
  1036.             return $result;
  1037.         }
  1038.         return $this->_affectedRows($connection$result);
  1039.     }
  1040.  
  1041.     // }}}
  1042.     // {{{ nextID()
  1043.  
  1044.     /**
  1045.      * Returns the next free id of a sequence
  1046.      *
  1047.      * @param string $seq_name name of the sequence
  1048.      * @param boolean $ondemand when true the sequence is
  1049.      *                           automatic created, if it
  1050.      *                           not exists
  1051.      *
  1052.      * @return mixed MDB2 Error Object or id
  1053.      * @access public
  1054.      */
  1055.     function nextID($seq_name$ondemand = true)
  1056.     {
  1057.         $sequence_name $this->quoteIdentifier($this->getSequenceName($seq_name)true);
  1058.         $seqcol_name $this->quoteIdentifier($this->options['seqcol_name']true);
  1059.         $query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (NULL)";
  1060.         $this->expectError(MDB2_ERROR_NOSUCHTABLE);
  1061.         $result =$this->_doQuery($querytrue);
  1062.         $this->popExpect();
  1063.         if (PEAR::isError($result)) {
  1064.             if ($ondemand && $result->getCode(== MDB2_ERROR_NOSUCHTABLE{
  1065.                 $this->loadModule('Manager'nulltrue);
  1066.                 $result $this->manager->createSequence($seq_name);
  1067.                 if (PEAR::isError($result)) {
  1068.                     return $this->raiseError($resultnullnull,
  1069.                         'on demand sequence '.$seq_name.' could not be created'__FUNCTION__);
  1070.                 else {
  1071.                     return $this->nextID($seq_namefalse);
  1072.                 }
  1073.             }
  1074.             return $result;
  1075.         }
  1076.         $value $this->lastInsertID();
  1077.         if (is_numeric($value)) {
  1078.             $query = "DELETE FROM $sequence_name WHERE $seqcol_name < $value";
  1079.             $result =$this->_doQuery($querytrue);
  1080.             if (PEAR::isError($result)) {
  1081.                 $this->warnings['nextID: could not delete previous sequence table values from '.$seq_name;
  1082.             }
  1083.         }
  1084.         return $value;
  1085.     }
  1086.  
  1087.     // }}}
  1088.     // {{{ lastInsertID()
  1089.  
  1090.     /**
  1091.      * Returns the autoincrement ID if supported or $id or fetches the current
  1092.      * ID in a sequence called: $table.(empty($field) ? '' : '_'.$field)
  1093.      *
  1094.      * @param string $table name of the table into which a new row was inserted
  1095.      * @param string $field name of the field into which a new row was inserted
  1096.      * @return mixed MDB2 Error Object or id
  1097.      * @access public
  1098.      */
  1099.     function lastInsertID($table = null$field = null)
  1100.     {
  1101.         // not using mysql_insert_id() due to http://pear.php.net/bugs/bug.php?id=8051
  1102.         return $this->queryOne('SELECT LAST_INSERT_ID()');
  1103.     }
  1104.  
  1105.     // }}}
  1106.     // {{{ currID()
  1107.  
  1108.     /**
  1109.      * Returns the current id of a sequence
  1110.      *
  1111.      * @param string $seq_name name of the sequence
  1112.      * @return mixed MDB2 Error Object or id
  1113.      * @access public
  1114.      */
  1115.     function currID($seq_name)
  1116.     {
  1117.         $sequence_name $this->quoteIdentifier($this->getSequenceName($seq_name)true);
  1118.         $seqcol_name $this->quoteIdentifier($this->options['seqcol_name']true);
  1119.         $query = "SELECT MAX($seqcol_name) FROM $sequence_name";
  1120.         return $this->queryOne($query'integer');
  1121.     }
  1122. }
  1123.  
  1124. /**
  1125.  * MDB2 MySQLi result driver
  1126.  *
  1127.  * @package MDB2
  1128.  * @category Database
  1129.  * @author  Lukas Smith <smith@pooteeweet.org>
  1130.  */
  1131. class MDB2_Result_mysqli extends MDB2_Result_Common
  1132. {
  1133.     // }}}
  1134.     // {{{ fetchRow()
  1135.  
  1136.     /**
  1137.      * Fetch a row and insert the data into an existing array.
  1138.      *
  1139.      * @param int       $fetchmode  how the array data should be indexed
  1140.      * @param int    $rownum    number of the row where the data can be found
  1141.      * @return int data array on success, a MDB2 error on failure
  1142.      * @access public
  1143.      */
  1144.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  1145.     {
  1146.         if (!is_null($rownum)) {
  1147.             $seek $this->seek($rownum);
  1148.             if (PEAR::isError($seek)) {
  1149.                 return $seek;
  1150.             }
  1151.         }
  1152.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  1153.             $fetchmode $this->db->fetchmode;
  1154.         }
  1155.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  1156.             $row @mysqli_fetch_assoc($this->result);
  1157.             if (is_array($row)
  1158.                 && $this->db->options['portability'MDB2_PORTABILITY_FIX_CASE
  1159.             {
  1160.                 $row array_change_key_case($row$this->db->options['field_case']);
  1161.             }
  1162.         else {
  1163.            $row @mysqli_fetch_row($this->result);
  1164.         }
  1165.  
  1166.         if (!$row{
  1167.             if ($this->result === false{
  1168.                 $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1169.                     'resultset has already been freed'__FUNCTION__);
  1170.                 return $err;
  1171.             }
  1172.             $null = null;
  1173.             return $null;
  1174.         }
  1175.         $mode $this->db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL;
  1176.         if ($mode{
  1177.             $this->db->_fixResultArrayValues($row$mode);
  1178.         }
  1179.         if (!empty($this->types)) {
  1180.             $row $this->db->datatype->convertResultRow($this->types$rowfalse);
  1181.         }
  1182.         if (!empty($this->values)) {
  1183.             $this->_assignBindColumns($row);
  1184.         }
  1185.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  1186.             $object_class $this->db->options['fetch_class'];
  1187.             if ($object_class == 'stdClass'{
  1188.                 $row = (object) $row;
  1189.             else {
  1190.                 $row &new $object_class($row);
  1191.             }
  1192.         }
  1193.         ++$this->rownum;
  1194.         return $row;
  1195.     }
  1196.  
  1197.     // }}}
  1198.     // {{{ _getColumnNames()
  1199.  
  1200.     /**
  1201.      * Retrieve the names of columns returned by the DBMS in a query result.
  1202.      *
  1203.      * @return  mixed   Array variable that holds the names of columns as keys
  1204.      *                   or an MDB2 error on failure.
  1205.      *                   Some DBMS may not return any columns when the result set
  1206.      *                   does not contain any rows.
  1207.      * @access private
  1208.      */
  1209.     function _getColumnNames()
  1210.     {
  1211.         $columns = array();
  1212.         $numcols $this->numCols();
  1213.         if (PEAR::isError($numcols)) {
  1214.             return $numcols;
  1215.         }
  1216.         for ($column = 0; $column $numcols$column++{
  1217.             $column_info @mysqli_fetch_field_direct($this->result$column);
  1218.             $columns[$column_info->name$column;
  1219.         }
  1220.         if ($this->db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  1221.             $columns array_change_key_case($columns$this->db->options['field_case']);
  1222.         }
  1223.         return $columns;
  1224.     }
  1225.  
  1226.     // }}}
  1227.     // {{{ numCols()
  1228.  
  1229.     /**
  1230.      * Count the number of columns returned by the DBMS in a query result.
  1231.      *
  1232.      * @return mixed integer value with the number of columns, a MDB2 error
  1233.      *                        on failure
  1234.      * @access public
  1235.      */
  1236.     function numCols()
  1237.     {
  1238.         $cols @mysqli_num_fields($this->result);
  1239.         if (is_null($cols)) {
  1240.             if ($this->result === false{
  1241.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1242.                     'resultset has already been freed'__FUNCTION__);
  1243.             elseif (is_null($this->result)) {
  1244.                 return count($this->types);
  1245.             }
  1246.             return $this->db->raiseError(nullnullnull,
  1247.                 'Could not get column count'__FUNCTION__);
  1248.         }
  1249.         return $cols;
  1250.     }
  1251.  
  1252.     // }}}
  1253.     // {{{ nextResult()
  1254.  
  1255.     /**
  1256.      * Move the internal result pointer to the next available result
  1257.      *
  1258.      * @return true on success, false if there is no more result set or an error object on failure
  1259.      * @access public
  1260.      */
  1261.     function nextResult()
  1262.     {
  1263.         $connection $this->db->getConnection();
  1264.         if (PEAR::isError($connection)) {
  1265.             return $connection;
  1266.         }
  1267.  
  1268.         if (!@mysqli_more_results($connection)) {
  1269.             return false;
  1270.         }
  1271.         if (!@mysqli_next_result($connection)) {
  1272.             return false;
  1273.         }
  1274.         if (!($this->result @mysqli_use_result($connection))) {
  1275.             return false;
  1276.         }
  1277.         return MDB2_OK;
  1278.     }
  1279.  
  1280.     // }}}
  1281.     // {{{ free()
  1282.  
  1283.     /**
  1284.      * Free the internal resources associated with result.
  1285.      *
  1286.      * @return boolean true on success, false if result is invalid
  1287.      * @access public
  1288.      */
  1289.     function free()
  1290.     {
  1291.         if (is_object($this->result&& $this->db->connection{
  1292.             $free @mysqli_free_result($this->result);
  1293.             if ($free === false{
  1294.                 return $this->db->raiseError(nullnullnull,
  1295.                     'Could not free result'__FUNCTION__);
  1296.             }
  1297.         }
  1298.         $this->result = false;
  1299.         return MDB2_OK;
  1300.     }
  1301. }
  1302.  
  1303. /**
  1304.  * MDB2 MySQLi buffered result driver
  1305.  *
  1306.  * @package MDB2
  1307.  * @category Database
  1308.  * @author  Lukas Smith <smith@pooteeweet.org>
  1309.  */
  1310. {
  1311.     // }}}
  1312.     // {{{ seek()
  1313.  
  1314.     /**
  1315.      * Seek to a specific row in a result set
  1316.      *
  1317.      * @param int    $rownum    number of the row where the data can be found
  1318.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1319.      * @access public
  1320.      */
  1321.     function seek($rownum = 0)
  1322.     {
  1323.         if ($this->rownum != ($rownum - 1&& !@mysqli_data_seek($this->result$rownum)) {
  1324.             if ($this->result === false{
  1325.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1326.                     'resultset has already been freed'__FUNCTION__);
  1327.             elseif (is_null($this->result)) {
  1328.                 return MDB2_OK;
  1329.             }
  1330.             return $this->db->raiseError(MDB2_ERROR_INVALIDnullnull,
  1331.                 'tried to seek to an invalid row number ('.$rownum.')'__FUNCTION__);
  1332.         }
  1333.         $this->rownum $rownum - 1;
  1334.         return MDB2_OK;
  1335.     }
  1336.  
  1337.     // }}}
  1338.     // {{{ valid()
  1339.  
  1340.     /**
  1341.      * Check if the end of the result set has been reached
  1342.      *
  1343.      * @return mixed true or false on sucess, a MDB2 error on failure
  1344.      * @access public
  1345.      */
  1346.     function valid()
  1347.     {
  1348.         $numrows $this->numRows();
  1349.         if (PEAR::isError($numrows)) {
  1350.             return $numrows;
  1351.         }
  1352.         return $this->rownum ($numrows - 1);
  1353.     }
  1354.  
  1355.     // }}}
  1356.     // {{{ numRows()
  1357.  
  1358.     /**
  1359.      * Returns the number of rows in a result object
  1360.      *
  1361.      * @return mixed MDB2 Error Object or the number of rows
  1362.      * @access public
  1363.      */
  1364.     function numRows()
  1365.     {
  1366.         $rows @mysqli_num_rows($this->result);
  1367.         if (is_null($rows)) {
  1368.             if ($this->result === false{
  1369.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1370.                     'resultset has already been freed'__FUNCTION__);
  1371.             elseif (is_null($this->result)) {
  1372.                 return 0;
  1373.             }
  1374.             return $this->db->raiseError(nullnullnull,
  1375.                 'Could not get row count'__FUNCTION__);
  1376.         }
  1377.         return $rows;
  1378.     }
  1379.  
  1380.     // }}}
  1381.     // {{{ nextResult()
  1382.  
  1383.     /**
  1384.      * Move the internal result pointer to the next available result
  1385.      *
  1386.      * @param valid result resource
  1387.      * @return true on success, false if there is no more result set or an error object on failure
  1388.      * @access public
  1389.      */
  1390.     function nextResult()
  1391.     {
  1392.         $connection $this->db->getConnection();
  1393.         if (PEAR::isError($connection)) {
  1394.             return $connection;
  1395.         }
  1396.  
  1397.         if (!@mysqli_more_results($connection)) {
  1398.             return false;
  1399.         }
  1400.         if (!@mysqli_next_result($connection)) {
  1401.             return false;
  1402.         }
  1403.         if (!($this->result @mysqli_store_result($connection))) {
  1404.             return false;
  1405.         }
  1406.         return MDB2_OK;
  1407.     }
  1408. }
  1409.  
  1410. /**
  1411.  * MDB2 MySQLi statement driver
  1412.  *
  1413.  * @package MDB2
  1414.  * @category Database
  1415.  * @author  Lukas Smith <smith@pooteeweet.org>
  1416.  */
  1417. class MDB2_Statement_mysqli extends MDB2_Statement_Common
  1418. {
  1419.     // {{{ _execute()
  1420.  
  1421.     /**
  1422.      * Execute a prepared query statement helper method.
  1423.      *
  1424.      * @param mixed $result_class string which specifies which result class to use
  1425.      * @param mixed $result_wrap_class string which specifies which class to wrap results in
  1426.      * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
  1427.      * @access private
  1428.      */
  1429.     function &_execute($result_class = true$result_wrap_class = false)
  1430.     {
  1431.         if (is_null($this->statement)) {
  1432.             $result =parent::_execute($result_class$result_wrap_class);
  1433.             return $result;
  1434.         }
  1435.         $this->db->last_query = $this->query;
  1436.         $this->db->debug($this->query'execute'array('is_manip' => $this->is_manip'when' => 'pre''parameters' => $this->values));
  1437.         if ($this->db->getOption('disable_query')) {
  1438.             $result $this->is_manip ? 0 : null;
  1439.             return $result;
  1440.         }
  1441.  
  1442.         $connection $this->db->getConnection();
  1443.         if (PEAR::isError($connection)) {
  1444.             return $connection;
  1445.         }
  1446.  
  1447.         if (!is_object($this->statement)) {
  1448.             $query 'EXECUTE '.$this->statement;
  1449.         }
  1450.         if (!empty($this->positions)) {
  1451.             $parameters = array(0 => $this->statement1 => '');
  1452.             $lobs = array();
  1453.             $i = 0;
  1454.             foreach ($this->positions as $parameter{
  1455.                 if (!array_key_exists($parameter$this->values)) {
  1456.                     return $this->db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  1457.                         'Unable to bind to missing placeholder: '.$parameter__FUNCTION__);
  1458.                 }
  1459.                 $value $this->values[$parameter];
  1460.                 $type array_key_exists($parameter$this->types$this->types[$parameter: null;
  1461.                 if (!is_object($this->statement)) {
  1462.                     if (is_resource($value|| $type == 'clob' || $type == 'blob'{
  1463.                         if (!is_resource($value&& preg_match('/^(\w+:\/\/)(.*)$/'$value$match)) {
  1464.                             if ($match[1== 'file://'{
  1465.                                 $value $match[2];
  1466.                             }
  1467.                             $value @fopen($value'r');
  1468.                             $close = true;
  1469.                         }
  1470.                         if (is_resource($value)) {
  1471.                             $data '';
  1472.                             while (!@feof($value)) {
  1473.                                 $data.= @fread($value$this->db->options['lob_buffer_length']);
  1474.                             }
  1475.                             if ($close{
  1476.                                 @fclose($value);
  1477.                             }
  1478.                             $value $data;
  1479.                         }
  1480.                     }
  1481.                     $quoted $this->db->quote($value$type);
  1482.                     if (PEAR::isError($quoted)) {
  1483.                         return $quoted;
  1484.                     }
  1485.                     $param_query 'SET @'.$parameter.' = '.$quoted;
  1486.                     $result $this->db->_doQuery($param_querytrue$connection);
  1487.                     if (PEAR::isError($result)) {
  1488.                         return $result;
  1489.                     }
  1490.                 else {
  1491.                     if (is_resource($value|| $type == 'clob' || $type == 'blob'{
  1492.                         $parameters[= null;
  1493.                         $parameters[1].= 'b';
  1494.                         $lobs[$i$parameter;
  1495.                     else {
  1496.                         $parameters[$this->db->quote($value$typefalse);
  1497.                         $parameters[1].= $this->db->datatype->mapPrepareDatatype($type);
  1498.                     }
  1499.                     ++$i;
  1500.                 }
  1501.             }
  1502.  
  1503.             if (!is_object($this->statement)) {
  1504.                 $query.= ' USING @'.implode(', @'array_values($this->positions));
  1505.             else {
  1506.                 $result @call_user_func_array('mysqli_stmt_bind_param'$parameters);
  1507.                 if ($result === false{
  1508.                     $err =$this->db->raiseError(nullnullnull,
  1509.                         'Unable to bind parameters'__FUNCTION__);
  1510.                     return $err;
  1511.                 }
  1512.  
  1513.                 foreach ($lobs as $i => $parameter{
  1514.                     $value $this->values[$parameter];
  1515.                     $close = false;
  1516.                     if (!is_resource($value)) {
  1517.                         $close = true;
  1518.                         if (preg_match('/^(\w+:\/\/)(.*)$/'$value$match)) {
  1519.                             if ($match[1== 'file://'{
  1520.                                 $value $match[2];
  1521.                             }
  1522.                             $value @fopen($value'r');
  1523.                         else {
  1524.                             $fp @tmpfile();
  1525.                             @fwrite($fp$value);
  1526.                             @rewind($fp);
  1527.                             $value $fp;
  1528.                         }
  1529.                     }
  1530.                     while (!@feof($value)) {
  1531.                         $data @fread($value$this->db->options['lob_buffer_length']);
  1532.                         @mysqli_stmt_send_long_data($this->statement$i$data);
  1533.                     }
  1534.                     if ($close{
  1535.                         @fclose($value);
  1536.                     }
  1537.                 }
  1538.             }
  1539.         }
  1540.  
  1541.         if (!is_object($this->statement)) {
  1542.             $result $this->db->_doQuery($query$this->is_manip$connection);
  1543.             if (PEAR::isError($result)) {
  1544.                 return $result;
  1545.             }
  1546.  
  1547.             if ($this->is_manip{
  1548.                 $affected_rows $this->db->_affectedRows($connection$result);
  1549.                 return $affected_rows;
  1550.             }
  1551.  
  1552.             $result =$this->db->_wrapResult($result$this->result_types,
  1553.                 $result_class$result_wrap_class$this->limit$this->offset);
  1554.         else {
  1555.             if (!@mysqli_stmt_execute($this->statement)) {
  1556.                 $err =$this->db->raiseError(nullnullnull,
  1557.                     'Unable to execute statement'__FUNCTION__);
  1558.                 return $err;
  1559.             }
  1560.  
  1561.             if ($this->is_manip{
  1562.                 $affected_rows @mysqli_stmt_affected_rows($this->statement);
  1563.                 return $affected_rows;
  1564.             }
  1565.  
  1566.             if ($this->db->options['result_buffering']{
  1567.                 @mysqli_stmt_store_result($this->statement);
  1568.             }
  1569.  
  1570.             $result =$this->db->_wrapResult($this->statement$this->result_types,
  1571.                 $result_class$result_wrap_class$this->limit$this->offset);
  1572.         }
  1573.  
  1574.         $this->db->debug($this->query'execute'array('is_manip' => $this->is_manip'when' => 'post''result' => $result));
  1575.         return $result;
  1576.     }
  1577.  
  1578.     // }}}
  1579.     // {{{ free()
  1580.  
  1581.     /**
  1582.      * Release resources allocated for the specified prepared query.
  1583.      *
  1584.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1585.      * @access public
  1586.      */
  1587.     function free()
  1588.     {
  1589.         if (is_null($this->positions)) {
  1590.             return $this->db->raiseError(MDB2_ERRORnullnull,
  1591.                 'Prepared statement has already been freed'__FUNCTION__);
  1592.         }
  1593.         $result = MDB2_OK;
  1594.  
  1595.         if (is_object($this->statement)) {
  1596.             if (!@mysqli_stmt_close($this->statement)) {
  1597.                 $result $this->db->raiseError(nullnullnull,
  1598.                     'Could not free statement'__FUNCTION__);
  1599.             }
  1600.         elseif (!is_null($this->statement)) {
  1601.             $connection $this->db->getConnection();
  1602.             if (PEAR::isError($connection)) {
  1603.                 return $connection;
  1604.             }
  1605.  
  1606.             $query 'DEALLOCATE PREPARE '.$this->statement;
  1607.             $result $this->db->_doQuery($querytrue$connection);
  1608.         }
  1609.  
  1610.         parent::free();
  1611.         return $result;
  1612.    }
  1613. }
  1614. ?>

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