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

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