commit | author | age
|
f45ec7
|
1 |
<?php |
S |
2 |
|
|
3 |
/* |
|
4 |
+-----------------------------------------------------------------------+ |
|
5 |
| program/include/rcube_db.inc | |
|
6 |
| | |
|
7 |
| This file is part of the RoundCube Webmail client | |
|
8 |
| Copyright (C) 2005, RoundCube Dev. - Switzerland | |
|
9 |
| Licensed under the GNU GPL | |
|
10 |
| | |
|
11 |
| PURPOSE: | |
|
12 |
| PEAR:DB wrapper class that implements PEAR DB functions | |
|
13 |
| See http://pear.php.net/package/DB | |
|
14 |
| | |
|
15 |
+-----------------------------------------------------------------------+ |
|
16 |
| Author: David Saez Padros <david@ols.es> | |
|
17 |
+-----------------------------------------------------------------------+ |
|
18 |
|
|
19 |
$Id$ |
|
20 |
|
|
21 |
*/ |
|
22 |
|
|
23 |
require_once('MDB2.php'); |
|
24 |
|
|
25 |
class rcube_db |
|
26 |
{ |
|
27 |
var $db_dsnw; // DSN for write operations |
|
28 |
var $db_dsnr; // DSN for read operations |
|
29 |
var $db_connected=false; // Already connected ? |
|
30 |
var $db_mode=''; // Connection mode |
|
31 |
var $db_handle=0; // Connection handle |
|
32 |
|
|
33 |
var $a_query_results = array('dummy'); |
|
34 |
var $last_res_id = 0; |
|
35 |
|
|
36 |
// PHP 5 constructor |
|
37 |
function __construct($db_dsnw,$db_dsnr='') |
|
38 |
{ |
|
39 |
if ($db_dsnr=='') $db_dsnr=$db_dsnw; |
|
40 |
|
|
41 |
$this->db_dsnw = $db_dsnw; |
|
42 |
$this->db_dsnr = $db_dsnr; |
|
43 |
|
|
44 |
$dsn_array = MDB2::parseDSN($db_dsnw); |
|
45 |
$this->db_provider = $dsn_array['phptype']; |
|
46 |
} |
|
47 |
|
|
48 |
// PHP 4 compatibility |
|
49 |
function rcube_db($db_dsnw,$db_dsnr='') |
|
50 |
{ |
|
51 |
$this->__construct($db_dsnw,$db_dsnr); |
|
52 |
} |
|
53 |
|
|
54 |
// Connect to specific database |
|
55 |
function dsn_connect($dsn) |
|
56 |
{ |
|
57 |
// Use persistent connections if available |
|
58 |
$dbh = MDB2::factory($dsn, array('persistent' => $true)); |
|
59 |
|
|
60 |
if (PEAR::isError($dbh)) |
|
61 |
raise_error(array('code' => 500, |
|
62 |
'type' => 'db', |
|
63 |
'line' => __LINE__, |
|
64 |
'file' => __FILE__, |
|
65 |
'message' => $dbh->getMessage()), TRUE, FALSE); |
|
66 |
|
|
67 |
else if ($this->db_provider=='sqlite') |
|
68 |
{ |
|
69 |
$dsn_array = MDB2::parseDSN($dsn); |
|
70 |
if (!filesize($dsn_array['database']) && !empty($this->sqlite_initials)) |
|
71 |
$this->_sqlite_create_database($dbh, $this->sqlite_initials); |
|
72 |
} |
|
73 |
|
|
74 |
return $dbh; |
|
75 |
} |
|
76 |
|
|
77 |
// Connect to appropiate databse |
|
78 |
function db_connect ($mode) |
|
79 |
{ |
|
80 |
$this->db_mode = $mode; |
|
81 |
|
|
82 |
// Already connected |
|
83 |
if ($this->db_connected) |
|
84 |
{ |
|
85 |
// no replication, current connection is ok |
|
86 |
if ($this->db_dsnw==$this->db_dsnr) return; |
|
87 |
|
|
88 |
// connected to master, current connection is ok |
|
89 |
if ($this->db_mode=='w') return; |
|
90 |
|
|
91 |
// Same mode, current connection is ok |
|
92 |
if ($this->db_mode==$mode) return; |
|
93 |
} |
|
94 |
|
|
95 |
if ($mode=='r') |
|
96 |
$dsn=$this->db_dsnr; |
|
97 |
else |
|
98 |
$dsn=$this->db_dsnw; |
|
99 |
|
|
100 |
$this->db_handle = $this->dsn_connect($dsn); |
|
101 |
$this->db_connected = true; |
|
102 |
} |
|
103 |
|
|
104 |
// Query database (read operations) |
|
105 |
|
|
106 |
function query($query) |
|
107 |
{ |
|
108 |
// Read or write ? |
|
109 |
if (strtolower(trim(substr($query,0,6)))=='select') |
|
110 |
$mode='r'; |
|
111 |
else |
|
112 |
$mode='w'; |
|
113 |
|
|
114 |
$this->db_connect($mode); |
|
115 |
|
|
116 |
if ($this->db_provider == 'sqlite') |
|
117 |
$query = $this->_sqlite_prepare_query($query); |
|
118 |
|
|
119 |
$result = $this->db_handle->query($query); |
|
120 |
|
|
121 |
if (PEAR::isError($result)) |
|
122 |
raise_error(array('code' => 500, |
|
123 |
'type' => 'db', |
|
124 |
'line' => __LINE__, |
|
125 |
'file' => __FILE__, |
|
126 |
'message' => $result->getMessage()), TRUE, FALSE); |
|
127 |
|
|
128 |
return $this->_add_result($result, $query); |
|
129 |
} |
|
130 |
|
|
131 |
function num_rows($res_id=NULL) |
|
132 |
{ |
|
133 |
if (!$this->db_handle) |
|
134 |
return FALSE; |
|
135 |
|
|
136 |
$result = $this->_get_result($res_id); |
|
137 |
|
|
138 |
if ($result) |
|
139 |
return $result->numRows(); |
|
140 |
else |
|
141 |
return FALSE; |
|
142 |
} |
|
143 |
|
|
144 |
function affected_rows($res_id=NULL) |
|
145 |
{ |
|
146 |
if (!$this->db_handle) |
|
147 |
return FALSE; |
|
148 |
|
|
149 |
return $this->db_handle->affectedRows(); |
|
150 |
} |
|
151 |
|
|
152 |
function insert_id($sequence = '') |
|
153 |
{ |
|
154 |
if (!$this->db_handle || $this->db_mode=='r') |
|
155 |
return FALSE; |
|
156 |
|
|
157 |
return $this->db_handle->lastInsertID(); |
|
158 |
} |
|
159 |
|
|
160 |
|
|
161 |
function fetch_assoc($res_id=NULL) |
|
162 |
{ |
|
163 |
$result = $this->_get_result($res_id); |
|
164 |
|
|
165 |
if (PEAR::isError($result)) |
|
166 |
{ |
|
167 |
raise_error( array('code' => 500, 'type' => 'db', 'line' => __LINE__, 'file' => __FILE__, |
|
168 |
'message' => $this->db_link->getMessage()), TRUE, FALSE); |
|
169 |
return FALSE; |
|
170 |
} |
|
171 |
|
|
172 |
return $result->fetchRow(MDB2_FETCHMODE_ASSOC); |
|
173 |
} |
|
174 |
|
|
175 |
function _add_result($res, $query) |
|
176 |
{ |
|
177 |
// sql error occured |
|
178 |
if (PEAR::isError($res)) |
|
179 |
{ |
|
180 |
raise_error(array('code' => 500, 'type' => 'db', 'line' => __LINE__, 'file' => __FILE__, 'message' => $res->getMessage() . " Query: " . substr(preg_replace('/[\r\n]+\s*/', ' ', $query), 0, 1024)), TRUE, FALSE); |
|
181 |
return FALSE; |
|
182 |
} |
|
183 |
else |
|
184 |
{ |
|
185 |
$res_id = sizeof($this->a_query_results); |
|
186 |
$this->a_query_results[$res_id] = $res; |
|
187 |
$this->last_res_id = $res_id; |
|
188 |
return $res_id; |
|
189 |
} |
|
190 |
} |
|
191 |
|
|
192 |
|
|
193 |
function _get_result($res_id) |
|
194 |
{ |
|
195 |
if ($res_id==NULL) |
|
196 |
$res_id = $this->last_res_id; |
|
197 |
|
|
198 |
if ($res_id && isset($this->a_query_results[$res_id])) |
|
199 |
return $this->a_query_results[$res_id]; |
|
200 |
else |
|
201 |
return FALSE; |
|
202 |
} |
|
203 |
|
|
204 |
|
|
205 |
// create a sqlite database from a file |
|
206 |
function _sqlite_create_database($dbh, $fileName) |
|
207 |
{ |
|
208 |
if (empty($fileName) || !is_string($fileName)) |
|
209 |
return ; |
|
210 |
|
|
211 |
$data = ''; |
|
212 |
if ($fd = fopen($fileName, 'r')) |
|
213 |
{ |
|
214 |
$data = fread($fd, filesize($fileName)); |
|
215 |
fclose($fd); |
|
216 |
} |
|
217 |
|
|
218 |
if (strlen($data)) |
|
219 |
sqlite_exec($dbh->connection, $data); |
|
220 |
} |
|
221 |
|
|
222 |
// transform a query so that it is sqlite2 compliant |
|
223 |
function _sqlite_prepare_query($query) |
|
224 |
{ |
|
225 |
if (!is_string($query)) |
|
226 |
return ($query); |
|
227 |
|
|
228 |
$search = array('/NOW\(\)/', |
|
229 |
'/`/'); |
|
230 |
$replace = array("datetime('now')", |
|
231 |
'"'); |
|
232 |
$query = preg_replace($search, $replace, $query); |
|
233 |
|
|
234 |
return ($query); |
|
235 |
} |
|
236 |
|
|
237 |
} |
|
238 |
|
|
239 |
?> |