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 && strpos($query, 'FOUND_ROWS()') === false
) {
64 $_res = mysqli_query(XDB
::$connec, "EXPLAIN $query");
66 while ($row = mysqli_fetch_assoc($_res)) {
69 $trace_data = array('query' => XDB
::_reformatQuery($query), 'explain' => $explain);
70 @mysqli_free_result
($_res);
71 $time_start = microtime();
72 } elseif ($globals->debug
& 1) {
73 $trace_data = array('query' => XDB
::_reformatQuery($query), 'explain' => array());
74 $time_start = microtime();
77 $res = mysqli_query(XDB
::$connec, $query);
79 if ($globals->debug
& 1) {
80 list($ue, $se) = explode(" ", microtime());
81 list($us, $ss) = explode(" ", $time_start);
82 $time = intval((($ue - $us) +
($se - $ss)) * 1000);
83 $trace_data['error'] = mysqli_error(XDB
::$connec);
84 $trace_data['exectime'] = $time;
85 $trace_data['rows'] = @mysqli_num_rows
($res) ?
mysqli_num_rows($res) : mysqli_affected_rows(XDB
::$connec);
86 $GLOBALS['XDB::trace_data'][] = $trace_data;
87 if (mysqli_errno(XDB
::$connec)) {
88 $GLOBALS['XDB::error'] = true
;
95 public static function query()
97 return new XOrgDBResult(XDB
::_prepare(func_get_args()));
100 public static function execute()
102 return XDB
::_query(XDB
::_prepare(func_get_args()));
105 public static function iterator()
107 return new XOrgDBIterator(XDB
::_prepare(func_get_args()));
110 public static function iterRow()
112 return new XOrgDBIterator(XDB
::_prepare(func_get_args()), MYSQL_NUM
);
115 public static function insertId()
117 return mysqli_insert_id(XDB
::$connec);
120 public static function _db_escape($var)
122 switch (gettype($var)) {
132 return "'".addslashes($var)."'";
139 return "'".addslashes(serialize($var))."'";
142 die(var_export($var, true
).' is not a valid for a database entry');
146 public static function trace_format(&$page, $template = 'skin/common.database-debug.tpl') {
147 $page->assign('trace_data', @$GLOBALS['XDB::trace_data']);
148 $page->assign('db_error', @$GLOBALS['XDB::error']);
149 return $page->fetch($template);
158 function XOrgDBResult($query)
160 $this->_res
= XDB
::_query($query);
165 mysqli_free_result($this->_res
);
171 return mysqli_fetch_row($this->_res
);
174 function _fetchAssoc()
176 return mysqli_fetch_assoc($this->_res
);
179 function fetchAllRow()
182 while ($result[] = mysqli_fetch_row($this->_res
)) { }
188 function fetchAllAssoc()
191 while ($result[] = mysqli_fetch_assoc($this->_res
)) { }
197 function fetchOneAssoc()
199 $tmp = $this->_fetchAssoc();
204 function fetchOneRow()
206 $tmp = $this->_fetchRow();
211 function fetchOneCell()
213 $tmp = $this->_fetchRow();
218 function fetchColumn($key = 0)
221 if (is_numeric($key)) {
222 while($tmp = $this->_fetchRow()) {
226 while($tmp = $this->_fetchAssoc()) {
236 return mysqli_num_rows($this->_res
);
245 private $_mode = MYSQL_ASSOC
;
247 function __construct($query, $mode = MYSQL_ASSOC
)
249 $this->_result
= new XOrgDBResult($query);
251 $this->_total
= $this->_result
->numRows();
252 $this->_mode
= $mode;
258 if ($this->_pos
> $this->_total
) {
259 $this->_result
->free();
263 return $this->_mode
!= MYSQL_ASSOC ?
$this->_result
->_fetchRow() : $this->_result
->_fetchAssoc();
268 return $this->_pos
== 1;
273 return $this->_last
== $this->_total
;
278 return $this->_total
;
282 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: