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.133 2006/08/20 19:38:26 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 = 65535;
  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.                     elseif (isset($positions[$parameter])) {
  857.                         $err =$this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  858.                             'named parameter names can only be used once per statement'__FUNCTION__);
  859.                         return $err;
  860.                     }
  861.                     $positions[$parameter$p_position;
  862.                     $query substr_replace($query'?'$positionstrlen($parameter)+1);
  863.                 else {
  864.                     $positions[$p_position;
  865.                 }
  866.                 $position $p_position + 1;
  867.             else {
  868.                 $position $p_position;
  869.             }
  870.         }
  871.         $connection $this->getConnection();
  872.         if (PEAR::isError($connection)) {
  873.             return $connection;
  874.         }
  875.  
  876.         if (!$is_manip{
  877.             $statement_name 'MDB2_Statement_'.$this->phptype.'_'.md5(time(rand());
  878.             $query = "PREPARE $statement_name FROM '$query'";
  879.  
  880.             $statement =$this->_doQuery($querytrue$connection);
  881.             if (PEAR::isError($statement)) {
  882.                 return $statement;
  883.             }
  884.             $statement $statement_name;
  885.         else {
  886.             $statement @mysqli_prepare($connection$query);
  887.             if (!$statement{
  888.                 $err =$this->raiseError(nullnullnull,
  889.                     'Unable to create prepared statement handle'__FUNCTION__);
  890.                 return $err;
  891.             }
  892.         }
  893.  
  894.         $class_name 'MDB2_Statement_'.$this->phptype;
  895.         $obj =new $class_name($this$statement$positions$query$types$result_types$is_manip$limit$offset);
  896.         $this->debug($query__FUNCTION__array('is_manip' => $is_manip'when' => 'post''result' => $obj));
  897.         return $obj;
  898.     }
  899.  
  900.     // }}}
  901.     // {{{ replace()
  902.  
  903.     /**
  904.      * Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT
  905.      * query, except that if there is already a row in the table with the same
  906.      * key field values, the REPLACE query just updates its values instead of
  907.      * inserting a new row.
  908.      *
  909.      * The REPLACE type of query does not make part of the SQL standards. Since
  910.      * practically only MySQL implements it natively, this type of query is
  911.      * emulated through this method for other DBMS using standard types of
  912.      * queries inside a transaction to assure the atomicity of the operation.
  913.      *
  914.      * @access public
  915.      *
  916.      * @param string $table name of the table on which the REPLACE query will
  917.      *   be executed.
  918.      * @param array $fields associative array that describes the fields and the
  919.      *   values that will be inserted or updated in the specified table. The
  920.      *   indexes of the array are the names of all the fields of the table. The
  921.      *   values of the array are also associative arrays that describe the
  922.      *   values and other properties of the table fields.
  923.      *
  924.      *   Here follows a list of field properties that need to be specified:
  925.      *
  926.      *     value:
  927.      *           Value to be assigned to the specified field. This value may be
  928.      *           of specified in database independent type format as this
  929.      *           function can perform the necessary datatype conversions.
  930.      *
  931.      *     Default:
  932.      *           this property is required unless the Null property
  933.      *           is set to 1.
  934.      *
  935.      *     type
  936.      *           Name of the type of the field. Currently, all types Metabase
  937.      *           are supported except for clob and blob.
  938.      *
  939.      *     Default: no type conversion
  940.      *
  941.      *     null
  942.      *           Boolean property that indicates that the value for this field
  943.      *           should be set to null.
  944.      *
  945.      *           The default value for fields missing in INSERT queries may be
  946.      *           specified the definition of a table. Often, the default value
  947.      *           is already null, but since the REPLACE may be emulated using
  948.      *           an UPDATE query, make sure that all fields of the table are
  949.      *           listed in this function argument array.
  950.      *
  951.      *     Default: 0
  952.      *
  953.      *     key
  954.      *           Boolean property that indicates that this field should be
  955.      *           handled as a primary key or at least as part of the compound
  956.      *           unique index of the table that will determine the row that will
  957.      *           updated if it exists or inserted a new row otherwise.
  958.      *
  959.      *           This function will fail if no key field is specified or if the
  960.      *           value of a key field is set to null because fields that are
  961.      *           part of unique index they may not be null.
  962.      *
  963.      *     Default: 0
  964.      *
  965.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  966.      */
  967.     function replace($table$fields)
  968.     {
  969.         $count count($fields);
  970.         $query $values '';
  971.         $keys $colnum = 0;
  972.         for (reset($fields)$colnum $countnext($fields)$colnum++{
  973.             $name key($fields);
  974.             if ($colnum > 0{
  975.                 $query .= ',';
  976.                 $values.= ',';
  977.             }
  978.             $query.= $name;
  979.             if (isset($fields[$name]['null']&& $fields[$name]['null']{
  980.                 $value 'NULL';
  981.             else {
  982.                 $type = isset($fields[$name]['type']$fields[$name]['type': null;
  983.                 $value $this->quote($fields[$name]['value']$type);
  984.             }
  985.             $values.= $value;
  986.             if (isset($fields[$name]['key']&& $fields[$name]['key']{
  987.                 if ($value === 'NULL'{
  988.                     return $this->raiseError(MDB2_ERROR_CANNOT_REPLACEnullnull,
  989.                         'key value '.$name.' may not be NULL'__FUNCTION__);
  990.                 }
  991.                 $keys++;
  992.             }
  993.         }
  994.         if ($keys == 0{
  995.             return $this->raiseError(MDB2_ERROR_CANNOT_REPLACEnullnull,
  996.                 'not specified which fields are keys'__FUNCTION__);
  997.         }
  998.  
  999.         $connection $this->getConnection();
  1000.         if (PEAR::isError($connection)) {
  1001.             return $connection;
  1002.         }
  1003.  
  1004.         $query = "REPLACE INTO $table ($query) VALUES ($values)";
  1005.         $result =$this->_doQuery($querytrue$connection);
  1006.         if (PEAR::isError($result)) {
  1007.             return $result;
  1008.         }
  1009.         return $this->_affectedRows($connection$result);
  1010.     }
  1011.  
  1012.     // }}}
  1013.     // {{{ nextID()
  1014.  
  1015.     /**
  1016.      * Returns the next free id of a sequence
  1017.      *
  1018.      * @param string $seq_name name of the sequence
  1019.      * @param boolean $ondemand when true the sequence is
  1020.      *                           automatic created, if it
  1021.      *                           not exists
  1022.      *
  1023.      * @return mixed MDB2 Error Object or id
  1024.      * @access public
  1025.      */
  1026.     function nextID($seq_name$ondemand = true)
  1027.     {
  1028.         $sequence_name $this->quoteIdentifier($this->getSequenceName($seq_name)true);
  1029.         $seqcol_name $this->quoteIdentifier($this->options['seqcol_name']true);
  1030.         $query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (NULL)";
  1031.         $this->expectError(MDB2_ERROR_NOSUCHTABLE);
  1032.         $result =$this->_doQuery($querytrue);
  1033.         $this->popExpect();
  1034.         if (PEAR::isError($result)) {
  1035.             if ($ondemand && $result->getCode(== MDB2_ERROR_NOSUCHTABLE{
  1036.                 $this->loadModule('Manager'nulltrue);
  1037.                 // Since we are creating the sequence on demand
  1038.                 // we know the first id = 1 so initialize the
  1039.                 // sequence at 2
  1040.                 $result $this->manager->createSequence($seq_name2);
  1041.                 if (PEAR::isError($result)) {
  1042.                     return $this->raiseError($resultnullnull,
  1043.                         'on demand sequence '.$seq_name.' could not be created'__FUNCTION__);
  1044.                 else {
  1045.                     // First ID of a newly created sequence is 1
  1046.                     return 1;
  1047.                 }
  1048.             }
  1049.             return $result;
  1050.         }
  1051.         $value $this->lastInsertID();
  1052.         if (is_numeric($value)) {
  1053.             $query = "DELETE FROM $sequence_name WHERE $seqcol_name < $value";
  1054.             $result =$this->_doQuery($querytrue);
  1055.             if (PEAR::isError($result)) {
  1056.                 $this->warnings['nextID: could not delete previous sequence table values from '.$seq_name;
  1057.             }
  1058.         }
  1059.         return $value;
  1060.     }
  1061.  
  1062.     // }}}
  1063.     // {{{ lastInsertID()
  1064.  
  1065.     /**
  1066.      * Returns the autoincrement ID if supported or $id or fetches the current
  1067.      * ID in a sequence called: $table.(empty($field) ? '' : '_'.$field)
  1068.      *
  1069.      * @param string $table name of the table into which a new row was inserted
  1070.      * @param string $field name of the field into which a new row was inserted
  1071.      * @return mixed MDB2 Error Object or id
  1072.      * @access public
  1073.      */
  1074.     function lastInsertID($table = null$field = null)
  1075.     {
  1076.         $connection $this->getConnection();
  1077.         if (PEAR::isError($connection)) {
  1078.             return $connection;
  1079.         }
  1080.         $value @mysqli_insert_id($connection);
  1081.         if (!$value{
  1082.             return $this->raiseError(nullnullnull,
  1083.                 'Could not get last insert ID'__FUNCTION__);
  1084.         }
  1085.         return $value;
  1086.     }
  1087.  
  1088.     // }}}
  1089.     // {{{ currID()
  1090.  
  1091.     /**
  1092.      * Returns the current id of a sequence
  1093.      *
  1094.      * @param string $seq_name name of the sequence
  1095.      * @return mixed MDB2 Error Object or id
  1096.      * @access public
  1097.      */
  1098.     function currID($seq_name)
  1099.     {
  1100.         $sequence_name $this->quoteIdentifier($this->getSequenceName($seq_name)true);
  1101.         $seqcol_name $this->quoteIdentifier($this->options['seqcol_name']true);
  1102.         $query = "SELECT MAX($seqcol_name) FROM $sequence_name";
  1103.         return $this->queryOne($query'integer');
  1104.     }
  1105. }
  1106.  
  1107. /**
  1108.  * MDB2 MySQLi result driver
  1109.  *
  1110.  * @package MDB2
  1111.  * @category Database
  1112.  * @author  Lukas Smith <smith@pooteeweet.org>
  1113.  */
  1114. class MDB2_Result_mysqli extends MDB2_Result_Common
  1115. {
  1116.     // }}}
  1117.     // {{{ fetchRow()
  1118.  
  1119.     /**
  1120.      * Fetch a row and insert the data into an existing array.
  1121.      *
  1122.      * @param int       $fetchmode  how the array data should be indexed
  1123.      * @param int    $rownum    number of the row where the data can be found
  1124.      * @return int data array on success, a MDB2 error on failure
  1125.      * @access public
  1126.      */
  1127.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  1128.     {
  1129.         if (!is_null($rownum)) {
  1130.             $seek $this->seek($rownum);
  1131.             if (PEAR::isError($seek)) {
  1132.                 return $seek;
  1133.             }
  1134.         }
  1135.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  1136.             $fetchmode $this->db->fetchmode;
  1137.         }
  1138.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  1139.             $row @mysqli_fetch_assoc($this->result);
  1140.             if (is_array($row)
  1141.                 && $this->db->options['portability'MDB2_PORTABILITY_FIX_CASE
  1142.             {
  1143.                 $row array_change_key_case($row$this->db->options['field_case']);
  1144.             }
  1145.         else {
  1146.            $row @mysqli_fetch_row($this->result);
  1147.         }
  1148.  
  1149.         if (!$row{
  1150.             if ($this->result === false{
  1151.                 $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1152.                     'resultset has already been freed'__FUNCTION__);
  1153.                 return $err;
  1154.             }
  1155.             $null = null;
  1156.             return $null;
  1157.         }
  1158.         $mode $this->db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL;
  1159.         if ($mode{
  1160.             $this->db->_fixResultArrayValues($row$mode);
  1161.         }
  1162.         if (!empty($this->types)) {
  1163.             $row $this->db->datatype->convertResultRow($this->types$rowfalse);
  1164.         }
  1165.         if (!empty($this->values)) {
  1166.             $this->_assignBindColumns($row);
  1167.         }
  1168.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  1169.             $object_class $this->db->options['fetch_class'];
  1170.             if ($object_class == 'stdClass'{
  1171.                 $row = (object) $row;
  1172.             else {
  1173.                 $row &new $object_class($row);
  1174.             }
  1175.         }
  1176.         ++$this->rownum;
  1177.         return $row;
  1178.     }
  1179.  
  1180.     // }}}
  1181.     // {{{ _getColumnNames()
  1182.  
  1183.     /**
  1184.      * Retrieve the names of columns returned by the DBMS in a query result.
  1185.      *
  1186.      * @return  mixed   Array variable that holds the names of columns as keys
  1187.      *                   or an MDB2 error on failure.
  1188.      *                   Some DBMS may not return any columns when the result set
  1189.      *                   does not contain any rows.
  1190.      * @access private
  1191.      */
  1192.     function _getColumnNames()
  1193.     {
  1194.         $columns = array();
  1195.         $numcols $this->numCols();
  1196.         if (PEAR::isError($numcols)) {
  1197.             return $numcols;
  1198.         }
  1199.         for ($column = 0; $column $numcols$column++{
  1200.             $column_info @mysqli_fetch_field_direct($this->result$column);
  1201.             $columns[$column_info->name$column;
  1202.         }
  1203.         if ($this->db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  1204.             $columns array_change_key_case($columns$this->db->options['field_case']);
  1205.         }
  1206.         return $columns;
  1207.     }
  1208.  
  1209.     // }}}
  1210.     // {{{ numCols()
  1211.  
  1212.     /**
  1213.      * Count the number of columns returned by the DBMS in a query result.
  1214.      *
  1215.      * @return mixed integer value with the number of columns, a MDB2 error
  1216.      *                        on failure
  1217.      * @access public
  1218.      */
  1219.     function numCols()
  1220.     {
  1221.         $cols @mysqli_num_fields($this->result);
  1222.         if (is_null($cols)) {
  1223.             if ($this->result === false{
  1224.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1225.                     'resultset has already been freed'__FUNCTION__);
  1226.             elseif (is_null($this->result)) {
  1227.                 return count($this->types);
  1228.             }
  1229.             return $this->db->raiseError(nullnullnull,
  1230.                 'Could not get column count'__FUNCTION__);
  1231.         }
  1232.         return $cols;
  1233.     }
  1234.  
  1235.     // }}}
  1236.     // {{{ nextResult()
  1237.  
  1238.     /**
  1239.      * Move the internal result pointer to the next available result
  1240.      *
  1241.      * @param valid result resource
  1242.      * @return true on success, false if there is no more result set or an error object on failure
  1243.      * @access public
  1244.      */
  1245.     function nextResult()
  1246.     {
  1247.         $connection $this->db->getConnection();
  1248.         if (PEAR::isError($connection)) {
  1249.             return $connection;
  1250.         }
  1251.  
  1252.         if (!@mysqli_more_results($connection)) {
  1253.             return false;
  1254.         }
  1255.         if (!@mysqli_next_result($connection)) {
  1256.             return false;
  1257.         }
  1258.         if (!($this->result @mysqli_use_result($connection))) {
  1259.             return false;
  1260.         }
  1261.         return MDB2_OK;
  1262.     }
  1263.  
  1264.     // }}}
  1265.     // {{{ free()
  1266.  
  1267.     /**
  1268.      * Free the internal resources associated with result.
  1269.      *
  1270.      * @return boolean true on success, false if result is invalid
  1271.      * @access public
  1272.      */
  1273.     function free()
  1274.     {
  1275.         if (is_object($this->result&& $this->db->connection{
  1276.             $free @mysqli_free_result($this->result);
  1277.             if ($free === false{
  1278.                 return $this->db->raiseError(nullnullnull,
  1279.                     'Could not free result'__FUNCTION__);
  1280.             }
  1281.         }
  1282.         $this->result = false;
  1283.         return MDB2_OK;
  1284.     }
  1285. }
  1286.  
  1287. /**
  1288.  * MDB2 MySQLi buffered result driver
  1289.  *
  1290.  * @package MDB2
  1291.  * @category Database
  1292.  * @author  Lukas Smith <smith@pooteeweet.org>
  1293.  */
  1294. {
  1295.     // }}}
  1296.     // {{{ seek()
  1297.  
  1298.     /**
  1299.      * Seek to a specific row in a result set
  1300.      *
  1301.      * @param int    $rownum    number of the row where the data can be found
  1302.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1303.      * @access public
  1304.      */
  1305.     function seek($rownum = 0)
  1306.     {
  1307.         if ($this->rownum != ($rownum - 1&& !@mysqli_data_seek($this->result$rownum)) {
  1308.             if ($this->result === false{
  1309.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1310.                     'resultset has already been freed'__FUNCTION__);
  1311.             elseif (is_null($this->result)) {
  1312.                 return MDB2_OK;
  1313.             }
  1314.             return $this->db->raiseError(MDB2_ERROR_INVALIDnullnull,
  1315.                 'tried to seek to an invalid row number ('.$rownum.')'__FUNCTION__);
  1316.         }
  1317.         $this->rownum $rownum - 1;
  1318.         return MDB2_OK;
  1319.     }
  1320.  
  1321.     // }}}
  1322.     // {{{ valid()
  1323.  
  1324.     /**
  1325.      * Check if the end of the result set has been reached
  1326.      *
  1327.      * @return mixed true or false on sucess, a MDB2 error on failure
  1328.      * @access public
  1329.      */
  1330.     function valid()
  1331.     {
  1332.         $numrows $this->numRows();
  1333.         if (PEAR::isError($numrows)) {
  1334.             return $numrows;
  1335.         }
  1336.         return $this->rownum ($numrows - 1);
  1337.     }
  1338.  
  1339.     // }}}
  1340.     // {{{ numRows()
  1341.  
  1342.     /**
  1343.      * Returns the number of rows in a result object
  1344.      *
  1345.      * @return mixed MDB2 Error Object or the number of rows
  1346.      * @access public
  1347.      */
  1348.     function numRows()
  1349.     {
  1350.         $rows @mysqli_num_rows($this->result);
  1351.         if (is_null($rows)) {
  1352.             if ($this->result === false{
  1353.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1354.                     'resultset has already been freed'__FUNCTION__);
  1355.             elseif (is_null($this->result)) {
  1356.                 return 0;
  1357.             }
  1358.             return $this->db->raiseError(nullnullnull,
  1359.                 'Could not get row count'__FUNCTION__);
  1360.         }
  1361.         return $rows;
  1362.     }
  1363.  
  1364.     // }}}
  1365.     // {{{ nextResult()
  1366.  
  1367.     /**
  1368.      * Move the internal result pointer to the next available result
  1369.      *
  1370.      * @param valid result resource
  1371.      * @return true on success, false if there is no more result set or an error object on failure
  1372.      * @access public
  1373.      */
  1374.     function nextResult()
  1375.     {
  1376.         $connection $this->db->getConnection();
  1377.         if (PEAR::isError($connection)) {
  1378.             return $connection;
  1379.         }
  1380.  
  1381.         if (!@mysqli_more_results($connection)) {
  1382.             return false;
  1383.         }
  1384.         if (!@mysqli_next_result($connection)) {
  1385.             return false;
  1386.         }
  1387.         if (!($this->result @mysqli_store_result($connection))) {
  1388.             return false;
  1389.         }
  1390.         return MDB2_OK;
  1391.     }
  1392. }
  1393.  
  1394. /**
  1395.  * MDB2 MySQLi statement driver
  1396.  *
  1397.  * @package MDB2
  1398.  * @category Database
  1399.  * @author  Lukas Smith <smith@pooteeweet.org>
  1400.  */
  1401. class MDB2_Statement_mysqli extends MDB2_Statement_Common
  1402. {
  1403.     // {{{ _execute()
  1404.  
  1405.     /**
  1406.      * Execute a prepared query statement helper method.
  1407.      *
  1408.      * @param mixed $result_class string which specifies which result class to use
  1409.      * @param mixed $result_wrap_class string which specifies which class to wrap results in
  1410.      * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
  1411.      * @access private
  1412.      */
  1413.     function &_execute($result_class = true$result_wrap_class = false)
  1414.     {
  1415.         if (is_null($this->statement)) {
  1416.             $result =parent::_execute($result_class$result_wrap_class);
  1417.             return $result;
  1418.         }
  1419.         $this->db->last_query = $this->query;
  1420.         $this->db->debug($this->query'execute'array('is_manip' => $this->is_manip'when' => 'pre''parameters' => $this->values));
  1421.         if ($this->db->getOption('disable_query')) {
  1422.             $result $this->is_manip ? 0 : null;
  1423.             return $result;
  1424.         }
  1425.  
  1426.         $connection $this->db->getConnection();
  1427.         if (PEAR::isError($connection)) {
  1428.             return $connection;
  1429.         }
  1430.  
  1431.         if (!is_object($this->statement)) {
  1432.             $query 'EXECUTE '.$this->statement;
  1433.         }
  1434.         if (!empty($this->positions)) {
  1435.             $parameters = array(0 => $this->statement1 => '');
  1436.             $lobs = array();
  1437.             $i = 0;
  1438.             foreach ($this->positions as $parameter => $foo{
  1439.                 if (!array_key_exists($parameter$this->values)) {
  1440.                     return $this->db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  1441.                         'Unable to bind to missing placeholder: '.$parameter__FUNCTION__);
  1442.                 }
  1443.                 $value $this->values[$parameter];
  1444.                 $type array_key_exists($parameter$this->types$this->types[$parameter: null;
  1445.                 if (!is_object($this->statement)) {
  1446.                     if (is_resource($value|| $type == 'clob' || $type == 'blob'{
  1447.                         if (!is_resource($value&& preg_match('/^(\w+:\/\/)(.*)$/'$value$match)) {
  1448.                             if ($match[1== 'file://'{
  1449.                                 $value $match[2];
  1450.                             }
  1451.                             $value @fopen($value'r');
  1452.                             $close = true;
  1453.                         }
  1454.                         if (is_resource($value)) {
  1455.                             $data '';
  1456.                             while (!@feof($value)) {
  1457.                                 $data.= @fread($value$this->db->options['lob_buffer_length']);
  1458.                             }
  1459.                             if ($close{
  1460.                                 @fclose($value);
  1461.                             }
  1462.                             $value $data;
  1463.                         }
  1464.                     }
  1465.                     $param_query 'SET @'.$parameter.' = '.$this->db->quote($value$type);
  1466.                     $result $this->db->_doQuery($param_querytrue$connection);
  1467.                     if (PEAR::isError($result)) {
  1468.                         return $result;
  1469.                     }
  1470.                 else {
  1471.                     if (is_resource($value|| $type == 'clob' || $type == 'blob'{
  1472.                         $parameters[= null;
  1473.                         $parameters[1].= 'b';
  1474.                         $lobs[$i$parameter;
  1475.                     else {
  1476.                         $parameters[$this->db->quote($value$typefalse);
  1477.                         $parameters[1].= $this->db->datatype->mapPrepareDatatype($type);
  1478.                     }
  1479.                     ++$i;
  1480.                 }
  1481.             }
  1482.  
  1483.             if (!is_object($this->statement)) {
  1484.                 $query.= ' USING @'.implode(', @'array_keys($this->positions));
  1485.             else {
  1486.                 $result @call_user_func_array('mysqli_stmt_bind_param'$parameters);
  1487.                 if ($result === false{
  1488.                     $err =$this->db->raiseError(nullnullnull,
  1489.                         'Unable to bind parameters'__FUNCTION__);
  1490.                     return $err;
  1491.                 }
  1492.  
  1493.                 foreach ($lobs as $i => $parameter{
  1494.                     $value $this->values[$parameter];
  1495.                     $close = false;
  1496.                     if (!is_resource($value)) {
  1497.                         $close = true;
  1498.                         if (preg_match('/^(\w+:\/\/)(.*)$/'$value$match)) {
  1499.                             if ($match[1== 'file://'{
  1500.                                 $value $match[2];
  1501.                             }
  1502.                             $value @fopen($value'r');
  1503.                         else {
  1504.                             $fp @tmpfile();
  1505.                             @fwrite($fp$value);
  1506.                             @rewind($fp);
  1507.                             $value $fp;
  1508.                         }
  1509.                     }
  1510.                     while (!@feof($value)) {
  1511.                         $data @fread($value$this->db->options['lob_buffer_length']);
  1512.                         @mysqli_stmt_send_long_data($this->statement$i$data);
  1513.                     }
  1514.                     if ($close{
  1515.                         @fclose($value);
  1516.                     }
  1517.                 }
  1518.             }
  1519.         }
  1520.  
  1521.         if (!is_object($this->statement)) {
  1522.             $result $this->db->_doQuery($query$this->is_manip$connection);
  1523.             if (PEAR::isError($result)) {
  1524.                 return $result;
  1525.             }
  1526.  
  1527.             if ($this->is_manip{
  1528.                 $affected_rows $this->db->_affectedRows($connection$result);
  1529.                 return $affected_rows;
  1530.             }
  1531.  
  1532.             $result =$this->db->_wrapResult($result$this->result_types,
  1533.                 $result_class$result_wrap_class$this->limit$this->offset);
  1534.         else {
  1535.             if (!@mysqli_stmt_execute($this->statement)) {
  1536.                 $err =$this->db->raiseError(nullnullnull,
  1537.                     'Unable to execute statement'__FUNCTION__);
  1538.                 return $err;
  1539.             }
  1540.  
  1541.             if ($this->is_manip{
  1542.                 $affected_rows @mysqli_stmt_affected_rows($this->statement);
  1543.                 return $affected_rows;
  1544.             }
  1545.  
  1546.             if ($this->db->options['result_buffering']{
  1547.                 @mysqli_stmt_store_result($this->statement);
  1548.             }
  1549.  
  1550.             $result =$this->db->_wrapResult($this->statement$this->result_types,
  1551.                 $result_class$result_wrap_class$this->limit$this->offset);
  1552.         }
  1553.  
  1554.         $this->db->debug($this->query'execute'array('is_manip' => $this->is_manip'when' => 'post''result' => $result));
  1555.         return $result;
  1556.     }
  1557.  
  1558.     // }}}
  1559.     // {{{ free()
  1560.  
  1561.     /**
  1562.      * Release resources allocated for the specified prepared query.
  1563.      *
  1564.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1565.      * @access public
  1566.      */
  1567.     function free()
  1568.     {
  1569.         if (is_null($this->positions)) {
  1570.             return $this->db->raiseError(MDB2_ERRORnullnull,
  1571.                 'Prepared statement has already been freed'__FUNCTION__);
  1572.         }
  1573.         $result = MDB2_OK;
  1574.  
  1575.         if (is_object($this->statement)) {
  1576.             if (!@mysqli_stmt_close($this->statement)) {
  1577.                 $result $this->db->raiseError(nullnullnull,
  1578.                     'Could not free statement'__FUNCTION__);
  1579.             }
  1580.         elseif (!is_null($this->statement)) {
  1581.             $connection $this->db->getConnection();
  1582.             if (PEAR::isError($connection)) {
  1583.                 return $connection;
  1584.             }
  1585.  
  1586.             $query 'DEALLOCATE PREPARE '.$this->statement;
  1587.             $result $this->db->_doQuery($querytrue$connection);
  1588.         }
  1589.  
  1590.         parent::free();
  1591.         return $result;
  1592.    }
  1593. }
  1594. ?>

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