[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/dbal/src/drivers/sqlite/ -> lmbSqliteConnection.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__) . '/lmbSqliteDbInfo.class.php');
  12  lmb_require(dirname(__FILE__) . '/lmbSqliteQueryStatement.class.php');
  13  lmb_require(dirname(__FILE__) . '/lmbSqliteInsertStatement.class.php');
  14  lmb_require(dirname(__FILE__) . '/lmbSqliteDropStatement.class.php');
  15  lmb_require(dirname(__FILE__) . '/lmbSqliteManipulationStatement.class.php');
  16  lmb_require(dirname(__FILE__) . '/lmbSqliteStatement.class.php');
  17  lmb_require(dirname(__FILE__) . '/lmbSqliteTypeInfo.class.php');
  18  lmb_require(dirname(__FILE__) . '/lmbSqliteRecord.class.php');
  19  
  20  /**

  21   * class lmbSqliteConnection.

  22   *

  23   * @package dbal

  24   * @version $Id$

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

 176  
 177    }
 178  }
 179  
 180  ?>


Generated: Wed Oct 15 04:31:08 2008 Cross-referenced by PHPXref 0.7