From 85bc9fc53e4c3e46b2f4e1b1eac7e2828d4869e6 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 14 Dec 2015 17:56:14 +0200 Subject: Change DB charset handling Close #4311 --- system/database/DB_driver.php | 32 ++-------------------- system/database/drivers/mssql/mssql_driver.php | 14 +--------- system/database/drivers/mysql/mysql_driver.php | 31 ++++++++++----------- system/database/drivers/mysqli/mysqli_driver.php | 20 +++++--------- system/database/drivers/postgre/postgre_driver.php | 20 +++++--------- 5 files changed, 32 insertions(+), 85 deletions(-) (limited to 'system/database') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 885a814be..af6b9f399 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -380,7 +380,7 @@ abstract class CI_DB_driver { /** * Initialize Database Settings * - * @return bool + * @return void * @throws RuntimeException In case of failure */ public function initialize() @@ -393,7 +393,7 @@ abstract class CI_DB_driver { */ if ($this->conn_id) { - return TRUE; + return; } // ---------------------------------------------------------------- @@ -433,9 +433,6 @@ abstract class CI_DB_driver { throw new RuntimeException('Unable to connect to the database.'); } } - - // Now we set the character set and that's all - return $this->db_set_charset($this->char_set); } // -------------------------------------------------------------------- @@ -510,31 +507,6 @@ abstract class CI_DB_driver { // -------------------------------------------------------------------- - /** - * Set client character set - * - * @param string - * @return bool - */ - public function db_set_charset($charset) - { - if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset)) - { - log_message('error', 'Unable to set database connection charset: '.$charset); - - if ($this->db_debug) - { - $this->display_error('db_unable_to_set_charset', $charset); - } - - return FALSE; - } - - return TRUE; - } - - // -------------------------------------------------------------------- - /** * The name of the platform in use (mysql, mssql, etc...) * diff --git a/system/database/drivers/mssql/mssql_driver.php b/system/database/drivers/mssql/mssql_driver.php index b9e310a3a..bf18babeb 100644 --- a/system/database/drivers/mssql/mssql_driver.php +++ b/system/database/drivers/mssql/mssql_driver.php @@ -108,6 +108,7 @@ class CI_DB_mssql_driver extends CI_DB { */ public function db_connect($persistent = FALSE) { + ini_set('mssql.charset', $this->char_set); $this->conn_id = ($persistent) ? mssql_pconnect($this->hostname, $this->username, $this->password) : mssql_connect($this->hostname, $this->username, $this->password); @@ -247,19 +248,6 @@ class CI_DB_mssql_driver extends CI_DB { // -------------------------------------------------------------------- - /** - * Set client character set - * - * @param string $charset - * @return bool - */ - protected function _db_set_charset($charset) - { - return (ini_set('mssql.charset', $charset) !== FALSE); - } - - // -------------------------------------------------------------------- - /** * Version number query string * diff --git a/system/database/drivers/mysql/mysql_driver.php b/system/database/drivers/mysql/mysql_driver.php index 9c630d0d6..76f50368f 100644 --- a/system/database/drivers/mysql/mysql_driver.php +++ b/system/database/drivers/mysql/mysql_driver.php @@ -147,12 +147,24 @@ class CI_DB_mysql_driver extends CI_DB { : FALSE; } - if ($this->stricton && is_resource($this->conn_id)) + if (is_resource($this->conn_id)) { - $this->simple_query('SET SESSION sql_mode="STRICT_ALL_TABLES"'); + if ( ! mysql_set_charset($this->char_set, $this->conn_id)) + { + log_message('error', "Database: Unable to set the configured connection charset ('{$this->char_set}')."); + $this->close(); + return ($this->db->debug) ? $this->display_error('db_unable_to_set_charset', $charset) : FALSE; + } + + if ($this->stricton) + { + $this->simple_query('SET SESSION sql_mode="STRICT_ALL_TABLES"'); + } + + return $this->conn_id; } - return $this->conn_id; + return FALSE; } // -------------------------------------------------------------------- @@ -199,19 +211,6 @@ class CI_DB_mysql_driver extends CI_DB { // -------------------------------------------------------------------- - /** - * Set client character set - * - * @param string $charset - * @return bool - */ - protected function _db_set_charset($charset) - { - return mysql_set_charset($charset, $this->conn_id); - } - - // -------------------------------------------------------------------- - /** * Database version number * diff --git a/system/database/drivers/mysqli/mysqli_driver.php b/system/database/drivers/mysqli/mysqli_driver.php index 827470078..f9a20ea2f 100644 --- a/system/database/drivers/mysqli/mysqli_driver.php +++ b/system/database/drivers/mysqli/mysqli_driver.php @@ -173,6 +173,13 @@ class CI_DB_mysqli_driver extends CI_DB { return ($this->db->db_debug) ? $this->db->display_error($message, '', TRUE) : FALSE; } + if ( ! $mysqli->set_charset($this->char_set)) + { + log_message('error', "Database: Unable to set the configured connection charset ('{$this->char_set}')."); + $mysqli->close(); + return ($this->db->db_debug) ? $this->display_error('db_unable_to_set_charset', $charset) : FALSE; + } + return $mysqli; } @@ -223,19 +230,6 @@ class CI_DB_mysqli_driver extends CI_DB { // -------------------------------------------------------------------- - /** - * Set client character set - * - * @param string $charset - * @return bool - */ - protected function _db_set_charset($charset) - { - return $this->conn_id->set_charset($charset); - } - - // -------------------------------------------------------------------- - /** * Database version number * diff --git a/system/database/drivers/postgre/postgre_driver.php b/system/database/drivers/postgre/postgre_driver.php index b1df326f7..e4db12d69 100644 --- a/system/database/drivers/postgre/postgre_driver.php +++ b/system/database/drivers/postgre/postgre_driver.php @@ -163,6 +163,13 @@ class CI_DB_postgre_driver extends CI_DB { return FALSE; } + if (pg_set_client_encoding($this->conn_id, $charset) !== 0) + { + log_message('error', "Database: Unable to set the configured connection charset ('{$this->char_set}')."); + pg_close($this->conn_id); + return ($this->db->db_debug) ? $this->display_error('db_unable_to_set_charset', $charset) : FALSE; + } + empty($this->schema) OR $this->simple_query('SET search_path TO '.$this->schema.',public'); } @@ -189,19 +196,6 @@ class CI_DB_postgre_driver extends CI_DB { // -------------------------------------------------------------------- - /** - * Set client character set - * - * @param string $charset - * @return bool - */ - protected function _db_set_charset($charset) - { - return (pg_set_client_encoding($this->conn_id, $charset) === 0); - } - - // -------------------------------------------------------------------- - /** * Database version number * -- cgit v1.2.3-24-g4f1b