Many new Active Record functions, and another whack of stuff
diff --git a/system/database/drivers/postgre/postgre_driver.php b/system/database/drivers/postgre/postgre_driver.php
index 88f08b2..63a72f5 100644
--- a/system/database/drivers/postgre/postgre_driver.php
+++ b/system/database/drivers/postgre/postgre_driver.php
@@ -35,7 +35,7 @@
 	 * database engines, so this string appears in each driver and is

 	 * used for the count_all() and count_all_results() functions.

 	 */

-	var $_count_string = "SELECT COUNT(*) AS numrows ";

+	var $_count_string = "SELECT COUNT(*) AS ";

 	var $_random_keyword = ' RANDOM()'; // database specific random keyword

 

 	/**

@@ -83,6 +83,22 @@
 	// --------------------------------------------------------------------

 

 	/**

+	 * Set client character set

+	 *

+	 * @access	public

+	 * @param	string

+	 * @param	string

+	 * @return	resource

+	 */

+	function db_set_charset($charset, $collation)

+	{

+		// TODO - add support if needed

+		return TRUE;

+	}

+

+	// --------------------------------------------------------------------

+	

+	/**

 	 * Version number query string

 	 *

 	 * @access	public

@@ -305,11 +321,19 @@
 	 * Generates a platform-specific query string so that the table names can be fetched

 	 *

 	 * @access	private

+	 * @param	boolean

 	 * @return	string

 	 */

-	function _list_tables()

+	function _list_tables($prefix_limit = FALSE)

 	{	

-		return "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";	

+		$sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";	

+		

+		if ($prefix_limit !== FALSE AND $this->dbprefix != '')

+		{

+			$sql .= " AND table_name LIKE '".$this->dbprefix."%'";

+		}

+		

+		return $sql;

 	}

 	

 	// --------------------------------------------------------------------

@@ -395,6 +419,58 @@
 	// --------------------------------------------------------------------

 

 	/**

+	 * Protect Identifiers

+	 *

+	 * This function adds backticks if appropriate based on db type

+	 *

+	 * @access	private

+	 * @param	mixed	the item to escape

+	 * @param	boolean	only affect the first word

+	 * @return	mixed	the item with backticks

+	 */

+	function _protect_identifiers($item, $first_word_only = FALSE)

+	{

+		if (is_array($item))

+		{

+			$escaped_array = array();

+

+			foreach($item as $k=>$v)

+			{

+				$escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v, $first_word_only);

+			}

+

+			return $escaped_array;

+		}	

+

+		// This function may get "item1 item2" as a string, and so

+		// we may need ""item1" "item2"" and not ""item1 item2""

+		if (strpos($item, ' ') !== FALSE)

+		{

+			// This function may get "field >= 1", and need it to return ""field" >= 1"

+			if ($first_word_only === TRUE)

+			{

+				return '"'.preg_replace('/ /', '" ', $item, 1);

+			}

+

+			$item = preg_replace('/(^|\s|\()([\w\d\-\_]+?)(\s|\)|$)/iS', '$1"$2"$3', $item);

+		}

+

+		$exceptions = array('AS', '/', '-', '%', '+', '*');

+		

+		foreach ($exceptions as $exception)

+		{

+			if (stristr($item, " "{$exception}" ") !== FALSE)

+			{

+				$item = preg_replace('/ "('.preg_quote($exception).')" /i', ' $1 ', $item);

+			}

+		}

+		

+		return $item;

+	}

+			

+	// --------------------------------------------------------------------

+

+	/**

 	 * Insert statement

 	 *

 	 * Generates a platform-specific insert string from the supplied data

@@ -421,9 +497,11 @@
 	 * @param	string	the table name

 	 * @param	array	the update data

 	 * @param	array	the where clause

+	 * @param	array	the orderby clause

+	 * @param	array	the limit clause

 	 * @return	string

 	 */

-	function _update($table, $values, $where, $limit = FALSE)

+	function _update($table, $values, $where, $orderby = array(), $limit = FALSE)

 	{

 		foreach($values as $key => $val)

 		{

@@ -431,8 +509,29 @@
 		}

 		

 		$limit = (!$limit) ? '' : ' LIMIT '.$limit;

+		

+		$orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';

 	

-		return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where).$limit;

+		return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where).$orderby.$limit;

+	}

+

+	

+	// --------------------------------------------------------------------

+

+	/**

+	 * Truncate statement

+	 *

+	 * Generates a platform-specific truncate string from the supplied data

+	 * If the database does not support the truncate() command

+	 * This function maps to "DELETE FROM table"

+	 *

+	 * @access	public

+	 * @param	string	the table name

+	 * @return	string

+	 */	

+	function _truncate($table)

+	{

+		return "TRUNCATE ".$this->_escape_table($table);

 	}

 	

 	// --------------------------------------------------------------------

@@ -445,17 +544,31 @@
 	 * @access	public

 	 * @param	string	the table name

 	 * @param	array	the where clause

+	 * @param	string	the limit clause

 	 * @return	string

 	 */	

-	function _delete($table, $where, $limit = FALSE)

+	function _delete($table, $where = array(), $like = array(), $limit = FALSE)

 	{

+		$conditions = '';

+

+		if (count($where) > 0 || count($like) > 0)

+		{

+			$conditions = "\nWHERE ";

+			$conditions .= implode("\n", $this->ar_where);

+

+			if (count($where) > 0 && count($like) > 0)

+			{

+				$conditions .= " AND ";

+			}

+			$conditions .= implode("\n", $like);

+		}

+

 		$limit = (!$limit) ? '' : ' LIMIT '.$limit;

 	

-		return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where).$limit;

+		return "DELETE FROM ".$table.$conditions.$limit;

 	}

 

 	// --------------------------------------------------------------------

-

 	/**

 	 * Limit string

 	 *