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

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