SQLite.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /** @package verysimple::DB::DataDriver */
  3. require_once("IDataDriver.php");
  4. require_once("verysimple/DB/ISqlFunction.php");
  5. require_once("verysimple/DB/DatabaseException.php");
  6. require_once("verysimple/DB/DatabaseConfig.php");
  7. /**
  8. * An implementation of IDataDriver that communicates with
  9. * a SQLite database file. This is one of the native drivers
  10. * supported by Phreeze
  11. *
  12. * @package verysimple::DB::DataDriver
  13. * @author VerySimple Inc. <[email protected]>
  14. * @copyright 1997-2010 VerySimple Inc.
  15. * @license http://www.gnu.org/licenses/lgpl.html LGPL
  16. * @version 1.0
  17. */
  18. class DataDriverSQLite implements IDataDriver
  19. {
  20. /**
  21. * @inheritdocs
  22. */
  23. function GetServerType()
  24. {
  25. return "SQLite";
  26. }
  27. function Ping($connection)
  28. {
  29. throw new DatabaseException("Not Implemented");
  30. }
  31. /**
  32. * @inheritdocs
  33. */
  34. function Open($connectionstring,$database,$username,$password,$charset='',$bootstrap='')
  35. {
  36. if (!class_exists("SQLite3")) throw new DatabaseException('SQLite3 extension is not enabled on this server.',DatabaseException::$CONNECTION_ERROR);
  37. if ( !$connection = new SQLite3($connectionstring, SQLITE3_OPEN_READWRITE,$password) )
  38. {
  39. throw new DatabaseException("Error connecting to database: Unable to open the database file.",DatabaseException::$CONNECTION_ERROR);
  40. }
  41. // charset is ignored with sqlite
  42. if ($bootstrap)
  43. {
  44. $statements = explode(';',$bootstrap);
  45. foreach ($statements as $sql)
  46. {
  47. try
  48. {
  49. $this->Execute($connection, $sql);
  50. }
  51. catch (Exception $ex)
  52. {
  53. throw new DatabaseException("problem with bootstrap sql: " . $ex->getMessage(),DatabaseException::$ERROR_IN_QUERY);
  54. }
  55. }
  56. }
  57. return $connection;
  58. }
  59. /**
  60. * @inheritdocs
  61. */
  62. function Close($connection)
  63. {
  64. @$connection->close(); // ignore warnings
  65. }
  66. /**
  67. * @inheritdocs
  68. */
  69. function Query($connection,$sql)
  70. {
  71. if ( !$rs = $connection->query($sql) )
  72. {
  73. throw new DatabaseException($connection->lastErrorMsg(),DatabaseException::$ERROR_IN_QUERY);
  74. }
  75. return $rs;
  76. }
  77. /**
  78. * @inheritdocs
  79. */
  80. function Execute($connection,$sql)
  81. {
  82. return $connection->exec($sql);
  83. }
  84. /**
  85. * @inheritdocs
  86. */
  87. function Fetch($connection,$rs)
  88. {
  89. return $rs->fetchArray(SQLITE3_ASSOC);
  90. }
  91. /**
  92. * @inheritdocs
  93. */
  94. function GetLastInsertId($connection)
  95. {
  96. return $connection->lastInsertRowID();
  97. }
  98. /**
  99. * @inheritdocs
  100. */
  101. function GetLastError($connection)
  102. {
  103. return $connection->lastErrorMsg();
  104. }
  105. /**
  106. * @inheritdocs
  107. */
  108. function Release($connection,$rs)
  109. {
  110. $rs->finalize();
  111. }
  112. /**
  113. * @inheritdocs
  114. * @TODO: use SQLite
  115. */
  116. function Escape($val)
  117. {
  118. return str_replace("'","''",$val);
  119. }
  120. /**
  121. * @inheritdocs
  122. */
  123. public function GetQuotedSql($val)
  124. {
  125. if ($val === null) return DatabaseConfig::$CONVERT_NULL_TO_EMPTYSTRING ? "''" : 'NULL';
  126. if ($val instanceof ISqlFunction) return $val->GetQuotedSql($this);
  127. return "'" . $this->Escape($val) . "'";
  128. }
  129. /**
  130. * @inheritdocs
  131. */
  132. function GetTableNames($connection, $dbname, $ommitEmptyTables = false)
  133. {
  134. if ($ommitEmptyTables) throw new DatabaseException("SQLite DataDriver doesn't support returning only non-empty tables. Set ommitEmptyTables arg to false to use this method.");
  135. $rs = $this->Query($connection,"SELECT name FROM sqlite_master WHERE type='table' and name != 'sqlite_sequence' ORDER BY name");
  136. $tables = array();
  137. while ( $row = $this->Fetch($connection,$rs) )
  138. {
  139. $tables[] = $row['name'];
  140. }
  141. return $tables;
  142. }
  143. /**
  144. * @inheritdocs
  145. */
  146. function Optimize($connection,$table)
  147. {
  148. if ($table) throw new DatabaseException("SQLite optimization is database-wide. Call Optimize() with a blank/null table arg to use this method.");
  149. $this->Execute($connection,"VACUUM");
  150. }
  151. }
  152. ?>