5e07bc25af96e43f88e4e676449e4f9e1861a856
[platal.git] / classes / XDB.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2006 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 var $_trace_data = array();
25
26 // {{{ function _prepare
27
28 function _prepare($args) {
29 $query = array_map(Array('XDB', '_db_escape'), $args);
30 $query[0] = str_replace('{?}', '%s', str_replace('%', '%%', $args[0]));
31 return call_user_func_array('sprintf', $query);
32 }
33
34 // }}}
35 // {{{ function _reformatQuery
36
37 function _reformatQuery($query)
38 {
39 $query = preg_split("/\n\\s*/", $query);
40 $length = 0;
41 foreach ($query as $line) {
42 if (preg_match('/^([A-Z]+(?: +(?:JOIN|BY))?) /', $line, $matches)
43 && $matches[1] != 'AND' && $matches[1] != 'OR') {
44 $length = max($length, strlen($matches[1]));
45 }
46 }
47 $res = '';
48 foreach ($query as $line) {
49 $local = -2;
50 if (preg_match('/^([A-Z]+(?: +(?:JOIN|BY))?) +(.*)/', $line, $matches)
51 && $matches[1] != 'AND' && $matches[1] != 'OR') {
52 $local = strlen($matches[1]);
53 $line = $matches[1] . ' ' . $matches[2];
54 }
55 $local = $length - $local;
56 $res .= str_repeat(' ', $local) . $line . "\n";
57 $length += 2 * (substr_count($line, '(') - substr_count($line, ')'));
58 }
59 return $res;
60 }
61
62 // }}}
63 // {{{ function _query
64
65 function _query($query) {
66 global $globals;
67
68 if ($globals->debug & 1) {
69 $_res = mysql_query("EXPLAIN $query");
70 $explain = array();
71 while ($row = @mysql_fetch_assoc($_res)) {
72 $explain[] = $row;
73 }
74 $trace_data = array('query' => XDB::_reformatQuery($query), 'explain' => $explain);
75 @mysql_free_result($_res);
76 }
77
78 $res = mysql_query($query);
79
80 if ($globals->debug & 1) {
81 $trace_data['error'] = mysql_error();
82 $GLOBALS['XDB::trace_data'][] = $trace_data;
83 }
84
85 return $res;
86 }
87
88 // }}}
89 // {{{ function query
90
91 function &query()
92 {
93 return new XOrgDBResult(XDB::_prepare(func_get_args()));
94 }
95
96 // }}}
97 // {{{ function execute()
98
99 function execute()
100 {
101 return XDB::_query(XDB::_prepare(func_get_args()));
102 }
103
104 // }}}
105 // {{{ function iterator()
106
107 function &iterator()
108 {
109 return new XOrgDBIterator(XDB::_prepare(func_get_args()));
110 }
111
112 // }}}
113 // {{{ function iterRow()
114
115 function &iterRow()
116 {
117 return new XOrgDBIterator(XDB::_prepare(func_get_args()), MYSQL_NUM);
118 }
119
120 // }}}
121 // {{{ function insertId()
122
123 function insertId()
124 {
125 return mysql_insert_id();
126 }
127
128 // }}}
129 // {{{ function _db_escape
130
131 function _db_escape($var)
132 {
133 switch (gettype($var)) {
134 case 'boolean':
135 return $var ? 1 : 0;
136
137 case 'integer':
138 case 'double':
139 case 'float':
140 return $var;
141
142 case 'string':
143 return "'".addslashes($var)."'";
144
145 case 'NULL':
146 return 'NULL';
147
148 case 'object':
149 case 'array':
150 return "'".addslashes(serialize($var))."'";
151
152 default:
153 die(var_export($var, true).' is not a valid for a database entry');
154 }
155 }
156
157 // }}}
158
159 function trace_format(&$page, $template = 'database-debug.tpl') {
160 $page->assign('trace_data', $GLOBALS['XDB::trace_data']);
161 return $page->fetch($template);
162 }
163 }
164
165 class XOrgDBResult
166 {
167 // {{{ properties
168
169 var $_res;
170
171 // }}}
172 // {{{ constructor
173
174 function XOrgDBResult($query)
175 {
176 $this->_res = XDB::_query($query);
177 }
178
179 // }}}
180 // {{{ destructor
181
182 function free()
183 {
184 mysql_free_result($this->_res);
185 unset($this);
186 }
187
188 // }}}
189 // {{{ function fetchRow
190
191 function _fetchRow()
192 {
193 return mysql_fetch_row($this->_res);
194 }
195
196 // }}}
197 // {{{ function fetchAssoc
198
199 function _fetchAssoc()
200 {
201 return mysql_fetch_assoc($this->_res);
202 }
203
204 // }}}
205 // {{{ function fetchAllRow
206
207 function fetchAllRow()
208 {
209 $result = Array();
210 while ($result[] = mysql_fetch_row($this->_res)) { }
211 array_pop($result);
212 $this->free();
213 return $result;
214 }
215
216 // }}}
217 // {{{ function fetchAllAssoc
218
219 function fetchAllAssoc()
220 {
221 $result = Array();
222 while ($result[] = mysql_fetch_assoc($this->_res)) { }
223 array_pop($result);
224 $this->free();
225 return $result;
226 }
227
228 // }}}
229 // {{{ function fetchOneAssoc()
230
231 function fetchOneAssoc()
232 {
233 $tmp = $this->_fetchAssoc();
234 $this->free();
235 return $tmp;
236 }
237
238 // }}}
239 // {{{ function fetchOneRow()
240
241 function fetchOneRow()
242 {
243 $tmp = $this->_fetchRow();
244 $this->free();
245 return $tmp;
246 }
247
248 // }}}
249 // {{{ function fetchOneCell()
250
251 function fetchOneCell()
252 {
253 $tmp = $this->_fetchRow();
254 $this->free();
255 return $tmp[0];
256 }
257
258 // }}}
259 // {{{ function fetchColumn()
260
261 function fetchColumn($key = 0)
262 {
263 $res = Array();
264 if (is_numeric($key)) {
265 while($tmp = $this->_fetchRow()) {
266 $res[] = $tmp[$key];
267 }
268 } else {
269 while($tmp = $this->_fetchAssoc()) {
270 $res[] = $tmp[$key];
271 }
272 }
273 $this->free();
274 return $res;
275 }
276
277 // }}}
278 // {{{ function numRows
279
280 function numRows()
281 {
282 return mysql_num_rows($this->_res);
283 }
284
285 // }}}
286 }
287
288 class XOrgDBIterator
289 {
290 // {{{ properties
291
292 var $_result;
293 var $_pos;
294 var $_total;
295 var $_mode = MYSQL_ASSOC;
296
297 // }}}
298 // {{{ constructor
299
300 function XOrgDBIterator($query, $mode = MYSQL_ASSOC)
301 {
302 $this->_result =& new XOrgDBResult($query);
303 $this->_pos = 0;
304 $this->_total = $this->_result->numRows();
305 $this->_mode = $mode;
306 }
307
308 // }}}
309 // {{{ function next ()
310
311 function next()
312 {
313 $this->_pos ++;
314 if ($this->_pos > $this->_total) {
315 $this->_result->free();
316 unset($this);
317 return null;
318 }
319 return $this->_mode != MYSQL_ASSOC ? $this->_result->_fetchRow() : $this->_result->_fetchAssoc();
320 }
321
322 // }}}
323 // {{{ function first
324
325 function first()
326 {
327 return $this->_pos == 1;
328 }
329
330 // }}}
331 // {{{ function last
332
333 function last()
334 {
335 return $this->_last == $this->_total;
336 }
337
338 // }}}
339 // {{{ function total()
340
341 function total()
342 {
343 return $this->_total;
344 }
345
346 // }}}
347 }
348
349 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
350 ?>