commit | author | age
|
1676e1
|
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 |
+-----------------------------------------------------------------------+ |
adf95d
|
16 |
| Author: David Saez Padros <david@ols.es> | |
1676e1
|
17 |
+-----------------------------------------------------------------------+ |
S |
18 |
|
|
19 |
$Id$ |
|
20 |
|
|
21 |
*/ |
|
22 |
|
|
23 |
require_once('DB.php'); |
|
24 |
|
|
25 |
class rcube_db |
|
26 |
{ |
adf95d
|
27 |
var $db_dsnw; // DSN for write operations |
T |
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 |
1676e1
|
32 |
|
S |
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 |
{ |
adf95d
|
39 |
if ($db_dsnr=='') $db_dsnr=$db_dsnw; |
T |
40 |
|
1676e1
|
41 |
$this->db_dsnw = $db_dsnw; |
S |
42 |
$this->db_dsnr = $db_dsnr; |
42b113
|
43 |
|
T |
44 |
$dsn_array = DB::parseDSN($db_dsnw); |
|
45 |
$this->db_provider = $dsn_array['phptype']; |
1676e1
|
46 |
} |
S |
47 |
|
|
48 |
// PHP 4 compatibility |
|
49 |
function rcube_db($db_dsnw,$db_dsnr='') |
|
50 |
{ |
|
51 |
$this->__construct($db_dsnw,$db_dsnr); |
|
52 |
} |
|
53 |
|
adf95d
|
54 |
// Connect to specific database |
T |
55 |
function dsn_connect($dsn) |
|
56 |
{ |
597170
|
57 |
// Use persistent connections if available |
adf95d
|
58 |
$dbh = DB::connect($dsn, array('persistent' => $true)); |
42b113
|
59 |
|
adf95d
|
60 |
if (DB::isError($dbh)) |
T |
61 |
raise_error(array('code' => 500, |
1676e1
|
62 |
'type' => 'db', |
S |
63 |
'line' => __LINE__, |
|
64 |
'file' => __FILE__, |
|
65 |
'message' => $dbh->getMessage()), TRUE, FALSE); |
42b113
|
66 |
|
597170
|
67 |
else if ($this->db_provider=='sqlite') |
T |
68 |
{ |
42b113
|
69 |
$dsn_array = DB::parseDSN($dsn); |
T |
70 |
if (!filesize($dsn_array['database']) && !empty($this->sqlite_initials)) |
|
71 |
$this->_sqlite_create_database($dbh, $this->sqlite_initials); |
597170
|
72 |
} |
T |
73 |
|
adf95d
|
74 |
return $dbh; |
T |
75 |
} |
1676e1
|
76 |
|
adf95d
|
77 |
// Connect to appropiate databse |
T |
78 |
function db_connect ($mode) |
|
79 |
{ |
42b113
|
80 |
$this->db_mode = $mode; |
T |
81 |
|
adf95d
|
82 |
// Already connected |
T |
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; |
1676e1
|
90 |
|
adf95d
|
91 |
// Same mode, current connection is ok |
T |
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; |
1676e1
|
99 |
|
adf95d
|
100 |
$this->db_handle = $this->dsn_connect($dsn); |
T |
101 |
$this->db_connected = true; |
|
102 |
} |
1676e1
|
103 |
|
adf95d
|
104 |
// Query database (read operations) |
T |
105 |
|
1676e1
|
106 |
function query($query) |
S |
107 |
{ |
adf95d
|
108 |
// Read or write ? |
T |
109 |
if (strtolower(trim(substr($query,0,6)))=='select') |
|
110 |
$mode='r'; |
|
111 |
else |
|
112 |
$mode='w'; |
|
113 |
|
597170
|
114 |
$this->db_connect($mode); |
T |
115 |
|
|
116 |
if ($this->db_provider == 'sqlite') |
|
117 |
$query = $this->_sqlite_prepare_query($query); |
|
118 |
|
1676e1
|
119 |
$result = $this->db_handle->query($query); |
S |
120 |
|
|
121 |
if (DB::isError($result)) |
42b113
|
122 |
raise_error(array('code' => 500, |
T |
123 |
'type' => 'db', |
597170
|
124 |
'line' => __LINE__, |
T |
125 |
'file' => __FILE__, |
|
126 |
'message' => $result->getMessage()), TRUE, FALSE); |
1676e1
|
127 |
|
S |
128 |
return $this->_add_result($result, $query); |
|
129 |
} |
|
130 |
|
adf95d
|
131 |
function db_execute ($query) |
T |
132 |
{ |
42b113
|
133 |
$this->db_connect('w'); |
597170
|
134 |
|
T |
135 |
if ($this->db_provider == 'sqlite') |
|
136 |
$query = $this->_sqlite_prepare_query($query); |
1676e1
|
137 |
|
adf95d
|
138 |
$result = $this->db_handle->query($query); |
1676e1
|
139 |
|
adf95d
|
140 |
} |
T |
141 |
|
|
142 |
function num_rows($res_id=NULL) |
1676e1
|
143 |
{ |
S |
144 |
if (!$this->db_handle) |
|
145 |
return FALSE; |
|
146 |
|
|
147 |
$result = $this->_get_result($res_id); |
|
148 |
|
|
149 |
if ($result) |
|
150 |
return $result->numRows(); |
|
151 |
else |
|
152 |
return FALSE; |
|
153 |
} |
|
154 |
|
|
155 |
function affected_rows($res_id=NULL) |
|
156 |
{ |
|
157 |
if (!$this->db_handle) |
|
158 |
return FALSE; |
|
159 |
|
d3d42b
|
160 |
return $this->db_handle->affectedRows(); |
1676e1
|
161 |
} |
S |
162 |
|
|
163 |
function insert_id($sequence = '') |
|
164 |
{ |
42b113
|
165 |
if (!$this->db_handle || $this->db_mode=='r') |
1676e1
|
166 |
return FALSE; |
S |
167 |
|
|
168 |
switch($this->db_provider) |
|
169 |
{ |
|
170 |
case 'pgsql': |
|
171 |
// PostgreSQL uses sequences |
|
172 |
$result =& $this->db_handle->getOne("SELECT CURRVAL('$sequence')"); |
42b113
|
173 |
if (DB::isError($result)) { |
1676e1
|
174 |
raise_error( array('code' => 500, 'type' => 'db', 'line' => __LINE__, 'file' => __FILE__, |
S |
175 |
'message' => $result->getMessage()), TRUE, TRUE); |
42b113
|
176 |
} |
1676e1
|
177 |
return $result; |
S |
178 |
|
|
179 |
case 'mysql': // This is unfortuneate |
42b113
|
180 |
return mysql_insert_id(); |
597170
|
181 |
|
T |
182 |
case 'sqlite': |
|
183 |
return sqlite_last_insert_rowid($this->db_handle->connection); |
|
184 |
|
1676e1
|
185 |
default: |
S |
186 |
die("portability issue with this database, please have the developer fix"); |
|
187 |
} |
|
188 |
} |
|
189 |
|
|
190 |
|
|
191 |
function fetch_assoc($res_id=NULL) |
|
192 |
{ |
|
193 |
$result = $this->_get_result($res_id); |
|
194 |
|
|
195 |
if (DB::isError($result)) |
42b113
|
196 |
{ |
1676e1
|
197 |
raise_error( array('code' => 500, 'type' => 'db', 'line' => __LINE__, 'file' => __FILE__, |
42b113
|
198 |
'message' => $this->db_link->getMessage()), TRUE, FALSE); |
T |
199 |
return FALSE; |
|
200 |
} |
1676e1
|
201 |
|
S |
202 |
return $result->fetchRow(DB_FETCHMODE_ASSOC); |
|
203 |
} |
|
204 |
|
|
205 |
function _add_result($res, $query) |
|
206 |
{ |
|
207 |
// sql error occured |
|
208 |
if (DB::isError($res)) |
|
209 |
{ |
adf95d
|
210 |
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); |
1676e1
|
211 |
return FALSE; |
S |
212 |
} |
|
213 |
else |
|
214 |
{ |
|
215 |
$res_id = sizeof($this->a_query_results); |
|
216 |
$this->a_query_results[$res_id] = $res; |
|
217 |
$this->last_res_id = $res_id; |
|
218 |
return $res_id; |
|
219 |
} |
|
220 |
} |
|
221 |
|
|
222 |
|
|
223 |
function _get_result($res_id) |
|
224 |
{ |
|
225 |
if ($res_id==NULL) |
|
226 |
$res_id = $this->last_res_id; |
|
227 |
|
|
228 |
if ($res_id && isset($this->a_query_results[$res_id])) |
|
229 |
return $this->a_query_results[$res_id]; |
|
230 |
else |
|
231 |
return FALSE; |
|
232 |
} |
|
233 |
|
597170
|
234 |
|
T |
235 |
// create a sqlite database from a file |
|
236 |
function _sqlite_create_database($dbh, $fileName) |
|
237 |
{ |
|
238 |
if (empty($fileName) || !is_string($fileName)) |
|
239 |
return ; |
|
240 |
|
|
241 |
$data = ''; |
42b113
|
242 |
if ($fd = fopen($fileName, 'r')) |
T |
243 |
{ |
|
244 |
$data = fread($fd, filesize($fileName)); |
|
245 |
fclose($fd); |
|
246 |
} |
597170
|
247 |
|
42b113
|
248 |
if (strlen($data)) |
T |
249 |
sqlite_exec($dbh->connection, $data); |
597170
|
250 |
} |
T |
251 |
|
|
252 |
// transform a query so that it is sqlite2 compliant |
|
253 |
function _sqlite_prepare_query($query) |
|
254 |
{ |
|
255 |
if (!is_string($query)) |
|
256 |
return ($query); |
|
257 |
|
|
258 |
$search = array('/NOW\(\)/', |
|
259 |
'/`/'); |
|
260 |
$replace = array("datetime('now')", |
|
261 |
'"'); |
|
262 |
$query = preg_replace($search, $replace, $query); |
|
263 |
|
|
264 |
return ($query); |
|
265 |
} |
|
266 |
|
1676e1
|
267 |
} |
S |
268 |
|
|
269 |
?> |