Merge pull request #1732 from IT-Can/mysql-compression

MySQL & MySQLi client compression
diff --git a/application/config/database.php b/application/config/database.php
index bb0d87b..4c5cad0 100644
--- a/application/config/database.php
+++ b/application/config/database.php
@@ -63,6 +63,7 @@
 | 				 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)
 |	['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.
@@ -93,6 +94,7 @@
 	'dbcollat' => 'utf8_general_ci',
 	'swap_pre' => '',
 	'autoinit' => TRUE,
+	'compress' => TRUE,
 	'stricton' => FALSE,
 	'failover' => array()
 );
diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php
index d63a1d9..4296815 100644
--- a/system/database/DB_driver.php
+++ b/system/database/DB_driver.php
@@ -51,6 +51,7 @@
 	public $char_set		= 'utf8';
 	public $dbcollat		= 'utf8_general_ci';
 	public $autoinit		= TRUE; // Whether to automatically initialize the DB
+	public $compress		= TRUE;
 	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 29db904..3547301 100644
--- a/system/database/drivers/mysql/mysql_driver.php
+++ b/system/database/drivers/mysql/mysql_driver.php
@@ -83,7 +83,14 @@
 	 */
 	public function db_connect()
 	{
-		return @mysql_connect($this->hostname, $this->username, $this->password, TRUE);
+		if ($this->compress === TRUE)
+		{
+			return @mysql_connect($this->hostname, $this->username, $this->password, TRUE, MYSQL_CLIENT_COMPRESS);
+		}
+		else
+		{
+			return @mysql_connect($this->hostname, $this->username, $this->password, TRUE);
+		}
 	}
 
 	// --------------------------------------------------------------------
@@ -95,7 +102,14 @@
 	 */
 	public function db_pconnect()
 	{
-		return @mysql_pconnect($this->hostname, $this->username, $this->password);
+		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);
+		}
 	}
 
 	// --------------------------------------------------------------------
diff --git a/system/database/drivers/mysqli/mysqli_driver.php b/system/database/drivers/mysqli/mysqli_driver.php
index be61aab..9558dfd 100644
--- a/system/database/drivers/mysqli/mysqli_driver.php
+++ b/system/database/drivers/mysqli/mysqli_driver.php
@@ -65,6 +65,17 @@
 	 */
 	public function db_connect()
 	{
+		// Use MySQL client compression?
+		if ($this->compress === TRUE)
+		{
+			$port = empty($this->port) ? NULL : $this->port;
+
+			$mysqli = mysqli_init();
+			$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);
@@ -85,6 +96,17 @@
 			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);
diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst
index d026ca3..eb4f1e7 100644
--- a/user_guide_src/source/changelog.rst
+++ b/user_guide_src/source/changelog.rst
@@ -142,6 +142,7 @@
    -  Added PDO support for ``list_fields()`` in :doc:`Database Results <database/results>`.
    -  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.
 
 -  Libraries
 
diff --git a/user_guide_src/source/database/configuration.rst b/user_guide_src/source/database/configuration.rst
index c17de60..636b5b5 100644
--- a/user_guide_src/source/database/configuration.rst
+++ b/user_guide_src/source/database/configuration.rst
@@ -28,6 +28,7 @@
 		'dbcollat' => 'utf8_general_ci',
 		'swap_pre' => '',
 		'autoinit' => TRUE,
+		'compress' => TRUE,
 		'stricton' => FALSE,
 		'failover' => array()
 	);
@@ -69,6 +70,7 @@
 				'dbcollat' => 'utf8_general_ci',
 				'swap_pre' => '',
 				'autoinit' => TRUE,
+				'compress' => TRUE,
 				'stricton' => FALSE
 			),
 			array(
@@ -86,6 +88,7 @@
 				'dbcollat' => 'utf8_general_ci',
 				'swap_pre' => '',
 				'autoinit' => TRUE,
+				'compress' => TRUE,
 				'stricton' => FALSE
 			)
 		);
@@ -115,6 +118,7 @@
 		'dbcollat' => 'utf8_general_ci',
 		'swap_pre' => '',
 		'autoinit' => TRUE,
+		'compress' => TRUE,
 		'stricton' => FALSE,
 		'failover' => array()
 	);
@@ -174,11 +178,12 @@
 			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.
 **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.
 			::
-			
+
 				$db['default']['port'] = 5432;
 ======================  ==================================================================================================