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