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 public static function _prepare($args)
28 $query = array_map(Array('XDB', '_db_escape'), $args);
29 $query[0] = str_replace('{?}', '%s', str_replace('%', '%%', $args[0]));
30 return call_user_func_array('sprintf', $query);
33 public static function _reformatQuery($query)
35 $query = preg_split("/\n\\s*/", $query);
37 foreach ($query as $key=>$line) {
39 if (preg_match('/^([A-Z]+(?:\s+(?:JOIN|BY|FROM|INTO))?)\s+(.*)/', $line, $matches)
40 && $matches[1] != 'AND' && $matches[1] != 'OR')
42 $local = strlen($matches[1]);
43 $line = $matches[1] . ' ' . $matches[2];
44 $length = max($length, $local);
46 $query[$key] = array($line, $local);
49 foreach ($query as $array) {
50 list($line, $local) = $array;
51 $local = max(0, $length - $local);
52 $res .= str_repeat(' ', $local) . $line . "\n";
53 $length +
= 2 * (substr_count($line, '(') - substr_count($line, ')'));
58 public static function _query($query)
62 if ($globals->debug
& 1) {
63 $_res = mysql_query("EXPLAIN $query");
65 while ($row = @mysql_fetch_assoc
($_res)) {
68 $trace_data = array('query' => XDB
::_reformatQuery($query), 'explain' => $explain);
69 @mysql_free_result
($_res);
70 $time_start = microtime();
73 $res = mysql_query($query);
75 if ($globals->debug
& 1) {
76 list($ue, $se) = explode(" ", microtime());
77 list($us, $ss) = explode(" ", $time_start);
78 $time = intval((($ue - $us) +
($se - $ss)) * 1000);
79 $trace_data['error'] = mysql_error();
80 $trace_data['exectime'] = $time;
81 $trace_data['rows'] = @mysql_num_rows
() ?
mysql_num_rows() : mysql_affected_rows();
82 $GLOBALS['XDB::trace_data'][] = $trace_data;
84 $GLOBALS['XDB::error'] = true
;
91 public static function query()
93 return new XOrgDBResult(XDB
::_prepare(func_get_args()));
96 public static function execute()
98 return XDB
::_query(XDB
::_prepare(func_get_args()));
101 public static function iterator()
103 return new XOrgDBIterator(XDB
::_prepare(func_get_args()));
106 public static function iterRow()
108 return new XOrgDBIterator(XDB
::_prepare(func_get_args()), MYSQL_NUM
);
111 public static function insertId()
113 return mysql_insert_id();
116 public static function _db_escape($var)
118 switch (gettype($var)) {
128 return "'".addslashes($var)."'";
135 return "'".addslashes(serialize($var))."'";
138 die(var_export($var, true
).' is not a valid for a database entry');
142 public static function trace_format(&$page, $template = 'skin/common.database-debug.tpl') {
143 $page->assign('trace_data', @$GLOBALS['XDB::trace_data']);
144 $page->assign('db_error', @$GLOBALS['XDB::error']);
145 return $page->fetch($template);
154 function XOrgDBResult($query)
156 $this->_res
= XDB
::_query($query);
161 mysql_free_result($this->_res
);
167 return mysql_fetch_row($this->_res
);
170 function _fetchAssoc()
172 return mysql_fetch_assoc($this->_res
);
175 function fetchAllRow()
178 while ($result[] = mysql_fetch_row($this->_res
)) { }
184 function fetchAllAssoc()
187 while ($result[] = mysql_fetch_assoc($this->_res
)) { }
193 function fetchOneAssoc()
195 $tmp = $this->_fetchAssoc();
200 function fetchOneRow()
202 $tmp = $this->_fetchRow();
207 function fetchOneCell()
209 $tmp = $this->_fetchRow();
214 function fetchColumn($key = 0)
217 if (is_numeric($key)) {
218 while($tmp = $this->_fetchRow()) {
222 while($tmp = $this->_fetchAssoc()) {
232 return mysql_num_rows($this->_res
);
241 private $_mode = MYSQL_ASSOC
;
243 function __construct($query, $mode = MYSQL_ASSOC
)
245 $this->_result
=& new XOrgDBResult($query);
247 $this->_total
= $this->_result
->numRows();
248 $this->_mode
= $mode;
254 if ($this->_pos
> $this->_total
) {
255 $this->_result
->free();
259 return $this->_mode
!= MYSQL_ASSOC ?
$this->_result
->_fetchRow() : $this->_result
->_fetchAssoc();
264 return $this->_pos
== 1;
269 return $this->_last
== $this->_total
;
274 return $this->_total
;
278 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: