df77e8219e1fe79901d909ab8273a2310f846e79
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);
35 // {{{ function _reformatQuery
37 function _reformatQuery($query)
39 $query = preg_split("/\n\\s*/", $query);
41 foreach ($query as $line) {
42 if (preg_match('/^([A-Z]+( (JOIN|BY))?) /', $line, $matches)
43 && $matches[1] != 'AND' && $matches[2] != 'OR') {
44 $length = max($length, strlen($matches[1]));
48 foreach ($query as $line) {
50 if (preg_match('/^([A-Z]+(?: (?:JOIN|BY))?) +(.*)/', $line, $matches)
51 && $matches[1] != 'AND' && $matches[2] != 'OR') {
52 $local = strlen($matches[1]);
53 $line = $matches[1] . ' ' . $matches[2];
55 $local = $length - $local;
56 $res .= str_repeat(' ', $local) . $line . "\n";
57 $length +
= 2 * (substr_count($line, '(') - substr_count($line, ')'));
63 // {{{ function _query
65 function _query($query) {
68 if ($globals->debug
& 1) {
69 $_res = mysql_query("EXPLAIN $query");
71 while ($row = @mysql_fetch_assoc
($_res)) {
74 $trace_data = array('query' => XDB
::_reformatQuery($query), 'explain' => $explain);
75 @mysql_free_result
($_res);
78 $res = mysql_query($query);
80 if ($globals->debug
& 1) {
81 $trace_data['error'] = mysql_error();
82 $GLOBALS['XDB::trace_data'][] = $trace_data;
93 return new XOrgDBResult(XDB
::_prepare(func_get_args()));
97 // {{{ function execute()
101 return XDB
::_query(XDB
::_prepare(func_get_args()));
105 // {{{ function iterator()
109 return new XOrgDBIterator(XDB
::_prepare(func_get_args()));
113 // {{{ function iterRow()
117 return new XOrgDBIterator(XDB
::_prepare(func_get_args()), MYSQL_NUM
);
121 // {{{ function insertId()
125 return mysql_insert_id();
129 // {{{ function _db_escape
131 function _db_escape($var)
133 switch (gettype($var)) {
143 return "'".addslashes($var)."'";
150 return "'".addslashes(serialize($var))."'";
153 die(var_export($var, true
).' is not a valid for a database entry');
159 function trace_format(&$page, $template = 'database-debug.tpl') {
160 $page->assign('trace_data', $GLOBALS['XDB::trace_data']);
161 return $page->fetch($template);
174 function XOrgDBResult($query)
176 $this->_res
= XDB
::_query($query);
184 mysql_free_result($this->_res
);
189 // {{{ function fetchRow
193 return mysql_fetch_row($this->_res
);
197 // {{{ function fetchAssoc
199 function _fetchAssoc()
201 return mysql_fetch_assoc($this->_res
);
205 // {{{ function fetchAllRow
207 function fetchAllRow()
210 while ($result[] = mysql_fetch_row($this->_res
)) { }
217 // {{{ function fetchAllAssoc
219 function fetchAllAssoc()
222 while ($result[] = mysql_fetch_assoc($this->_res
)) { }
229 // {{{ function fetchOneAssoc()
231 function fetchOneAssoc()
233 $tmp = $this->_fetchAssoc();
239 // {{{ function fetchOneRow()
241 function fetchOneRow()
243 $tmp = $this->_fetchRow();
249 // {{{ function fetchOneCell()
251 function fetchOneCell()
253 $tmp = $this->_fetchRow();
259 // {{{ function fetchColumn()
261 function fetchColumn($key = 0)
264 if (is_numeric($key)) {
265 while($tmp = $this->_fetchRow()) {
269 while($tmp = $this->_fetchAssoc()) {
278 // {{{ function numRows
282 return mysql_num_rows($this->_res
);
295 var $_mode = MYSQL_ASSOC
;
300 function XOrgDBIterator($query, $mode = MYSQL_ASSOC
)
302 $this->_result
=& new XOrgDBResult($query);
304 $this->_total
= $this->_result
->numRows();
305 $this->_mode
= $mode;
309 // {{{ function next ()
314 if ($this->_pos
> $this->_total
) {
315 $this->_result
->free();
319 return $this->_mode
!= MYSQL_ASSOC ?
$this->_result
->_fetchRow() : $this->_result
->_fetchAssoc();
323 // {{{ function first
327 return $this->_pos
== 1;
335 return $this->_last
== $this->_total
;
339 // {{{ function total()
343 return $this->_total
;
349 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: