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-2004 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@backendmedia.com>                         |
  44. // +----------------------------------------------------------------------+
  45. //
  46. // $Id: mysqli.php,v 1.21 2005/04/25 13:09:03 lsmith Exp $
  47. //
  48.  
  49. /**
  50.  * MDB2 MySQL driver
  51.  *
  52.  * @package MDB2
  53.  * @category Database
  54.  * @author  Lukas Smith <smith@backendmedia.com>
  55.  */
  56. {
  57.     // {{{ properties
  58.     var $escape_quotes = "\\";
  59.  
  60.     var $dummy_primary_key = 'dummy_primary_key';
  61.  
  62.     // }}}
  63.     // {{{ constructor
  64.  
  65.     /**
  66.     * Constructor
  67.     */
  68.     function __construct()
  69.     {
  70.         parent::__construct();
  71.  
  72.         $this->phptype = 'mysqli';
  73.         $this->dbsyntax = 'mysqli';
  74.  
  75.         $this->supported['sequences'= true;
  76.         $this->supported['indexes'= true;
  77.         $this->supported['affected_rows'= true;
  78.         $this->supported['transactions'= false;
  79.         $this->supported['summary_functions'= true;
  80.         $this->supported['order_by_text'= true;
  81.         $this->supported['current_id'= true;
  82.         $this->supported['limit_queries'= true;
  83.         $this->supported['LOBs'= true;
  84.         $this->supported['replace'= true;
  85.         $this->supported['sub_selects'= false;
  86.         $this->supported['auto_increment'= true;
  87.  
  88.         $this->options['default_table_type'= null;
  89.     }
  90.  
  91.     // }}}
  92.     // {{{ errorInfo()
  93.  
  94.     /**
  95.      * This method is used to collect information about an error
  96.      *
  97.      * @param integer $error 
  98.      * @return array 
  99.      * @access public
  100.      */
  101.     function errorInfo($error = null)
  102.     {
  103.         if ($this->connection{
  104.             $native_code @mysqli_errno($this->connection);
  105.             $native_msg  @mysqli_error($this->connection);
  106.         else {
  107.             $native_code @mysqli_errno();
  108.             $native_msg  @mysqli_error();
  109.         }
  110.         if (is_null($error)) {
  111.             static $ecode_map;
  112.             if (empty($ecode_map)) {
  113.                 $ecode_map = array(
  114.                     1004 => MDB2_ERROR_CANNOT_CREATE,
  115.                     1005 => MDB2_ERROR_CANNOT_CREATE,
  116.                     1006 => MDB2_ERROR_CANNOT_CREATE,
  117.                     1007 => MDB2_ERROR_ALREADY_EXISTS,
  118.                     1008 => MDB2_ERROR_CANNOT_DROP,
  119.                     1022 => MDB2_ERROR_ALREADY_EXISTS,
  120.                     1044 => MDB2_ERROR_ACCESS_VIOLATION,
  121.                     1046 => MDB2_ERROR_NODBSELECTED,
  122.                     1048 => MDB2_ERROR_CONSTRAINT,
  123.                     1049 => MDB2_ERROR_NOSUCHDB,
  124.                     1050 => MDB2_ERROR_ALREADY_EXISTS,
  125.                     1051 => MDB2_ERROR_NOSUCHTABLE,
  126.                     1054 => MDB2_ERROR_NOSUCHFIELD,
  127.                     1061 => MDB2_ERROR_ALREADY_EXISTS,
  128.                     1062 => MDB2_ERROR_ALREADY_EXISTS,
  129.                     1064 => MDB2_ERROR_SYNTAX,
  130.                     1091 => MDB2_ERROR_NOT_FOUND,
  131.                     1100 => MDB2_ERROR_NOT_LOCKED,
  132.                     1136 => MDB2_ERROR_VALUE_COUNT_ON_ROW,
  133.                     1142 => MDB2_ERROR_ACCESS_VIOLATION,
  134.                     1146 => MDB2_ERROR_NOSUCHTABLE,
  135.                     1216 => MDB2_ERROR_CONSTRAINT,
  136.                     1217 => MDB2_ERROR_CONSTRAINT,
  137.                 );
  138.             }
  139.             if ($this->options['portability'MDB2_PORTABILITY_ERRORS{
  140.                 $ecode_map[1022MDB2_ERROR_CONSTRAINT;
  141.                 $ecode_map[1048MDB2_ERROR_CONSTRAINT_NOT_NULL;
  142.                 $ecode_map[1062MDB2_ERROR_CONSTRAINT;
  143.             else {
  144.                 // Doing this in case mode changes during runtime.
  145.                 $ecode_map[1022MDB2_ERROR_ALREADY_EXISTS;
  146.                 $ecode_map[1048MDB2_ERROR_CONSTRAINT;
  147.                 $ecode_map[1062MDB2_ERROR_ALREADY_EXISTS;
  148.             }
  149.             if (isset($ecode_map[$native_code])) {
  150.                 $error $ecode_map[$native_code];
  151.             }
  152.         }
  153.         return array($error$native_code$native_msg);
  154.     }
  155.  
  156.     // }}}
  157.     // {{{ escape()
  158.  
  159.     /**
  160.      * Quotes a string so it can be safely used in a query. It will quote
  161.      * the text so it can safely be used within a query.
  162.      *
  163.      * @param string $text the input string to quote
  164.      * @return string quoted string
  165.      * @access public
  166.      */
  167.     function escape($text)
  168.     {
  169.         return @mysqli_escape_string($this->connection$text);
  170.     }
  171.  
  172.     // }}}
  173.     // {{{ quoteIdentifier()
  174.  
  175.     /**
  176.      * Quote a string so it can be safely used as a table or column name
  177.      *
  178.      * Quoting style depends on which database driver is being used.
  179.      *
  180.      * MySQL can't handle the backtick character (<kbd>`</kbd>) in
  181.      * table or column names.
  182.      *
  183.      * @param string $str  identifier name to be quoted
  184.      *
  185.      * @return string  quoted identifier string
  186.      *
  187.      * @access public
  188.      * @internal
  189.      */
  190.     function quoteIdentifier($str)
  191.     {
  192.         return '`' $str '`';
  193.     }
  194.  
  195.     // }}}
  196.     // {{{ beginTransaction()
  197.  
  198.     /**
  199.      * Start a transaction.
  200.      *
  201.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  202.      * @access public
  203.      */
  204.     function beginTransaction()
  205.     {
  206.         $this->debug('starting transaction''beginTransaction');
  207.         if (!$this->supports('transactions')) {
  208.             return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  209.                 'beginTransaction: transactions are not in use');
  210.         }
  211.         if ($this->in_transaction{
  212.             return MDB2_OK;  //nothing to do
  213.         }
  214.         $result $this->_doQuery('SET AUTOCOMMIT = 0'true);
  215.         if (PEAR::isError($result)) {
  216.             return $result;
  217.         }
  218.         $this->in_transaction = true;
  219.         return MDB2_OK;
  220.     }
  221.  
  222.     // }}}
  223.     // {{{ commit()
  224.  
  225.     /**
  226.      * Commit the database changes done during a transaction that is in
  227.      * progress.
  228.      *
  229.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  230.      * @access public
  231.      */
  232.     function commit()
  233.     {
  234.         $this->debug('commit transaction''commit');
  235.         if (!$this->supports('transactions')) {
  236.             return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  237.                 'commit: transactions are not in use');
  238.         }
  239.         if (!$this->in_transaction{
  240.             return $this->raiseError(MDB2_ERRORnullnull,
  241.                 'commit: transaction changes are being auto committed');
  242.         }
  243.         $result $this->_doQuery('COMMIT'true);
  244.         if (PEAR::isError($result)) {
  245.             return $result;
  246.         }
  247.         $result $this->_doQuery('SET AUTOCOMMIT = 1'true);
  248.         if (PEAR::isError($result)) {
  249.             return $result;
  250.         }
  251.         $this->in_transaction = false;
  252.         return MDB2_OK;
  253.     }
  254.  
  255.     // }}}
  256.     // {{{ rollback()
  257.  
  258.     /**
  259.      * Cancel any database changes done during a transaction that is in
  260.      * progress.
  261.      *
  262.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  263.      * @access public
  264.      */
  265.     function rollback()
  266.     {
  267.         $this->debug('rolling back transaction''rollback');
  268.         if (!$this->supports('transactions')) {
  269.             return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  270.                 'rollback: transactions are not in use');
  271.         }
  272.         if (!$this->in_transaction{
  273.             return $this->raiseError(MDB2_ERRORnullnull,
  274.                 'rollback: transactions can not be rolled back when changes are auto committed');
  275.         }
  276.         $result $this->_doQuery('ROLLBACK'true);
  277.         if (PEAR::isError($result)) {
  278.             return $result;
  279.         }
  280.         $result $this->_doQuery('SET AUTOCOMMIT = 1'true);
  281.         if (PEAR::isError($result)) {
  282.             return $result;
  283.         }
  284.         $this->in_transaction = false;
  285.         return MDB2_OK;
  286.  
  287.     }
  288.  
  289.     // }}}
  290.     // {{{ connect()
  291.  
  292.     /**
  293.      * Connect to the database
  294.      *
  295.      * @return true on success, MDB2 Error Object on failure
  296.      */
  297.     function connect()
  298.     {
  299.         if (is_object($this->connection)) {
  300.             if (count(array_diff($this->connected_dsn$this->dsn)) == 0{
  301.                 return MDB2_OK;
  302.             }
  303.             $this->connection = 0;
  304.         }
  305.  
  306.         if (!PEAR::loadExtension($this->phptype)) {
  307.             return $this->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  308.                 'connect: extension '.$this->phptype.' is not compiled into PHP');
  309.         }
  310.  
  311.         @ini_set('track_errors'true);
  312.         $php_errormsg '';
  313.  
  314.         if ($this->options['ssl'=== true{
  315.             $init @mysqli_init();
  316.             @mysqli_ssl_set(
  317.                 $init,
  318.                 empty($this->dsn['key'])    ? null : $this->dsn['key'],
  319.                 empty($this->dsn['cert'])   ? null : $this->dsn['cert'],
  320.                 empty($this->dsn['ca'])     ? null : $this->dsn['ca'],
  321.                 empty($this->dsn['capath']? null : $this->dsn['capath'],
  322.                 empty($this->dsn['cipher']? null : $this->dsn['cipher']
  323.             );
  324.             if ($connection @mysqli_real_connect(
  325.                     $init,
  326.                     $this->dsn['hostspec'],
  327.                     $this->dsn['username'],
  328.                     $this->dsn['password'],
  329.                     $this->database_name,
  330.                     $this->dsn['port'],
  331.                     $this->dsn['socket']))
  332.             {
  333.                 $connection $init;
  334.             }
  335.         else {
  336.             $connection @mysqli_connect(
  337.                 $this->dsn['hostspec'],
  338.                 $this->dsn['username'],
  339.                 $this->dsn['password'],
  340.                 $this->database_name,
  341.                 $this->dsn['port'],
  342.                 $this->dsn['socket']
  343.             );
  344.         }
  345.  
  346.         @ini_restore('track_errors');
  347.  
  348.         if (!$connection{
  349.             if (($err @mysqli_connect_error()) != ''{
  350.                 return $this->raiseError(MDB2_ERROR_CONNECT_FAILEDnullnull$err);
  351.             else {
  352.                 return $this->raiseError(MDB2_ERROR_CONNECT_FAILEDnullnull$php_errormsg);
  353.             }
  354.         }
  355.  
  356.         $this->connection = $connection;
  357.         $this->connected_dsn = $this->dsn;
  358.         $this->connected_database_name = $this->database_name;
  359.         $this->dbsyntax = $this->dsn['dbsyntax'$this->dsn['dbsyntax'$this->phptype;
  360.  
  361.         $this->supported['transactions'= false;
  362.         if ($this->options['default_table_type']{
  363.             switch (strtoupper($this->options['default_table_type'])) {
  364.             case 'BERKELEYDB':
  365.                 $this->options['default_table_type''BDB';
  366.             case 'BDB':
  367.             case 'INNODB':
  368.             case 'GEMINI':
  369.                 $this->supported['transactions'= true;
  370.                 break;
  371.             case 'HEAP':
  372.             case 'ISAM':
  373.             case 'MERGE':
  374.             case 'MRG_MYISAM':
  375.             case 'MYISAM':
  376.                 break;
  377.             default:
  378.                 $this->warnings[$default_table_type.
  379.                     ' is not a supported default table type';
  380.             }
  381.         }
  382.  
  383.         if ($this->options['use_transactions'&& !$this->supports('transactions')) {
  384.             $this->warnings[$this->options['default_table_type'].
  385.                 ' is not a transaction-safe default table type; switched to INNODB';
  386.             $this->options['default_table_type''INNODB';
  387.             $this->supported['transactions'= true;
  388.         }
  389.  
  390.         return MDB2_OK;
  391.     }
  392.  
  393.     // }}}
  394.     // {{{ disconnect()
  395.  
  396.     /**
  397.      * Log out and disconnect from the database.
  398.      *
  399.      * @return mixed true on success, false if not connected and error
  400.      *                 object on error
  401.      * @access public
  402.      */
  403.     function disconnect($force = true)
  404.     {
  405.         if (is_object($this->connection)) {
  406.             if ($force{
  407.                 @mysqli_close($this->connection);
  408.             }
  409.             $this->connection = 0;
  410.         }
  411.         return MDB2_OK;
  412.     }
  413.  
  414.     // }}}
  415.     // {{{ _doQuery()
  416.  
  417.     /**
  418.      * Execute a query
  419.      * @param string $query  query
  420.      * @param boolean $isManip  if the query is a manipulation query
  421.      * @param resource $connection 
  422.      * @param string $database_name 
  423.      * @return result or error object
  424.      * @access protected
  425.      */
  426.     function _doQuery($query$isManip = false$connection = null$database_name = null)
  427.     {
  428.         $this->last_query = $query;
  429.         $this->debug($query'query');
  430.         if ($this->options['disable_query']{
  431.             if ($isManip{
  432.                 return MDB2_OK;
  433.             }
  434.             return null;
  435.         }
  436.  
  437.         if (is_null($connection)) {
  438.             $error $this->connect();
  439.             if (PEAR::isError($error)) {
  440.                 return $error;
  441.             }
  442.             $connection $this->connection;
  443.         }
  444.         if (is_null($database_name)) {
  445.             $database_name $this->database_name;
  446.         }
  447.  
  448.         if ($database_name{
  449.             if ($database_name != $this->connected_database_name{
  450.                 if (!@mysqli_select_db($connection$database_name)) {
  451.                     return $this->raiseError();
  452.                 }
  453.                 $this->connected_database_name = $database_name;
  454.             }
  455.         }
  456.  
  457.         $function $this->options['result_buffering']
  458.             ? 'mysqli_query' 'mysqli_unbuffered_query';
  459.         $result @$function($connection$query);
  460.         if (!$result{
  461.             return $this->raiseError();
  462.         }
  463.  
  464.         if ($isManip{
  465.             return @mysqli_affected_rows($connection);
  466.         }
  467.         return $result;
  468.     }
  469.  
  470.     // }}}
  471.     // {{{ _modifyQuery()
  472.  
  473.     /**
  474.      * Changes a query string for various DBMS specific reasons
  475.      *
  476.      * @param string $query  query to modify
  477.      * @return the new (modified) query
  478.      * @access protected
  479.      */
  480.     function _modifyQuery($query$isManip$limit$offset)
  481.     {
  482.         if ($limit > 0
  483.             && !preg_match('/LIMIT\s*\d(\s*(,|OFFSET)\s*\d+)?/i'$query)
  484.         {
  485.             $query rtrim($query);
  486.             if (substr($query-1== ';'{
  487.                 $query substr($query0-1);
  488.             }
  489.             if ($isManip{
  490.                 return $query . " LIMIT $limit";
  491.             else {
  492.                 return $query . " LIMIT $offset$limit";
  493.             }
  494.         }
  495.         return $query;
  496.     }
  497.  
  498.     // }}}
  499.     // {{{ subSelect()
  500.  
  501.     /**
  502.      * simple subselect emulation for Mysql
  503.      *
  504.      * @access public
  505.      *
  506.      * @param string $query the SQL query for the subselect that may only
  507.      *                       return a column
  508.      * @param string $type determines type of the field
  509.      *
  510.      * @return string comma seperated values
  511.      */
  512.     function subSelect($query$type = false)
  513.     {
  514.         if ($this->supports('sub_selects')) {
  515.             return $query;
  516.         }
  517.         $col $this->queryCol($query$type);
  518.         if (!is_array($col|| count($col== 0{
  519.             return 'NULL';
  520.         }
  521.         if ($type{
  522.             $this->loadModule('Datatype');
  523.             return $this->datatype->implodeArray($col$type);
  524.         }
  525.         return implode(', '$col);
  526.     }
  527.  
  528.     // }}}
  529.     // {{{ replace()
  530.  
  531.     /**
  532.      * Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT
  533.      * query, except that if there is already a row in the table with the same
  534.      * key field values, the REPLACE query just updates its values instead of
  535.      * inserting a new row.
  536.      *
  537.      * The REPLACE type of query does not make part of the SQL standards. Since
  538.      * practically only MySQL implements it natively, this type of query is
  539.      * emulated through this method for other DBMS using standard types of
  540.      * queries inside a transaction to assure the atomicity of the operation.
  541.      *
  542.      * @access public
  543.      *
  544.      * @param string $table name of the table on which the REPLACE query will
  545.      *   be executed.
  546.      * @param array $fields associative array that describes the fields and the
  547.      *   values that will be inserted or updated in the specified table. The
  548.      *   indexes of the array are the names of all the fields of the table. The
  549.      *   values of the array are also associative arrays that describe the
  550.      *   values and other properties of the table fields.
  551.      *
  552.      *   Here follows a list of field properties that need to be specified:
  553.      *
  554.      *     value:
  555.      *           Value to be assigned to the specified field. This value may be
  556.      *           of specified in database independent type format as this
  557.      *           function can perform the necessary datatype conversions.
  558.      *
  559.      *     Default:
  560.      *           this property is required unless the Null property
  561.      *           is set to 1.
  562.      *
  563.      *     type
  564.      *           Name of the type of the field. Currently, all types Metabase
  565.      *           are supported except for clob and blob.
  566.      *
  567.      *     Default: no type conversion
  568.      *
  569.      *     null
  570.      *           Boolean property that indicates that the value for this field
  571.      *           should be set to null.
  572.      *
  573.      *           The default value for fields missing in INSERT queries may be
  574.      *           specified the definition of a table. Often, the default value
  575.      *           is already null, but since the REPLACE may be emulated using
  576.      *           an UPDATE query, make sure that all fields of the table are
  577.      *           listed in this function argument array.
  578.      *
  579.      *     Default: 0
  580.      *
  581.      *     key
  582.      *           Boolean property that indicates that this field should be
  583.      *           handled as a primary key or at least as part of the compound
  584.      *           unique index of the table that will determine the row that will
  585.      *           updated if it exists or inserted a new row otherwise.
  586.      *
  587.      *           This function will fail if no key field is specified or if the
  588.      *           value of a key field is set to null because fields that are
  589.      *           part of unique index they may not be null.
  590.      *
  591.      *     Default: 0
  592.      *
  593.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  594.      */
  595.     function replace($table$fields)
  596.     {
  597.         $count count($fields);
  598.         $query $values '';
  599.         $keys $colnum = 0;
  600.         for (reset($fields)$colnum $countnext($fields)$colnum++{
  601.             $name key($fields);
  602.             if ($colnum > 0{
  603.                 $query  .= ',';
  604.                 $values .= ',';
  605.             }
  606.             $query .= $name;
  607.             if (isset($fields[$name]['null']&& $fields[$name]['null']{
  608.                 $value 'NULL';
  609.             else {
  610.                 $value $this->quote($fields[$name]['value']$fields[$name]['type']);
  611.             }
  612.             $values .= $value;
  613.             if (isset($fields[$name]['key']&& $fields[$name]['key']{
  614.                 if ($value === 'NULL'{
  615.                     return $this->raiseError(MDB2_ERROR_CANNOT_REPLACEnullnull,
  616.                         'replace: key value '.$name.' may not be NULL');
  617.                 }
  618.                 $keys++;
  619.             }
  620.         }
  621.         if ($keys == 0{
  622.             return $this->raiseError(MDB2_ERROR_CANNOT_REPLACEnullnull,
  623.                 'replace: not specified which fields are keys');
  624.         }
  625.         $query = "REPLACE INTO $table ($query) VALUES ($values)";
  626.         $this->last_query = $query;
  627.         $this->debug($query'query');
  628.         return $this->_doQuery($querytrue);
  629.     }
  630.  
  631.     // }}}
  632.     // {{{ nextID()
  633.  
  634.     /**
  635.      * returns the next free id of a sequence
  636.      *
  637.      * @param string  $seq_name name of the sequence
  638.      * @param boolean $ondemand when true the seqence is
  639.      *                           automatic created, if it
  640.      *                           not exists
  641.      *
  642.      * @return mixed MDB2 Error Object or id
  643.      * @access public
  644.      */
  645.     function nextID($seq_name$ondemand = true)
  646.     {
  647.         $sequence_name $this->getSequenceName($seq_name);
  648.         $query = "INSERT INTO $sequence_name (".$this->options['seqcol_name'].") VALUES (NULL)";
  649.         $this->expectError(MDB2_ERROR_NOSUCHTABLE);
  650.         $result $this->_doQuery($querytrue);
  651.         $this->popExpect();
  652.         if (PEAR::isError($result)) {
  653.             if ($ondemand && $result->getCode(== MDB2_ERROR_NOSUCHTABLE{
  654.                 $this->loadModule('Manager');
  655.                 // Since we are creating the sequence on demand
  656.                 // we know the first id = 1 so initialize the
  657.                 // sequence at 2
  658.                 $result $this->manager->createSequence($seq_name2);
  659.                 if (PEAR::isError($result)) {
  660.                     return $this->raiseError(MDB2_ERRORnullnull,
  661.                         'nextID: on demand sequence '.$seq_name.' could not be created');
  662.                 else {
  663.                     // First ID of a newly created sequence is 1
  664.                     return 1;
  665.                 }
  666.             }
  667.             return $result;
  668.         }
  669.         $value @mysqli_insert_id($this->connection);
  670.         if (is_numeric($value)) {
  671.             $query = "DELETE FROM $sequence_name WHERE ".$this->options['seqcol_name']." < $value";
  672.             $result $this->_doQuery($querytrue);
  673.             if (PEAR::isError($result)) {
  674.                 $this->warnings['nextID: could not delete previous sequence table values from '.$seq_name;
  675.             }
  676.         }
  677.         return $value;
  678.     }
  679.  
  680.     // }}}
  681.     // {{{ getAfterID()
  682.  
  683.     /**
  684.      * returns the autoincrement ID if supported or $id
  685.      *
  686.      * @param mixed $id value as returned by getBeforeId()
  687.      * @param string $table name of the table into which a new row was inserted
  688.      * @return mixed MDB2 Error Object or id
  689.      * @access public
  690.      */
  691.     function getAfterID($id$table)
  692.     {
  693.         $this->loadModule('Native');
  694.         return $this->native->getInsertID();
  695.     }
  696.  
  697.     // }}}
  698.     // {{{ currID()
  699.  
  700.     /**
  701.      * returns the current id of a sequence
  702.      *
  703.      * @param string  $seq_name name of the sequence
  704.      * @return mixed MDB2 Error Object or id
  705.      * @access public
  706.      */
  707.     function currID($seq_name)
  708.     {
  709.         $sequence_name $this->getSequenceName($seq_name);
  710.         return $this->queryOne("SELECT MAX(".$this->options['seqcol_name'].") FROM $sequence_name"'integer');
  711.     }
  712. }
  713.  
  714. class MDB2_Result_mysqli extends MDB2_Result_Common
  715. {
  716.     // }}}
  717.     // {{{ fetchRow()
  718.  
  719.     /**
  720.      * Fetch a row and insert the data into an existing array.
  721.      *
  722.      * @param int       $fetchmode  how the array data should be indexed
  723.      * @param int    $rownum    number of the row where the data can be found
  724.      * @return int data array on success, a MDB2 error on failure
  725.      * @access public
  726.      */
  727.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  728.     {
  729.         if (!is_null($rownum)) {
  730.             $seek $this->seek($rownum);
  731.             if (PEAR::isError($seek)) {
  732.                 return $seek;
  733.             }
  734.         }
  735.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  736.             $fetchmode $this->db->fetchmode;
  737.         }
  738.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  739.             $row @mysqli_fetch_assoc($this->result);
  740.             if (is_array($row)
  741.                 && $this->db->options['portability'MDB2_PORTABILITY_LOWERCASE
  742.             {
  743.                 $row array_change_key_case($rowCASE_LOWER);
  744.             }
  745.         else {
  746.            $row @mysqli_fetch_row($this->result);
  747.         }
  748.  
  749.         if (!$row{
  750.             if (is_null($this->result)) {
  751.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  752.                     'fetchRow: resultset has already been freed');
  753.             }
  754.             return null;
  755.         }
  756.         if ($this->db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL{
  757.             $this->db->_convertEmptyArrayValuesToNull($row);
  758.         }
  759.         if (isset($this->values)) {
  760.             $this->_assignBindColumns($row);
  761.         }
  762.         if (isset($this->types)) {
  763.             $row $this->db->datatype->convertResultRow($this->types$row);
  764.         }
  765.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  766.             $object_class $this->db->options['fetch_class'];
  767.             if ($object_class == 'stdClass'{
  768.                 $row = (object) $row;
  769.             else {
  770.                 $row &new $object_class($row);
  771.             }
  772.         }
  773.         ++$this->rownum;
  774.         return $row;
  775.     }
  776.  
  777.     // }}}
  778.     // {{{ _getColumnNames()
  779.  
  780.     /**
  781.      * Retrieve the names of columns returned by the DBMS in a query result.
  782.      *
  783.      * @return mixed                an associative array variable
  784.      *                               that will hold the names of columns. The
  785.      *                               indexes of the array are the column names
  786.      *                               mapped to lower case and the values are the
  787.      *                               respective numbers of the columns starting
  788.      *                               from 0. Some DBMS may not return any
  789.      *                               columns when the result set does not
  790.      *                               contain any rows.
  791.      *
  792.      *                               a MDB2 error on failure
  793.      * @access private
  794.      */
  795.     function _getColumnNames()
  796.     {
  797.         $columns = array();
  798.         $numcols $this->numCols();
  799.         if (PEAR::isError($numcols)) {
  800.             return $numcols;
  801.         }
  802.         for ($column = 0; $column $numcols$column++{
  803.             $column_info @mysqli_fetch_field_direct($this->result$column);
  804.             $columns[$column_info->name$column;
  805.         }
  806.         if ($this->db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  807.             $columns array_change_key_case($columnsCASE_LOWER);
  808.         }
  809.         return $columns;
  810.     }
  811.  
  812.     // }}}
  813.     // {{{ numCols()
  814.  
  815.     /**
  816.      * Count the number of columns returned by the DBMS in a query result.
  817.      *
  818.      * @return mixed integer value with the number of columns, a MDB2 error
  819.      *                        on failure
  820.      * @access public
  821.      */
  822.     function numCols()
  823.     {
  824.         $cols @mysqli_num_fields($this->result);
  825.         if (is_null($cols)) {
  826.             if (is_null($this->result)) {
  827.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  828.                     'numCols: resultset has already been freed');
  829.             }
  830.             return $this->db->raiseError();
  831.         }
  832.         return $cols;
  833.     }
  834.  
  835.     // }}}
  836.     // {{{ free()
  837.  
  838.     /**
  839.      * Free the internal resources associated with result.
  840.      *
  841.      * @return boolean true on success, false if result is invalid
  842.      * @access public
  843.      */
  844.     function free()
  845.     {
  846.         @mysqli_free_result($this->result);
  847.         $this->result = null;
  848.         return MDB2_OK;
  849.     }
  850. }
  851.  
  852. class MDB2_BufferedResult_mysqli extends MDB2_Result_mysqli
  853. {
  854.     // }}}
  855.     // {{{ seek()
  856.  
  857.     /**
  858.     * seek to a specific row in a result set
  859.     *
  860.     * @param int    $rownum    number of the row where the data can be found
  861.     * @return mixed MDB2_OK on success, a MDB2 error on failure
  862.     * @access public
  863.     */
  864.     function seek($rownum = 0)
  865.     {
  866.         if ($this->rownum != ($rownum - 1&& !@mysqli_data_seek($this->result$rownum)) {
  867.             if (is_null($this->result)) {
  868.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  869.                     'seek: resultset has already been freed');
  870.             }
  871.             return $this->db->raiseError(MDB2_ERROR_INVALIDnullnull,
  872.                 'seek: tried to seek to an invalid row number ('.$rownum.')');
  873.         }
  874.         $this->rownum $rownum - 1;
  875.         return MDB2_OK;
  876.     }
  877.  
  878.     // }}}
  879.     // {{{ valid()
  880.  
  881.     /**
  882.     * check if the end of the result set has been reached
  883.     *
  884.     * @return mixed true or false on sucess, a MDB2 error on failure
  885.     * @access public
  886.     */
  887.     function valid()
  888.     {
  889.         $numrows $this->numRows();
  890.         if (PEAR::isError($numrows)) {
  891.             return $numrows;
  892.         }
  893.         return $this->rownum ($numrows - 1);
  894.     }
  895.  
  896.     // }}}
  897.     // {{{ numRows()
  898.  
  899.     /**
  900.     * returns the number of rows in a result object
  901.     *
  902.     * @return mixed MDB2 Error Object or the number of rows
  903.     * @access public
  904.     */
  905.     function numRows()
  906.     {
  907.         $rows @mysqli_num_rows($this->result);
  908.         if (is_null($rows)) {
  909.             if (is_null($this->result)) {
  910.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  911.                     'numRows: resultset has already been freed');
  912.             }
  913.             return $this->raiseError();
  914.         }
  915.         return $rows;
  916.     }
  917. }
  918.  
  919. class MDB2_Statement_mysqli extends MDB2_Statement_Common
  920. {
  921.  
  922. }
  923. ?>

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