Source for file Db2.php

Documentation is available at Db2.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. Doctrine::autoload('Doctrine_Adapter');
  22. /**
  23.  * Doctrine_Adapter_Db2
  24.  * IBM DB2 Adapter  [BORROWED FROM ZEND FRAMEWORK]
  25.  *
  26.  * @package     Doctrine
  27.  * @subpackage  Doctrine_Adapter
  28.  * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
  29.  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
  30.  * @category    Object Relational Mapping
  31.  * @link        www.phpdoctrine.com
  32.  * @since       1.0
  33.  * @version     $Revision: 1080 $
  34.  */
  35. {
  36.     /**
  37.      * User-provided configuration.
  38.      *
  39.      * Basic keys are:
  40.      *
  41.      * username   => (string)  Connect to the database as this username.
  42.      * password   => (string)  Password associated with the username.
  43.      * host       => (string)  What host to connect to (default 127.0.0.1)
  44.      * dbname     => (string)  The name of the database to user
  45.      * protocol   => (string)  Protocol to use, defaults to "TCPIP"
  46.      * port       => (integer) Port number to use for TCP/IP if protocol is "TCPIP"
  47.      * persistent => (boolean) Set TRUE to use a persistent connection (db2_pconnect)
  48.      *
  49.      * @var array 
  50.      */
  51.     protected $_config = array(
  52.         'dbname'       => null,
  53.         'username'     => null,
  54.         'password'     => null,
  55.         'host'         => 'localhost',
  56.         'port'         => '50000',
  57.         'protocol'     => 'TCPIP',
  58.         'persistent'   => false
  59.     );
  60.  
  61.     /**
  62.      * Execution mode
  63.      *
  64.      * @var int execution flag (DB2_AUTOCOMMIT_ON or DB2_AUTOCOMMIT_OFF)
  65.      * @access protected
  66.      */
  67.     protected $_execute_mode = DB2_AUTOCOMMIT_ON;
  68.  
  69.     /**
  70.      * Table name of the last accessed table for an insert operation
  71.      * This is a DB2-Adapter-specific member variable with the utmost
  72.      * probability you might not find it in other adapters...
  73.      *
  74.      * @var string 
  75.      * @access protected
  76.      */
  77.     protected $_lastInsertTable = null;
  78.  
  79.      /**
  80.      * Constructor.
  81.      *
  82.      * $config is an array of key/value pairs containing configuration
  83.      * options.  These options are common to most adapters:
  84.      *
  85.      * dbname         => (string) The name of the database to user
  86.      * username       => (string) Connect to the database as this username.
  87.      * password       => (string) Password associated with the username.
  88.      * host           => (string) What host to connect to, defaults to localhost
  89.      * port           => (string) The port of the database, defaults to 50000
  90.      * persistent     => (boolean) Whether to use a persistent connection or not, defaults to false
  91.      * protocol       => (string) The network protocol, defaults to TCPIP
  92.      * options        => (array)  Other database options such as autocommit, case, and cursor options
  93.      *
  94.      * @param array $config An array of configuration keys.
  95.      */
  96.     public function __construct(array $config)
  97.     {
  98.         if isset($config['password'])) {
  99.             throw new Doctrine_Adapter_Db2_Exception("Configuration array must have a key for 'password' for login credentials.");
  100.         }
  101.  
  102.         if isset($config['username'])) {
  103.             throw new Doctrine_Adapter_Db2_Exception("Configuration array must have a key for 'username' for login credentials.");
  104.         }
  105.  
  106.         if isset($config['dbname'])) {
  107.             throw new Doctrine_Adapter_Db2_Exception("Configuration array must have a key for 'dbname' that names the database instance.");
  108.         }
  109.  
  110.         // keep the config
  111.         $this->_config array_merge($this->_config(array) $config);
  112.  
  113.         // create a profiler object
  114.         $enabled false;
  115.         if (array_key_exists('profiler'$this->_config)) {
  116.             $enabled = (bool) $this->_config['profiler'];
  117.             unset($this->_config['profiler']);
  118.         }
  119.  
  120.         $this->_profiler new Doctrine_Profiler($enabled);
  121.     }
  122.  
  123.     /**
  124.      * Creates a connection resource.
  125.      *
  126.      * @return void 
  127.      */
  128.     protected function _connect()
  129.     {
  130.         if (is_resource($this->_connection)) {
  131.             // connection already exists
  132.             return;
  133.         }
  134.  
  135.         if extension_loaded('ibm_db2')) {
  136.             throw new Doctrine_Adapter_Db2_Exception('The IBM DB2 extension is required for this adapter but not loaded');
  137.         }
  138.  
  139.         if ($this->_config['persistent']{
  140.             // use persistent connection
  141.             $conn_func_name 'db2_pconnect';
  142.         else {
  143.             // use "normal" connection
  144.             $conn_func_name 'db2_connect';
  145.         }
  146.  
  147.         if (!isset($this->_config['options'])) {
  148.             // config options were not set, so set it to an empty array
  149.             $this->_config['options'array();
  150.         }
  151.  
  152.         if (!isset($this->_config['options']['autocommit'])) {
  153.             // set execution mode
  154.             $this->_config['options']['autocommit'&$this->_execute_mode;
  155.         }
  156.  
  157.         if ($this->_config['host'!== 'localhost'{
  158.             // if the host isn't localhost, use extended connection params
  159.             $dbname 'DRIVER={IBM DB2 ODBC DRIVER}' .
  160.                      ';DATABASE=' $this->_config['dbname'.
  161.                      ';HOSTNAME=' $this->_config['host'.
  162.                      ';PORT='     $this->_config['port'.
  163.                      ';PROTOCOL=' $this->_config['protocol'.
  164.                      ';UID='      $this->_config['username'.
  165.                      ';PWD='      $this->_config['password'.';';
  166.             $this->_connection $conn_func_name(
  167.                 $dbname,
  168.                 null,
  169.                 null,
  170.                 $this->_config['options']
  171.             );
  172.         else {
  173.             // host is localhost, so use standard connection params
  174.             $this->_connection $conn_func_name(
  175.                 $this->_config['dbname'],
  176.                 $this->_config['username'],
  177.                 $this->_config['password'],
  178.                 $this->_config['options']
  179.             );
  180.         }
  181.  
  182.         // check the connection
  183.         if (!$this->_connection{
  184.             throw new Doctrine_Adapter_Db2_Exception(db2_conn_errormsg()db2_conn_error());
  185.         }
  186.     }
  187.  
  188.     /**
  189.      * Force the connection to close.
  190.      *
  191.      * @return void 
  192.      */
  193.     public function closeConnection()
  194.     {
  195.         db2_close($this->_connection);
  196.         $this->_connection null;
  197.     }
  198.  
  199.     /**
  200.      * Returns an SQL statement for preparation.
  201.      *
  202.      * @param string $sql The SQL statement with placeholders.
  203.      * @return Doctrine_Statement_Db2 
  204.      */
  205.     public function prepare($sql)
  206.     {
  207.         $this->_connect();
  208.         $stmt new Doctrine_Statement_Db2($this$sql);
  209.         $stmt->setFetchMode($this->_fetchMode);
  210.         return $stmt;
  211.     }
  212.  
  213.     /**
  214.      * Gets the execution mode
  215.      *
  216.      * @return int the execution mode (DB2_AUTOCOMMIT_ON or DB2_AUTOCOMMIT_OFF)
  217.      */
  218.     public function _getExecuteMode()
  219.     {
  220.         return $this->_execute_mode;
  221.     }
  222.  
  223.     /**
  224.      * @param integer $mode 
  225.      * @return void 
  226.      */
  227.     public function _setExecuteMode($mode)
  228.     {
  229.         switch ($mode{
  230.             case DB2_AUTOCOMMIT_OFF:
  231.             case DB2_AUTOCOMMIT_ON:
  232.                 $this->_execute_mode $mode;
  233.                 db2_autocommit($this->_connection$mode);
  234.                 break;
  235.             default:
  236.                 throw new Doctrine_Adapter_Db2_Exception("execution mode not supported");
  237.                 break;
  238.         }
  239.     }
  240.  
  241.     /**
  242.      * Quote a raw string.
  243.      *
  244.      * @param string $value     Raw string
  245.      * @return string           Quoted string
  246.      */
  247.     protected function _quote($value)
  248.     {
  249.         /**
  250.          * Some releases of the IBM DB2 extension appear
  251.          * to be missing the db2_escape_string() method.
  252.          * The method was added in ibm_db2.c revision 1.53
  253.          * according to cvs.php.net.  But the function is
  254.          * not present in my build of PHP 5.2.1.
  255.          */
  256.         if (function_exists('db2_escape_string')) {
  257.             return db2_escape_string($value);
  258.         }
  259.         return parent::_quote($value);
  260.     }
  261.  
  262.     /**
  263.      * @return string 
  264.      */
  265.     public function getQuoteIdentifierSymbol()
  266.     {
  267.         $info db2_server_info($this->_connection);
  268.         $identQuote $info->IDENTIFIER_QUOTE_CHAR;
  269.         return $identQuote;
  270.     }
  271.     /**
  272.      * Begin a transaction.
  273.      *
  274.      * @return void 
  275.      */
  276.     protected function _beginTransaction()
  277.     {
  278.         $this->_setExecuteMode(DB2_AUTOCOMMIT_OFF);
  279.     }
  280.  
  281.     /**
  282.      * Commit a transaction.
  283.      *
  284.      * @return void 
  285.      */
  286.     protected function _commit()
  287.     {
  288.         if (!db2_commit($this->_connection)) {
  289.             throw new Doctrine_Adapter_Db2_Exception(
  290.                 db2_conn_errormsg($this->_connection),
  291.                 db2_conn_error($this->_connection));
  292.         }
  293.  
  294.         $this->_setExecuteMode(DB2_AUTOCOMMIT_ON);
  295.     }
  296.  
  297.     /**
  298.      * Rollback a transaction.
  299.      *
  300.      * @return void 
  301.      */
  302.     protected function _rollBack()
  303.     {
  304.         if (!db2_rollback($this->_connection)) {
  305.             throw new Doctrine_Adapter_Db2_Exception(
  306.                 db2_conn_errormsg($this->_connection),
  307.                 db2_conn_error($this->_connection));
  308.         }
  309.         $this->_setExecuteMode(DB2_AUTOCOMMIT_ON);
  310.     }
  311.  
  312.     /**
  313.      * Set the fetch mode.
  314.      *
  315.      * @param integer $mode 
  316.      * @return void 
  317.      */
  318.     public function setFetchMode($mode)
  319.     {
  320.         switch ($mode{
  321.             case Doctrine::FETCH_NUM:   // seq array
  322.             case Doctrine::FETCH_ASSOC// assoc array
  323.             case Doctrine::FETCH_BOTH:  // seq+assoc array
  324.             case Doctrine::FETCH_OBJ:   // object
  325.                 $this->_fetchMode $mode;
  326.                 break;
  327.             default:
  328.                 throw new Doctrine_Adapter_Db2_Exception('Invalid fetch mode specified');
  329.                 break;
  330.         }
  331.     }
  332. }