Thomas
2013-10-14 566747af00ae413c942a7c6702e24c044af36f17
commit | author | age
0d94fd 1 <?php
AM 2
3d231c 3 /**
0d94fd 4  +-----------------------------------------------------------------------+
AM 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  *
9ab346 24  * @package    Framework
AM 25  * @subpackage Database
0d94fd 26  */
AM 27 class rcube_db_sqlite extends rcube_db
28 {
88107d 29     public $db_provider = 'sqlite';
TB 30
8c2375 31     /**
AM 32      * Prepare connection
33      */
0d94fd 34     protected function conn_prepare($dsn)
AM 35     {
36         // Create database file, required by PDO to exist on connection
37         if (!empty($dsn['database']) && !file_exists($dsn['database'])) {
146106 38             $created = touch($dsn['database']);
AM 39
40             // File mode setting, for compat. with MDB2
41             if (!empty($dsn['mode']) && $created) {
42                 chmod($dsn['database'], octdec($dsn['mode']));
43             }
0d94fd 44         }
AM 45     }
46
8c2375 47     /**
AM 48      * Configure connection, create database if not exists
49      */
0d94fd 50     protected function conn_configure($dsn, $dbh)
AM 51     {
52         // Initialize database structure in file is empty
53         if (!empty($dsn['database']) && !filesize($dsn['database'])) {
9be2f4 54             $data = file_get_contents(RCUBE_INSTALL_PATH . 'SQL/sqlite.initial.sql');
0d94fd 55
AM 56             if (strlen($data)) {
329eae 57                 $this->debug('INITIALIZE DATABASE');
0d94fd 58
AM 59                 $q = $dbh->exec($data);
60
61                 if ($q === false) {
146106 62                     $error = $dbh->errorInfo();
0d94fd 63                     $this->db_error = true;
AM 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     /**
14226f 75      * Return SQL statement to convert a field value into a unix timestamp
AM 76      *
77      * @param string $field Field name
78      *
79      * @return string  SQL statement to use in query
80      * @deprecated
0d94fd 81      */
14226f 82     public function unixtimestamp($field)
0d94fd 83     {
14226f 84         return "strftime('%s', $field)";
0d94fd 85     }
AM 86
87     /**
14226f 88      * Return SQL function for current time and date
AM 89      *
aa44ce 90      * @param int $interval Optional interval (in seconds) to add/subtract
AM 91      *
14226f 92      * @return string SQL function to use in query
0d94fd 93      */
aa44ce 94     public function now($interval = 0)
0d94fd 95     {
aa44ce 96         if ($interval) {
AM 97             $add = ($interval > 0 ? '+' : '') . intval($interval) . ' seconds';
98         }
99
100         return "datetime('now'" . ($add ? ",'$add'" : "") . ")";
0d94fd 101     }
AM 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
a8ffab 114             $this->tables = $q ? $q->fetchAll(PDO::FETCH_COLUMN, 0) : array();
0d94fd 115         }
AM 116
117         return $this->tables;
118     }
119
120     /**
121      * Returns list of columns in database table
122      *
3d231c 123      * @param string $table Table name
0d94fd 124      *
AM 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
8c2375 154     /**
AM 155      * Build DSN string for PDO constructor
156      */
0d94fd 157     protected function dsn_string($dsn)
AM 158     {
159         return $dsn['phptype'] . ':' . $dsn['database'];
160     }
161 }