Source for file Mysqli.php

Documentation is available at Mysqli.php

  1. <?php
  2. /*
  3.  *  $Id: Mock.php 1080 2007-02-10 18:17:08Z romanb $
  4.  *
  5.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16.  *
  17.  * This software consists of voluntary contributions made by many individuals
  18.  * and is licensed under the LGPL. For more information, see
  19.  * <http://www.phpdoctrine.com>.
  20.  */
  21. /**
  22.  * Doctrine_Adapter_Mysqli
  23.  * This class is used for special testing purposes.
  24.  *
  25.  * @package     Doctrine
  26.  * @subpackage  Doctrine_Adapter
  27.  * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
  28.  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
  29.  * @category    Object Relational Mapping
  30.  * @link        www.phpdoctrine.com
  31.  * @since       1.0
  32.  * @version     $Revision: 1080 $
  33.  */
  34. {
  35.     /**
  36.      * Creates a connection to the database.
  37.      *
  38.      * @return void 
  39.      * @throws Doctrine_Adapter_Mysqli_Exception
  40.      */
  41.     protected function _connect()
  42.     {
  43.         if ($this->_connection{
  44.             return;
  45.         }
  46.         // Suppress connection warnings here.
  47.         // Throw an exception instead.
  48.         @$this->_connection new mysqli(
  49.             $this->_config['host'],
  50.             $this->_config['username'],
  51.             $this->_config['password'],
  52.             $this->_config['dbname']
  53.         );
  54.         if ($this->_connection === false || mysqli_connect_errno()) {
  55.             throw new Doctrine_Adapter_Mysqli_Exception(mysqli_connect_error());
  56.         }
  57.     }
  58.  
  59.     /**
  60.      * Force the connection to close.
  61.      *
  62.      * @return void 
  63.      */
  64.     public function closeConnection()
  65.     {
  66.         $this->_connection->close();
  67.         $this->_connection null;
  68.     }
  69.  
  70.     /**
  71.      * Prepare a statement and return a PDOStatement-like object.
  72.      *
  73.      * @param  string  $sql  SQL query
  74.      * @return Doctrine_Statement_Mysqli 
  75.      */
  76.     public function prepare($sql)
  77.     {
  78.         $this->_connect();
  79.         $stmt new Doctrine_Statement_Mysqli($this$sql);
  80.         $stmt->setFetchMode($this->_fetchMode);
  81.         return $stmt;
  82.     }
  83.  
  84.     /**
  85.      * Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT column.
  86.      *
  87.      * As a convention, on RDBMS brands that support sequences
  88.      * (e.g. Oracle, PostgreSQL, DB2), this method forms the name of a sequence
  89.      * from the arguments and returns the last id generated by that sequence.
  90.      * On RDBMS brands that support IDENTITY/AUTOINCREMENT columns, this method
  91.      * returns the last value generated for such a column, and the table name
  92.      * argument is disregarded.
  93.      *
  94.      * MySQL does not support sequences, so $tableName and $primaryKey are ignored.
  95.      *
  96.      * @param string $tableName   OPTIONAL Name of table.
  97.      * @param string $primaryKey  OPTIONAL Name of primary key column.
  98.      * @return integer 
  99.      */
  100.     public function lastInsertId($tableName null$primaryKey null)
  101.     {
  102.         $mysqli $this->_connection;
  103.         return $mysqli->insert_id;
  104.     }
  105.  
  106.     /**
  107.      * Begin a transaction.
  108.      *
  109.      * @return void 
  110.      */
  111.     protected function _beginTransaction()
  112.     {
  113.         $this->_connect();
  114.         $this->_connection->autocommit(false);
  115.     }
  116.  
  117.     /**
  118.      * Commit a transaction.
  119.      *
  120.      * @return void 
  121.      */
  122.     protected function _commit()
  123.     {
  124.         $this->_connect();
  125.         $this->_connection->commit();
  126.         $this->_connection->autocommit(true);
  127.     }
  128.  
  129.     /**
  130.      * Roll-back a transaction.
  131.      *
  132.      * @return void 
  133.      */
  134.     protected function _rollBack()
  135.     {
  136.         $this->_connect();
  137.         $this->_connection->rollback();
  138.         $this->_connection->autocommit(true);
  139.     }
  140. }