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 private static $mysqli = null
;
26 public static function connect()
29 XDB
::$mysqli = new mysqli($globals->dbhost
, $globals->dbuser
, $globals->dbpwd
, $globals->dbdb
);
30 if (mysqli_connect_errno() && $globals->debug
& 1) {
31 $GLOBALS['XDB::trace_data'][] = array('query' => 'MySQLI connection', 'explain' => array(),
32 'error' => mysqli_connect_error(), 'exectime' => 0, 'rows' => 0);
33 $GLOBALS['XDB::error'] = true
;
36 XDB
::$mysqli->autocommit(true
);
37 XDB
::$mysqli->set_charset($globals->dbcharset
);
41 public static function _prepare($args)
43 $query = array_map(Array('XDB', '_db_escape'), $args);
44 $query[0] = str_replace('{?}', '%s', str_replace('%', '%%', $args[0]));
45 return call_user_func_array('sprintf', $query);
48 public static function _reformatQuery($query)
50 $query = preg_split("/\n\\s*/", trim($query));
52 foreach ($query as $key=>$line) {
54 if (preg_match('/^([A-Z]+(?:\s+(?:JOIN|BY|FROM|INTO))?)\s+(.*)/u', $line, $matches)
55 && $matches[1] != 'AND' && $matches[1] != 'OR')
57 $local = strlen($matches[1]);
58 $line = $matches[1] . ' ' . $matches[2];
59 $length = max($length, $local);
61 $query[$key] = array($line, $local);
64 foreach ($query as $array) {
65 list($line, $local) = $array;
66 $local = max(0, $length - $local);
67 $res .= str_repeat(' ', $local) . $line . "\n";
68 $length +
= 2 * (substr_count($line, '(') - substr_count($line, ')'));
73 public static function _query($query)
77 if (!XDB
::$mysqli && !XDB
::connect()) {
81 if ($globals->debug
& 1) {
83 if (strpos($query, 'FOUND_ROWS()') === false
) {
84 $res = XDB
::$mysqli->query("EXPLAIN $query");
86 while ($row = $res->fetch_assoc()) {
92 $trace_data = array('query' => XDB
::_reformatQuery($query), 'explain' => $explain);
93 $time_start = microtime();
96 $res = XDB
::$mysqli->query($query);
98 if ($globals->debug
& 1) {
99 list($ue, $se) = explode(" ", microtime());
100 list($us, $ss) = explode(" ", $time_start);
101 $time = intval((($ue - $us) +
($se - $ss)) * 1000);
102 $trace_data['error'] = XDB
::$mysqli->error
;
103 $trace_data['exectime'] = $time;
104 $trace_data['rows'] = @$res->num_rows ?
$res->num_rows
: XDB
::$mysqli->affected_rows
;
105 $GLOBALS['XDB::trace_data'][] = $trace_data;
106 if (XDB
::$mysqli->errno
) {
107 $GLOBALS['XDB::error'] = true
;
113 public static function query()
115 return new XOrgDBResult(XDB
::_prepare(func_get_args()));
118 public static function execute()
120 return XDB
::_query(XDB
::_prepare(func_get_args()));
123 public static function iterator()
125 return new XOrgDBIterator(XDB
::_prepare(func_get_args()));
128 public static function iterRow()
130 return new XOrgDBIterator(XDB
::_prepare(func_get_args()), MYSQL_NUM
);
133 public static function insertId()
135 return XDB
::$mysqli->insert_id
;
138 public static function _db_escape($var)
140 switch (gettype($var)) {
150 return "'".addslashes($var)."'";
157 return "'".addslashes(serialize($var))."'";
160 die(var_export($var, true
).' is not a valid for a database entry');
164 public static function trace_format(&$page, $template = 'skin/common.database-debug.tpl') {
165 $page->assign('trace_data', @$GLOBALS['XDB::trace_data']);
166 $page->assign('db_error', @$GLOBALS['XDB::error']);
167 return $page->fetch($template);
176 function XOrgDBResult($query)
178 $this->_res
= XDB
::_query($query);
189 return $this->_res
->fetch_row();
192 function _fetchAssoc()
194 return $this->_res
->fetch_assoc();
197 function fetchAllRow()
200 while ($result[] = $this->_res
->fetch_row());
206 function fetchAllAssoc()
209 while ($result[] = $this->_res
->fetch_assoc());
215 function fetchOneAssoc()
217 $tmp = $this->_fetchAssoc();
222 function fetchOneRow()
224 $tmp = $this->_fetchRow();
229 function fetchOneCell()
231 $tmp = $this->_fetchRow();
236 function fetchColumn($key = 0)
239 if (is_numeric($key)) {
240 while($tmp = $this->_fetchRow()) {
244 while($tmp = $this->_fetchAssoc()) {
254 return $this->_res
->num_rows
;
263 private $_mode = MYSQL_ASSOC
;
265 function __construct($query, $mode = MYSQL_ASSOC
)
267 $this->_result
= new XOrgDBResult($query);
269 $this->_total
= $this->_result
->numRows();
270 $this->_mode
= $mode;
276 if ($this->_pos
> $this->_total
) {
277 $this->_result
->free();
281 return $this->_mode
!= MYSQL_ASSOC ?
$this->_result
->_fetchRow() : $this->_result
->_fetchAssoc();
286 return $this->_pos
== 1;
291 return $this->_last
== $this->_total
;
296 return $this->_total
;
300 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: