From d84373e4d385f50e6425be807c85a72f714e458f Mon Sep 17 00:00:00 2001 From: rMalie Date: Mon, 5 Sep 2011 13:55:11 +0000 Subject: [PATCH] [+] Add native MySQLi support --- cache/class_index.php | 2 + classes/Db.php | 6 +- classes/DbMySQLi.php | 171 ++++++++++++++++++++++++++++++++++ override/classes/DbMySQLi.php | 7 ++ 4 files changed, 183 insertions(+), 3 deletions(-) create mode 100644 classes/DbMySQLi.php create mode 100644 override/classes/DbMySQLi.php diff --git a/cache/class_index.php b/cache/class_index.php index 4ef7adb63..0dc716854 100644 --- a/cache/class_index.php +++ b/cache/class_index.php @@ -76,6 +76,8 @@ 'DateRange' => 'override/classes/DateRange.php', 'DbCore' => 'classes/Db.php', 'Db' => 'override/classes/Db.php', + 'DbMySQLiCore' => 'classes/DbMySQLi.php', + 'DbMySQLi' => 'override/classes/DbMySQLi.php', 'DeliveryCore' => 'classes/Delivery.php', 'Delivery' => 'override/classes/Delivery.php', 'DiscountCore' => 'classes/Discount.php', diff --git a/classes/Db.php b/classes/Db.php index 7ca9b285e..a3fe13e8e 100644 --- a/classes/Db.php +++ b/classes/Db.php @@ -170,9 +170,9 @@ abstract class DbCore if (!isset(self::$_instance[$idServer])) { - $class = _DB_TYPE_; - if (!class_exists($class)) - $class = 'MySQL'; + $class = 'MySQL'; + if (class_exists('mysqli', false)) + $class = 'DbMySQLi'; self::$_instance[$idServer] = new $class(self::$_servers[$idServer]['server'], self::$_servers[$idServer]['user'], self::$_servers[$idServer]['password'], self::$_servers[$idServer]['database']); } diff --git a/classes/DbMySQLi.php b/classes/DbMySQLi.php new file mode 100644 index 000000000..c0f58bffa --- /dev/null +++ b/classes/DbMySQLi.php @@ -0,0 +1,171 @@ + +* @copyright 2007-2011 PrestaShop SA +* @version Release: $Revision$ +* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*/ + +class DbMySQLiCore extends Db +{ + /** + * @see DbCore::connect() + */ + public function connect() + { + $this->_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()) + die(Tools::displayError('Link to database cannot be established : '.mysqli_connect_error())); + + // UTF-8 support + if (!$this->_link->query('SET NAMES \'utf8\'')) + die(Tools::displayError('PrestaShop Fatal error: no utf-8 support. Please check your server configuration.')); + + return $this->_link; + } + + /** + * @see DbCore::disconnect() + */ + public function disconnect() + { + $this->_link->close(); + } + + /** + * @see DbCore::_query() + */ + protected function _query($sql) + { + return $this->_link->query($sql); + } + + /** + * @see DbCore::nextRow() + */ + public function nextRow($result = false) + { + if (!$result) + $result = $this->_result; + return $result->fetch_assoc(); + } + + /** + * @see DbCore::_numRows() + */ + protected function _numRows($result) + { + return $result->num_rows; + } + + /** + * @see DbCore::Insert_ID() + */ + public function Insert_ID() + { + return $this->_link->insert_id; + } + + /** + * @see DbCore::Affected_Rows() + */ + public function Affected_Rows() + { + return $this->_link->affected_rows; + } + + /** + * @see DbCore::getMsgError() + */ + public function getMsgError($query = false) + { + return $this->_link->error; + } + + /** + * @see DbCore::getNumberError() + */ + public function getNumberError() + { + return $this->_link->errno; + } + + /** + * @see DbCore::getVersion() + */ + public function getVersion() + { + return $this->getValue('SELECT VERSION()'); + } + + /** + * @see DbCore::_escape() + */ + public function _escape($str) + { + return $this->_link->real_escape_string($str); + } + + /** + * @see DbCore::set_db() + */ + public function set_db($db_name) + { + return $this->_link->query('USE '.pSQL($db_name)); + } + + /** + * tryToConnect return 0 if the connection succeed and the database can be selected. + * @since 1.4.4.0, the parameter $newDbLink (default true) has been added. + * + * @param string $server mysql server name + * @param string $user mysql user + * @param string $pwd mysql user password + * @param string $db mysql database name + * @param boolean $newDbLink Param not used in this class + * @return integer + */ + static public function tryToConnect($server, $user, $pwd, $db, $newDbLink = true) + { + $link = new mysqli($server, $user, $password, $db); + + // 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; + + // UTF-8 support + if (!$this->_link->query('SET NAMES \'utf8\'')) + die(Tools::displayError('PrestaShop Fatal error: no utf-8 support. Please check your server configuration.')); + } + + static public function tryUTF8($server, $user, $pwd) + { + $link = new mysqli($server, $user, $password, $db); + $ret = $link->query("SET NAMES 'UTF8'"); + $link->close(); + return $ret; + } +} diff --git a/override/classes/DbMySQLi.php b/override/classes/DbMySQLi.php new file mode 100644 index 000000000..33e45aa09 --- /dev/null +++ b/override/classes/DbMySQLi.php @@ -0,0 +1,7 @@ +