2 /***************************************************************************
3 * Copyright (C) 2003-2006 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 **************************************************************************/
24 // {{{ function _prepare
26 function _prepare($args) {
27 $query = array_map(Array('XDB', '_db_escape'), $args);
28 $query[0] = str_replace('{?}', '%s', str_replace('%', '%%', $args[0]));
29 return call_user_func_array('sprintf', $query);
37 return new XOrgDBResult(XDB
::_prepare(func_get_args()));
41 // {{{ function execute()
45 return $globals->db
->query(XDB
::_prepare(func_get_args()));
49 // {{{ function iterator()
53 return new XOrgDBIterator(XDB
::_prepare(func_get_args()));
57 // {{{ function iterRow()
61 return new XOrgDBIterator(XDB
::_prepare(func_get_args()), MYSQL_NUM
);
65 // {{{ function insertId()
69 return mysql_insert_id();
73 // {{{ function _db_escape
75 function _db_escape($var)
77 switch (gettype($var)) {
87 return "'".addslashes($var)."'";
94 return "'".addslashes(serialize($var))."'";
97 die(var_export($var, true
).' is not a valid for a database entry');
113 function XOrgDBResult($query)
116 if (strpos($query, 'SQL_CALC_FOUND_ROWS') === false
) {
117 $this->_res
= $globals->db
->query($query);
119 $this->_res
= mysql_query($query);
128 mysql_free_result($this->_res
);
133 // {{{ function fetchRow
137 return mysql_fetch_row($this->_res
);
141 // {{{ function fetchAssoc
143 function _fetchAssoc()
145 return mysql_fetch_assoc($this->_res
);
149 // {{{ function fetchAllRow
151 function fetchAllRow()
154 while ($result[] = mysql_fetch_row($this->_res
)) { }
161 // {{{ function fetchAllAssoc
163 function fetchAllAssoc()
166 while ($result[] = mysql_fetch_assoc($this->_res
)) { }
173 // {{{ function fetchOneAssoc()
175 function fetchOneAssoc()
177 $tmp = $this->_fetchAssoc();
183 // {{{ function fetchOneRow()
185 function fetchOneRow()
187 $tmp = $this->_fetchRow();
193 // {{{ function fetchOneCell()
195 function fetchOneCell()
197 $tmp = $this->_fetchRow();
203 // {{{ function fetchColumn()
205 function fetchColumn($key = 0)
208 if (is_numeric($key)) {
209 while($tmp = $this->_fetchRow()) {
213 while($tmp = $this->_fetchAssoc()) {
222 // {{{ function numRows
226 return mysql_num_rows($this->_res
);
239 var $_mode = MYSQL_ASSOC
;
244 function XOrgDBIterator($query, $mode = MYSQL_ASSOC
)
246 $this->_result
=& new XOrgDBResult($query);
248 $this->_total
= $this->_result
->numRows();
249 $this->_mode
= $mode;
253 // {{{ function next ()
258 if ($this->_pos
> $this->_total
) {
259 $this->_result
->free();
263 return $this->_mode
!= MYSQL_ASSOC ?
$this->_result
->_fetchRow() : $this->_result
->_fetchAssoc();
267 // {{{ function first
271 return $this->_pos
== 1;
279 return $this->_last
== $this->_total
;
283 // {{{ function total()
287 return $this->_total
;
293 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: