class XDB
{
- var $_trace_data = array();
+ private static $mysqli = null;
+
+ public static function connect()
+ {
+ global $globals;
+ XDB::$mysqli = new mysqli($globals->dbhost, $globals->dbuser, $globals->dbpwd, $globals->dbdb);
+ if ($globals->debug & 1) {
+ $bt = new PlBacktrace('MySQL');
+ if (mysqli_connect_errno()) {
+ $bt->newEvent("MySQLI connection", 0, mysqli_connect_error());
+ return false;
+ }
+ }
+ XDB::$mysqli->autocommit(true);
+ XDB::$mysqli->set_charset($globals->dbcharset);
+ return true;
+ }
public static function _prepare($args)
{
$length = 0;
foreach ($query as $key=>$line) {
$local = -2;
- if (preg_match('/^([A-Z]+(?:\s+(?:JOIN|BY|FROM|INTO))?)\s+(.*)/', $line, $matches)
+ if (preg_match('/^([A-Z]+(?:\s+(?:JOIN|BY|FROM|INTO))?)\s+(.*)/u', $line, $matches)
&& $matches[1] != 'AND' && $matches[1] != 'OR')
{
$local = strlen($matches[1]);
{
global $globals;
+ if (!XDB::$mysqli && !XDB::connect()) {
+ return false;
+ }
+
if ($globals->debug & 1) {
- $_res = mysql_query("EXPLAIN $query");
$explain = array();
- while ($row = @mysql_fetch_assoc($_res)) {
- $explain[] = $row;
+ if (strpos($query, 'FOUND_ROWS()') === false) {
+ $res = XDB::$mysqli->query("EXPLAIN $query");
+ if ($res) {
+ while ($row = $res->fetch_assoc()) {
+ $explain[] = $row;
+ }
+ $res->free();
+ }
}
- $trace_data = array('query' => XDB::_reformatQuery($query), 'explain' => $explain);
- @mysql_free_result($_res);
- $time_start = microtime();
+ PlBacktrace::$bt['MySQL']->start(XDB::_reformatQuery($query));
}
- $res = mysql_query($query);
-
+ $res = XDB::$mysqli->query($query);
+
if ($globals->debug & 1) {
- list($ue, $se) = explode(" ", microtime());
- list($us, $ss) = explode(" ", $time_start);
- $time = intval((($ue - $us) + ($se - $ss)) * 1000);
- $trace_data['error'] = mysql_error();
- $trace_data['exectime'] = $time;
- $trace_data['rows'] = @mysql_num_rows() ? mysql_num_rows() : mysql_affected_rows();
- $GLOBALS['XDB::trace_data'][] = $trace_data;
- if (mysql_errno()) {
- $GLOBALS['XDB::error'] = true;
- }
+ PlBacktrace::$bt['MySQL']->stop(@$res->num_rows ? $res->num_rows : XDB::$mysqli->affected_rows,
+ XDB::$mysqli->error,
+ $explain);
}
-
return $res;
}
public static function insertId()
{
- return mysql_insert_id();
+ return XDB::$mysqli->insert_id;
+ }
+
+ public static function errno()
+ {
+ return XDB::$mysqli->errno;
+ }
+
+ public static function error()
+ {
+ return XDB::$mysqli->error;
+ }
+
+ public static function affectedRows()
+ {
+ return XDB::$mysqli->affected_rows;
}
public static function _db_escape($var)
die(var_export($var, true).' is not a valid for a database entry');
}
}
-
- public static function trace_format(&$page, $template = 'skin/common.database-debug.tpl') {
- $page->assign('trace_data', @$GLOBALS['XDB::trace_data']);
- $page->assign('db_error', @$GLOBALS['XDB::error']);
- return $page->fetch($template);
- }
}
class XOrgDBResult
{
- var $_res;
+ private $_res;
function XOrgDBResult($query)
{
function free()
{
- mysql_free_result($this->_res);
+ $this->_res->free();
unset($this);
}
function _fetchRow()
{
- return mysql_fetch_row($this->_res);
+ return $this->_res->fetch_row();
}
function _fetchAssoc()
{
- return mysql_fetch_assoc($this->_res);
+ return $this->_res->fetch_assoc();
}
function fetchAllRow()
{
$result = Array();
- while ($result[] = mysql_fetch_row($this->_res)) { }
+ while ($result[] = $this->_res->fetch_row());
array_pop($result);
$this->free();
return $result;
function fetchAllAssoc()
{
$result = Array();
- while ($result[] = mysql_fetch_assoc($this->_res)) { }
+ while ($result[] = $this->_res->fetch_assoc());
array_pop($result);
$this->free();
return $result;
return $res;
}
+ function fetchOneField()
+ {
+ return $this->_res->fetch_field();
+ }
+
+ function fetchFields()
+ {
+ $res = array();
+ while ($res[] = $this->fetchOneField());
+ return $res;
+ }
+
function numRows()
{
- return mysql_num_rows($this->_res);
+ return $this->_res->num_rows;
+ }
+
+ function fieldCount()
+ {
+ return $this->_res->field_count;
}
}
-class XOrgDBIterator
+require_once dirname(__FILE__) . '/pliterator.php';
+
+class XOrgDBIterator implements PlIterator
{
private $_result;
private $_pos;
private $_total;
+ private $_fpos;
+ private $_fields;
private $_mode = MYSQL_ASSOC;
function __construct($query, $mode = MYSQL_ASSOC)
$this->_result = new XOrgDBResult($query);
$this->_pos = 0;
$this->_total = $this->_result->numRows();
+ $this->_fpost = 0;
+ $this->_fields = $this->_result->fieldCount();
$this->_mode = $mode;
}
function last()
{
- return $this->_last == $this->_total;
+ return $this->_pos == $this->_total;
}
function total()
{
return $this->_total;
}
+
+ function nextField()
+ {
+ $this->_fpos++;
+ if ($this->_fpos > $this->_fields) {
+ return null;
+ }
+ return $this->_result->fetchOneField();
+ }
+
+ function firstField()
+ {
+ return $this->_fpos == 1;
+ }
+
+ function lastField()
+ {
+ return $this->_fpos == $this->_fields;
+ }
+
+ function totalFields()
+ {
+ return $this->_fields;
+ }
}
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
?>