Give names to debug flags and add a new flag for smarty debugging:
[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 & DEBUG_BT) {
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', '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 & DEBUG_BT) {
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 & DEBUG_BT) {
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 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 public function __construct($query)
179 {
180 $this->_res = XDB::_query($query);
181 }
182
183 public function free()
184 {
185 if ($this->_res) {
186 $this->_res->free();
187 }
188 unset($this);
189 }
190
191 protected function _fetchRow()
192 {
193 return $this->_res ? $this->_res->fetch_row() : null;
194 }
195
196 protected function _fetchAssoc()
197 {
198 return $this->_res ? $this->_res->fetch_assoc() : null;
199 }
200
201 public function fetchAllRow()
202 {
203 $result = Array();
204 if (!$this->_res) {
205 return $result;
206 }
207 while ($result[] = $this->_res->fetch_row());
208 array_pop($result);
209 $this->free();
210 return $result;
211 }
212
213 public function fetchAllAssoc()
214 {
215 $result = Array();
216 if (!$this->_res) {
217 return $result;
218 }
219 while ($result[] = $this->_res->fetch_assoc());
220 array_pop($result);
221 $this->free();
222 return $result;
223 }
224
225 public function fetchOneAssoc()
226 {
227 $tmp = $this->_fetchAssoc();
228 $this->free();
229 return $tmp;
230 }
231
232 public function fetchOneRow()
233 {
234 $tmp = $this->_fetchRow();
235 $this->free();
236 return $tmp;
237 }
238
239 public function fetchOneCell()
240 {
241 $tmp = $this->_fetchRow();
242 $this->free();
243 return $tmp[0];
244 }
245
246 public function fetchColumn($key = 0)
247 {
248 $res = Array();
249 if (is_numeric($key)) {
250 while($tmp = $this->_fetchRow()) {
251 $res[] = $tmp[$key];
252 }
253 } else {
254 while($tmp = $this->_fetchAssoc()) {
255 $res[] = $tmp[$key];
256 }
257 }
258 $this->free();
259 return $res;
260 }
261
262 public function fetchOneField()
263 {
264 return $this->_res ? $this->_res->fetch_field() : null;
265 }
266
267 public function fetchFields()
268 {
269 $res = array();
270 while ($res[] = $this->fetchOneField());
271 return $res;
272 }
273
274 public function numRows()
275 {
276 return $this->_res ? $this->_res->num_rows : 0;
277 }
278
279 public function fieldCount()
280 {
281 return $this->_res ? $this->_res->field_count : 0;
282 }
283 }
284
285 require_once dirname(__FILE__) . '/pliterator.php';
286
287 class XOrgDBIterator extends XOrgDBResult implements PlIterator
288 {
289 private $_result;
290 private $_pos;
291 private $_total;
292 private $_fpos;
293 private $_fields;
294 private $_mode = MYSQL_ASSOC;
295
296 public function __construct($query, $mode = MYSQL_ASSOC)
297 {
298 parent::__construct($query);
299 $this->_pos = 0;
300 $this->_total = $this->numRows();
301 $this->_fpost = 0;
302 $this->_fields = $this->fieldCount();
303 $this->_mode = $mode;
304 }
305
306 public function next()
307 {
308 $this->_pos ++;
309 if ($this->_pos > $this->_total) {
310 $this->free();
311 unset($this);
312 return null;
313 }
314 return $this->_mode != MYSQL_ASSOC ? $this->_fetchRow() : $this->_fetchAssoc();
315 }
316
317 public function first()
318 {
319 return $this->_pos == 1;
320 }
321
322 public function last()
323 {
324 return $this->_pos == $this->_total;
325 }
326
327 public function total()
328 {
329 return $this->_total;
330 }
331
332 public function nextField()
333 {
334 $this->_fpos++;
335 if ($this->_fpos > $this->_fields) {
336 return null;
337 }
338 return $this->fetchOneField();
339 }
340
341 public function firstField()
342 {
343 return $this->_fpos == 1;
344 }
345
346 public function lastField()
347 {
348 return $this->_fpos == $this->_fields;
349 }
350
351 public function totalFields()
352 {
353 return $this->_fields;
354 }
355 }
356
357 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
358 ?>