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 |
22 | class 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 |
135 | class XOrgDBResult |
136 | { |
137 | // {{{ properties |
138 | |
139 | var $_res; |
140 | |
141 | // }}} |
142 | // {{{ constructor |
143 | |
144 | function XOrgDBResult($query) |
145 | { |
755abda6 |
146 | $this->_res = XDB::_query($query); |
0337d704 |
147 | } |
148 | |
149 | // }}} |
150 | // {{{ destructor |
151 | |
152 | function free() |
153 | { |
154 | mysql_free_result($this->_res); |
155 | unset($this); |
156 | } |
157 | |
158 | // }}} |
159 | // {{{ function fetchRow |
160 | |
161 | function _fetchRow() |
162 | { |
163 | return mysql_fetch_row($this->_res); |
164 | } |
165 | |
166 | // }}} |
167 | // {{{ function fetchAssoc |
168 | |
169 | function _fetchAssoc() |
170 | { |
171 | return mysql_fetch_assoc($this->_res); |
172 | } |
173 | |
174 | // }}} |
175 | // {{{ function fetchAllRow |
176 | |
177 | function fetchAllRow() |
178 | { |
179 | $result = Array(); |
180 | while ($result[] = mysql_fetch_row($this->_res)) { } |
181 | array_pop($result); |
182 | $this->free(); |
183 | return $result; |
184 | } |
185 | |
186 | // }}} |
187 | // {{{ function fetchAllAssoc |
188 | |
189 | function fetchAllAssoc() |
190 | { |
191 | $result = Array(); |
192 | while ($result[] = mysql_fetch_assoc($this->_res)) { } |
193 | array_pop($result); |
194 | $this->free(); |
195 | return $result; |
196 | } |
197 | |
198 | // }}} |
199 | // {{{ function fetchOneAssoc() |
200 | |
201 | function fetchOneAssoc() |
202 | { |
203 | $tmp = $this->_fetchAssoc(); |
204 | $this->free(); |
205 | return $tmp; |
206 | } |
207 | |
208 | // }}} |
209 | // {{{ function fetchOneRow() |
210 | |
211 | function fetchOneRow() |
212 | { |
213 | $tmp = $this->_fetchRow(); |
214 | $this->free(); |
215 | return $tmp; |
216 | } |
217 | |
218 | // }}} |
219 | // {{{ function fetchOneCell() |
220 | |
221 | function fetchOneCell() |
222 | { |
223 | $tmp = $this->_fetchRow(); |
224 | $this->free(); |
225 | return $tmp[0]; |
226 | } |
227 | |
228 | // }}} |
229 | // {{{ function fetchColumn() |
230 | |
231 | function fetchColumn($key = 0) |
232 | { |
233 | $res = Array(); |
234 | if (is_numeric($key)) { |
235 | while($tmp = $this->_fetchRow()) { |
236 | $res[] = $tmp[$key]; |
237 | } |
238 | } else { |
239 | while($tmp = $this->_fetchAssoc()) { |
240 | $res[] = $tmp[$key]; |
241 | } |
242 | } |
243 | $this->free(); |
244 | return $res; |
245 | } |
246 | |
247 | // }}} |
248 | // {{{ function numRows |
13a25546 |
249 | |
0337d704 |
250 | function numRows() |
251 | { |
252 | return mysql_num_rows($this->_res); |
253 | } |
254 | |
255 | // }}} |
256 | } |
257 | |
da88cfee |
258 | class XOrgDBIterator |
0337d704 |
259 | { |
260 | // {{{ properties |
261 | |
262 | var $_result; |
263 | var $_pos; |
264 | var $_total; |
265 | var $_mode = MYSQL_ASSOC; |
266 | |
267 | // }}} |
268 | // {{{ constructor |
13a25546 |
269 | |
0337d704 |
270 | function XOrgDBIterator($query, $mode = MYSQL_ASSOC) |
271 | { |
272 | $this->_result =& new XOrgDBResult($query); |
273 | $this->_pos = 0; |
274 | $this->_total = $this->_result->numRows(); |
275 | $this->_mode = $mode; |
276 | } |
277 | |
278 | // }}} |
279 | // {{{ function next () |
13a25546 |
280 | |
0337d704 |
281 | function next() |
282 | { |
283 | $this->_pos ++; |
284 | if ($this->_pos > $this->_total) { |
285 | $this->_result->free(); |
286 | unset($this); |
287 | return null; |
288 | } |
289 | return $this->_mode != MYSQL_ASSOC ? $this->_result->_fetchRow() : $this->_result->_fetchAssoc(); |
290 | } |
291 | |
292 | // }}} |
293 | // {{{ function first |
294 | |
295 | function first() |
296 | { |
297 | return $this->_pos == 1; |
298 | } |
299 | |
300 | // }}} |
301 | // {{{ function last |
302 | |
303 | function last() |
304 | { |
305 | return $this->_last == $this->_total; |
306 | } |
307 | |
308 | // }}} |
309 | // {{{ function total() |
310 | |
311 | function total() |
312 | { |
313 | return $this->_total; |
314 | } |
315 | |
316 | // }}} |
317 | } |
318 | |
0337d704 |
319 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: |
320 | ?> |