X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=classes%2Fxdb.php;h=5bb1fefa3f4439b86d677b886a71b3779bd1e983;hb=756ff73fbb0cdd01cce2eda27ba18e1cc99bd735;hp=3aa45cdeee0fbc136e9f6bc039f630a9f80b978d;hpb=2b1ee50b0196216dbe4f37e3b039469d6003ca23;p=platal.git diff --git a/classes/xdb.php b/classes/xdb.php index 3aa45cd..5bb1fef 100644 --- a/classes/xdb.php +++ b/classes/xdb.php @@ -1,6 +1,6 @@ dbhost, $globals->dbuser, $globals->dbpwd, $globals->dbdb); - if ($globals->debug & 1) { + if ($globals->debug & DEBUG_BT) { $bt = new PlBacktrace('MySQL'); if (mysqli_connect_errno()) { $bt->newEvent("MySQLI connection", 0, mysqli_connect_error()); @@ -41,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); } @@ -79,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"); @@ -94,8 +94,8 @@ class XDB } $res = XDB::$mysqli->query($query); - - if ($globals->debug & 1) { + + if ($globals->debug & DEBUG_BT) { PlBacktrace::$bt['MySQL']->stop(@$res->num_rows ? $res->num_rows : XDB::$mysqli->affected_rows, XDB::$mysqli->error, $explain); @@ -110,7 +110,12 @@ class XDB public static function execute() { - return XDB::_query(XDB::_prepare(func_get_args())); + global $globals; + $args = func_get_args(); + if ($globals->mode != 'rw' && !strpos($args[0], 'logger')) { + return; + } + return XDB::_query(XDB::_prepare($args)); } public static function iterator() @@ -134,7 +139,7 @@ class XDB } public static function error() - { + { return XDB::$mysqli->error; } @@ -143,7 +148,7 @@ class XDB return XDB::$mysqli->affected_rows; } - public static function _db_escape($var) + public static function escape($var) { switch (gettype($var)) { case 'boolean': @@ -175,67 +180,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)) { @@ -251,32 +264,32 @@ class XOrgDBResult return $res; } - function fetchOneField() + public function fetchOneField() { - return $this->_res->fetch_field(); + return $this->_res ? $this->_res->fetch_field() : null; } - function fetchFields() + public function fetchFields() { $res = array(); while ($res[] = $this->fetchOneField()); return $res; } - function numRows() + public function numRows() { - return $this->_res->num_rows; + return $this->_res ? $this->_res->num_rows : 0; } - function fieldCount() + public function fieldCount() { - return $this->_res->field_count; + return $this->_res ? $this->_res->field_count : 0; } } require_once dirname(__FILE__) . '/pliterator.php'; -class XOrgDBIterator implements PlIterator +class XOrgDBIterator extends XOrgDBResult implements PlIterator { private $_result; private $_pos; @@ -285,62 +298,62 @@ class XOrgDBIterator implements PlIterator 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->_result->fieldCount(); + $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->_pos == $this->_total; } - function total() + public function total() { return $this->_total; } - function nextField() + public function nextField() { $this->_fpos++; if ($this->_fpos > $this->_fields) { return null; } - return $this->_result->fetchOneField(); + return $this->fetchOneField(); } - function firstField() + public function firstField() { return $this->_fpos == 1; } - function lastField() + public function lastField() { return $this->_fpos == $this->_fields; } - function totalFields() + public function totalFields() { return $this->_fields; }