DB count_all() not returns an integer always
Added some syntactical improvements within DB (braces)
Fixed a bug when doing 'random' on order_by() (#5706).
Fixed a bug where adding a primary key through Forge could fail (#5731).
Fixed a bug when using DB cache on multiple databases (#5737).
diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php
index b8dce6d..c757e6a 100644
--- a/system/database/DB_active_rec.php
+++ b/system/database/DB_active_rec.php
@@ -913,7 +913,7 @@
 			
 			$orderby = implode(', ', $temp);			
 		}
-		else
+		else if ($direction != $this->_random_keyword)
 		{
 			$orderby = $this->_protect_identifiers($orderby);
 		}
diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php
index fde0a43..dac4c8b 100644
--- a/system/database/DB_driver.php
+++ b/system/database/DB_driver.php
@@ -613,7 +613,7 @@
 	 */	
 	function is_write_type($sql)
 	{
-		if ( ! preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql))
+		if ( ! preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql))
 		{
 			return FALSE;
 		}
@@ -1086,12 +1086,15 @@
 		{
 			return TRUE;
 		}
-	
-		if ( ! @include(BASEPATH.'database/DB_cache'.EXT))
+
+		if ( ! class_exists('CI_DB_Cache'))
 		{
-			return $this->cache_off();
+			if ( ! @include(BASEPATH.'database/DB_cache'.EXT))
+			{
+				return $this->cache_off();
+			}
 		}
-		
+
 		$this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
 		return TRUE;
 	}
@@ -1196,7 +1199,19 @@
 		{
 			$protect_identifiers = $this->_protect_identifiers;
 		}
-		
+
+		if (is_array($item))
+		{
+			$escaped_array = array();
+
+			foreach($item as $k => $v)
+			{
+				$escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v);
+			}
+
+			return $escaped_array;
+		}
+
 		// Convert tabs or multiple spaces into single spaces
 		$item = preg_replace('/[\t| ]+/', ' ', $item);
 	
diff --git a/system/database/drivers/mssql/mssql_driver.php b/system/database/drivers/mssql/mssql_driver.php
index 78f81da..addbd6b 100644
--- a/system/database/drivers/mssql/mssql_driver.php
+++ b/system/database/drivers/mssql/mssql_driver.php
@@ -314,15 +314,19 @@
 	function count_all($table = '')
 	{
 		if ($table == '')
-			return '0';
-	
-		$query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
+		{
+			return 0;
+		}
+
+		$query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
 
 		if ($query->num_rows() == 0)
-			return '0';
+		{
+			return 0;
+		}
 
 		$row = $query->row();
-		return $row->numrows;
+		return (int) $row->numrows;
 	}
 
 	// --------------------------------------------------------------------
diff --git a/system/database/drivers/mysql/mysql_driver.php b/system/database/drivers/mysql/mysql_driver.php
index 943b3c0..a0cdb58 100644
--- a/system/database/drivers/mysql/mysql_driver.php
+++ b/system/database/drivers/mysql/mysql_driver.php
@@ -325,15 +325,19 @@
 	function count_all($table = '')
 	{
 		if ($table == '')
-			return '0';
+		{
+			return 0;
+		}
 	
-		$query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
-		
+		$query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
+
 		if ($query->num_rows() == 0)
-			return '0';
+		{
+			return 0;
+		}
 
 		$row = $query->row();
-		return (int)$row->numrows;
+		return (int) $row->numrows;
 	}
 
 	// --------------------------------------------------------------------
diff --git a/system/database/drivers/mysqli/mysqli_driver.php b/system/database/drivers/mysqli/mysqli_driver.php
index ce458f2..9ef18e0 100644
--- a/system/database/drivers/mysqli/mysqli_driver.php
+++ b/system/database/drivers/mysqli/mysqli_driver.php
@@ -316,15 +316,19 @@
 	function count_all($table = '')
 	{
 		if ($table == '')
-			return '0';
-		
-		$query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
-		
+		{
+			return 0;
+		}
+
+		$query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
+
 		if ($query->num_rows() == 0)
-			return '0';
+		{
+			return 0;
+		}
 
 		$row = $query->row();
-		return $row->numrows;
+		return (int) $row->numrows;
 	}
 
 	// --------------------------------------------------------------------
diff --git a/system/database/drivers/oci8/oci8_driver.php b/system/database/drivers/oci8/oci8_driver.php
index 0629a59..b949a96 100644
--- a/system/database/drivers/oci8/oci8_driver.php
+++ b/system/database/drivers/oci8/oci8_driver.php
@@ -420,17 +420,19 @@
 	function count_all($table = '')
 	{
 		if ($table == '')
-			return '0';
+		{
+			return 0;
+		}
 
-		$query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
+		$query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
 
 		if ($query == FALSE)
-			{
+		{
 			return 0;
-			}
+		}
 
 		$row = $query->row();
-		return $row->NUMROWS;
+		return (int) $row->numrows;
 	}
 
 	// --------------------------------------------------------------------
diff --git a/system/database/drivers/odbc/odbc_driver.php b/system/database/drivers/odbc/odbc_driver.php
index 8fcbcfe..0f8b420 100644
--- a/system/database/drivers/odbc/odbc_driver.php
+++ b/system/database/drivers/odbc/odbc_driver.php
@@ -289,15 +289,19 @@
 	function count_all($table = '')
 	{
 		if ($table == '')
-			return '0';
-	
-		$query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
-	
+		{
+			return 0;
+		}
+
+		$query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
+
 		if ($query->num_rows() == 0)
-			return '0';
+		{
+			return 0;
+		}
 
 		$row = $query->row();
-		return $row->numrows;
+		return (int) $row->numrows;
 	}
 
 	// --------------------------------------------------------------------
diff --git a/system/database/drivers/postgre/postgre_driver.php b/system/database/drivers/postgre/postgre_driver.php
index da0b0f2..9d53b1e 100644
--- a/system/database/drivers/postgre/postgre_driver.php
+++ b/system/database/drivers/postgre/postgre_driver.php
@@ -329,15 +329,19 @@
 	function count_all($table = '')
 	{
 		if ($table == '')
-			return '0';
+		{
+			return 0;
+		}
 
-		$query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
-				
+		$query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
+
 		if ($query->num_rows() == 0)
-			return '0';
+		{
+			return 0;
+		}
 
 		$row = $query->row();
-		return $row->numrows;
+		return (int) $row->numrows;
 	}
 
 	// --------------------------------------------------------------------
diff --git a/system/database/drivers/sqlite/sqlite_driver.php b/system/database/drivers/sqlite/sqlite_driver.php
index 058cef7..3ef88db 100644
--- a/system/database/drivers/sqlite/sqlite_driver.php
+++ b/system/database/drivers/sqlite/sqlite_driver.php
@@ -301,15 +301,19 @@
 	function count_all($table = '')
 	{
 		if ($table == '')
-			return '0';
-	
-		$query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
+		{
+			return 0;
+		}
+
+		$query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
 		
 		if ($query->num_rows() == 0)
-			return '0';
+		{
+			return 0;
+		}
 
 		$row = $query->row();
-		return $row->numrows;
+		return (int) $row->numrows;
 	}
 
 	// --------------------------------------------------------------------
diff --git a/user_guide/changelog.html b/user_guide/changelog.html
index 90cc7ca..00deea2 100644
--- a/user_guide/changelog.html
+++ b/user_guide/changelog.html
@@ -75,8 +75,12 @@
 	<li>Database
 		<ul>
 			<li>Added where_in to the list of expected arguments received by <kbd>delete()</kbd>.</li>
+			<li>Fixed a bug when doing 'random' on order_by() (#5706).</li>
+			<li>Fixed a bug where adding a primary key through Forge could fail (#5731).</li>
+			<li>Fixed a bug when using DB cache on multiple databases (#5737).</li>
 		</ul>
-	</li>	<li>Helpers
+	</li>
+	<li>Helpers
 		<ul>
 			<li>Added a doctype() function to the <a href="helpers/html_helper.html">HTML helper</a>.</li>
 			<li>Added ability to force lowercase for url_title() in the <a href="helpers/url_helper.html">URL helper</a>.</li>