[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/dbal/src/drivers/oci/ -> lmbOciConnection.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  lmb_require('limb/dbal/src/exception/lmbDbException.class.php');
  10  lmb_require('limb/dbal/src/drivers/lmbDbConnection.interface.php');
  11  lmb_require(dirname(__FILE__) . '/lmbOciDbInfo.class.php');
  12  lmb_require(dirname(__FILE__) . '/lmbOciQueryStatement.class.php');
  13  lmb_require(dirname(__FILE__) . '/lmbOciInsertStatement.class.php');
  14  lmb_require(dirname(__FILE__) . '/lmbOciUpdateStatement.class.php');
  15  lmb_require(dirname(__FILE__) . '/lmbOciManipulationStatement.class.php');
  16  lmb_require(dirname(__FILE__) . '/lmbOciStatement.class.php');
  17  lmb_require(dirname(__FILE__) . '/lmbOciTypeInfo.class.php');
  18  lmb_require(dirname(__FILE__) . '/lmbOciRecord.class.php');
  19  
  20  /**

  21   * class lmbOciConnection.

  22   *

  23   * @package dbal

  24   * @version $Id: lmbOciConnection.class.php 5945 2007-06-06 08:31:43Z pachanga $

  25   */
  26  class lmbOciConnection implements lmbDbConnection
  27  {
  28    protected $connectionId;
  29    protected $config;
  30  
  31    //Transaction state. Should be either OCI_COMMIT_ON_SUCCESS or OCI_DEFAULT

  32    protected $tstate = OCI_COMMIT_ON_SUCCESS;
  33  
  34    function __construct($config)
  35    {
  36      $this->config = $config;
  37    }
  38  
  39    function getConfig()
  40    {
  41      return $this->config;
  42    }
  43  
  44    function getType()
  45    {
  46      return 'oci';
  47    }
  48  
  49    function getConnectionId()
  50    {
  51      if(!isset($this->connectionId))
  52        $this->connect();
  53      return $this->connectionId;
  54    }
  55  
  56    function getHash()
  57    {
  58      return crc32(serialize($this->config));
  59    }
  60  
  61    //based on Creole code

  62    function connect()
  63    {
  64      $user                = $this->config->get('user');
  65      $pw                    = $this->config->get('password');
  66      $hostspec        = $this->config->get('host');
  67      $port       = $this->config->get('port');
  68      $db                    = $this->config->get('database');
  69      $persistent    = $this->config->get('persistent');
  70      $charset    = $this->config->get('charset');
  71  
  72      $connect_function = $persistent ? 'oci_pconnect' : 'oci_connect';
  73  
  74      @ini_set('track_errors', true);
  75  
  76      if($hostspec && $port)
  77        $hostspec .= ':' . $port;
  78  
  79      if($db && $hostspec && $user && $pw)
  80        $conn = @$connect_function($user, $pw, "//$hostspec/$db", $charset);
  81      elseif ($hostspec && $user && $pw)
  82        $conn = @$connect_function($user, $pw, $hostspec, $charset);
  83      else
  84        $conn = false;
  85  
  86      @ini_restore('track_errors');
  87  
  88      if($conn == false)
  89        $this->_raiseError();
  90  
  91      $this->connectionId = $conn;
  92  
  93      //connected ok, need to set a few environment settings

  94      //please note, if this is changed, the function setTimestamp and setDate in OCI8PreparedStatement.php

  95      //must be changed to match

  96      //$sql = "ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'";

  97      //$this->execute($sql);

  98    }
  99  
 100    function __wakeup()
 101    {
 102      $this->connectionId = null;
 103    }
 104  
 105    function disconnect()
 106    {
 107      if($this->connectionId)
 108      {
 109        oci_close($this->connectionId);
 110        $this->connectionId = null;
 111      }
 112    }
 113  
 114    function _raiseError($stmt = null)
 115    {
 116      if($stmt)
 117        $error  = oci_error($stmt);
 118      else
 119        $error  = oci_error();
 120      if(is_array($error))
 121        throw new lmbDbException($error['message'] . ' in ' . $error['sqltext'],
 122                                 array('code' => $error['code'],
 123                                       'offset' => $error['offset']));
 124      else
 125        throw new lmbDbException('Some unknown oci error occured');
 126    }
 127  
 128    function execute($sql)
 129    {
 130      $stmt = oci_parse($this->getConnectionId(), $sql);
 131      return $this->executeStatement($stmt);
 132    }
 133  
 134    function executeStatement($stmt)
 135    {
 136      $result = oci_execute($stmt, $this->tstate);
 137      if($result === false)
 138        $this->_raiseError($stmt);
 139      return $stmt;
 140    }
 141  
 142    function beginTransaction()
 143    {
 144      $this->tstate = OCI_DEFAULT;
 145    }
 146  
 147    function commitTransaction()
 148    {
 149      if(!oci_commit($this->connectionId))
 150        $this->_raiseError();
 151      $this->tstate = OCI_COMMIT_ON_SUCCESS;
 152    }
 153  
 154    function rollbackTransaction()
 155    {
 156      if(!oci_rollback($this->connectionId))
 157        $this->_raiseError();
 158      $this->tstate = OCI_COMMIT_ON_SUCCESS;
 159    }
 160  
 161    function newStatement($sql)
 162    {
 163      if(preg_match('/^\s*\(*\s*(\w+).*$/m', $sql, $match))
 164        $statement = $match[1];
 165      else
 166        $statement = $sql;
 167  
 168      switch(strtoupper($statement))
 169      {
 170        case 'SELECT':
 171          if(stripos($sql, ' FROM ') === false) //a quick hack
 172            $sql = $sql . ' FROM DUAL';
 173        case 'SHOW':
 174        case 'DESCRIBE':
 175        case 'EXPLAIN':
 176          return new lmbOciQueryStatement($this, $sql);
 177        case 'INSERT':
 178          return new lmbOciInsertStatement($this, $sql);
 179        case 'UPDATE':
 180          return new lmbOciUpdateStatement($this, $sql);
 181        case 'DELETE':
 182          return new lmbOciManipulationStatement($this, $sql);
 183        default:
 184          return new lmbOciStatement($this, $sql);
 185      }
 186    }
 187  
 188    function getTypeInfo()
 189    {
 190      return new lmbOciTypeInfo();
 191    }
 192  
 193    function getDatabaseInfo()
 194    {
 195      return new lmbOciDbInfo($this, $this->config['database'], true);
 196    }
 197  
 198    function quoteIdentifier($id)
 199    {
 200      if(!$id)
 201        return '';
 202      $pieces = explode('.', $id);
 203      $quoted = '"' . strtoupper($pieces[0]) . '"';
 204      if(isset($pieces[1]))
 205         $quoted .= '."' . strtoupper($pieces[1]) . '"';
 206      return $quoted;
 207    }
 208  
 209    function getSequenceValue($table, $colname)
 210    {
 211      $seq = substr("{$table}", 0, 26) . "_seq";
 212      return (int)$this->newStatement("SELECT $seq.CURRVAL FROM DUAL")->getOneValue();
 213    }
 214  }
 215  ?>


Generated: Thu Aug 21 04:38:04 2008 Cross-referenced by PHPXref 0.7