From 9d7ec6da614656f64b6773c3f8dfc00ad7259d18 Mon Sep 17 00:00:00 2001 From: rMalie Date: Thu, 5 Jan 2012 14:28:10 +0000 Subject: [PATCH] // Check if at least one table with same prefix already exists in new installer git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@12194 b9a71923-0436-4b27-9f14-aed3839534dd --- classes/db/Db.php | 27 +++++++++++++++++++++++++++ classes/db/DbMySQLi.php | 20 ++++++++++++++++++++ classes/db/DbPDO.php | 23 +++++++++++++++++++++-- classes/db/MySQL.php | 21 +++++++++++++++++++++ install-new/models/database.php | 4 ++++ 5 files changed, 93 insertions(+), 2 deletions(-) diff --git a/classes/db/Db.php b/classes/db/Db.php index e7ab279d4..4925acf5e 100644 --- a/classes/db/Db.php +++ b/classes/db/Db.php @@ -601,20 +601,47 @@ abstract class DbCore return call_user_func_array(array(Db::getClass(), 'tryUTF8'), array($server, $user, $pwd)); } + /** + * Try a connection to the database and check if at least one table with same prefix exists + * + * @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 $prefix Tables prefix + * @return bool + */ + public static function hasTableWithSamePrefix($server, $user, $pwd, $db, $prefix) + { + return call_user_func_array(array(Db::getClass(), 'hasTableWithSamePrefix'), array($server, $user, $pwd, $db, $prefix)); + } + + /** + * @deprecated 1.5.0 + */ public static function s($sql, $use_cache = true) { + Tools::displayAsDeprecated(); return Db::getInstance()->executeS($sql, true, $use_cache); } + /** + * @deprecated 1.5.0 + */ public static function ps($sql, $use_cache = 1) { + Tools::displayAsDeprecated(); $ret = Db::s($sql, $use_cache); p($ret); return $ret; } + /** + * @deprecated 1.5.0 + */ public static function ds($sql, $use_cache = 1) { + Tools::displayAsDeprecated(); Db::s($sql, $use_cache); die(); } diff --git a/classes/db/DbMySQLi.php b/classes/db/DbMySQLi.php index b4a7c145d..aed30ef9a 100644 --- a/classes/db/DbMySQLi.php +++ b/classes/db/DbMySQLi.php @@ -138,6 +138,23 @@ class DbMySQLiCore extends Db return $this->link->query('USE '.pSQL($db_name)); } + /** + * @see Db::hasTableWithSamePrefix() + */ + public static function hasTableWithSamePrefix($server, $user, $pwd, $db, $prefix) + { + $link = @new mysqli($server, $user, $pwd, $db); + if (mysqli_connect_error()) + return false; + + $sql = 'SHOW TABLES LIKE \''.$prefix.'%\''; + $result = $link->query($sql); + return (bool)$result->fetch_assoc(); + } + + /** + * @see Db::checkConnection() + */ static public function tryToConnect($server, $user, $pwd, $db, $newDbLink = true, $engine = null) { $link = @new mysqli($server, $user, $pwd, $db); @@ -158,6 +175,9 @@ class DbMySQLiCore extends Db return 0; } + /** + * @see Db::checkEncoding() + */ static public function tryUTF8($server, $user, $pwd) { $link = @new mysqli($server, $user, $pwd, $db); diff --git a/classes/db/DbPDO.php b/classes/db/DbPDO.php index 4444fc060..640ff5894 100644 --- a/classes/db/DbPDO.php +++ b/classes/db/DbPDO.php @@ -149,7 +149,26 @@ class DbPDOCore extends Db } /** - * @see DbCore::tryToConnect() + * @see Db::hasTableWithSamePrefix() + */ + public static function hasTableWithSamePrefix($server, $user, $pwd, $db, $prefix) + { + try + { + $link = @new PDO('mysql:dbname='.$db.';host='.$server, $user, $pwd); + } + catch (PDOException $e) + { + return false; + } + + $sql = 'SHOW TABLES LIKE \''.$prefix.'%\''; + $result = $link->query($sql); + return (bool)$result->fetch(); + } + + /** + * @see Db::checkConnection() */ static public function tryToConnect($server, $user, $pwd, $db, $newDbLink = true, $engine = null) { @@ -177,7 +196,7 @@ class DbPDOCore extends Db } /** - * @see DbCore::tryUTF8() + * @see Db::checkEncoding() */ static public function tryUTF8($server, $user, $pwd) { diff --git a/classes/db/MySQL.php b/classes/db/MySQL.php index 2dfc74998..708eee3c1 100644 --- a/classes/db/MySQL.php +++ b/classes/db/MySQL.php @@ -136,6 +136,24 @@ class MySQLCore extends Db return mysql_select_db($db_name, $this->link); } + /** + * @see Db::hasTableWithSamePrefix() + */ + public static function hasTableWithSamePrefix($server, $user, $pwd, $db, $prefix) + { + if (!$link = @mysql_connect($server, $user, $pwd, true)) + return false; + if (!@mysql_select_db($db, $link)) + return false; + + $sql = 'SHOW TABLES LIKE \''.$prefix.'%\''; + $result = mysql_query($sql); + return (bool)@mysql_fetch_assoc($result); + } + + /** + * @see Db::checkConnection() + */ public static function tryToConnect($server, $user, $pwd, $db, $newDbLink = true, $engine = null) { if (!$link = @mysql_connect($server, $user, $pwd, $newDbLink)) @@ -157,6 +175,9 @@ class MySQLCore extends Db return 0; } + /** + * @see Db::checkEncoding() + */ static public function tryUTF8($server, $user, $pwd) { $link = @mysql_connect($server, $user, $pwd); diff --git a/install-new/models/database.php b/install-new/models/database.php index 71cb4f621..24a8c57f9 100644 --- a/install-new/models/database.php +++ b/install-new/models/database.php @@ -66,6 +66,10 @@ class InstallModelDatabase extends InstallAbstractModel case 0: if (!Db::checkEncoding($server, $login, $password)) $errors[] = $this->language->l('Cannot convert database data to utf-8'); + + // Check if a table with same prefix already exists + if (Db::hasTableWithSamePrefix($server, $login, $password, $database, $prefix)) + $errors[] = $this->language->l('At least one table with same prefix was already found, please change your prefix or drop your database'); break; case 1: