X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;ds=sidebyside;f=classes%2Fxdb.php;h=34c10e80d3caa4a72615912e35ac3e8220280ac6;hb=8b1f8e12d444062ef63a0db3a8fa94582a9778c3;hp=958b3e2ce36339945e3e502a8d02eec3d18d2f52;hpb=6995a9b92e68cffcf7a8375080f9e5a210acf0af;p=platal.git diff --git a/classes/xdb.php b/classes/xdb.php index 958b3e2..34c10e8 100644 --- a/classes/xdb.php +++ b/classes/xdb.php @@ -23,17 +23,13 @@ class XDB { var $_trace_data = array(); - // {{{ public static function _prepare - - public static function _prepare($args) { + public static function _prepare($args) + { $query = array_map(Array('XDB', '_db_escape'), $args); $query[0] = str_replace('{?}', '%s', str_replace('%', '%%', $args[0])); return call_user_func_array('sprintf', $query); } - // }}} - // {{{ public static function _reformatQuery - public static function _reformatQuery($query) { $query = preg_split("/\n\\s*/", $query); @@ -41,7 +37,8 @@ class XDB foreach ($query as $key=>$line) { $local = -2; if (preg_match('/^([A-Z]+(?:\s+(?:JOIN|BY|FROM|INTO))?)\s+(.*)/', $line, $matches) - && $matches[1] != 'AND' && $matches[1] != 'OR') { + && $matches[1] != 'AND' && $matches[1] != 'OR') + { $local = strlen($matches[1]); $line = $matches[1] . ' ' . $matches[2]; $length = max($length, $local); @@ -58,10 +55,8 @@ class XDB return $res; } - // }}} - // {{{ public static function _query - - public static function _query($query) { + public static function _query($query) + { global $globals; if ($globals->debug & 1) { @@ -72,12 +67,18 @@ class XDB } $trace_data = array('query' => XDB::_reformatQuery($query), 'explain' => $explain); @mysql_free_result($_res); + $time_start = microtime(); } $res = mysql_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; @@ -87,49 +88,31 @@ class XDB return $res; } - // }}} - // {{{ public static function query - public static function query() { return new XOrgDBResult(XDB::_prepare(func_get_args())); } - // }}} - // {{{ public static function execute() - public static function execute() { return XDB::_query(XDB::_prepare(func_get_args())); } - // }}} - // {{{ public static function iterator() - public static function iterator() { return new XOrgDBIterator(XDB::_prepare(func_get_args())); } - // }}} - // {{{ public static function iterRow() - public static function iterRow() { return new XOrgDBIterator(XDB::_prepare(func_get_args()), MYSQL_NUM); } - // }}} - // {{{ public static function insertId() - public static function insertId() { return mysql_insert_id(); } - // }}} - // {{{ public static function _db_escape - public static function _db_escape($var) { switch (gettype($var)) { @@ -156,9 +139,7 @@ class XDB } } - // }}} - - public static function trace_format(&$page, $template = 'database-debug.tpl') { + 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); @@ -167,46 +148,30 @@ class XDB class XOrgDBResult { - // {{{ properties var $_res; - // }}} - // {{{ constructor - function XOrgDBResult($query) { $this->_res = XDB::_query($query); } - // }}} - // {{{ destructor - function free() { mysql_free_result($this->_res); unset($this); } - // }}} - // {{{ function fetchRow - function _fetchRow() { return mysql_fetch_row($this->_res); } - // }}} - // {{{ function fetchAssoc - function _fetchAssoc() { return mysql_fetch_assoc($this->_res); } - // }}} - // {{{ function fetchAllRow - function fetchAllRow() { $result = Array(); @@ -216,9 +181,6 @@ class XOrgDBResult return $result; } - // }}} - // {{{ function fetchAllAssoc - function fetchAllAssoc() { $result = Array(); @@ -228,9 +190,6 @@ class XOrgDBResult return $result; } - // }}} - // {{{ function fetchOneAssoc() - function fetchOneAssoc() { $tmp = $this->_fetchAssoc(); @@ -238,9 +197,6 @@ class XOrgDBResult return $tmp; } - // }}} - // {{{ function fetchOneRow() - function fetchOneRow() { $tmp = $this->_fetchRow(); @@ -248,9 +204,6 @@ class XOrgDBResult return $tmp; } - // }}} - // {{{ function fetchOneCell() - function fetchOneCell() { $tmp = $this->_fetchRow(); @@ -258,9 +211,6 @@ class XOrgDBResult return $tmp[0]; } - // }}} - // {{{ function fetchColumn() - function fetchColumn($key = 0) { $res = Array(); @@ -277,30 +227,20 @@ class XOrgDBResult return $res; } - // }}} - // {{{ function numRows - function numRows() { return mysql_num_rows($this->_res); } - - // }}} } class XOrgDBIterator { - // {{{ properties + private $_result; + private $_pos; + private $_total; + private $_mode = MYSQL_ASSOC; - var $_result; - var $_pos; - var $_total; - var $_mode = MYSQL_ASSOC; - - // }}} - // {{{ constructor - - function XOrgDBIterator($query, $mode = MYSQL_ASSOC) + function __construct($query, $mode = MYSQL_ASSOC) { $this->_result =& new XOrgDBResult($query); $this->_pos = 0; @@ -308,9 +248,6 @@ class XOrgDBIterator $this->_mode = $mode; } - // }}} - // {{{ function next () - function next() { $this->_pos ++; @@ -322,31 +259,20 @@ class XOrgDBIterator return $this->_mode != MYSQL_ASSOC ? $this->_result->_fetchRow() : $this->_result->_fetchAssoc(); } - // }}} - // {{{ function first - function first() { return $this->_pos == 1; } - // }}} - // {{{ function last - function last() { return $this->_last == $this->_total; } - // }}} - // {{{ function total() - function total() { return $this->_total; } - - // }}} } // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: