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

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