Source for file Oracle.php

Documentation is available at Oracle.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_Oracle
  24.  * [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.      * dbname   => Either the name of the local Oracle instance, or the
  44.      *             name of the entry in tnsnames.ora to which you want to connect.
  45.      *
  46.      * @var array 
  47.      */
  48.     protected $_config = array(
  49.         'dbname'       => null,
  50.         'username'     => null,
  51.         'password'     => null,
  52.     );
  53.  
  54.     /**
  55.      * @var integer 
  56.      */
  57.     protected $_execute_mode = OCI_COMMIT_ON_SUCCESS;
  58.  
  59.     /**
  60.      * Constructor.
  61.      *
  62.      * $config is an array of key/value pairs containing configuration
  63.      * options.  These options are common to most adapters:
  64.      *
  65.      * username => (string) Connect to the database as this username.
  66.      * password => (string) Password associated with the username.
  67.      * dbname   => Either the name of the local Oracle instance, or the
  68.      *             name of the entry in tnsnames.ora to which you want to connect.
  69.      *
  70.      * @param array $config An array of configuration keys.
  71.      * @throws Doctrine_Adapter_Exception
  72.      */
  73.     public function __construct(array $config)
  74.     {
  75.         if isset($config['password']|| isset($config['username'])) {
  76.             throw new Doctrine_Adapter_Exception('config array must have at least a username and a password');
  77.         }
  78.  
  79.         // @todo Let this protect backward-compatibility for one release, then remove
  80.         if isset($config['database']|| isset($config['dbname'])) {
  81.             $config['dbname'$config['database'];
  82.             unset($config['database']);
  83.             trigger_error("Deprecated config key 'database', use 'dbname' instead."E_USER_NOTICE);
  84.         }
  85.  
  86.         // keep the config
  87.         $this->_config array_merge($this->_config(array) $config);
  88.  
  89.         // create a profiler object
  90.         $enabled false;
  91.         if (array_key_exists('profiler'$this->_config)) {
  92.             $enabled = (bool) $this->_config['profiler'];
  93.             unset($this->_config['profiler']);
  94.         }
  95.  
  96.         $this->_profiler new Doctrine_Profiler($enabled);
  97.     }
  98.  
  99.     /**
  100.      * Creates a connection resource.
  101.      *
  102.      * @return void 
  103.      * @throws Doctrine_Adapter_Oracle_Exception
  104.      */
  105.     protected function _connect()
  106.     {
  107.         if (is_resource($this->_connection)) {
  108.             // connection already exists
  109.             return;
  110.         }
  111.  
  112.         if (!extension_loaded('oci8')) {
  113.             throw new Doctrine_Adapter_Oracle_Exception('The OCI8 extension is required for this adapter but not loaded');
  114.         }
  115.  
  116.         if (isset($this->_config['dbname'])) {
  117.             $this->_connection @oci_connect(
  118.                 $this->_config['username'],
  119.                 $this->_config['password'],
  120.                 $this->_config['dbname']);
  121.         else {
  122.             $this->_connection oci_connect(
  123.                 $this->_config['username'],
  124.                 $this->_config['password']);
  125.         }
  126.  
  127.         // check the connection
  128.         if (!$this->_connection{
  129.             throw new Doctrine_Adapter_Oracle_Exception(oci_error());
  130.         }
  131.     }
  132.  
  133.     /**
  134.      * Force the connection to close.
  135.      *
  136.      * @return void 
  137.      */
  138.     public function closeConnection()
  139.     {
  140.         if (is_resource($this->_connection)) {
  141.             oci_close($this->_connection);
  142.         }
  143.         $this->_connection null;
  144.     }
  145.  
  146.     /**
  147.      * Returns an SQL statement for preparation.
  148.      *
  149.      * @param string $sql The SQL statement with placeholders.
  150.      * @return Doctrine_Statement_Oracle 
  151.      */
  152.     public function prepare($sql)
  153.     {
  154.         $this->_connect();
  155.         $stmt new Doctrine_Statement_Oracle($this$sql);
  156.         $stmt->setFetchMode($this->_fetchMode);
  157.         return $stmt;
  158.     }
  159.  
  160.     /**
  161.      * Quote a raw string.
  162.      *
  163.      * @param string $value     Raw string
  164.      * @return string           Quoted string
  165.      */
  166.     protected function _quote($value)
  167.     {
  168.         $value str_replace("'""''"$value);
  169.         return "'" addcslashes($value"\000\n\r\\\032""'";
  170.     }
  171.  
  172.     /**
  173.      * Quote a table identifier and alias.
  174.      *
  175.      * @param string|array|Doctrine_Expr$ident The identifier or expression.
  176.      * @param string $alias An alias for the table.
  177.      * @return string The quoted identifier and alias.
  178.      */
  179.     public function quoteTableAs($ident$alias)
  180.     {
  181.         // Oracle doesn't allow the 'AS' keyword between the table identifier/expression and alias.
  182.         return $this->_quoteIdentifierAs($ident$alias' ');
  183.     }
  184.     /**
  185.      * Leave autocommit mode and begin a transaction.
  186.      *
  187.      * @return void 
  188.      */
  189.     protected function _beginTransaction()
  190.     {
  191.         $this->_setExecuteMode(OCI_DEFAULT);
  192.     }
  193.     /**
  194.      * Commit a transaction and return to autocommit mode.
  195.      *
  196.      * @return void 
  197.      * @throws Doctrine_Adapter_Oracle_Exception
  198.      */
  199.     protected function _commit()
  200.     {
  201.         if (!oci_commit($this->_connection)) {
  202.             throw new Doctrine_Adapter_Oracle_Exception(oci_error($this->_connection));
  203.         }
  204.         $this->_setExecuteMode(OCI_COMMIT_ON_SUCCESS);
  205.     }
  206.     /**
  207.      * Roll back a transaction and return to autocommit mode.
  208.      *
  209.      * @return void 
  210.      * @throws Doctrine_Adapter_Oracle_Exception
  211.      */
  212.     protected function _rollBack()
  213.     {
  214.         if (!oci_rollback($this->_connection)) {
  215.             throw new Doctrine_Adapter_Oracle_Exception(oci_error($this->_connection));
  216.         }
  217.         $this->_setExecuteMode(OCI_COMMIT_ON_SUCCESS);
  218.     }
  219.  
  220.     /**
  221.      * Set the fetch mode.
  222.      *
  223.      * @todo Support FETCH_CLASS and FETCH_INTO.
  224.      *
  225.      * @param integer $mode A fetch mode.
  226.      * @return void 
  227.      * @throws Doctrine_Adapter_Exception
  228.      */
  229.     public function setFetchMode($mode)
  230.     {
  231.         switch ($mode{
  232.             case Doctrine::FETCH_NUM:   // seq array
  233.             case Doctrine::FETCH_ASSOC// assoc array
  234.             case Doctrine::FETCH_BOTH:  // seq+assoc array
  235.             case Doctrine::FETCH_OBJ:   // object
  236.                 $this->_fetchMode $mode;
  237.                 break;
  238.             default:
  239.                 throw new Doctrine_Adapter_Exception('Invalid fetch mode specified');
  240.                 break;
  241.         }
  242.     }
  243.     /**
  244.      * @param integer $mode 
  245.      * @throws Doctrine_Adapter_Exception
  246.      */
  247.     private function _setExecuteMode($mode)
  248.     {
  249.         switch($mode{
  250.             case OCI_COMMIT_ON_SUCCESS:
  251.             case OCI_DEFAULT:
  252.             case OCI_DESCRIBE_ONLY:
  253.                 $this->_execute_mode $mode;
  254.                 break;
  255.             default:
  256.                 throw new Doctrine_Adapter_Exception('wrong execution mode specified');
  257.                 break;
  258.         }
  259.     }
  260.     /**
  261.      * @return 
  262.      */
  263.     public function _getExecuteMode()
  264.     {
  265.         return $this->_execute_mode;
  266.     }
  267. }