From 2f8bf9b4c5ee9bc183e17fd36b54be12a1bf75bb Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 12 Oct 2012 20:37:52 +0300 Subject: Set MySQL client compression to FALSE by default (problems reported with it), fix some typos, add encrypted database connections support and fix SQLSRV CharacterSet setting --- application/config/database.php | 10 +-- system/database/DB_driver.php | 2 +- system/database/drivers/mysql/mysql_driver.php | 26 ++++---- system/database/drivers/mysqli/mysqli_driver.php | 49 +++++---------- .../drivers/pdo/subdrivers/pdo_mysql_driver.php | 7 +++ .../drivers/pdo/subdrivers/pdo_sqlsrv_driver.php | 4 +- system/database/drivers/sqlsrv/sqlsrv_driver.php | 9 +-- system/helpers/date_helper.php | 2 +- user_guide_src/source/changelog.rst | 71 +++++++++++----------- user_guide_src/source/database/configuration.rst | 15 +++-- 10 files changed, 93 insertions(+), 102 deletions(-) diff --git a/application/config/database.php b/application/config/database.php index 4c5cad03f..32340263b 100644 --- a/application/config/database.php +++ b/application/config/database.php @@ -43,7 +43,7 @@ | ['password'] The password used to connect to the database | ['database'] The name of the database you want to connect to | ['dbdriver'] The database driver. e.g.: mysqli. - Currently supported: +| Currently supported: | cubrid, ibase, mssql, mysql, mysqli, oci8, | odbc, pdo, postgre, sqlite, sqlite3, sqlsrv | ['dbprefix'] You can add an optional prefix, which will be added @@ -63,7 +63,8 @@ | Sites using Latin-1 or UTF-8 database character set and collation are unaffected. | ['swap_pre'] A default table prefix that should be swapped with the dbprefix | ['autoinit'] Whether or not to automatically initialize the database. -| ['compress'] Whether or not to use client compression (only MySQL and MySQLi) +| ['encrypt'] Whether or not to use an encrypted connection. +| ['compress'] Whether or not to use client compression (MySQL only) | ['stricton'] TRUE/FALSE - forces 'Strict Mode' connections | - good for ensuring strict SQL while developing | ['failover'] array - A array with 0 or more data for connections if the main should fail. @@ -72,7 +73,7 @@ | make active. By default there is only one group (the 'default' group). | | The $query_builder variables lets you determine whether or not to load -| the query builder class +| the query builder class. */ $active_group = 'default'; @@ -94,7 +95,8 @@ $db['default'] = array( 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'autoinit' => TRUE, - 'compress' => TRUE, + 'encrypt' => FALSE, + 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array() ); diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index ea2a53eb2..fef388bbf 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -51,7 +51,7 @@ abstract class CI_DB_driver { public $char_set = 'utf8'; public $dbcollat = 'utf8_general_ci'; public $autoinit = TRUE; // Whether to automatically initialize the DB - public $compress = TRUE; + public $encrypt = FALSE; public $swap_pre = ''; public $port = ''; public $pconnect = FALSE; diff --git a/system/database/drivers/mysql/mysql_driver.php b/system/database/drivers/mysql/mysql_driver.php index 7262591ee..336db971d 100644 --- a/system/database/drivers/mysql/mysql_driver.php +++ b/system/database/drivers/mysql/mysql_driver.php @@ -41,6 +41,7 @@ class CI_DB_mysql_driver extends CI_DB { public $dbdriver = 'mysql'; + public $compress = FALSE; // The character used for escaping protected $_escape_char = '`'; @@ -75,18 +76,20 @@ class CI_DB_mysql_driver extends CI_DB { /** * Non-persistent database connection * + * @param bool * @return resource */ - public function db_connect() + public function db_connect($persistent = FALSE) { - if ($this->compress === TRUE) - { - return @mysql_connect($this->hostname, $this->username, $this->password, TRUE, MYSQL_CLIENT_COMPRESS); - } - else + $connect_func = ($persistent === TRUE) ? 'mysql_pconnect' : 'mysql_connect'; + $client_flags = ($this->compress === FALSE) ? 0 : MYSQL_CLIENT_COMPRESS; + + if ($this->encrypt === TRUE) { - return @mysql_connect($this->hostname, $this->username, $this->password, TRUE); + $client_flags = $client_flags | MYSQL_CLIENT_SSL; } + + return @$connect_func($this->hostname, $this->username, $this->password, TRUE, $client_flags); } // -------------------------------------------------------------------- @@ -98,14 +101,7 @@ class CI_DB_mysql_driver extends CI_DB { */ public function db_pconnect() { - if ($this->compress === TRUE) - { - return @mysql_pconnect($this->hostname, $this->username, $this->password, MYSQL_CLIENT_COMPRESS); - } - else - { - return @mysql_pconnect($this->hostname, $this->username, $this->password); - } + return $this->db_connect(TRUE); } // -------------------------------------------------------------------- diff --git a/system/database/drivers/mysqli/mysqli_driver.php b/system/database/drivers/mysqli/mysqli_driver.php index b5a1e26ed..f77176c16 100644 --- a/system/database/drivers/mysqli/mysqli_driver.php +++ b/system/database/drivers/mysqli/mysqli_driver.php @@ -41,6 +41,7 @@ class CI_DB_mysqli_driver extends CI_DB { public $dbdriver = 'mysqli'; + public $compress = FALSE; // The character used for escaping protected $_escape_char = '`'; @@ -57,24 +58,21 @@ class CI_DB_mysqli_driver extends CI_DB { /** * Non-persistent database connection * + * @param bool * @return object + * @todo SSL support */ - public function db_connect() + public function db_connect($persistent = FALSE) { - // Use MySQL client compression? - if ($this->compress === TRUE) - { - $port = empty($this->port) ? NULL : $this->port; - - $mysqli = new mysqli(); - @$mysqli->real_connect($this->hostname, $this->username, $this->password, $this->database, $port, NULL, MYSQLI_CLIENT_COMPRESS); - - return $mysqli; - } - - return empty($this->port) - ? @new mysqli($this->hostname, $this->username, $this->password, $this->database) - : @new mysqli($this->hostname, $this->username, $this->password, $this->database, $this->port); + // Persistent connection support was added in PHP 5.3.0 + $hostname = ($persistent === TRUE && is_php('5.3')) + ? 'p:'.$this->hostname : $this->hostname; + $port = empty($this->port) ? NULL : $this->port; + $client_flags = ($this->compress === TRUE) ? MYSQLI_CLIENT_COMPRESS : 0; + $mysqli = new mysqli(); + + return @$mysqli->real_connect($hostname, $this->username, $this->password, $this->database, $port, NULL, $client_flags) + ? $mysqli : FALSE; } // -------------------------------------------------------------------- @@ -86,26 +84,7 @@ class CI_DB_mysqli_driver extends CI_DB { */ public function db_pconnect() { - // Persistent connection support was added in PHP 5.3.0 - if ( ! is_php('5.3')) - { - return $this->db_connect(); - } - - // Use MySQL client compression? - if ($this->compress === TRUE) - { - $port = empty($this->port) ? NULL : $this->port; - - $mysqli = mysqli_init(); - $mysqli->real_connect('p:'.$this->hostname, $this->username, $this->password, $this->database, $port, NULL, MYSQLI_CLIENT_COMPRESS); - - return $mysqli; - } - - return empty($this->port) - ? @new mysqli('p:'.$this->hostname, $this->username, $this->password, $this->database) - : @new mysqli('p:'.$this->hostname, $this->username, $this->password, $this->database, $this->port); + return $this->db_connect(TRUE); } // -------------------------------------------------------------------- diff --git a/system/database/drivers/pdo/subdrivers/pdo_mysql_driver.php b/system/database/drivers/pdo/subdrivers/pdo_mysql_driver.php index 42446889a..a54311712 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_mysql_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_mysql_driver.php @@ -41,6 +41,7 @@ class CI_DB_pdo_mysql_driver extends CI_DB_pdo_driver { public $subdriver = 'mysql'; + public $compress = FALSE; protected $_escape_char = '`'; @@ -79,6 +80,7 @@ class CI_DB_pdo_mysql_driver extends CI_DB_pdo_driver { * * @param bool * @return object + * @todo SSL support */ public function db_connect($persistent = FALSE) { @@ -93,6 +95,11 @@ class CI_DB_pdo_mysql_driver extends CI_DB_pdo_driver { .(empty($this->dbcollat) ? '' : ' COLLATE '.$this->dbcollat); } + if ($this->compress === TRUE) + { + $this->options[PDO::MYSQL_ATTR_COMPRESS] = TRUE; + } + return parent::db_connect($persistent); } diff --git a/system/database/drivers/pdo/subdrivers/pdo_sqlsrv_driver.php b/system/database/drivers/pdo/subdrivers/pdo_sqlsrv_driver.php index fd1c4b214..3154fddb9 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_sqlsrv_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_sqlsrv_driver.php @@ -78,9 +78,9 @@ class CI_DB_pdo_sqlsrv_driver extends CI_DB_pdo_driver { $this->dsn .= ';ConnectionPooling='.$this->ConnectionPooling; } - if (isset($this->Encrypt)) + if ($this->encrypt === TRUE) { - $this->dsn .= ';Encrypt='.$this->Encrypt; + $this->dsn .= ';Encrypt=1'; } if (isset($this->TraceOn)) diff --git a/system/database/drivers/sqlsrv/sqlsrv_driver.php b/system/database/drivers/sqlsrv/sqlsrv_driver.php index 3ffbd4c18..be321ff11 100644 --- a/system/database/drivers/sqlsrv/sqlsrv_driver.php +++ b/system/database/drivers/sqlsrv/sqlsrv_driver.php @@ -57,15 +57,16 @@ class CI_DB_sqlsrv_driver extends CI_DB { */ public function db_connect($pooling = FALSE) { - // Check for a UTF-8 charset being passed as CI's default 'utf8'. - $character_set = (0 === strcasecmp('utf8', $this->char_set)) ? 'UTF-8' : $this->char_set; + $charset = in_array(strtolower($this->char_set), array('utf-8', 'utf8'), TRUE) + ? 'UTF-8' : SQLSRV_ENC_CHAR; $connection = array( 'UID' => empty($this->username) ? '' : $this->username, 'PWD' => empty($this->password) ? '' : $this->password, 'Database' => $this->database, - 'ConnectionPooling' => $pooling ? 1 : 0, - 'CharacterSet' => $character_set, + 'ConnectionPooling' => ($pooling === TRUE) ? 1 : 0, + 'CharacterSet' => $charset, + 'Encrypt' => ($this->encrypt === TRUE) ? 1 : 0, 'ReturnDatesAsStrings' => 1 ); diff --git a/system/helpers/date_helper.php b/system/helpers/date_helper.php index 955d74542..51b2b76db 100644 --- a/system/helpers/date_helper.php +++ b/system/helpers/date_helper.php @@ -669,7 +669,7 @@ if ( ! function_exists('date_range')) * @param int unix_start UNIX timestamp of period start date * @param int unix_end|days UNIX timestamp of period end date * or interval in days. - * @param mixed is_unix Specifies wether the second parameter + * @param mixed is_unix Specifies whether the second parameter * is a UNIX timestamp or a day interval * - TRUE or 'unix' for a timestamp * - FALSE or 'days' for an interval diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 747791cd5..145853a52 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -87,65 +87,66 @@ Release Date: Not Released - :doc:`Query Builder ` changes include: - Renamed the Active Record class to Query Builder to remove confusion with the Active Record design pattern. - - Added the ability to insert objects with insert_batch(). - - Added new methods that return the SQL string of queries without executing them: get_compiled_select(), get_compiled_insert(), get_compiled_update(), get_compiled_delete(). - - Added an optional parameter that allows to disable escaping (useful for custom fields) for methods join(), order_by(), where_in(), or_where_in(), where_not_in(), or_where_not_in(). - - Added support for join() with multiple conditions. - - Added support for USING in join(). - - Changed limit() to ignore NULL values instead of always casting to integer. - - Changed offset() to ignore empty values instead of always casting to integer. + - Added the ability to insert objects with ``insert_batch()``. + - Added new methods that return the SQL string of queries without executing them: ``get_compiled_select()``, ``get_compiled_insert()``, ``get_compiled_update()``, ``get_compiled_delete()``. + - Added an optional parameter that allows to disable escaping (useful for custom fields) for methods ``join()``, ``order_by()``, ``where_in()``, ``or_where_in()``, ``where_not_in()``, ``or_where_not_in()``. + - Added support for ``join()`` with multiple conditions. + - Added support for *USING* in ``join()``. + - Changed ``limit()`` to ignore NULL values instead of always casting to integer. + - Changed ``offset()`` to ignore empty values instead of always casting to integer. - Improved support for the MySQLi driver, including: - OOP style of the PHP extension is now used, instead of the procedural aliases. - Server version checking is now done via ``mysqli::$server_info`` instead of running an SQL query. - Added persistent connections support for PHP >= 5.3. - - Added support for backup() in :doc:`Database Utilities `. - - Added 'dsn' configuration setting for drivers that support DSN strings (PDO, PostgreSQL, Oracle, ODBC, CUBRID). + - Added support for ``backup()`` in :doc:`Database Utilities `. + - Added *dsn* configuration setting for drivers that support DSN strings (PDO, PostgreSQL, Oracle, ODBC, CUBRID). - Improved PDO database support. - - Added Interbase/Firebird database support via the 'ibase' driver. - - Added an optional database name parameter to db_select(). - - Replaced the _error_message() and _error_number() methods with error(), that returns an array containing the last database error code and message. - - Improved version() implementation so that drivers that have a native function to get the version number don't have to be defined in the core DB_driver class. + - Added Interbase/Firebird database support via the *ibase* driver. + - Added an optional database name parameter to ``db_select()``. + - Replaced the ``_error_message()`` and ``_error_number()`` methods with ``error()``, which returns an array containing the last database error code and message. + - Improved ``version()`` implementation so that drivers that have a native function to get the version number don't have to be defined in the core ``DB_driver`` class. - Improved support of the PostgreSQL driver, including: - ``pg_version()`` is now used to get the database version number, when possible. - Added ``db_set_charset()`` support. - Added support for ``optimize_table()`` in :doc:`Database Utilities ` (rebuilds table indexes). - Added boolean data type support in ``escape()``. - Added ``update_batch()`` support. - - Removed ``limit()`` and ``order_by()`` support for UPDATE and DELETE queries as PostgreSQL does not support those features. + - Removed ``limit()`` and ``order_by()`` support for *UPDATE* and *DELETE* queries as PostgreSQL does not support those features. - Added a work-around for dead persistent connections to be re-created after a database restart. - - Added a constructor to the DB_result class and moved all driver-specific properties and logic out of the base DB_driver class to allow better abstraction. - - Removed protect_identifiers() and renamed internal method _protect_identifiers() to it instead - it was just an alias. - - Renamed internal method _escape_identifiers() to escape_identifiers(). - - Updated escape_identifiers() to accept an array of fields as well as strings. + - Added a constructor to the ``DB_result`` class and moved all driver-specific properties and logic out of the base ``DB_driver`` class to allow better abstraction. + - Removed ``protect_identifiers()`` and renamed internal method ``_protect_identifiers()`` to it instead - it was just an alias. + - Renamed internal method ``_escape_identifiers()`` to ``escape_identifiers()``. + - Updated ``escape_identifiers()`` to accept an array of fields as well as strings. - MySQL and MySQLi drivers now require at least MySQL version 5.1. - - db_set_charset() now only requires one parameter (collation was only needed due to legacy support for MySQL versions prior to 5.1). + - ``db_set_charset()`` now only requires one parameter (collation was only needed due to legacy support for MySQL versions prior to 5.1). - Added support for SQLite3 database driver. - Improved support of the CUBRID driver, including: - Added DSN string support. - Added persistent connections support. - - Improved list_databases() in :doc:`Database Utility ` (until now only the currently used database was returned). + - Improved ``list_databases()`` in :doc:`Database Utility ` (until now only the currently used database was returned). - Improved support of the MSSQL and SQLSRV drivers, including: - Added random ordering support. - - Added support for optimize_table() in :doc:`Database Utility `. - - Added escaping with QUOTE_IDENTIFIER setting detection. + - Added support for ``optimize_table()`` in :doc:`Database Utility `. + - Added escaping with *QUOTE_IDENTIFIER* setting detection. - Added port handling support for UNIX-based systems (MSSQL driver). - - Added OFFSET support for SQL Server 2005 and above. + - Added *OFFSET* support for SQL Server 2005 and above. - Improved support of the Oracle (OCI8) driver, including: - Added DSN string support (Easy Connect and TNS). - - Added support for drop_table() in :doc:`Database Forge `. - - Added support for list_databases() in :doc:`Database Utilities `. + - Added support for ``drop_table()`` in :doc:`Database Forge `. + - Added support for ``list_databases()`` in :doc:`Database Utilities `. - Generally improved for speed and cleaned up all of its components. - - num_rows() is now only called explicitly by the developer and no longer re-executes statements. + - ``num_rows()`` is now only called explicitly by the developer and no longer re-executes statements. - Improved support of the SQLite driver, including: - - Added support for replace() in :doc:`Query Builder `. - - Added support for drop_table() in :doc:`Database Forge `. - - Added ODBC support for create_database(), drop_database() and drop_table() in :doc:`Database Forge `. - - Added PDO support for create_database(), drop_database and drop_table() in :doc:`Database Forge `. - - Added unbuffered_row() method for getting a row without prefetching whole result (consume less memory). + - Added support for ``replace()`` in :doc:`Query Builder `. + - Added support for ``drop_table()`` in :doc:`Database Forge `. + - Added ODBC support for ``create_database()``, ``drop_database()`` and ``drop_table()`` in :doc:`Database Forge `. + - Added PDO support for ``create_database()``, ``drop_database()`` and ``drop_table()`` in :doc:`Database Forge `. + - Added ``unbuffered_row()`` method for getting a row without prefetching whole result (consume less memory). - Added PDO support for ``list_fields()`` in :doc:`Database Results `. - - Added capability for packages to hold database.php config files + - Added capability for packages to hold *database.php* config files - Added subdrivers support (currently only used by PDO). - - Added client compression support for MySQL and MySQLi. + - Added MySQL client compression support. + - Added encrypted connections support (for *mysql*, *sqlsrv* and PDO with *sqlsrv*). - Removed :doc:`Loader Class ` from Database error tracing to better find the likely culprit. - Libraries @@ -337,8 +338,8 @@ Bug fixes for 3.0 - Fixed a bug (#520) - :doc:`Date Helper ` function nice_date() failed when the optional second parameter is not passed. - Fixed a bug (#167) - ``$config['permitted_uri_chars']`` didn't affect URL-encoded characters. - Fixed a bug (#318) - :doc:`Profiling ` setting *query_toggle_count* was not settable as described in the manual. -- Fixed a bug (#938) - :doc:`Config Library ` method site_url() added a question mark to the URL string when query strings are enabled even if it already existed. -- Fixed a bug (#999) - :doc:`Config Library ` method site_url() always appended ``$config['url_suffix']`` to the end of the URL string, regardless of wether a query string exists in it. +- Fixed a bug (#938) - :doc:`Config Library ` method ``site_url()`` added a question mark to the URL string when query strings are enabled even if it already existed. +- Fixed a bug (#999) - :doc:`Config Library ` method ``site_url()`` always appended ``$config['url_suffix']`` to the end of the URL string, regardless of whether a query string exists in it. - Fixed a bug where :doc:`URL Helper ` function anchor_popup() ignored the attributes argument if it is not an array. - Fixed a bug (#1328) - :doc:`Form Validation Library ` didn't properly check the type of the form fields before processing them. - Fixed a bug (#79) - :doc:`Form Validation Library ` didn't properly validate array fields that use associative keys or have custom indexes. diff --git a/user_guide_src/source/database/configuration.rst b/user_guide_src/source/database/configuration.rst index 636b5b5b2..668496324 100644 --- a/user_guide_src/source/database/configuration.rst +++ b/user_guide_src/source/database/configuration.rst @@ -28,7 +28,8 @@ prototype:: 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'autoinit' => TRUE, - 'compress' => TRUE, + 'encrypt' => FALSE, + 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array() ); @@ -70,7 +71,8 @@ These failovers can be specified by setting the failover for a connection like t 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'autoinit' => TRUE, - 'compress' => TRUE, + 'encrypt' => FALSE, + 'compress' => FALSE, 'stricton' => FALSE ), array( @@ -88,7 +90,8 @@ These failovers can be specified by setting the failover for a connection like t 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'autoinit' => TRUE, - 'compress' => TRUE, + 'encrypt' => FALSE, + 'compress' => FALSE, 'stricton' => FALSE ) ); @@ -118,7 +121,8 @@ example, to set up a "test" environment you would do this:: 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'autoinit' => TRUE, - 'compress' => TRUE, + 'compress' => FALSE, + 'encrypt' => FALSE, 'stricton' => FALSE, 'failover' => array() ); @@ -178,7 +182,8 @@ Explanation of Values: customizable by the end user. **autoinit** Whether or not to automatically connect to the database when the library loads. If set to false, the connection will take place prior to executing the first query. -**compress** Whether or not to use client compression for MySQL or MySQLi. +**encrypt** Whether or not to use an encrypted connection. +**compress** Whether or not to use client compression (MySQL only). **stricton** TRUE/FALSE (boolean) - Whether to force "Strict Mode" connections, good for ensuring strict SQL while developing an application. **port** The database port number. To use this value you have to add a line to the database config array. -- cgit v1.2.3-24-g4f1b From 98ebf4351f8aad58504cd7318ddd94faf0dec482 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 12 Oct 2012 20:44:03 +0300 Subject: Fix mysql's db_connect() --- system/database/drivers/mysql/mysql_driver.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/system/database/drivers/mysql/mysql_driver.php b/system/database/drivers/mysql/mysql_driver.php index 336db971d..99bf55942 100644 --- a/system/database/drivers/mysql/mysql_driver.php +++ b/system/database/drivers/mysql/mysql_driver.php @@ -81,7 +81,6 @@ class CI_DB_mysql_driver extends CI_DB { */ public function db_connect($persistent = FALSE) { - $connect_func = ($persistent === TRUE) ? 'mysql_pconnect' : 'mysql_connect'; $client_flags = ($this->compress === FALSE) ? 0 : MYSQL_CLIENT_COMPRESS; if ($this->encrypt === TRUE) @@ -89,7 +88,9 @@ class CI_DB_mysql_driver extends CI_DB { $client_flags = $client_flags | MYSQL_CLIENT_SSL; } - return @$connect_func($this->hostname, $this->username, $this->password, TRUE, $client_flags); + return ($persistent === TRUE) + ? @mysql_pconnect($this->hostname, $this->username, $this->password, $client_flags) + : @mysql_connect($this->hostname, $this->username, $this->password, TRUE, $client_flags); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b