2 /***************************************************************************
3 * Copyright (C) 2003-2004 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 **************************************************************************/
33 // {{{ function _prepare
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);
46 return new XOrgDBResult($this->_prepare(func_get_args()));
50 // {{{ function execute()
54 return $globals->db
->query($this->_prepare(func_get_args()));
58 // {{{ function iterator()
62 return new XOrgDBIterator($this->_prepare(func_get_args()));
66 // {{{ function iterRow()
70 return new XOrgDBIterator($this->_prepare(func_get_args()), MYSQL_NUM
);
74 // {{{ function _db_escape
76 function _db_escape($var)
78 switch (gettype($var)) {
88 return "'".addslashes($var)."'";
95 return "'".addslashes(serialize($var))."'";
98 die(var_export($var, true
).' is not a valid for a database entry');
106 // {{{ class XOrgDBResult
117 function XOrgDBResult($query)
120 if (strpos($query, 'SQL_CALC_FOUND_ROWS') === false
) {
121 $this->_res
= $globals->db
->query($query);
123 $this->_res
= mysql_query($query);
132 mysql_free_result($this->_res
);
137 // {{{ function fetchRow
141 return mysql_fetch_row($this->_res
);
145 // {{{ function fetchAssoc
147 function _fetchAssoc()
149 return mysql_fetch_assoc($this->_res
);
153 // {{{ function fetchAllRow
155 function fetchAllRow()
158 while ($result[] = mysql_fetch_row($this->_res
)) { }
165 // {{{ function fetchAllAssoc
167 function fetchAllAssoc()
170 while ($result[] = mysql_fetch_assoc($this->_res
)) { }
177 // {{{ function fetchOneAssoc()
179 function fetchOneAssoc()
181 $tmp = $this->_fetchAssoc();
187 // {{{ function fetchOneRow()
189 function fetchOneRow()
191 $tmp = $this->_fetchRow();
197 // {{{ function fetchOneCell()
199 function fetchOneCell()
201 $tmp = $this->_fetchRow();
207 // {{{ function fetchColumn()
209 function fetchColumn($key = 0)
212 if (is_numeric($key)) {
213 while($tmp = $this->_fetchRow()) {
217 while($tmp = $this->_fetchAssoc()) {
226 // {{{ function numRows
230 return mysql_num_rows($this->_res
);
237 // {{{ class XOrgDBIterator
239 class XOrgDBIterator
extends XOrgIterator
246 var $_mode = MYSQL_ASSOC
;
251 function XOrgDBIterator($query, $mode = MYSQL_ASSOC
)
253 $this->_result
=& new XOrgDBResult($query);
255 $this->_total
= $this->_result
->numRows();
256 $this->_mode
= $mode;
260 // {{{ function next ()
265 if ($this->_pos
> $this->_total
) {
266 $this->_result
->free();
270 return $this->_mode
!= MYSQL_ASSOC ?
$this->_result
->_fetchRow() : $this->_result
->_fetchAssoc();
274 // {{{ function first
278 return $this->_pos
== 1;
286 return $this->_last
== $this->_total
;
290 // {{{ function total()
294 return $this->_total
;
302 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: