2 /***************************************************************************
3 * Copyright (C) 2003-2007 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 public static $connec = null
;
25 var $_trace_data = array();
27 public static 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);
34 public static function _reformatQuery($query)
36 $query = preg_split("/\n\\s*/", trim($query));
38 foreach ($query as $key=>$line) {
40 if (preg_match('/^([A-Z]+(?:\s+(?:JOIN|BY|FROM|INTO))?)\s+(.*)/u', $line, $matches)
41 && $matches[1] != 'AND' && $matches[1] != 'OR')
43 $local = strlen($matches[1]);
44 $line = $matches[1] . ' ' . $matches[2];
45 $length = max($length, $local);
47 $query[$key] = array($line, $local);
50 foreach ($query as $array) {
51 list($line, $local) = $array;
52 $local = max(0, $length - $local);
53 $res .= str_repeat(' ', $local) . $line . "\n";
54 $length +
= 2 * (substr_count($line, '(') - substr_count($line, ')'));
59 public static function _query($query)
63 if ($globals->debug
& 1) {
65 if (strpos($query, 'FOUND_ROWS()') === false
) {
66 $_res = mysqli_query(XDB
::$connec, "EXPLAIN $query");
68 while ($row = mysqli_fetch_assoc($_res)) {
71 @mysqli_free_result
($_res);
74 $trace_data = array('query' => XDB
::_reformatQuery($query), 'explain' => $explain);
75 $time_start = microtime();
78 $res = mysqli_query(XDB
::$connec, $query);
80 if ($globals->debug
& 1) {
81 list($ue, $se) = explode(" ", microtime());
82 list($us, $ss) = explode(" ", $time_start);
83 $time = intval((($ue - $us) +
($se - $ss)) * 1000);
84 $trace_data['error'] = mysqli_error(XDB
::$connec);
85 $trace_data['exectime'] = $time;
86 $trace_data['rows'] = @mysqli_num_rows
($res) ?
mysqli_num_rows($res) : mysqli_affected_rows(XDB
::$connec);
87 $GLOBALS['XDB::trace_data'][] = $trace_data;
88 if (mysqli_errno(XDB
::$connec)) {
89 $GLOBALS['XDB::error'] = true
;
96 public static function query()
98 return new XOrgDBResult(XDB
::_prepare(func_get_args()));
101 public static function execute()
103 return XDB
::_query(XDB
::_prepare(func_get_args()));
106 public static function iterator()
108 return new XOrgDBIterator(XDB
::_prepare(func_get_args()));
111 public static function iterRow()
113 return new XOrgDBIterator(XDB
::_prepare(func_get_args()), MYSQL_NUM
);
116 public static function insertId()
118 return mysqli_insert_id(XDB
::$connec);
121 public static function _db_escape($var)
123 switch (gettype($var)) {
133 return "'".addslashes($var)."'";
140 return "'".addslashes(serialize($var))."'";
143 die(var_export($var, true
).' is not a valid for a database entry');
147 public static function trace_format(&$page, $template = 'skin/common.database-debug.tpl') {
148 $page->assign('trace_data', @$GLOBALS['XDB::trace_data']);
149 $page->assign('db_error', @$GLOBALS['XDB::error']);
150 return $page->fetch($template);
159 function XOrgDBResult($query)
161 $this->_res
= XDB
::_query($query);
166 mysqli_free_result($this->_res
);
172 return mysqli_fetch_row($this->_res
);
175 function _fetchAssoc()
177 return mysqli_fetch_assoc($this->_res
);
180 function fetchAllRow()
183 while ($result[] = mysqli_fetch_row($this->_res
)) { }
189 function fetchAllAssoc()
192 while ($result[] = mysqli_fetch_assoc($this->_res
)) { }
198 function fetchOneAssoc()
200 $tmp = $this->_fetchAssoc();
205 function fetchOneRow()
207 $tmp = $this->_fetchRow();
212 function fetchOneCell()
214 $tmp = $this->_fetchRow();
219 function fetchColumn($key = 0)
222 if (is_numeric($key)) {
223 while($tmp = $this->_fetchRow()) {
227 while($tmp = $this->_fetchAssoc()) {
237 return mysqli_num_rows($this->_res
);
246 private $_mode = MYSQL_ASSOC
;
248 function __construct($query, $mode = MYSQL_ASSOC
)
250 $this->_result
= new XOrgDBResult($query);
252 $this->_total
= $this->_result
->numRows();
253 $this->_mode
= $mode;
259 if ($this->_pos
> $this->_total
) {
260 $this->_result
->free();
264 return $this->_mode
!= MYSQL_ASSOC ?
$this->_result
->_fetchRow() : $this->_result
->_fetchAssoc();
269 return $this->_pos
== 1;
274 return $this->_last
== $this->_total
;
279 return $this->_total
;
283 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: