commit | author | age
|
0d94fd
|
1 |
<?php |
AM |
2 |
|
|
3 |
/* |
|
4 |
+-----------------------------------------------------------------------+ |
|
5 |
| program/include/rcube_db_sqlite.php | |
|
6 |
| | |
|
7 |
| This file is part of the Roundcube Webmail client | |
|
8 |
| Copyright (C) 2005-2012, The Roundcube Dev Team | |
|
9 |
| | |
|
10 |
| Licensed under the GNU General Public License version 3 or | |
|
11 |
| any later version with exceptions for skins & plugins. | |
|
12 |
| See the README file for a full license statement. | |
|
13 |
| | |
|
14 |
| PURPOSE: | |
|
15 |
| Database wrapper class that implements PHP PDO functions | |
|
16 |
| for SQLite database | |
|
17 |
| | |
|
18 |
+-----------------------------------------------------------------------+ |
|
19 |
| Author: Aleksander Machniak <alec@alec.pl> | |
|
20 |
+-----------------------------------------------------------------------+ |
|
21 |
*/ |
|
22 |
|
|
23 |
|
|
24 |
/** |
|
25 |
* Database independent query interface |
|
26 |
* |
|
27 |
* This is a wrapper for the PHP PDO |
|
28 |
* |
|
29 |
* @package Database |
|
30 |
* @version 1.0 |
|
31 |
*/ |
|
32 |
class rcube_db_sqlite extends rcube_db |
|
33 |
{ |
8c2375
|
34 |
/** |
AM |
35 |
* Database character set |
|
36 |
*/ |
0d94fd
|
37 |
protected function set_charset($charset) |
AM |
38 |
{ |
|
39 |
} |
|
40 |
|
8c2375
|
41 |
/** |
AM |
42 |
* Prepare connection |
|
43 |
*/ |
0d94fd
|
44 |
protected function conn_prepare($dsn) |
AM |
45 |
{ |
|
46 |
// Create database file, required by PDO to exist on connection |
|
47 |
if (!empty($dsn['database']) && !file_exists($dsn['database'])) { |
|
48 |
touch($dsn['database']); |
|
49 |
} |
|
50 |
} |
|
51 |
|
8c2375
|
52 |
/** |
AM |
53 |
* Configure connection, create database if not exists |
|
54 |
*/ |
0d94fd
|
55 |
protected function conn_configure($dsn, $dbh) |
AM |
56 |
{ |
|
57 |
// we emulate via callback some missing functions |
|
58 |
$dbh->sqliteCreateFunction('unix_timestamp', array('rcube_db_sqlite', 'sqlite_unix_timestamp'), 1); |
|
59 |
$dbh->sqliteCreateFunction('now', array('rcube_db_sqlite', 'sqlite_now'), 0); |
|
60 |
|
|
61 |
// Initialize database structure in file is empty |
|
62 |
if (!empty($dsn['database']) && !filesize($dsn['database'])) { |
|
63 |
$data = file_get_contents(INSTALL_PATH . 'SQL/sqlite.initial.sql'); |
|
64 |
|
|
65 |
if (strlen($data)) { |
|
66 |
if ($this->options['debug_mode']) { |
|
67 |
$this::debug('INITIALIZE DATABASE'); |
|
68 |
} |
|
69 |
|
|
70 |
$q = $dbh->exec($data); |
|
71 |
|
|
72 |
if ($q === false) { |
|
73 |
$error = $this->dbh->errorInfo(); |
|
74 |
$this->db_error = true; |
|
75 |
$this->db_error_msg = sprintf('[%s] %s', $error[1], $error[2]); |
|
76 |
|
|
77 |
rcube::raise_error(array('code' => 500, 'type' => 'db', |
|
78 |
'line' => __LINE__, 'file' => __FILE__, |
|
79 |
'message' => $this->db_error_msg), true, false); |
|
80 |
} |
|
81 |
} |
|
82 |
} |
|
83 |
} |
|
84 |
|
|
85 |
/** |
|
86 |
* Callback for sqlite: unix_timestamp() |
|
87 |
*/ |
|
88 |
public static function sqlite_unix_timestamp($timestamp = '') |
|
89 |
{ |
|
90 |
$timestamp = trim($timestamp); |
|
91 |
if (!$timestamp) { |
|
92 |
$ret = time(); |
|
93 |
} |
|
94 |
else if (!preg_match('/^[0-9]+$/s', $timestamp)) { |
|
95 |
$ret = strtotime($timestamp); |
|
96 |
} |
|
97 |
else { |
|
98 |
$ret = $timestamp; |
|
99 |
} |
|
100 |
|
|
101 |
return $ret; |
|
102 |
} |
|
103 |
|
|
104 |
/** |
|
105 |
* Callback for sqlite: now() |
|
106 |
*/ |
|
107 |
public static function sqlite_now() |
|
108 |
{ |
|
109 |
return date("Y-m-d H:i:s"); |
|
110 |
} |
|
111 |
|
|
112 |
/** |
|
113 |
* Returns list of tables in database |
|
114 |
* |
|
115 |
* @return array List of all tables of the current database |
|
116 |
*/ |
|
117 |
public function list_tables() |
|
118 |
{ |
|
119 |
if ($this->tables === null) { |
|
120 |
$q = $this->query('SELECT name FROM sqlite_master' |
|
121 |
.' WHERE type = \'table\' ORDER BY name'); |
|
122 |
|
|
123 |
if ($res = $this->_get_result($q)) { |
|
124 |
$this->tables = $res->fetchAll(PDO::FETCH_COLUMN, 0); |
|
125 |
} |
|
126 |
else { |
|
127 |
$this->tables = array(); |
|
128 |
} |
|
129 |
} |
|
130 |
|
|
131 |
return $this->tables; |
|
132 |
} |
|
133 |
|
|
134 |
/** |
|
135 |
* Returns list of columns in database table |
|
136 |
* |
|
137 |
* @param string Table name |
|
138 |
* |
|
139 |
* @return array List of table cols |
|
140 |
*/ |
|
141 |
public function list_cols($table) |
|
142 |
{ |
|
143 |
$q = $this->query('SELECT sql FROM sqlite_master WHERE type = ? AND name = ?', |
|
144 |
array('table', $table)); |
|
145 |
|
|
146 |
$columns = array(); |
|
147 |
|
|
148 |
if ($sql = $this->fetch_array($q)) { |
|
149 |
$sql = $sql[0]; |
|
150 |
$start_pos = strpos($sql, '('); |
|
151 |
$end_pos = strrpos($sql, ')'); |
|
152 |
$sql = substr($sql, $start_pos+1, $end_pos-$start_pos-1); |
|
153 |
$lines = explode(',', $sql); |
|
154 |
|
|
155 |
foreach ($lines as $line) { |
|
156 |
$line = explode(' ', trim($line)); |
|
157 |
|
|
158 |
if ($line[0] && strpos($line[0], '--') !== 0) { |
|
159 |
$column = $line[0]; |
|
160 |
$columns[] = trim($column, '"'); |
|
161 |
} |
|
162 |
} |
|
163 |
} |
|
164 |
|
|
165 |
return $columns; |
|
166 |
} |
|
167 |
|
8c2375
|
168 |
/** |
AM |
169 |
* Build DSN string for PDO constructor |
|
170 |
*/ |
0d94fd
|
171 |
protected function dsn_string($dsn) |
AM |
172 |
{ |
|
173 |
return $dsn['phptype'] . ':' . $dsn['database']; |
|
174 |
} |
|
175 |
} |