From 0380bf854bc295fb66fb8e1071d63a3537cdba2c Mon Sep 17 00:00:00 2001 From: x2003bruneau Date: Sun, 4 Feb 2007 17:08:12 +0000 Subject: [PATCH] Use XDB in all CLI scripts and remove all mysql_ calls git-svn-id: svn+ssh://murphy/home/svn/platal/trunk@1480 839d8a87-29fc-0310-9880-83ba4fa771e5 --- bin/cron/checkdb.php | 15 ++++---- bin/cron/clean.php | 4 +-- bin/cron/emails.check.php | 6 ++-- classes/xdb.php | 79 +++++++++++++++++++++++++++++++++++++++++- include/notifs.inc.php | 2 +- upgrade/0.9.14/geoloc.utf8.php | 7 ++-- 6 files changed, 97 insertions(+), 16 deletions(-) diff --git a/bin/cron/checkdb.php b/bin/cron/checkdb.php index 3bd7c7f..f7bea48 100755 --- a/bin/cron/checkdb.php +++ b/bin/cron/checkdb.php @@ -27,16 +27,19 @@ require('./connect.db.inc.php'); require("Console/Getopt.php"); -function check($sql, $commentaire='') { - $res = mysql_query($sql); - if ($err = mysql_error()) echo $err; - if (mysql_num_rows($res)>0) { +function check($sql, $commentaire='') +{ + $it = XDB::iterRow($sql); + if ($err = XDB::error()) echo $err; + if ($it->total() > 0) { echo "Erreur pour la verification : $commentaire\n$sql\n\n"; echo "|"; - while($col = mysql_fetch_field($res)) echo "\t".$col->name."\t|"; + while($col = $it->nextField()) { + echo "\t".$col->name."\t|"; + } echo "\n"; - while ($arr = mysql_fetch_row($res)) { + while ($arr = $it->next()) { echo "|"; foreach ($arr as $val) echo "\t$val\t|"; echo "\n"; diff --git a/bin/cron/clean.php b/bin/cron/clean.php index 7d1f517..ee844fd 100755 --- a/bin/cron/clean.php +++ b/bin/cron/clean.php @@ -24,8 +24,8 @@ require('./connect.db.inc.php'); function query ($sql) { XDB::execute($sql); - if (mysql_errno() != 0) { - echo "error in \"$sql\" :\n", mysql_error(),"\n"; + if (XDB::errno() != 0) { + echo "error in \"$sql\" :\n", XDB::error(), "\n"; } } diff --git a/bin/cron/emails.check.php b/bin/cron/emails.check.php index afb3ed5..99958cc 100755 --- a/bin/cron/emails.check.php +++ b/bin/cron/emails.check.php @@ -58,9 +58,9 @@ if (count($conflits) > 0) { echo "\n\n"; $sql = "INSERT IGNORE INTO emails_watch (email, state, detection, last) VALUES " . join(", ", $insert); - Xdb::execute($sql); - if (mysql_errno() != 0) { - echo 'Error : ' . mysql_error() . "\n$sql"; + XDB::execute($sql); + if (XDB::errno() != 0) { + echo 'Error : ' . XDB::error() . "\n$sql"; } } diff --git a/classes/xdb.php b/classes/xdb.php index 35f455f..6d0223f 100644 --- a/classes/xdb.php +++ b/classes/xdb.php @@ -100,6 +100,7 @@ class XDB list($us, $ss) = explode(" ", $time_start); $time = intval((($ue - $us) + ($se - $ss)) * 1000); $trace_data['error'] = XDB::$mysqli->error; + $trace_data['errno'] = XDB::$mysqli->errno; $trace_data['exectime'] = $time; $trace_data['rows'] = @$res->num_rows ? $res->num_rows : XDB::$mysqli->affected_rows; $GLOBALS['XDB::trace_data'][] = $trace_data; @@ -135,6 +136,37 @@ class XDB return XDB::$mysqli->insert_id; } + public static function errno() + { + global $globals; + if ($globals->debug & 1) { + $count = count($GLOBALS['XDB::trace_data']); + if (!$count) { + return 0; + } + return $GLOBALS['XDB::trace_data'][$count - 1]['errno']; + } + return XDB::$mysqli->errno; + } + + public static function error() + { + global $globals; + if ($globals->debug & 1) { + $count = count($GLOBALS['XDB::trace_data']); + if (!$count) { + return null; + } + return $GLOBALS['XDB::trace_data'][$count - 1]['error']; + } + return XDB::$mysqli->error; + } + + public static function affectedRows() + { + return XDB::$mysqli->affected_rows; + } + public static function _db_escape($var) { switch (gettype($var)) { @@ -249,10 +281,27 @@ class XOrgDBResult return $res; } + function fetchOneField() + { + return $this->_res->fetch_field(); + } + + function fetchFields() + { + $res = array(); + while ($res[] = $this->fetchOneField()); + return $res; + } + function numRows() { return $this->_res->num_rows; } + + function fieldCount() + { + return $this->_res->field_count; + } } class XOrgDBIterator @@ -260,6 +309,8 @@ class XOrgDBIterator private $_result; private $_pos; private $_total; + private $_fpos; + private $_fields; private $_mode = MYSQL_ASSOC; function __construct($query, $mode = MYSQL_ASSOC) @@ -267,6 +318,8 @@ class XOrgDBIterator $this->_result = new XOrgDBResult($query); $this->_pos = 0; $this->_total = $this->_result->numRows(); + $this->_fpost = 0; + $this->_fields = $this->_result->fieldCount(); $this->_mode = $mode; } @@ -288,13 +341,37 @@ class XOrgDBIterator 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 enc=utf-8: diff --git a/include/notifs.inc.php b/include/notifs.inc.php index 96eeddb..0295890 100644 --- a/include/notifs.inc.php +++ b/include/notifs.inc.php @@ -287,7 +287,7 @@ class WatchSub { XDB::execute('DELETE FROM watch_sub WHERE uid={?}', $this->_uid); foreach (Env::v($ind) as $key=>$val) { XDB::query('INSERT INTO watch_sub SELECT {?},id FROM watch_cat WHERE id={?}', $this->_uid, $key); - if(mysql_affected_rows()) { + if(XDB::affectedRows()) { $this->_data[$key] = $key; } } diff --git a/upgrade/0.9.14/geoloc.utf8.php b/upgrade/0.9.14/geoloc.utf8.php index 6ec1649..635c018 100755 --- a/upgrade/0.9.14/geoloc.utf8.php +++ b/upgrade/0.9.14/geoloc.utf8.php @@ -1,7 +1,9 @@ #!/usr/bin/php5 dbcharset = 'latin1'; function is_utf8($s) { @@ -9,7 +11,6 @@ function is_utf8($s) } $tables = array ('city', 'city_in_maps', 'maps', 'pays', 'region'); -XDB::execute("SET NAMES 'latin1'"); foreach ($tables as $table) { $res = XDB::query("SELECT * FROM geoloc_$table"); if (!$res) { @@ -21,10 +22,10 @@ foreach ($tables as $table) { $from = array(); $to = array(); foreach ($array as $key=>$value) { - $from[] = $key . '="' . mysql_real_escape_string($value) . '"'; + $from[] = $key . '="' . XDB::_db_escape($value) . '"'; $valued = utf8_decode($value); if (is_utf8($value) && $valued != $value) { - $to[] = $key . '="' . mysql_real_escape_string($valued) .'"'; + $to[] = $key . '="' . XDB::_db_escape($valued) .'"'; } } if (!empty($to)) { -- 2.1.4