6c52afdd6817000612208f05cf109cee3d501e74
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 var $_trace_data = array();
26 // {{{ function _prepare
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);
36 function _query($query) {
39 if ($globals->debug
& 1) {
40 $_res = mysql_query("EXPLAIN $query");
42 while ($row = @mysql_fetch_assoc
($_res)) {
45 $trace_data = array('query' => $query, 'explain' => $explain);
46 @mysql_free_result
($_res);
49 $res = mysql_query($query);
51 if ($globals->debug
& 1) {
52 $trace_data['error'] = mysql_error();
53 $GLOBALS['XDB::trace_data'][] = $trace_data;
63 return new XOrgDBResult(XDB
::_prepare(func_get_args()));
67 // {{{ function execute()
71 return XDB
::_query(XDB
::_prepare(func_get_args()));
75 // {{{ function iterator()
79 return new XOrgDBIterator(XDB
::_prepare(func_get_args()));
83 // {{{ function iterRow()
87 return new XOrgDBIterator(XDB
::_prepare(func_get_args()), MYSQL_NUM
);
91 // {{{ function insertId()
95 return mysql_insert_id();
99 // {{{ function _db_escape
101 function _db_escape($var)
103 switch (gettype($var)) {
113 return "'".addslashes($var)."'";
120 return "'".addslashes(serialize($var))."'";
123 die(var_export($var, true
).' is not a valid for a database entry');
129 function trace_format(&$page, $template = 'database-debug.tpl') {
130 $page->assign('trace_data', $GLOBALS['XDB::trace_data']);
131 return $page->fetch($template);
144 function XOrgDBResult($query)
146 if (strpos($query, 'SQL_CALC_FOUND_ROWS') === false
) {
147 $this->_res
= XDB
::_query($query);
149 $this->_res
= mysql_query($query);
158 mysql_free_result($this->_res
);
163 // {{{ function fetchRow
167 return mysql_fetch_row($this->_res
);
171 // {{{ function fetchAssoc
173 function _fetchAssoc()
175 return mysql_fetch_assoc($this->_res
);
179 // {{{ function fetchAllRow
181 function fetchAllRow()
184 while ($result[] = mysql_fetch_row($this->_res
)) { }
191 // {{{ function fetchAllAssoc
193 function fetchAllAssoc()
196 while ($result[] = mysql_fetch_assoc($this->_res
)) { }
203 // {{{ function fetchOneAssoc()
205 function fetchOneAssoc()
207 $tmp = $this->_fetchAssoc();
213 // {{{ function fetchOneRow()
215 function fetchOneRow()
217 $tmp = $this->_fetchRow();
223 // {{{ function fetchOneCell()
225 function fetchOneCell()
227 $tmp = $this->_fetchRow();
233 // {{{ function fetchColumn()
235 function fetchColumn($key = 0)
238 if (is_numeric($key)) {
239 while($tmp = $this->_fetchRow()) {
243 while($tmp = $this->_fetchAssoc()) {
252 // {{{ function numRows
256 return mysql_num_rows($this->_res
);
269 var $_mode = MYSQL_ASSOC
;
274 function XOrgDBIterator($query, $mode = MYSQL_ASSOC
)
276 $this->_result
=& new XOrgDBResult($query);
278 $this->_total
= $this->_result
->numRows();
279 $this->_mode
= $mode;
283 // {{{ function next ()
288 if ($this->_pos
> $this->_total
) {
289 $this->_result
->free();
293 return $this->_mode
!= MYSQL_ASSOC ?
$this->_result
->_fetchRow() : $this->_result
->_fetchAssoc();
297 // {{{ function first
301 return $this->_pos
== 1;
309 return $this->_last
== $this->_total
;
313 // {{{ function total()
317 return $this->_total
;
323 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: