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['errno'] = XDB
::$mysqli->errno
;
104 $trace_data['exectime'] = $time;
105 $trace_data['rows'] = @$res->num_rows ?
$res->num_rows
: XDB
::$mysqli->affected_rows
;
106 $GLOBALS['XDB::trace_data'][] = $trace_data;
107 if (XDB
::$mysqli->errno
) {
108 $GLOBALS['XDB::error'] = true
;
114 public static function query()
116 return new XOrgDBResult(XDB
::_prepare(func_get_args()));
119 public static function execute()
121 return XDB
::_query(XDB
::_prepare(func_get_args()));
124 public static function iterator()
126 return new XOrgDBIterator(XDB
::_prepare(func_get_args()));
129 public static function iterRow()
131 return new XOrgDBIterator(XDB
::_prepare(func_get_args()), MYSQL_NUM
);
134 public static function insertId()
136 return XDB
::$mysqli->insert_id
;
139 public static function errno()
142 if ($globals->debug
& 1) {
143 $count = count($GLOBALS['XDB::trace_data']);
147 return $GLOBALS['XDB::trace_data'][$count - 1]['errno'];
149 return XDB
::$mysqli->errno
;
152 public static function error()
155 if ($globals->debug
& 1) {
156 $count = count($GLOBALS['XDB::trace_data']);
160 return $GLOBALS['XDB::trace_data'][$count - 1]['error'];
162 return XDB
::$mysqli->error
;
165 public static function affectedRows()
167 return XDB
::$mysqli->affected_rows
;
170 public static function _db_escape($var)
172 switch (gettype($var)) {
182 return "'".addslashes($var)."'";
189 return "'".addslashes(serialize($var))."'";
192 die(var_export($var, true
).' is not a valid for a database entry');
196 public static function trace_format(&$page, $template = 'skin/common.database-debug.tpl') {
197 $page->assign('trace_data', @$GLOBALS['XDB::trace_data']);
198 $page->assign('db_error', @$GLOBALS['XDB::error']);
199 return $page->fetch($template);
208 function XOrgDBResult($query)
210 $this->_res
= XDB
::_query($query);
221 return $this->_res
->fetch_row();
224 function _fetchAssoc()
226 return $this->_res
->fetch_assoc();
229 function fetchAllRow()
232 while ($result[] = $this->_res
->fetch_row());
238 function fetchAllAssoc()
241 while ($result[] = $this->_res
->fetch_assoc());
247 function fetchOneAssoc()
249 $tmp = $this->_fetchAssoc();
254 function fetchOneRow()
256 $tmp = $this->_fetchRow();
261 function fetchOneCell()
263 $tmp = $this->_fetchRow();
268 function fetchColumn($key = 0)
271 if (is_numeric($key)) {
272 while($tmp = $this->_fetchRow()) {
276 while($tmp = $this->_fetchAssoc()) {
284 function fetchOneField()
286 return $this->_res
->fetch_field();
289 function fetchFields()
292 while ($res[] = $this->fetchOneField());
298 return $this->_res
->num_rows
;
301 function fieldCount()
303 return $this->_res
->field_count
;
314 private $_mode = MYSQL_ASSOC
;
316 function __construct($query, $mode = MYSQL_ASSOC
)
318 $this->_result
= new XOrgDBResult($query);
320 $this->_total
= $this->_result
->numRows();
322 $this->_fields
= $this->_result
->fieldCount();
323 $this->_mode
= $mode;
329 if ($this->_pos
> $this->_total
) {
330 $this->_result
->free();
334 return $this->_mode
!= MYSQL_ASSOC ?
$this->_result
->_fetchRow() : $this->_result
->_fetchAssoc();
339 return $this->_pos
== 1;
344 return $this->_pos
== $this->_total
;
349 return $this->_total
;
355 if ($this->_fpos
> $this->_fields
) {
358 return $this->_result
->fetchOneField();
361 function firstField()
363 return $this->_fpos
== 1;
368 return $this->_fpos
== $this->_fields
;
371 function totalFields()
373 return $this->_fields
;
377 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: