X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=classes%2Fxdb.php;h=502af952d9f32fe915447345cf68096bc66cce93;hb=dcdcd18aeb2b29ddfa7768317b14ca0a277dd654;hp=35f455f90b64aa2783583c89c78556ae5e90d80f;hpb=821744e06ed5c0c6b332682ee81cdc32c0925996;p=platal.git diff --git a/classes/xdb.php b/classes/xdb.php index 35f455f..502af95 100644 --- a/classes/xdb.php +++ b/classes/xdb.php @@ -27,11 +27,12 @@ class XDB { global $globals; XDB::$mysqli = new mysqli($globals->dbhost, $globals->dbuser, $globals->dbpwd, $globals->dbdb); - if (mysqli_connect_errno() && $globals->debug & 1) { - $GLOBALS['XDB::trace_data'][] = array('query' => 'MySQLI connection', 'explain' => array(), - 'error' => mysqli_connect_error(), 'exectime' => 0, 'rows' => 0); - $GLOBALS['XDB::error'] = true; - return false; + if ($globals->debug & DEBUG_BT) { + $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); @@ -40,7 +41,7 @@ class XDB public static function _prepare($args) { - $query = array_map(Array('XDB', '_db_escape'), $args); + $query = array_map(Array('XDB', 'escape'), $args); $query[0] = str_replace('{?}', '%s', str_replace('%', '%%', $args[0])); return call_user_func_array('sprintf', $query); } @@ -78,7 +79,7 @@ class XDB return false; } - if ($globals->debug & 1) { + if ($globals->debug & DEBUG_BT) { $explain = array(); if (strpos($query, 'FOUND_ROWS()') === false) { $res = XDB::$mysqli->query("EXPLAIN $query"); @@ -89,23 +90,15 @@ class XDB $res->free(); } } - $trace_data = array('query' => XDB::_reformatQuery($query), 'explain' => $explain); - $time_start = microtime(); + PlBacktrace::$bt['MySQL']->start(XDB::_reformatQuery($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'] = XDB::$mysqli->error; - $trace_data['exectime'] = $time; - $trace_data['rows'] = @$res->num_rows ? $res->num_rows : XDB::$mysqli->affected_rows; - $GLOBALS['XDB::trace_data'][] = $trace_data; - if (XDB::$mysqli->errno) { - $GLOBALS['XDB::error'] = true; - } + + if ($globals->debug & DEBUG_BT) { + PlBacktrace::$bt['MySQL']->stop(@$res->num_rows ? $res->num_rows : XDB::$mysqli->affected_rows, + XDB::$mysqli->error, + $explain); } return $res; } @@ -135,7 +128,22 @@ class XDB return XDB::$mysqli->insert_id; } - public static function _db_escape($var) + 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 escape($var) { switch (gettype($var)) { case 'boolean': @@ -160,12 +168,6 @@ class XDB 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 @@ -173,67 +175,75 @@ class XOrgDBResult private $_res; - function XOrgDBResult($query) + public function __construct($query) { $this->_res = XDB::_query($query); } - function free() + public function free() { - $this->_res->free(); + if ($this->_res) { + $this->_res->free(); + } unset($this); } - function _fetchRow() + protected function _fetchRow() { - return $this->_res->fetch_row(); + return $this->_res ? $this->_res->fetch_row() : null; } - function _fetchAssoc() + protected function _fetchAssoc() { - return $this->_res->fetch_assoc(); + return $this->_res ? $this->_res->fetch_assoc() : null; } - function fetchAllRow() + public function fetchAllRow() { $result = Array(); + if (!$this->_res) { + return $result; + } while ($result[] = $this->_res->fetch_row()); array_pop($result); $this->free(); return $result; } - function fetchAllAssoc() + public function fetchAllAssoc() { $result = Array(); + if (!$this->_res) { + return $result; + } while ($result[] = $this->_res->fetch_assoc()); array_pop($result); $this->free(); return $result; } - function fetchOneAssoc() + public function fetchOneAssoc() { $tmp = $this->_fetchAssoc(); $this->free(); return $tmp; } - function fetchOneRow() + public function fetchOneRow() { $tmp = $this->_fetchRow(); $this->free(); return $tmp; } - function fetchOneCell() + public function fetchOneCell() { $tmp = $this->_fetchRow(); $this->free(); return $tmp[0]; } - function fetchColumn($key = 0) + public function fetchColumn($key = 0) { $res = Array(); if (is_numeric($key)) { @@ -249,52 +259,99 @@ class XOrgDBResult return $res; } - function numRows() + public function fetchOneField() + { + return $this->_res ? $this->_res->fetch_field() : null; + } + + public function fetchFields() + { + $res = array(); + while ($res[] = $this->fetchOneField()); + return $res; + } + + public function numRows() + { + return $this->_res ? $this->_res->num_rows : 0; + } + + public function fieldCount() { - return $this->_res->num_rows; + return $this->_res ? $this->_res->field_count : 0; } } -class XOrgDBIterator +require_once dirname(__FILE__) . '/pliterator.php'; + +class XOrgDBIterator extends XOrgDBResult implements PlIterator { private $_result; private $_pos; private $_total; + private $_fpos; + private $_fields; private $_mode = MYSQL_ASSOC; - function __construct($query, $mode = MYSQL_ASSOC) + public function __construct($query, $mode = MYSQL_ASSOC) { - $this->_result = new XOrgDBResult($query); + parent::__construct($query); $this->_pos = 0; - $this->_total = $this->_result->numRows(); + $this->_total = $this->numRows(); + $this->_fpost = 0; + $this->_fields = $this->fieldCount(); $this->_mode = $mode; } - function next() + public function next() { $this->_pos ++; if ($this->_pos > $this->_total) { - $this->_result->free(); + $this->free(); unset($this); return null; } - return $this->_mode != MYSQL_ASSOC ? $this->_result->_fetchRow() : $this->_result->_fetchAssoc(); + return $this->_mode != MYSQL_ASSOC ? $this->_fetchRow() : $this->_fetchAssoc(); } - function first() + public function first() { return $this->_pos == 1; } - function last() + public function last() { - return $this->_last == $this->_total; + return $this->_pos == $this->_total; } - function total() + public function total() { return $this->_total; } + + public function nextField() + { + $this->_fpos++; + if ($this->_fpos > $this->_fields) { + return null; + } + return $this->fetchOneField(); + } + + public function firstField() + { + return $this->_fpos == 1; + } + + public function lastField() + { + return $this->_fpos == $this->_fields; + } + + public function totalFields() + { + return $this->_fields; + } } // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: