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 ($globals->debug
& 1) {
31 $bt = new PlBacktrace('MySQL');
32 if (mysqli_connect_errno()) {
33 $bt->newEvent("MySQLI connection", 0, mysqli_connect_error());
37 XDB
::$mysqli->autocommit(true
);
38 XDB
::$mysqli->set_charset($globals->dbcharset
);
42 public static function _prepare($args)
44 $query = array_map(Array('XDB', '_db_escape'), $args);
45 $query[0] = str_replace('{?}', '%s', str_replace('%', '%%', $args[0]));
46 return call_user_func_array('sprintf', $query);
49 public static function _reformatQuery($query)
51 $query = preg_split("/\n\\s*/", trim($query));
53 foreach ($query as $key=>$line) {
55 if (preg_match('/^([A-Z]+(?:\s+(?:JOIN|BY|FROM|INTO))?)\s+(.*)/u', $line, $matches)
56 && $matches[1] != 'AND' && $matches[1] != 'OR')
58 $local = strlen($matches[1]);
59 $line = $matches[1] . ' ' . $matches[2];
60 $length = max($length, $local);
62 $query[$key] = array($line, $local);
65 foreach ($query as $array) {
66 list($line, $local) = $array;
67 $local = max(0, $length - $local);
68 $res .= str_repeat(' ', $local) . $line . "\n";
69 $length +
= 2 * (substr_count($line, '(') - substr_count($line, ')'));
74 public static function _query($query)
78 if (!XDB
::$mysqli && !XDB
::connect()) {
82 if ($globals->debug
& 1) {
84 if (strpos($query, 'FOUND_ROWS()') === false
) {
85 $res = XDB
::$mysqli->query("EXPLAIN $query");
87 while ($row = $res->fetch_assoc()) {
93 PlBacktrace
::$bt['MySQL']->start(XDB
::_reformatQuery($query));
96 $res = XDB
::$mysqli->query($query);
98 if ($globals->debug
& 1) {
99 PlBacktrace
::$bt['MySQL']->stop(@$res->num_rows ?
$res->num_rows
: XDB
::$mysqli->affected_rows
,
106 public static function query()
108 return new XOrgDBResult(XDB
::_prepare(func_get_args()));
111 public static function execute()
113 return XDB
::_query(XDB
::_prepare(func_get_args()));
116 public static function iterator()
118 return new XOrgDBIterator(XDB
::_prepare(func_get_args()));
121 public static function iterRow()
123 return new XOrgDBIterator(XDB
::_prepare(func_get_args()), MYSQL_NUM
);
126 public static function insertId()
128 return XDB
::$mysqli->insert_id
;
131 public static function errno()
133 return XDB
::$mysqli->errno
;
136 public static function error()
138 return XDB
::$mysqli->error
;
141 public static function affectedRows()
143 return XDB
::$mysqli->affected_rows
;
146 public static function _db_escape($var)
148 switch (gettype($var)) {
158 return "'".addslashes($var)."'";
165 return "'".addslashes(serialize($var))."'";
168 die(var_export($var, true
).' is not a valid for a database entry');
178 public function __construct($query)
180 $this->_res
= XDB
::_query($query);
183 public function free()
191 protected function _fetchRow()
193 return $this->_res ?
$this->_res
->fetch_row() : null
;
196 protected function _fetchAssoc()
198 return $this->_res ?
$this->_res
->fetch_assoc() : null
;
201 public function fetchAllRow()
207 while ($result[] = $this->_res
->fetch_row());
213 public function fetchAllAssoc()
219 while ($result[] = $this->_res
->fetch_assoc());
225 public function fetchOneAssoc()
227 $tmp = $this->_fetchAssoc();
232 public function fetchOneRow()
234 $tmp = $this->_fetchRow();
239 public function fetchOneCell()
241 $tmp = $this->_fetchRow();
246 public function fetchColumn($key = 0)
249 if (is_numeric($key)) {
250 while($tmp = $this->_fetchRow()) {
254 while($tmp = $this->_fetchAssoc()) {
262 public function fetchOneField()
264 return $this->_res ?
$this->_res
->fetch_field() : null
;
267 public function fetchFields()
270 while ($res[] = $this->fetchOneField());
274 public function numRows()
276 return $this->_res ?
$this->_res
->num_rows
: 0;
279 public function fieldCount()
281 return $this->_res ?
$this->_res
->field_count
: 0;
285 require_once dirname(__FILE__
) . '/pliterator.php';
287 class XOrgDBIterator
extends XOrgDBResult
implements PlIterator
294 private $_mode = MYSQL_ASSOC
;
296 public function __construct($query, $mode = MYSQL_ASSOC
)
298 parent
::__construct($query);
300 $this->_total
= $this->numRows();
302 $this->_fields
= $this->fieldCount();
303 $this->_mode
= $mode;
306 public function next()
309 if ($this->_pos
> $this->_total
) {
314 return $this->_mode
!= MYSQL_ASSOC ?
$this->_fetchRow() : $this->_fetchAssoc();
317 public function first()
319 return $this->_pos
== 1;
322 public function last()
324 return $this->_pos
== $this->_total
;
327 public function total()
329 return $this->_total
;
332 public function nextField()
335 if ($this->_fpos
> $this->_fields
) {
338 return $this->fetchOneField();
341 public function firstField()
343 return $this->_fpos
== 1;
346 public function lastField()
348 return $this->_fpos
== $this->_fields
;
351 public function totalFields()
353 return $this->_fields
;
357 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: