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($host, $user, $pwd, $db, $charset = 'utf8', $debug = 0)
28 XDB
::$mysqli = new mysqli($host, $user, $pwd, $db);
29 if (mysqli_connect_errno() && $debug & 1) {
30 $GLOBALS['XDB::trace_data'][] = array('query' => 'MySQLI connection', 'explain' => array(),
31 'error' => mysqli_connect_error(), 'exectime' => 0, 'rows' => 0);
32 $GLOBALS['XDB::error'] = true
;
35 XDB
::$mysqli->autocommit(true
);
36 XDB
::$mysqli->set_charset($charset);
40 public static function _prepare($args)
42 $query = array_map(Array('XDB', '_db_escape'), $args);
43 $query[0] = str_replace('{?}', '%s', str_replace('%', '%%', $args[0]));
44 return call_user_func_array('sprintf', $query);
47 public static function _reformatQuery($query)
49 $query = preg_split("/\n\\s*/", trim($query));
51 foreach ($query as $key=>$line) {
53 if (preg_match('/^([A-Z]+(?:\s+(?:JOIN|BY|FROM|INTO))?)\s+(.*)/u', $line, $matches)
54 && $matches[1] != 'AND' && $matches[1] != 'OR')
56 $local = strlen($matches[1]);
57 $line = $matches[1] . ' ' . $matches[2];
58 $length = max($length, $local);
60 $query[$key] = array($line, $local);
63 foreach ($query as $array) {
64 list($line, $local) = $array;
65 $local = max(0, $length - $local);
66 $res .= str_repeat(' ', $local) . $line . "\n";
67 $length +
= 2 * (substr_count($line, '(') - substr_count($line, ')'));
72 public static function _query($query)
76 if ($globals->debug
& 1) {
78 if (strpos($query, 'FOUND_ROWS()') === false
) {
79 $res = XDB
::$mysqli->query("EXPLAIN $query");
81 while ($row = $res->fetch_assoc()) {
87 $trace_data = array('query' => XDB
::_reformatQuery($query), 'explain' => $explain);
88 $time_start = microtime();
91 $res = XDB
::$mysqli->query($query);
93 if ($globals->debug
& 1) {
94 list($ue, $se) = explode(" ", microtime());
95 list($us, $ss) = explode(" ", $time_start);
96 $time = intval((($ue - $us) +
($se - $ss)) * 1000);
97 $trace_data['error'] = XDB
::$mysqli->error
;
98 $trace_data['exectime'] = $time;
99 $trace_data['rows'] = @$res->num_rows ?
$res->num_rows
: XDB
::$mysqli->affected_rows
;
100 $GLOBALS['XDB::trace_data'][] = $trace_data;
101 if (XDB
::$mysqli->errno
) {
102 $GLOBALS['XDB::error'] = true
;
108 public static function query()
110 return new XOrgDBResult(XDB
::_prepare(func_get_args()));
113 public static function execute()
115 return XDB
::_query(XDB
::_prepare(func_get_args()));
118 public static function iterator()
120 return new XOrgDBIterator(XDB
::_prepare(func_get_args()));
123 public static function iterRow()
125 return new XOrgDBIterator(XDB
::_prepare(func_get_args()), MYSQL_NUM
);
128 public static function insertId()
130 return XDB
::$mysqli->insert_id
;
133 public static function _db_escape($var)
135 switch (gettype($var)) {
145 return "'".addslashes($var)."'";
152 return "'".addslashes(serialize($var))."'";
155 die(var_export($var, true
).' is not a valid for a database entry');
159 public static function trace_format(&$page, $template = 'skin/common.database-debug.tpl') {
160 $page->assign('trace_data', @$GLOBALS['XDB::trace_data']);
161 $page->assign('db_error', @$GLOBALS['XDB::error']);
162 return $page->fetch($template);
171 function XOrgDBResult($query)
173 $this->_res
= XDB
::_query($query);
184 return $this->_res
->fetch_row();
187 function _fetchAssoc()
189 return $this->_res
->fetch_assoc();
192 function fetchAllRow()
195 while ($result[] = $this->_res
->fetch_row());
201 function fetchAllAssoc()
204 while ($result[] = $this->_res
->fetch_assoc());
210 function fetchOneAssoc()
212 $tmp = $this->_fetchAssoc();
217 function fetchOneRow()
219 $tmp = $this->_fetchRow();
224 function fetchOneCell()
226 $tmp = $this->_fetchRow();
231 function fetchColumn($key = 0)
234 if (is_numeric($key)) {
235 while($tmp = $this->_fetchRow()) {
239 while($tmp = $this->_fetchAssoc()) {
249 return $this->_res
->num_rows
;
258 private $_mode = MYSQL_ASSOC
;
260 function __construct($query, $mode = MYSQL_ASSOC
)
262 $this->_result
= new XOrgDBResult($query);
264 $this->_total
= $this->_result
->numRows();
265 $this->_mode
= $mode;
271 if ($this->_pos
> $this->_total
) {
272 $this->_result
->free();
276 return $this->_mode
!= MYSQL_ASSOC ?
$this->_result
->_fetchRow() : $this->_result
->_fetchAssoc();
281 return $this->_pos
== 1;
286 return $this->_last
== $this->_total
;
291 return $this->_total
;
295 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: