[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/dbal/src/drivers/pgsql/ -> lmbPgsqlConnection.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/drivers/lmbDbConnection.interface.php');
  10  lmb_require(dirname(__FILE__) . '/lmbPgsqlQueryStatement.class.php');
  11  lmb_require(dirname(__FILE__) . '/lmbPgsqlDropStatement.class.php');
  12  lmb_require(dirname(__FILE__) . '/lmbPgsqlInsertStatement.class.php');
  13  lmb_require(dirname(__FILE__) . '/lmbPgsqlManipulationStatement.class.php');
  14  lmb_require(dirname(__FILE__) . '/lmbPgsqlStatement.class.php');
  15  lmb_require(dirname(__FILE__) . '/lmbPgsqlDbInfo.class.php');
  16  lmb_require(dirname(__FILE__) . '/lmbPgsqlTypeInfo.class.php');
  17  
  18  /**

  19   * class lmbPgsqlConnection.

  20   *

  21   * @package dbal

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

  23   */
  24  class lmbPgsqlConnection implements lmbDbConnection
  25  {
  26    protected $connectionId;
  27    protected $config;
  28  
  29    function __construct($config)
  30    {
  31      $this->config = $config;
  32    }
  33  
  34    function getType()
  35    {
  36      return 'pgsql';
  37    }
  38  
  39    function getConnectionId()
  40    {
  41      if(!isset($this->connectionId))
  42      {
  43        $this->connect();
  44      }
  45      return $this->connectionId;
  46    }
  47  
  48    function getHash()
  49    {
  50      return crc32(serialize($this->config));
  51    }
  52  
  53    function connect()
  54    {
  55  
  56      global $php_errormsg;
  57  
  58      $persistent = $this->config['persistent'];
  59  
  60      $connstr = '';
  61  
  62      if($host = $this->config['host'])
  63      {
  64        $connstr = 'host=' . $host;
  65      }
  66      if($port = $this->config['port'])
  67      {
  68        $connstr .= ' port=' . $port;
  69      }
  70      if($database = $this->config['database'])
  71      {
  72        $connstr .= ' dbname=\'' . addslashes($database) . '\'';
  73      }
  74      if($user = $this->config['user'])
  75      {
  76        $connstr .= ' user=\'' . addslashes($user) . '\'';
  77      }
  78      if($password = $this->config['password'])
  79      {
  80        $connstr .= ' password=\'' . addslashes($password) . '\'';
  81      }
  82  
  83      if($persistent)
  84      {
  85        $conn = @pg_pconnect($connstr);
  86      }
  87      else
  88      {
  89        $conn = @pg_connect($connstr);
  90      }
  91  
  92      if(!is_resource($conn))
  93      {
  94        $this->_raiseError($php_errormsg);
  95      }
  96  
  97      if($charset = $this->config['charset'])
  98      {
  99        pg_set_client_encoding($conn, $charset);
 100      }
 101  
 102      $this->connectionId = $conn;
 103    }
 104  
 105    function __wakeup()
 106    {
 107      $this->connectionId = null;
 108    }
 109  
 110    function disconnect()
 111    {
 112      if($this->connectionId)
 113      {
 114        @pg_close($this->connectionId);
 115        $this->connectionId = null;
 116      }
 117    }
 118  
 119    function _raiseError($msg)
 120    {
 121      throw new lmbException($msg .($this->connectionId ?  ' last pgsql driver error: ' . pg_last_error($this->connectionId) : ''));
 122    }
 123  
 124    function execute($sql)
 125    {
 126      $result = @pg_query($this->getConnectionId(), $sql);
 127      if($result === false)
 128      {
 129        $this->_raiseError($sql);
 130      }
 131      return $result;
 132    }
 133  
 134    function beginTransaction()
 135    {
 136      $this->execute('BEGIN');
 137    }
 138  
 139    function commitTransaction()
 140    {
 141      $this->execute('COMMIT');
 142    }
 143  
 144    function rollbackTransaction()
 145    {
 146      $this->execute('ROLLBACK');
 147    }
 148  
 149    function newStatement($sql)
 150    {
 151      if(preg_match('/^\s*\(*\s*(\w+).*$/m', $sql, $match))
 152      {
 153        $statement = $match[1];
 154      }
 155      else
 156      {
 157        $statement = $sql;
 158      }
 159      switch(strtoupper($statement))
 160      {
 161        case 'SELECT':
 162        case 'SHOW':
 163        case 'DESCRIBE':
 164        case 'EXPLAIN':
 165        return new lmbPgsqlQueryStatement($this, $sql);
 166        case 'DROP':
 167        return new lmbPgsqlDropStatement($this, $sql);
 168        case 'INSERT':
 169        return new lmbPgsqlInsertStatement($this, $sql);
 170        case 'UPDATE':
 171        case 'DELETE':
 172        return new lmbPgsqlManipulationStatement($this, $sql);
 173        default:
 174        return new lmbPgsqlStatement($this, $sql);
 175      }
 176    }
 177  
 178    function getTypeInfo()
 179    {
 180      return new lmbPgsqlTypeInfo();
 181    }
 182  
 183    function getDatabaseInfo()
 184    {
 185      return new lmbPgsqlDbInfo($this, $this->config['database'], true);
 186    }
 187  
 188    function quoteIdentifier($id)
 189    {
 190      if(!$id)
 191        return '';
 192      $pieces = explode('.', $id);
 193      $quoted = '"' . $pieces[0] . '"';
 194      if(isset($pieces[1]))
 195         $quoted .= '."' . $pieces[1] . '"';
 196      return $quoted;
 197    }
 198  
 199    function getSequenceValue($table, $colname)
 200    {
 201      $seq = "{$table}_{$colname}_seq";
 202      return (int)$this->newStatement("SELECT currval('$seq')")->getOneValue();
 203    }
 204  }
 205  
 206  ?>


Generated: Sat Nov 22 03:48:54 2008 Cross-referenced by PHPXref 0.7