[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/dbal/src/drivers/mysql/ -> lmbMysqlConnection.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  
  10  lmb_require('limb/dbal/src/drivers/lmbDbConnection.interface.php');
  11  lmb_require(dirname(__FILE__) . '/lmbMysqlDbInfo.class.php');
  12  lmb_require(dirname(__FILE__) . '/lmbMysqlQueryStatement.class.php');
  13  lmb_require(dirname(__FILE__) . '/lmbMysqlInsertStatement.class.php');
  14  lmb_require(dirname(__FILE__) . '/lmbMysqlManipulationStatement.class.php');
  15  lmb_require(dirname(__FILE__) . '/lmbMysqlStatement.class.php');
  16  lmb_require(dirname(__FILE__) . '/lmbMysqlTypeInfo.class.php');
  17  
  18  /**

  19   * class lmbMysqlConnection.

  20   *

  21   * @package dbal

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

  23   */
  24  class lmbMysqlConnection implements lmbDbConnection
  25  {
  26  
  27    protected $connectionId;
  28    protected $config;
  29  
  30    function __construct($config)
  31    {
  32      $this->config = $config;
  33    }
  34  
  35    function getType()
  36    {
  37      return 'mysql';
  38    }
  39  
  40    function getConnectionId()
  41    {
  42      if(!isset($this->connectionId))
  43      {
  44        $this->connect();
  45      }
  46      return $this->connectionId;
  47    }
  48  
  49    function getHash()
  50    {
  51      return crc32(serialize($this->config));
  52    }
  53  
  54    function connect()
  55    {
  56      $this->connectionId = mysql_connect($this->config['host'],
  57                                          $this->config['user'],
  58                                          $this->config['password'],
  59                                          true);
  60  
  61      if($this->connectionId === false)
  62      {
  63        $this->_raiseError();
  64      }
  65  
  66      if(mysql_select_db($this->config['database'], $this->connectionId) === false)
  67      {
  68        $this->_raiseError();
  69      }
  70  
  71      if($charset = $this->config['charset'])
  72      {
  73        mysql_query("SET NAMES '$charset'",  $this->connectionId);
  74      }
  75    }
  76  
  77    function __wakeup()
  78    {
  79      $this->connectionId = null;
  80    }
  81  
  82    function disconnect()
  83    {
  84      if($this->connectionId)
  85      {
  86        mysql_close($this->connectionId);
  87        $this->connectionId = null;
  88      }
  89    }
  90  
  91    function _raiseError($sql = null)
  92    {
  93      if(!$this->getConnectionId())
  94        throw new lmbDbException('Could not connect to host "' . $this->config['host'] . '" and database "' . $this->config['database'] . '"');
  95  
  96      $errno = mysql_errno($this->getConnectionId());
  97      $id = 'DB_ERROR';
  98      $info = array('driver' => 'lmbMysql');
  99      if($errno != 0)
 100      {
 101        $info['errorno'] = $errno;
 102        $info['error'] = mysql_error($this->getConnectionId());
 103        $id .= '_MESSAGE';
 104      }
 105      if(!is_null($sql))
 106      {
 107        $info['sql'] = $sql;
 108        $id .= '_SQL';
 109      }
 110      throw new lmbDbException(mysql_error($this->getConnectionId()) . ' SQL: '. $sql, $info);
 111    }
 112  
 113    function execute($sql)
 114    {
 115      $result = mysql_query($sql, $this->getConnectionId());
 116      if($result === false)
 117      {
 118        $this->_raiseError($sql);
 119      }
 120      return $result;
 121    }
 122  
 123    function beginTransaction()
 124    {
 125      $this->execute('BEGIN');
 126    }
 127  
 128    function commitTransaction()
 129    {
 130      $this->execute('COMMIT');
 131    }
 132  
 133    function rollbackTransaction()
 134    {
 135      $this->execute('ROLLBACK');
 136    }
 137  
 138    function newStatement($sql)
 139    {
 140      if(preg_match('/^\s*\(*\s*(\w+).*$/m', $sql, $match))
 141      {
 142        $statement = $match[1];
 143      }
 144      else
 145      {
 146        $statement = $sql;
 147      }
 148      switch(strtoupper($statement))
 149      {
 150        case 'SELECT':
 151        case 'SHOW':
 152        case 'DESCRIBE':
 153        case 'EXPLAIN':
 154        return new lmbMysqlQueryStatement($this, $sql);
 155        case 'INSERT':
 156        return new lmbMysqlInsertStatement($this, $sql);
 157        case 'UPDATE':
 158        case 'DELETE':
 159        return new lmbMysqlManipulationStatement($this, $sql);
 160        default:
 161        return new lmbMysqlStatement($this, $sql);
 162      }
 163    }
 164  
 165    function getTypeInfo()
 166    {
 167      return new lmbMysqlTypeInfo();
 168    }
 169  
 170  
 171    function getDatabaseInfo()
 172    {
 173      return new lmbMysqlDbInfo($this, $this->config['database'], true);
 174    }
 175  
 176    function quoteIdentifier($id)
 177    {
 178      if(!$id)
 179        return '';
 180  
 181      $pieces = explode('.', $id);
 182      $quoted = '`' . $pieces[0] . '`';
 183      if(isset($pieces[1]))
 184         $quoted .= '.`' . $pieces[1] . '`';
 185      return $quoted;
 186    }
 187  
 188    function getSequenceValue($table, $colname)
 189    {
 190      return mysql_insert_id($this->connectionId);//???

 191  
 192    }
 193  }
 194  
 195  ?>


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