PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/dbal/src/drivers/sqlite/lmbSqliteConnection.class.php

https://github.com/knevcher/korchasa_limb
PHP | 184 lines | 140 code | 31 blank | 13 comment | 11 complexity | fea1ef9f9b4ca5ea9c5bdb5b40b1da8f MD5 | raw file
  1. <?php
  2. /*
  3. * Limb PHP Framework
  4. *
  5. * @link http://limb-project.com
  6. * @copyright Copyright &copy; 2004-2009 BIT(http://bit-creative.com)
  7. * @license LGPL http://www.gnu.org/copyleft/lesser.html
  8. */
  9. lmb_require('limb/dbal/src/drivers/lmbDbBaseConnection.class.php');
  10. lmb_require(dirname(__FILE__) . '/lmbSqliteDbInfo.class.php');
  11. lmb_require(dirname(__FILE__) . '/lmbSqliteQueryStatement.class.php');
  12. lmb_require(dirname(__FILE__) . '/lmbSqliteInsertStatement.class.php');
  13. lmb_require(dirname(__FILE__) . '/lmbSqliteDropStatement.class.php');
  14. lmb_require(dirname(__FILE__) . '/lmbSqliteManipulationStatement.class.php');
  15. lmb_require(dirname(__FILE__) . '/lmbSqliteStatement.class.php');
  16. lmb_require(dirname(__FILE__) . '/lmbSqliteTypeInfo.class.php');
  17. lmb_require(dirname(__FILE__) . '/lmbSqliteRecord.class.php');
  18. /**
  19. * class lmbSqliteConnection.
  20. *
  21. * @package dbal
  22. * @version $Id$
  23. */
  24. class lmbSqliteConnection extends lmbDbBaseConnection
  25. {
  26. protected $connectionId;
  27. protected $in_transaction = false;
  28. function getFunctionForSystemSupportCheck()
  29. {
  30. return 'sqlite_open';
  31. }
  32. function getType()
  33. {
  34. return 'sqlite';
  35. }
  36. function getConnectionId()
  37. {
  38. if(!is_resource($this->connectionId))
  39. $this->connect();
  40. return $this->connectionId;
  41. }
  42. function connect()
  43. {
  44. $this->connectionId = sqlite_open($this->config['database'], 0666, $error);
  45. if($this->connectionId === false)
  46. $this->_raiseError();
  47. }
  48. function __wakeup()
  49. {
  50. $this->connectionId = null;
  51. }
  52. function disconnect()
  53. {
  54. if(is_resource($this->connectionId))
  55. {
  56. sqlite_close($this->connectionId);
  57. $this->connectionId = null;
  58. }
  59. }
  60. function _raiseError($sql = null)
  61. {
  62. if(!$this->connectionId)
  63. throw new lmbDbException('Could not connect to database "' . $this->config['database'] . '"');
  64. $errno = sqlite_last_error($this->connectionId);
  65. $info = array('driver' => 'sqlite');
  66. $info['errorno'] = $errno;
  67. $info['db'] = $this->config['database'];
  68. if(!is_null($sql))
  69. $info['sql'] = $sql;
  70. throw new lmbDbException(sqlite_error_string($errno) . ' SQL: '. $sql, $info);
  71. }
  72. function execute($sql)
  73. {
  74. $result = sqlite_query($this->getConnectionId(), $sql);
  75. if($result === false)
  76. $this->_raiseError($sql);
  77. return $result;
  78. }
  79. function executeStatement($stmt)
  80. {
  81. return (bool)$this->execute($stmt->getSQL());
  82. }
  83. function beginTransaction()
  84. {
  85. $this->execute('BEGIN');
  86. $this->in_transaction = true;
  87. }
  88. function commitTransaction()
  89. {
  90. if($this->in_transaction)
  91. {
  92. $this->execute('COMMIT');
  93. $this->in_transaction = false;
  94. }
  95. }
  96. function rollbackTransaction()
  97. {
  98. if($this->in_transaction)
  99. {
  100. $this->execute('ROLLBACK');
  101. $this->in_transaction = false;
  102. }
  103. }
  104. function newStatement($sql)
  105. {
  106. if(preg_match('/^\s*\(*\s*(\w+).*$/m', $sql, $match))
  107. $statement = $match[1];
  108. else
  109. $statement = $sql;
  110. switch(strtoupper($statement))
  111. {
  112. case 'SELECT':
  113. case 'SHOW':
  114. case 'DESCRIBE':
  115. case 'EXPLAIN':
  116. return new lmbSqliteQueryStatement($this, $sql);
  117. case 'INSERT':
  118. return new lmbSqliteInsertStatement($this, $sql);
  119. case 'DROP':
  120. return new lmbSqliteDropStatement($this, $sql);
  121. case 'UPDATE':
  122. case 'DELETE':
  123. return new lmbSqliteManipulationStatement($this, $sql);
  124. default:
  125. return new lmbSqliteStatement($this, $sql);
  126. }
  127. }
  128. function getTypeInfo()
  129. {
  130. return new lmbSqliteTypeInfo();
  131. }
  132. function getDatabaseInfo()
  133. {
  134. return new lmbSqliteDbInfo($this, $this->config['database'], true);
  135. }
  136. function quoteIdentifier($id)
  137. {
  138. if(!$id) return '';
  139. $pieces = explode('.', $id);
  140. $quoted = '"' . $pieces[0] . '"';
  141. if(isset($pieces[1]))
  142. $quoted .= '."' . $pieces[1] . '"';
  143. return $quoted;
  144. }
  145. function escape($string)
  146. {
  147. return sqlite_escape_string($string);
  148. }
  149. function getSequenceValue($table, $colname)
  150. {
  151. return sqlite_last_insert_rowid($this->connectionId);//???
  152. }
  153. }