X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=classes%2Fxdb.php;h=34c10e80d3caa4a72615912e35ac3e8220280ac6;hb=8b1f8e12d444062ef63a0db3a8fa94582a9778c3;hp=70b1f247b185715be047b24e215984a7ca06ff67;hpb=42a50827dc2ac2b13ddaf77ea16c0989cd8b960d;p=platal.git diff --git a/classes/xdb.php b/classes/xdb.php index 70b1f24..34c10e8 100644 --- a/classes/xdb.php +++ b/classes/xdb.php @@ -23,25 +23,22 @@ class XDB { var $_trace_data = array(); - // {{{ function _prepare - - 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); } - // }}} - // {{{ function _reformatQuery - - function _reformatQuery($query) + public static function _reformatQuery($query) { $query = preg_split("/\n\\s*/", $query); $length = 0; 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; } - // }}} - // {{{ function _query - - 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,50 +88,32 @@ class XDB return $res; } - // }}} - // {{{ function query - - function &query() + public static function query() { return new XOrgDBResult(XDB::_prepare(func_get_args())); } - // }}} - // {{{ function execute() - - function execute() + public static function execute() { return XDB::_query(XDB::_prepare(func_get_args())); } - // }}} - // {{{ function iterator() - - function &iterator() + public static function iterator() { return new XOrgDBIterator(XDB::_prepare(func_get_args())); } - // }}} - // {{{ function iterRow() - - function &iterRow() + public static function iterRow() { return new XOrgDBIterator(XDB::_prepare(func_get_args()), MYSQL_NUM); } - // }}} - // {{{ function insertId() - - function insertId() + public static function insertId() { return mysql_insert_id(); } - // }}} - // {{{ function _db_escape - - function _db_escape($var) + public static function _db_escape($var) { switch (gettype($var)) { case 'boolean': @@ -156,57 +139,39 @@ class XDB } } - // }}} - - function trace_format(&$page, $template = 'database-debug.tpl') { - $page->assign('trace_data', $GLOBALS['XDB::trace_data']); - $page->assign('db_error', $GLOBALS['XDB::error']); + 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 { - // {{{ 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: