From 7ef8474e4ca093a3b89c0b22637c7fe6928c8d09 Mon Sep 17 00:00:00 2001 From: rMalie Date: Wed, 7 Sep 2011 12:25:47 +0000 Subject: [PATCH] // Fix tryToConnect methods for DB git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@8386 b9a71923-0436-4b27-9f14-aed3839534dd --- classes/Db.php | 50 +++++++++++++------------------------------- classes/DbMySQLi.php | 41 +++--------------------------------- classes/MySQL.php | 33 ----------------------------- 3 files changed, 18 insertions(+), 106 deletions(-) diff --git a/classes/Db.php b/classes/Db.php index 580c6d781..62abd6ca4 100644 --- a/classes/Db.php +++ b/classes/Db.php @@ -136,22 +136,6 @@ abstract class DbCore */ abstract public function getNumberError(); - /** - * Try a connection to te database - * - * @param string $newDbLink - * @return int - */ - abstract public function tryConnection($newDbLink = true); - - /** - * Try to change encoding on a database - * - * @param string $encoding - * @return bool - */ - abstract public function tryEncoding($encoding = 'UTF8'); - /* do not remove, useful for some modules */ abstract public function set_db($db_name); @@ -175,27 +159,25 @@ abstract class DbCore } if (!isset(self::$_instance[$idServer])) - self::$_instance[$idServer] = Db::factory(self::$_servers[$idServer]['server'], self::$_servers[$idServer]['user'], self::$_servers[$idServer]['password'], self::$_servers[$idServer]['database']); - + { + $class = Db::getClass(); + self::$_instance[$idServer] = new $class(self::$_servers[$idServer]['server'], self::$_servers[$idServer]['user'], self::$_servers[$idServer]['password'], self::$_servers[$idServer]['database']); + } + return self::$_instance[$idServer]; } /** - * Get child layer class instance + * Get child layer class * - * @param string $server Server address - * @param string $user User login - * @param string $password User password - * @param string $database Database name - * @param bool $connect If false, don't connect in constructor - * @return Db + * @return string */ - public static function factory($server, $user, $password, $database, $connect = true) + public static function getClass() { $class = 'MySQL'; if (class_exists('mysqli', false)) $class = 'DbMySQLi'; - return new $class($server, $user, $password, $database, $connect); + return $class; } /** @@ -505,27 +487,25 @@ abstract class DbCore * @param string $user Login for database connection * @param string $pwd Password for database connection * @param string $db Database name + * @param bool $newDbLink * @return int */ - static public function checkConnection($server, $user, $pwd, $db) + static public function checkConnection($server, $user, $pwd, $db, $newDbLink = true) { - return Db::factory($server, $user, $pwd, $db, false)->tryConnection(); + return call_user_func_array(array(Db::getClass(), 'tryToConnect'), array($server, $user, $pwd, $db, $newDbLink)); } - /** + /** * Try a connection to te database * * @param string $server Server address * @param string $user Login for database connection * @param string $pwd Password for database connection - * @param string $db Database name - * @param string $newDbLink Database name * @return int - * @todo remake with static */ - static public function checkEncoding($server, $user, $pwd, $encoding = 'UTF8') + static public function checkEncoding($server, $user, $pwd) { - return Db::factory($server, $user, $pwd, '', false)->tryEncoding($encoding); + return call_user_func_array(array(Db::getClass(), 'tryToConnect'), array($server, $user, $pwd)); } /** diff --git a/classes/DbMySQLi.php b/classes/DbMySQLi.php index e52a4b55e..dc1e76506 100644 --- a/classes/DbMySQLi.php +++ b/classes/DbMySQLi.php @@ -134,54 +134,19 @@ class DbMySQLiCore extends Db { return $this->_link->query('USE '.pSQL($db_name)); } - - /** - * @see DbCore::tryConnection() - */ - public function tryConnection($newDbLink = true) - { - $link = @new mysqli($this->_server, $this->_user, $this->_password, $this->_database); - - // Do not use object way for error because this work bad before PHP 5.2.9 - if (mysqli_connect_error()) - return 1; - $link->close(); - return 0; - } - - /** - * @see DbCore::tryEncoding() - */ - public function tryEncoding($encoding = 'UTF8') - { - $link = @new mysqli($this->_server, $this->_user, $this->_password, $this->_database); - $ret = $link->query("SET NAMES '".pSQL($encoding)."'"); - $link->close(); - return $ret; - } - - /** - * @deprecated since 1.5.0 - */ + static public function tryToConnect($server, $user, $pwd, $db, $newDbLink = true) { - Tools::displayAsDeprecated(); - - $link = new mysqli($server, $user, $pwd, $db); + $link = @new mysqli($server, $user, $pwd, $db); if (mysqli_connect_error()) return 1; $link->close(); return 0; } - /** - * @deprecated since 1.5.0 - */ static public function tryUTF8($server, $user, $pwd) { - Tools::displayAsDeprecated(); - - $link = new mysqli($server, $user, $pwd, $db); + $link = @new mysqli($server, $user, $pwd, $db); $ret = $link->query("SET NAMES 'UTF8'"); $link->close(); return $ret; diff --git a/classes/MySQL.php b/classes/MySQL.php index 5f526943f..5e972f1cb 100644 --- a/classes/MySQL.php +++ b/classes/MySQL.php @@ -136,36 +136,6 @@ class MySQLCore extends Db return mysql_select_db($db_name, $this->_link); } - /** - * @see DbCore::tryConnection() - */ - public function tryConnection($newDbLink = true) - { - if (!$link = @mysql_connect($server, $user, $pwd, $newDbLink)) - return 1; - if (!@mysql_select_db($db, $link)) - return 2; - @mysql_close($link); - return 0; - } - - /** - * @see DbCore::tryEncoding() - */ - public function tryEncoding($encoding = 'UTF8') - { - $link = @mysql_connect($server, $user, $pwd); - if (!mysql_query('SET NAMES \''.pSQL($encoding).'\'', $link)) - $ret = false; - else - $ret = true; - @mysql_close($link); - return $ret; - } - - /** - * @deprecated since 1.5.0 - */ public static function tryToConnect($server, $user, $pwd, $db, $newDbLink = true) { if (!$link = @mysql_connect($server, $user, $pwd, $newDbLink)) @@ -176,9 +146,6 @@ class MySQLCore extends Db return 0; } - /** - * @deprecated since 1.5.0 - */ static public function tryUTF8($server, $user, $pwd) { $link = @mysql_connect($server, $user, $pwd);