Aleksander Machniak
2012-08-08 2bbc3da52aee81e920e46778d68278bd31f7bb6b
commit | author | age
0d94fd 1 <?php
AM 2
3d231c 3 /**
0d94fd 4  +-----------------------------------------------------------------------+
AM 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  *
3d231c 29  * @package Database
AM 30  * @version 1.0
0d94fd 31  */
AM 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'])) {
146106 48             $created = touch($dsn['database']);
AM 49
50             // File mode setting, for compat. with MDB2
51             if (!empty($dsn['mode']) && $created) {
52                 chmod($dsn['database'], octdec($dsn['mode']));
53             }
0d94fd 54         }
AM 55     }
56
8c2375 57     /**
AM 58      * Configure connection, create database if not exists
59      */
0d94fd 60     protected function conn_configure($dsn, $dbh)
AM 61     {
62         // we emulate via callback some missing functions
63         $dbh->sqliteCreateFunction('unix_timestamp', array('rcube_db_sqlite', 'sqlite_unix_timestamp'), 1);
64         $dbh->sqliteCreateFunction('now', array('rcube_db_sqlite', 'sqlite_now'), 0);
65
66         // Initialize database structure in file is empty
67         if (!empty($dsn['database']) && !filesize($dsn['database'])) {
68             $data = file_get_contents(INSTALL_PATH . 'SQL/sqlite.initial.sql');
69
70             if (strlen($data)) {
329eae 71                 $this->debug('INITIALIZE DATABASE');
0d94fd 72
AM 73                 $q = $dbh->exec($data);
74
75                 if ($q === false) {
146106 76                     $error = $dbh->errorInfo();
0d94fd 77                     $this->db_error = true;
AM 78                     $this->db_error_msg = sprintf('[%s] %s', $error[1], $error[2]);
79
80                     rcube::raise_error(array('code' => 500, 'type' => 'db',
81                         'line' => __LINE__, 'file' => __FILE__,
82                         'message' => $this->db_error_msg), true, false);
83                 }
84             }
85         }
86     }
87
88     /**
89      * Callback for sqlite: unix_timestamp()
90      */
91     public static function sqlite_unix_timestamp($timestamp = '')
92     {
93         $timestamp = trim($timestamp);
94         if (!$timestamp) {
95             $ret = time();
96         }
97         else if (!preg_match('/^[0-9]+$/s', $timestamp)) {
98             $ret = strtotime($timestamp);
99         }
100         else {
101             $ret = $timestamp;
102         }
103
104         return $ret;
105     }
106
107     /**
108      * Callback for sqlite: now()
109      */
110     public static function sqlite_now()
111     {
112         return date("Y-m-d H:i:s");
113     }
114
115     /**
116      * Returns list of tables in database
117      *
118      * @return array List of all tables of the current database
119      */
120     public function list_tables()
121     {
122         if ($this->tables === null) {
123             $q = $this->query('SELECT name FROM sqlite_master'
124                 .' WHERE type = \'table\' ORDER BY name');
125
126             if ($res = $this->_get_result($q)) {
127                 $this->tables = $res->fetchAll(PDO::FETCH_COLUMN, 0);
128             }
129             else {
130                 $this->tables = array();
131             }
132         }
133
134         return $this->tables;
135     }
136
137     /**
138      * Returns list of columns in database table
139      *
3d231c 140      * @param string $table Table name
0d94fd 141      *
AM 142      * @return array List of table cols
143      */
144     public function list_cols($table)
145     {
146         $q = $this->query('SELECT sql FROM sqlite_master WHERE type = ? AND name = ?',
147             array('table', $table));
148
149         $columns = array();
150
151         if ($sql = $this->fetch_array($q)) {
152             $sql       = $sql[0];
153             $start_pos = strpos($sql, '(');
154             $end_pos   = strrpos($sql, ')');
155             $sql       = substr($sql, $start_pos+1, $end_pos-$start_pos-1);
156             $lines     = explode(',', $sql);
157
158             foreach ($lines as $line) {
159                 $line = explode(' ', trim($line));
160
161                 if ($line[0] && strpos($line[0], '--') !== 0) {
162                     $column = $line[0];
163                     $columns[] = trim($column, '"');
164                 }
165             }
166         }
167
168         return $columns;
169     }
170
8c2375 171     /**
AM 172      * Build DSN string for PDO constructor
173      */
0d94fd 174     protected function dsn_string($dsn)
AM 175     {
176         return $dsn['phptype'] . ':' . $dsn['database'];
177     }
178 }