You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rcube_db_sqlite.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------+
  4. | This file is part of the Roundcube Webmail client |
  5. | Copyright (C) 2005-2012, The Roundcube Dev Team |
  6. | |
  7. | Licensed under the GNU General Public License version 3 or |
  8. | any later version with exceptions for skins & plugins. |
  9. | See the README file for a full license statement. |
  10. | |
  11. | PURPOSE: |
  12. | Database wrapper class that implements PHP PDO functions |
  13. | for SQLite database |
  14. +-----------------------------------------------------------------------+
  15. | Author: Aleksander Machniak <alec@alec.pl> |
  16. +-----------------------------------------------------------------------+
  17. */
  18. /**
  19. * Database independent query interface
  20. * This is a wrapper for the PHP PDO
  21. *
  22. * @package Framework
  23. * @subpackage Database
  24. */
  25. class rcube_db_sqlite extends rcube_db
  26. {
  27. public $db_provider = 'sqlite';
  28. /**
  29. * Prepare connection
  30. */
  31. protected function conn_prepare($dsn)
  32. {
  33. // Create database file, required by PDO to exist on connection
  34. if (!empty($dsn['database']) && !file_exists($dsn['database'])) {
  35. $created = touch($dsn['database']);
  36. // File mode setting, for compat. with MDB2
  37. if (!empty($dsn['mode']) && $created) {
  38. chmod($dsn['database'], octdec($dsn['mode']));
  39. }
  40. }
  41. }
  42. /**
  43. * Configure connection, create database if not exists
  44. */
  45. protected function conn_configure($dsn, $dbh)
  46. {
  47. // Initialize database structure in file is empty
  48. if (!empty($dsn['database']) && !filesize($dsn['database'])) {
  49. $data = file_get_contents(RCUBE_INSTALL_PATH . 'SQL/sqlite.initial.sql');
  50. if (strlen($data)) {
  51. $this->debug('INITIALIZE DATABASE');
  52. $q = $dbh->exec($data);
  53. if ($q === false) {
  54. $error = $dbh->errorInfo();
  55. $this->db_error = true;
  56. $this->db_error_msg = sprintf('[%s] %s', $error[1], $error[2]);
  57. rcube::raise_error(array('code' => 500, 'type' => 'db',
  58. 'line' => __LINE__, 'file' => __FILE__,
  59. 'message' => $this->db_error_msg), true, false);
  60. }
  61. }
  62. }
  63. }
  64. /**
  65. * Return SQL statement to convert a field value into a unix timestamp
  66. *
  67. * @param string $field Field name
  68. *
  69. * @return string SQL statement to use in query
  70. * @deprecated
  71. */
  72. public function unixtimestamp($field)
  73. {
  74. return "strftime('%s', $field)";
  75. }
  76. /**
  77. * Return SQL function for current time and date
  78. *
  79. * @param int $interval Optional interval (in seconds) to add/subtract
  80. *
  81. * @return string SQL function to use in query
  82. */
  83. public function now($interval = 0)
  84. {
  85. if ($interval) {
  86. $add = ($interval > 0 ? '+' : '') . intval($interval) . ' seconds';
  87. }
  88. return "datetime('now'" . ($add ? ",'$add'" : "") . ")";
  89. }
  90. /**
  91. * Returns list of tables in database
  92. *
  93. * @return array List of all tables of the current database
  94. */
  95. public function list_tables()
  96. {
  97. if ($this->tables === null) {
  98. $q = $this->query('SELECT name FROM sqlite_master'
  99. .' WHERE type = \'table\' ORDER BY name');
  100. $this->tables = $q ? $q->fetchAll(PDO::FETCH_COLUMN, 0) : array();
  101. }
  102. return $this->tables;
  103. }
  104. /**
  105. * Returns list of columns in database table
  106. *
  107. * @param string $table Table name
  108. *
  109. * @return array List of table cols
  110. */
  111. public function list_cols($table)
  112. {
  113. $q = $this->query('SELECT sql FROM sqlite_master WHERE type = ? AND name = ?',
  114. array('table', $table));
  115. $columns = array();
  116. if ($sql = $this->fetch_array($q)) {
  117. $sql = $sql[0];
  118. $start_pos = strpos($sql, '(');
  119. $end_pos = strrpos($sql, ')');
  120. $sql = substr($sql, $start_pos+1, $end_pos-$start_pos-1);
  121. $lines = explode(',', $sql);
  122. foreach ($lines as $line) {
  123. $line = explode(' ', trim($line));
  124. if ($line[0] && strpos($line[0], '--') !== 0) {
  125. $column = $line[0];
  126. $columns[] = trim($column, '"');
  127. }
  128. }
  129. }
  130. return $columns;
  131. }
  132. /**
  133. * Build DSN string for PDO constructor
  134. */
  135. protected function dsn_string($dsn)
  136. {
  137. return $dsn['phptype'] . ':' . $dsn['database'];
  138. }
  139. }