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

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