Table editor also recognise date fields
[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
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
59 // {{{ function query
60
61 function &query()
62 {
63 return new XOrgDBResult(XDB::_prepare(func_get_args()));
64 }
65
66 // }}}
67 // {{{ function execute()
68
69 function execute()
70 {
71 return XDB::_query(XDB::_prepare(func_get_args()));
72 }
73
74 // }}}
75 // {{{ function iterator()
76
77 function &iterator()
78 {
79 return new XOrgDBIterator(XDB::_prepare(func_get_args()));
80 }
81
82 // }}}
83 // {{{ function iterRow()
84
85 function &iterRow()
86 {
87 return new XOrgDBIterator(XDB::_prepare(func_get_args()), MYSQL_NUM);
88 }
89
90 // }}}
91 // {{{ function insertId()
92
93 function insertId()
94 {
95 return mysql_insert_id();
96 }
97
98 // }}}
99 // {{{ function _db_escape
100
101 function _db_escape($var)
102 {
103 switch (gettype($var)) {
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');
124 }
125 }
126
127 // }}}
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 }
133 }
134
135 class XOrgDBResult
136 {
137 // {{{ properties
138
139 var $_res;
140
141 // }}}
142 // {{{ constructor
143
144 function XOrgDBResult($query)
145 {
146 $this->_res = XDB::_query($query);
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
249
250 function numRows()
251 {
252 return mysql_num_rows($this->_res);
253 }
254
255 // }}}
256 }
257
258 class XOrgDBIterator
259 {
260 // {{{ properties
261
262 var $_result;
263 var $_pos;
264 var $_total;
265 var $_mode = MYSQL_ASSOC;
266
267 // }}}
268 // {{{ constructor
269
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 ()
280
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
319 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
320 ?>