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