PHP5-ize all base classes...
[platal.git] / classes / xdb.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2007 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 **************************************************************************/
21
22 class XDB
23 {
24 private static $mysqli = null;
25
26 public static function connect()
27 {
28 global $globals;
29 XDB::$mysqli = new mysqli($globals->dbhost, $globals->dbuser, $globals->dbpwd, $globals->dbdb);
30 if ($globals->debug & 1) {
31 $bt = new PlBacktrace('MySQL');
32 if (mysqli_connect_errno()) {
33 $bt->newEvent("MySQLI connection", 0, mysqli_connect_error());
34 return false;
35 }
36 }
37 XDB::$mysqli->autocommit(true);
38 XDB::$mysqli->set_charset($globals->dbcharset);
39 return true;
40 }
41
42 public static function _prepare($args)
43 {
44 $query = array_map(Array('XDB', '_db_escape'), $args);
45 $query[0] = str_replace('{?}', '%s', str_replace('%', '%%', $args[0]));
46 return call_user_func_array('sprintf', $query);
47 }
48
49 public static function _reformatQuery($query)
50 {
51 $query = preg_split("/\n\\s*/", trim($query));
52 $length = 0;
53 foreach ($query as $key=>$line) {
54 $local = -2;
55 if (preg_match('/^([A-Z]+(?:\s+(?:JOIN|BY|FROM|INTO))?)\s+(.*)/u', $line, $matches)
56 && $matches[1] != 'AND' && $matches[1] != 'OR')
57 {
58 $local = strlen($matches[1]);
59 $line = $matches[1] . ' ' . $matches[2];
60 $length = max($length, $local);
61 }
62 $query[$key] = array($line, $local);
63 }
64 $res = '';
65 foreach ($query as $array) {
66 list($line, $local) = $array;
67 $local = max(0, $length - $local);
68 $res .= str_repeat(' ', $local) . $line . "\n";
69 $length += 2 * (substr_count($line, '(') - substr_count($line, ')'));
70 }
71 return $res;
72 }
73
74 public static function _query($query)
75 {
76 global $globals;
77
78 if (!XDB::$mysqli && !XDB::connect()) {
79 return false;
80 }
81
82 if ($globals->debug & 1) {
83 $explain = array();
84 if (strpos($query, 'FOUND_ROWS()') === false) {
85 $res = XDB::$mysqli->query("EXPLAIN $query");
86 if ($res) {
87 while ($row = $res->fetch_assoc()) {
88 $explain[] = $row;
89 }
90 $res->free();
91 }
92 }
93 PlBacktrace::$bt['MySQL']->start(XDB::_reformatQuery($query));
94 }
95
96 $res = XDB::$mysqli->query($query);
97
98 if ($globals->debug & 1) {
99 PlBacktrace::$bt['MySQL']->stop(@$res->num_rows ? $res->num_rows : XDB::$mysqli->affected_rows,
100 XDB::$mysqli->error,
101 $explain);
102 }
103 return $res;
104 }
105
106 public static function query()
107 {
108 return new XOrgDBResult(XDB::_prepare(func_get_args()));
109 }
110
111 public static function execute()
112 {
113 return XDB::_query(XDB::_prepare(func_get_args()));
114 }
115
116 public static function iterator()
117 {
118 return new XOrgDBIterator(XDB::_prepare(func_get_args()));
119 }
120
121 public static function iterRow()
122 {
123 return new XOrgDBIterator(XDB::_prepare(func_get_args()), MYSQL_NUM);
124 }
125
126 public static function insertId()
127 {
128 return XDB::$mysqli->insert_id;
129 }
130
131 public static function errno()
132 {
133 return XDB::$mysqli->errno;
134 }
135
136 public static function error()
137 {
138 return XDB::$mysqli->error;
139 }
140
141 public static function affectedRows()
142 {
143 return XDB::$mysqli->affected_rows;
144 }
145
146 public static function _db_escape($var)
147 {
148 switch (gettype($var)) {
149 case 'boolean':
150 return $var ? 1 : 0;
151
152 case 'integer':
153 case 'double':
154 case 'float':
155 return $var;
156
157 case 'string':
158 return "'".addslashes($var)."'";
159
160 case 'NULL':
161 return 'NULL';
162
163 case 'object':
164 case 'array':
165 return "'".addslashes(serialize($var))."'";
166
167 default:
168 die(var_export($var, true).' is not a valid for a database entry');
169 }
170 }
171 }
172
173 class XOrgDBResult
174 {
175
176 private $_res;
177
178 function XOrgDBResult($query)
179 {
180 $this->_res = XDB::_query($query);
181 }
182
183 function free()
184 {
185 $this->_res->free();
186 unset($this);
187 }
188
189 function _fetchRow()
190 {
191 return $this->_res->fetch_row();
192 }
193
194 function _fetchAssoc()
195 {
196 return $this->_res->fetch_assoc();
197 }
198
199 function fetchAllRow()
200 {
201 $result = Array();
202 while ($result[] = $this->_res->fetch_row());
203 array_pop($result);
204 $this->free();
205 return $result;
206 }
207
208 function fetchAllAssoc()
209 {
210 $result = Array();
211 while ($result[] = $this->_res->fetch_assoc());
212 array_pop($result);
213 $this->free();
214 return $result;
215 }
216
217 function fetchOneAssoc()
218 {
219 $tmp = $this->_fetchAssoc();
220 $this->free();
221 return $tmp;
222 }
223
224 function fetchOneRow()
225 {
226 $tmp = $this->_fetchRow();
227 $this->free();
228 return $tmp;
229 }
230
231 function fetchOneCell()
232 {
233 $tmp = $this->_fetchRow();
234 $this->free();
235 return $tmp[0];
236 }
237
238 function fetchColumn($key = 0)
239 {
240 $res = Array();
241 if (is_numeric($key)) {
242 while($tmp = $this->_fetchRow()) {
243 $res[] = $tmp[$key];
244 }
245 } else {
246 while($tmp = $this->_fetchAssoc()) {
247 $res[] = $tmp[$key];
248 }
249 }
250 $this->free();
251 return $res;
252 }
253
254 function fetchOneField()
255 {
256 return $this->_res->fetch_field();
257 }
258
259 function fetchFields()
260 {
261 $res = array();
262 while ($res[] = $this->fetchOneField());
263 return $res;
264 }
265
266 function numRows()
267 {
268 return $this->_res->num_rows;
269 }
270
271 function fieldCount()
272 {
273 return $this->_res->field_count;
274 }
275 }
276
277 require_once dirname(__FILE__) . '/pliterator.php';
278
279 class XOrgDBIterator implements PlIterator
280 {
281 private $_result;
282 private $_pos;
283 private $_total;
284 private $_fpos;
285 private $_fields;
286 private $_mode = MYSQL_ASSOC;
287
288 function __construct($query, $mode = MYSQL_ASSOC)
289 {
290 $this->_result = new XOrgDBResult($query);
291 $this->_pos = 0;
292 $this->_total = $this->_result->numRows();
293 $this->_fpost = 0;
294 $this->_fields = $this->_result->fieldCount();
295 $this->_mode = $mode;
296 }
297
298 function next()
299 {
300 $this->_pos ++;
301 if ($this->_pos > $this->_total) {
302 $this->_result->free();
303 unset($this);
304 return null;
305 }
306 return $this->_mode != MYSQL_ASSOC ? $this->_result->_fetchRow() : $this->_result->_fetchAssoc();
307 }
308
309 function first()
310 {
311 return $this->_pos == 1;
312 }
313
314 function last()
315 {
316 return $this->_pos == $this->_total;
317 }
318
319 function total()
320 {
321 return $this->_total;
322 }
323
324 function nextField()
325 {
326 $this->_fpos++;
327 if ($this->_fpos > $this->_fields) {
328 return null;
329 }
330 return $this->_result->fetchOneField();
331 }
332
333 function firstField()
334 {
335 return $this->_fpos == 1;
336 }
337
338 function lastField()
339 {
340 return $this->_fpos == $this->_fields;
341 }
342
343 function totalFields()
344 {
345 return $this->_fields;
346 }
347 }
348
349 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
350 ?>