From 0094ffe75fcea9fde0effe1e7de48465cb3c9760 Mon Sep 17 00:00:00 2001
From: xue <>
Date: Thu, 27 Sep 2007 02:30:10 +0000
Subject: Fixed #709.

---
 HISTORY                                    |  1 +
 framework/Caching/TDbCache.php             | 97 +++++++++++++++++++++++-------
 framework/Exceptions/messages/messages.txt |  4 +-
 3 files changed, 80 insertions(+), 22 deletions(-)

diff --git a/HISTORY b/HISTORY
index 28eace7c..0801645c 100644
--- a/HISTORY
+++ b/HISTORY
@@ -19,6 +19,7 @@ ENH: Ticket#676 - Updated ActiveRecord Oracle Drives (Qiang)
 ENH: Ticket#678 - Improved DateTimeFormatInfo performance (Stever)
 ENH: Ticket#681 - TUrlMapping can construct custom URLs now (Qiang)
 ENH: Ticket#692,700 - Added support to wildcard in pages configuration; added IP filters in auth rules. (Qiang)
+ENH: Ticket#709 - Added TDbCache.ConnectionID (Qiang)
 ENH: Added THead requirement check (Qiang)
 ENH: Added TOutputCache.CacheTime (Qiang)
 ENH: Added "remember login" support to TAuthManager and the related modules/classes (Qiang)
diff --git a/framework/Caching/TDbCache.php b/framework/Caching/TDbCache.php
index 302348ce..6c7b1a4e 100644
--- a/framework/Caching/TDbCache.php
+++ b/framework/Caching/TDbCache.php
@@ -25,9 +25,8 @@ Prado::using('System.Data.TDbConnection');
  * By default, TDbCache creates and uses an SQLite database under the application
  * runtime directory. You may change this default setting by specifying the following
  * properties:
- * - {@link setConnectionString ConnectionString}
- * - {@link setUsername Username}
- * - {@link setPassword Pasword}
+ * - {@link setConnectionID ConnectionID} or
+ * - {@link setConnectionString ConnectionString}, {@link setUsername Username} and {@link setPassword Pasword}.
  *
  * The cached data is stored in a table in the specified database.
  * By default, the name of the table is called 'pradocache'. If the table does not
@@ -54,9 +53,6 @@ Prado::using('System.Data.TDbConnection');
  * Do not use the same database file for multiple applications using TDbCache.
  * Also note, cache is shared by all user sessions of an application.
  *
- * To use this module, the sqlite PHP extension must be loaded. Note, Sqlite extension
- * is no longer loaded by default since PHP 5.1.
- *
  * Some usage examples of TDbCache are as follows,
  * <code>
  * $cache=new TDbCache;  // TDbCache may also be loaded as a Prado application module
@@ -80,6 +76,10 @@ Prado::using('System.Data.TDbConnection');
  */
 class TDbCache extends TCache
 {
+	/**
+	 * @var string the ID of TDataSourceConfig module
+	 */
+	private $_connID='';
 	/**
 	 * @var TDbConnection the DB connection instance
 	 */
@@ -92,6 +92,9 @@ class TDbCache extends TCache
 	 * @var boolean whether the cache DB table should be created automatically
 	 */
 	private $_autoCreate=true;
+	private $_username='';
+	private $_password='';
+	private $_connectionString='';
 
 	/**
 	 * Destructor.
@@ -116,13 +119,6 @@ class TDbCache extends TCache
 	public function init($config)
 	{
 		$db=$this->getDbConnection();
-		if($db->getConnectionString()==='')
-		{
-			// default to SQLite3 database
-			$dbFile=$this->getApplication()->getRuntimePath().'/sqlite3.cache';
-			$db->setConnectionString('sqlite:'.$dbFile);
-		}
-
 		$db->setActive(true);
 
 		$sql='DELETE FROM '.$this->_cacheTable.' WHERE expire<>0 AND expire<'.time();
@@ -139,28 +135,87 @@ class TDbCache extends TCache
 				$db->createCommand($sql)->execute();
 			}
 			else
-				throw TConfigurationException('pdocache_cachetable_inexistent',$this->_cacheTable);
+				throw TConfigurationException('db_cachetable_inexistent',$this->_cacheTable);
 		}
 
 		parent::init($config);
 	}
 
+	/**
+	 * Creates the DB connection.
+	 * @param string the module ID for TDataSourceConfig
+	 * @return TDbConnection the created DB connection
+	 * @throws TConfigurationException if module ID is invalid or empty
+	 */
+	protected function createDbConnection()
+	{
+		if($this->_connID!=='')
+		{
+			$config=$this->getApplication()->getModule($connectionID);
+			if($config instanceof TDataSourceConfig)
+				return $config->getDbConnection();
+			else
+				throw new TConfigurationException('dbcache_connectionid_invalid',$this->_connID);
+		}
+		else
+		{
+			$db=new TDbConnection;
+			if($this->_connectionString!=='')
+			{
+				$db->setConnectionString($this->_connectionString);
+				if($this->_username!=='')
+					$db->setUsername($this->_username);
+				if($this->_password!=='')
+					$db->setPassword($this->_password);
+			}
+			else
+			{
+				// default to SQLite3 database
+				$dbFile=$this->getApplication()->getRuntimePath().'/sqlite3.cache';
+				$db->setConnectionString('sqlite:'.$dbFile);
+			}
+			return $db;
+		}
+	}
+
 	/**
 	 * @return TDbConnection the DB connection instance
 	 */
 	public function getDbConnection()
 	{
 		if($this->_db===null)
-			$this->_db=new TDbConnection;
+			$this->_db=$this->createDbConnection();
 		return $this->_db;
 	}
 
+	/**
+	 * @return string the ID of a {@link TDataSourceConfig} module. Defaults to empty string, meaning not set.
+	 * @since 3.1.1
+	 */
+	public function getConnectionID()
+	{
+		return $this->_connID;
+	}
+
+	/**
+	 * Sets the ID of a TDataSourceConfig module.
+	 * The datasource module will be used to establish the DB connection for this cache module.
+	 * The database connection can also be specified via {@link setConnectionString ConnectionString}.
+	 * When both ConnectionID and ConnectionString are specified, the former takes precedence.
+	 * @param string ID of the {@link TDataSourceConfig} module
+	 * @since 3.1.1
+	 */
+	public function setConnectionID($value)
+	{
+		$this->_connID=$value;
+	}
+
 	/**
 	 * @return string The Data Source Name, or DSN, contains the information required to connect to the database.
 	 */
 	public function getConnectionString()
 	{
-		return $this->getDbConnection()->getConnectionString();
+		return $this->_connectionString;
 	}
 
 	/**
@@ -169,7 +224,7 @@ class TDbCache extends TCache
 	 */
 	public function setConnectionString($value)
 	{
-		$this->getDbConnection()->setConnectionString($value);
+		$this->_connectionString=$value;
 	}
 
 	/**
@@ -177,7 +232,7 @@ class TDbCache extends TCache
 	 */
 	public function getUsername()
 	{
-		return $this->getDbConnection()->getUsername();
+		return $this->_username;
 	}
 
 	/**
@@ -185,7 +240,7 @@ class TDbCache extends TCache
 	 */
 	public function setUsername($value)
 	{
-		$this->getDbConnection()->setUsername($value);
+		$this->_username=$value;
 	}
 
 	/**
@@ -193,7 +248,7 @@ class TDbCache extends TCache
 	 */
 	public function getPassword()
 	{
-		return $this->getDbConnection()->getPassword();
+		return $this->_password;
 	}
 
 	/**
@@ -201,7 +256,7 @@ class TDbCache extends TCache
 	 */
 	public function setPassword($value)
 	{
-		$this->getDbConnection()->setPassword($value);
+		$this->_password=$value;
 	}
 
 	/**
diff --git a/framework/Exceptions/messages/messages.txt b/framework/Exceptions/messages/messages.txt
index 086bdb06..4429cfd7 100644
--- a/framework/Exceptions/messages/messages.txt
+++ b/framework/Exceptions/messages/messages.txt
@@ -451,4 +451,6 @@ cachepagestatepersister_cache_required  = TCachePageStatePersister requires a ca
 cachepagestatepersister_timeout_invalid = TCachePageStatePersister.Timeout must be an integer no less than zero.
 cachepagestatepersister_pagestate_corrupted = Page state is corrupted.
 
-conditional_condition_invalid			= TConditional.Condition '{0}' is not a valid PHP expression: {1}
\ No newline at end of file
+conditional_condition_invalid			= TConditional.Condition '{0}' is not a valid PHP expression: {1}
+
+db_cachetable_inexistent				= TDbCache cannot find DB table '{0}' to store cached data.
\ No newline at end of file
-- 
cgit v1.2.3