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