diff options
author | wei <> | 2006-12-17 22:20:50 +0000 |
---|---|---|
committer | wei <> | 2006-12-17 22:20:50 +0000 |
commit | 2570226fbac3e26b1e94896b50d1db4bc1aa3308 (patch) | |
tree | 421108ccbdc0ef021e6af4fa35b1d6bcbc352b37 /framework | |
parent | ddc0de38f64e5834ce04f0407a8416172b596655 (diff) |
Add TDataSourceConfig, TSqlMapConfig, TActiveRecordConfig
Diffstat (limited to 'framework')
-rw-r--r-- | framework/Data/ActiveRecord/TActiveRecordConfig.php | 108 | ||||
-rw-r--r-- | framework/Data/ActiveRecord/TActiveRecordGateway.php | 62 | ||||
-rw-r--r-- | framework/Data/ActiveRecord/TActiveRecordManager.php | 19 | ||||
-rw-r--r-- | framework/Data/SqlMap/Configuration/TSqlMapStatement.php | 28 | ||||
-rw-r--r-- | framework/Data/SqlMap/Configuration/TSqlMapXmlConfiguration.php | 13 | ||||
-rw-r--r-- | framework/Data/SqlMap/Statements/TMappedStatement.php | 4 | ||||
-rw-r--r-- | framework/Data/SqlMap/Statements/TSqlMapSelect.php | 29 | ||||
-rw-r--r-- | framework/Data/SqlMap/TSqlMapConfig.php | 163 | ||||
-rw-r--r-- | framework/Data/SqlMap/TSqlMapGateway.php | 8 | ||||
-rw-r--r-- | framework/Data/SqlMap/TSqlMapManager.php | 25 | ||||
-rw-r--r-- | framework/Data/TDataSourceConfig.php | 146 |
11 files changed, 527 insertions, 78 deletions
diff --git a/framework/Data/ActiveRecord/TActiveRecordConfig.php b/framework/Data/ActiveRecord/TActiveRecordConfig.php new file mode 100644 index 00000000..5670e64c --- /dev/null +++ b/framework/Data/ActiveRecord/TActiveRecordConfig.php @@ -0,0 +1,108 @@ +<?php
+/**
+ * TActiveRecordConfig class file.
+ *
+ * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
+ * @link http://www.pradosoft.com/
+ * @copyright Copyright © 2005-2007 PradoSoft
+ * @license http://www.pradosoft.com/license/
+ * @version $Id$
+ * @package System.Data.ActiveRecord
+ */
+
+Prado::using('System.Data.TDataSourceConfig');
+
+/**
+ * TActiveRecordConfig module configuration class.
+ *
+ * Database configuration for the default ActiveRecord manager instance.
+ *
+ * Example: application.xml configuration
+ * <code>
+ * <modules>
+ * <module class="System.Data.ActiveRecord.TActiveRecordConfig" EnableCache="true">
+ * <database ConnectionString="mysql:host=localhost;dbname=test"
+ * Username="dbuser" Password="dbpass" />
+ * </module>
+ * </modules>
+ * </code>
+ *
+ * MySQL database definition:
+ * <code>
+ * CREATE TABLE `blogs` (
+ * `blog_id` int(10) unsigned NOT NULL auto_increment,
+ * `blog_name` varchar(255) NOT NULL,
+ * `blog_author` varchar(255) NOT NULL,
+ * PRIMARY KEY (`blog_id`)
+ * ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+ * </code>
+ *
+ * Record php class:
+ * <code>
+ * class Blogs extends TActiveRecord
+ * {
+ * public $blog_id;
+ * public $blog_name;
+ * public $blog_author;
+ *
+ * public static function finder()
+ * {
+ * return self::getRecordFinder('Blogs');
+ * }
+ * }
+ * </code>
+ *
+ * Usage example:
+ * <code>
+ * class Home extends TPage
+ * {
+ * function onLoad($param)
+ * {
+ * $blogs = Blogs::finder()->findAll();
+ * print_r($blogs);
+ * }
+ * }
+ * </code>
+ *
+ * @author Wei Zhuo <weizho[at]gmail[dot]com>
+ * @version $Id$
+ * @package System.Data.ActiveRecord
+ * @since 3.1
+ */
+class TActiveRecordConfig extends TDataSourceConfig
+{
+ private $_enableCache=false;
+
+ /**
+ * Initialize the active record manager.
+ * @param TXmlDocument xml configuration.
+ */
+ public function init($xml)
+ {
+ parent::init($xml);
+ Prado::using('System.Data.ActiveRecord.TActiveRecordManager');
+ $manager = TActiveRecordManager::getInstance();
+ if($this->getEnableCache())
+ $manager->setCache($this->getApplication()->getCache());
+ $manager->setDbConnection($this->getDbConnection());
+ }
+
+ /**
+ * Set true to cache the table meta data.
+ * @param boolean true to cache sqlmap instance.
+ */
+ public function setEnableCache($value)
+ {
+ $this->_enableCache = TPropertyValue::ensureBoolean($value, false);
+ }
+
+ /**
+ * @return boolean true if table meta data should be cached, false otherwise.
+ */
+ public function getEnableCache()
+ {
+ return $this->_enableCache;
+ }
+}
+ +?>
\ No newline at end of file diff --git a/framework/Data/ActiveRecord/TActiveRecordGateway.php b/framework/Data/ActiveRecord/TActiveRecordGateway.php index 19c8552f..e7ea5e46 100644 --- a/framework/Data/ActiveRecord/TActiveRecordGateway.php +++ b/framework/Data/ActiveRecord/TActiveRecordGateway.php @@ -22,7 +22,7 @@ class TActiveRecordGateway extends TComponent
{
private $_manager;
- private $_tables=array();
+ private $_tables=array(); //meta data cache.
/**
* Property name for optional table name in TActiveRecord.
@@ -68,35 +68,67 @@ class TActiveRecordGateway extends TComponent }
/**
- * Gets the meta data for given database and table.
+ * @param TActiveRecord active record.
+ * @return TDbMetaData table meta data, null if not found.
*/
- public function getMetaData(TActiveRecord $record)
+ protected function getCachedMetaData($record)
{
$type=get_class($record);
- if(!isset($this->_tables[$type]))
+ if(isset($this->_tables[$type]))
+ return $this->_tables[$type];
+ if(($cache=$this->getManager()->getCache())!==null)
{
- $conn = $record->getDbConnection();
- $inspector = $this->getManager()->getTableInspector($conn);
- $table = $this->getTableName($record);
- $this->_tables[$type] = $inspector->getTableMetaData($table);
+ //force loading of the table inspector to load the required classes
+ // before unserializing cached meta data.
+ $this->getManager()->getTableInspector($record->getDbConnection());
+ $data = $cache->get($this->getMetaDataCacheKey($record));
+ if($data !== false && $data !== null)
+ {
+ $this->_tables[$type] = $data;
+ return $data;
+ }
}
- return $this->_tables[$type];
}
/**
- * @param array table meta data.
+ * @param TActiveRecord active record.
+ * @return string cache key, using connection string + record class name
+ */
+ protected function getMetaDataCacheKey($record)
+ {
+ $conn = $record->getDbConnection()->getConnectionString();
+ return $conn.':'.get_class($record);
+ }
+
+ /**
+ * Cache the meta data, tries the application cache if applicable.
+ * @param TActiveRecord active record.
+ * @param TDbMetaData table meta data
+ * @return TDbMetaData table meta data.
*/
- public function setAllMetaData($data)
+ protected function cacheMetaData($record,$data)
{
- $this->_tables=$data;
+ $type = get_class($record);
+ if(($cache=$this->getManager()->getCache())!==null)
+ $cache->set($this->getMetaDataCacheKey($record), $data);
+ $this->_tables[$type] = $data;
+ return $data;
}
/**
- * @return array all table meta data.
+ * Gets the meta data for given database and table.
*/
- public function getAllMetaData()
+ public function getMetaData(TActiveRecord $record)
{
- return $this->_tables;
+ $type=get_class($record);
+ if(!($data = $this->getCachedMetaData($record)))
+ {
+ $conn = $record->getDbConnection();
+ $inspector = $this->getManager()->getTableInspector($conn);
+ $table = $this->getTableName($record);
+ $data = $this->cacheMetaData($record,$inspector->getTableMetaData($table));
+ }
+ return $data;
}
/**
diff --git a/framework/Data/ActiveRecord/TActiveRecordManager.php b/framework/Data/ActiveRecord/TActiveRecordManager.php index 883abbf2..7f239a34 100644 --- a/framework/Data/ActiveRecord/TActiveRecordManager.php +++ b/framework/Data/ActiveRecord/TActiveRecordManager.php @@ -11,6 +11,7 @@ */
Prado::using('System.Data.TDbConnection');
+Prado::using('System.Data.ActiveRecord.TActiveRecord');
Prado::using('System.Data.ActiveRecord.Exceptions.TActiveRecordException');
Prado::using('System.Data.ActiveRecord.TActiveRecordGateway');
Prado::using('System.Data.ActiveRecord.TActiveRecordStateRegistry');
@@ -46,6 +47,24 @@ class TActiveRecordManager extends TComponent private $_meta=array();
private $_connection;
+ private $_cache;
+
+ /**
+ * @return ICache application cache.
+ */
+ public function getCache()
+ {
+ return $this->_cache;
+ }
+
+ /**
+ * @param ICache application cache
+ */
+ public function setCache($value)
+ {
+ $this->_cache=$value;
+ }
+
/**
* @param TDbConnection default database connection
*/
diff --git a/framework/Data/SqlMap/Configuration/TSqlMapStatement.php b/framework/Data/SqlMap/Configuration/TSqlMapStatement.php index 253d2090..5d2640e9 100644 --- a/framework/Data/SqlMap/Configuration/TSqlMapStatement.php +++ b/framework/Data/SqlMap/Configuration/TSqlMapStatement.php @@ -295,6 +295,22 @@ class TSqlMapStatement extends TComponent }
/**
+ * TSqlMapSelect class file.
+ *
+ * @author Wei Zhuo <weizho[at]gmail[dot]com>
+ * @version $Id$
+ * @package System.Data.SqlMap.Statements
+ * @since 3.1
+ */
+class TSqlMapSelect extends TSqlMapStatement
+{
+ private $_generate;
+
+ public function getGenerate(){ return $this->_generate; }
+ public function setGenerate($value){ $this->_generate = $value; }
+}
+
+/**
* TSqlMapDelete class corresponds to the <delete> element.
*
* @author Wei Zhuo <weizho[at]gmail[dot]com>
@@ -339,18 +355,6 @@ class TSqlMapInsert extends TSqlMapStatement }
/**
- * TSqlMapSelect class corresponds to <select> element.
- *
- * @author Wei Zhuo <weizho[at]gmail[dot]com>
- * @version $Id$
- * @package System.Data.SqlMap.Configuration
- * @since 3.1
- */
-class TSqlMapSelect extends TSqlMapStatement
-{
-}
-
-/**
* TSqlMapUpdate class corresponds to <update> element.
*
* @author Wei Zhuo <weizho[at]gmail[dot]com>
diff --git a/framework/Data/SqlMap/Configuration/TSqlMapXmlConfiguration.php b/framework/Data/SqlMap/Configuration/TSqlMapXmlConfiguration.php index eccf4f6e..d92a0f07 100644 --- a/framework/Data/SqlMap/Configuration/TSqlMapXmlConfiguration.php +++ b/framework/Data/SqlMap/Configuration/TSqlMapXmlConfiguration.php @@ -180,6 +180,10 @@ class TSqlMapXmlConfiguration extends TSqlMapXmlConfigBuilder foreach($document->xpath('//connection[last()]') as $conn)
$this->loadDatabaseConnection($conn);
+ //try to load configuration in the current config file.
+ $mapping = new TSqlMapXmlMappingConfiguration($this);
+ $mapping->configure($filename);
+
foreach($document->xpath('//sqlMap') as $sqlmap)
$this->loadSqlMappingFiles($sqlmap);
@@ -222,9 +226,12 @@ class TSqlMapXmlConfiguration extends TSqlMapXmlConfigBuilder */
protected function loadSqlMappingFiles($node)
{
- $mapping = new TSqlMapXmlMappingConfiguration($this);
- $filename = $this->getAbsoluteFilePath($this->_configFile, (string)$node['resource']);
- $mapping->configure($filename);
+ if(strlen($resource = (string)$node['resource']) > 0)
+ {
+ $mapping = new TSqlMapXmlMappingConfiguration($this);
+ $filename = $this->getAbsoluteFilePath($this->_configFile, $resource);
+ $mapping->configure($filename);
+ }
}
/**
diff --git a/framework/Data/SqlMap/Statements/TMappedStatement.php b/framework/Data/SqlMap/Statements/TMappedStatement.php index 0989b6ff..feba00a4 100644 --- a/framework/Data/SqlMap/Statements/TMappedStatement.php +++ b/framework/Data/SqlMap/Statements/TMappedStatement.php @@ -106,7 +106,7 @@ class TMappedStatement extends TComponent implements IMappedStatement */
protected function initialGroupByResults()
{
- $this->_groupBy = new TSQLMapObjectCollectionTree();
+ $this->_groupBy = new TSqlMapObjectCollectionTree();
}
/**
@@ -956,7 +956,7 @@ class TPostSelectBinding * @package System.Data.SqlMap.Statements
* @since 3.1
*/
-class TSQLMapObjectCollectionTree
+class TSqlMapObjectCollectionTree
{
/**
* @var array object graph as tree
diff --git a/framework/Data/SqlMap/Statements/TSqlMapSelect.php b/framework/Data/SqlMap/Statements/TSqlMapSelect.php deleted file mode 100644 index 258eae3f..00000000 --- a/framework/Data/SqlMap/Statements/TSqlMapSelect.php +++ /dev/null @@ -1,29 +0,0 @@ -<?php
-/**
- * TSqlMapSelect class file.
- *
- * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
- * @link http://www.pradosoft.com/
- * @copyright Copyright © 2005-2007 PradoSoft
- * @license http://www.pradosoft.com/license/
- * @version $Id$
- * @package System.Data.SqlMap.Statements
- */
-
-/**
- * TSqlMapSelect class file.
- *
- * @author Wei Zhuo <weizho[at]gmail[dot]com>
- * @version $Id$
- * @package System.Data.SqlMap.Statements
- * @since 3.1
- */
-class TSqlMapSelect extends TSqlMapStatement
-{
- private $_generate;
-
- public function getGenerate(){ return $this->_generate; }
- public function setGenerate($value){ $this->_generate = $value; }
-}
-
-?>
\ No newline at end of file diff --git a/framework/Data/SqlMap/TSqlMapConfig.php b/framework/Data/SqlMap/TSqlMapConfig.php new file mode 100644 index 00000000..883a11e1 --- /dev/null +++ b/framework/Data/SqlMap/TSqlMapConfig.php @@ -0,0 +1,163 @@ +<?php
+/**
+ * TSqlMapConfig class file.
+ *
+ * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
+ * @link http://www.pradosoft.com/
+ * @copyright Copyright © 2005-2007 PradoSoft
+ * @license http://www.pradosoft.com/license/
+ * @version $Id$
+ * @package System.Data.SqlMap
+ */
+
+Prado::using('System.Data.TDataSourceConfig');
+
+/**
+ * TSqlMapConfig module configuration class.
+ *
+ * Database connection and TSqlMapManager configuration.
+ *
+ * @author Wei Zhuo <weizho[at]gmail[dot]com>
+ * @version $Id$
+ * @package System
+ * @since version
+ */
+class TSqlMapConfig extends TDataSourceConfig
+{
+ private $_configFile;
+ private $_sqlmap;
+ private $_enableCache=false;
+
+ /**
+ * File extension of external configuration file
+ */
+ const CONFIG_FILE_EXT='.xml';
+
+ /**
+ * @return string module ID + configuration file path.
+ */
+ private function getCacheKey()
+ {
+ return $this->getID().$this->getConfigFile();
+ }
+
+ /**
+ * Deletes the configuration cache.
+ */
+ public function clearCache()
+ {
+ $cache = $this->getApplication()->getCache();
+ if(!is_null($cache))
+ $cache->delete($this->getCacheKey());
+ }
+
+ /**
+ * Saves the current SqlMap manager to cache.
+ * @return boolean true if SqlMap manager was cached, false otherwise.
+ */
+ protected function cacheSqlMapManager($manager)
+ {
+ if($this->getEnableCache())
+ {
+ $cache = $this->getApplication()->getCache();
+ if(!is_null($cache))
+ return $cache->add($this->getCacheKey(), $manager);
+ }
+ return false;
+ }
+
+ /**
+ * Loads SqlMap manager from cache.
+ * @return TSqlMapManager SqlMap manager intance if load was successful, null otherwise.
+ */
+ protected function loadCachedSqlMapManager()
+ {
+ if($this->getEnableCache())
+ {
+ $cache = $this->getApplication()->getCache();
+ if(!is_null($cache))
+ {
+ $manager = $cache->get($this->getCacheKey());
+ if($manager instanceof TSqlMapManager)
+ return $manager;
+ }
+ }
+ }
+
+ /**
+ * @return string SqlMap configuration file.
+ */
+ public function getConfigFile()
+ {
+ return $this->_configFile;
+ }
+
+ /**
+ * @param string external configuration file in namespace format. The file
+ * extension must be '.xml'.
+ * @throws TConfigurationException if the file is invalid.
+ */
+ public function setConfigFile($value)
+ {
+ if(is_file($value))
+ $this->_configFile=$value;
+ else
+ {
+ $file = Prado::getPathOfNamespace($value,self::CONFIG_FILE_EXT);
+ if(is_null($file) || !is_file($file))
+ throw new TConfigurationException('sqlmap_configfile_invalid',$value);
+ else
+ $this->_configFile = $file;
+ }
+ }
+
+ /**
+ * Set true to cache sqlmap instances.
+ * @param boolean true to cache sqlmap instance.
+ */
+ public function setEnableCache($value)
+ {
+ $this->_enableCache = TPropertyValue::ensureBoolean($value, false);
+ }
+
+ /**
+ * @return boolean true if configuration should be cached, false otherwise.
+ */
+ public function getEnableCache()
+ {
+ return $this->_enableCache;
+ }
+
+ /**
+ * Configure the data mapper using sqlmap configuration file.
+ * If cache is enabled, the data mapper instance is cached.
+ * @return TSqlMapGateway SqlMap gateway instance.
+ */
+ protected function createSqlMapGateway()
+ {
+ Prado::using('System.Data.SqlMap.TSqlMapManager');
+ if(($manager = $this->loadCachedSqlMapManager())===null)
+ {
+ $manager = new TSqlMapManager($this->getDbConnection());
+ if(strlen($file=$this->getConfigFile()) > 0)
+ {
+ $manager->configureXml($file);
+ $this->cacheSqlMapManager($manager);
+ }
+ }
+ return $manager->getSqlmapGateway();
+ }
+
+ /**
+ * Initialize the sqlmap if necessary, returns the TSqlMapGateway instance.
+ * @return TSqlMapGateway SqlMap gateway instance.
+ */
+ public function getClient()
+ {
+ if($this->_sqlmap===null )
+ $this->_sqlmap=$this->createSqlMapGateway();
+ return $this->_sqlmap;
+ }
+}
+ +?>
\ No newline at end of file diff --git a/framework/Data/SqlMap/TSqlMapGateway.php b/framework/Data/SqlMap/TSqlMapGateway.php index 799db446..b2819740 100644 --- a/framework/Data/SqlMap/TSqlMapGateway.php +++ b/framework/Data/SqlMap/TSqlMapGateway.php @@ -56,14 +56,6 @@ class TSqlMapGateway extends TComponent }
/**
- * @param TDbConnection new database connection.
- */
- public function setDbConnection($conn)
- {
- $this->getSqlMapManager()->setDbConnection($conn);
- }
-
- /**
* Executes a Sql SELECT statement that returns that returns data
* to populate a single object instance.
*
diff --git a/framework/Data/SqlMap/TSqlMapManager.php b/framework/Data/SqlMap/TSqlMapManager.php index 32656bd5..37d50f5d 100644 --- a/framework/Data/SqlMap/TSqlMapManager.php +++ b/framework/Data/SqlMap/TSqlMapManager.php @@ -14,10 +14,12 @@ Prado::using('System.Data.SqlMap.TSqlMapGateway'); Prado::using('System.Data.SqlMap.DataMapper.TSqlMapException');
Prado::using('System.Data.SqlMap.DataMapper.TSqlMapTypeHandlerRegistry');
Prado::using('System.Data.SqlMap.DataMapper.TSqlMapCache');
-Prado::using('System.Data.SqlMap.DataMapper.*');
+Prado::using('System.Data.SqlMap.Configuration.TSqlMapStatement');
Prado::using('System.Data.SqlMap.Configuration.*');
+Prado::using('System.Data.SqlMap.DataMapper.*');
Prado::using('System.Data.SqlMap.Statements.*');
+
/**
* TSqlMapManager class holds the sqlmap configuation result maps, statements
* parameter maps and a type handler factory.
@@ -27,7 +29,8 @@ Prado::using('System.Data.SqlMap.Statements.*'); *
* <code>
* $conn = new TDbConnection($dsn,$dbuser,$dbpass);
- * $manager = new TSqlMapManager($conn, 'mydb-sqlmap.xml');
+ * $manager = new TSqlMapManager($conn);
+ * $manager->configureXml('mydb-sqlmap.xml');
* $sqlmap = $manager->getSqlMapGateway();
* $result = $sqlmap->queryForObject('Products');
* </code>
@@ -46,9 +49,6 @@ class TSqlMapManager extends TComponent private $_cacheModels;
private $_connection;
-
- private $_configFile;
-
private $_gateway;
/**
@@ -56,10 +56,9 @@ class TSqlMapManager extends TComponent * @param TDbConnection database connection
* @param string configuration file.
*/
- public function __construct($connection=null,$configFile=null)
+ public function __construct($connection=null)
{
$this->_connection=$connection;
- $this->_configFile=$configFile;
$this->_mappedStatements=new TMap;
$this->_resultMaps=new TMap;
@@ -120,14 +119,22 @@ class TSqlMapManager extends TComponent }
/**
+ * Loads and parses the SqlMap configuration file.
+ * @param string xml configuration file.
+ */
+ public function configureXml($file)
+ {
+ $config = new TSqlMapXmlConfiguration($this);
+ $config->configure($file);
+ }
+
+ /**
* Configures the current TSqlMapManager using the given xml configuration file
* defined in {@link ConfigFile setConfigFile()}.
* @return TSqlMapGateway create and configure a new TSqlMapGateway.
*/
protected function createSqlMapGateway()
{
- $config = new TSqlMapXmlConfiguration($this);
- $config->configure($this->getConfigFile());
return new TSqlMapGateway($this);
}
diff --git a/framework/Data/TDataSourceConfig.php b/framework/Data/TDataSourceConfig.php new file mode 100644 index 00000000..2cf74106 --- /dev/null +++ b/framework/Data/TDataSourceConfig.php @@ -0,0 +1,146 @@ +<?php +/**
+ * TDataSourceConfig class file.
+ *
+ * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
+ * @link http://www.pradosoft.com/
+ * @copyright Copyright © 2005-2007 PradoSoft
+ * @license http://www.pradosoft.com/license/
+ * @version $Id$
+ * @package System.Data
+ */
+
+/**
+ * TDataSourceConfig module class provides <module> configuration for database connections.
+ *
+ * Example usage: mysql connection
+ * <code>
+ * <modules>
+ * <module id="db1">
+ * <database ConnectionString="mysqli:host=localhost;dbname=test"
+ * username="dbuser" password="dbpass" />
+ * </module>
+ * </modules>
+ * </code>
+ *
+ * Usage in php:
+ * <code>
+ * class Home extends TPage
+ * {
+ * function onLoad($param)
+ * {
+ * $db = $this->Application->Modules['db1']->DbConnection;
+ * $command->createCommand('...'); //...
+ * }
+ * }
+ * </code>
+ *
+ * The properties of <connection> are those of the class TDbConnection.
+ * Set {@link setConnectionClass} attribute for a custom database connection class
+ * that extends the TDbConnection class.
+ *
+ * @author Wei Zhuo <weizho[at]gmail[dot]com>
+ * @version $Id$
+ * @package System.Data
+ * @since 3.1
+ */
+class TDataSourceConfig extends TModule
+{
+ private $_connID;
+ private $_conn;
+ private $_connClass='System.Data.TDbConnection';
+
+ /**
+ * Initalize the database connection properties from attributes in <database> tag.
+ * @param TXmlDocument xml configuration.
+ */
+ public function init($xml)
+ {
+ if($prop=$xml->getElementByTagName('database'))
+ {
+ $db=$this->getDbConnection();
+ foreach($prop->getAttributes() as $name=>$value)
+ $db->setSubproperty($name,$value);
+ }
+ }
+
+ /**
+ * The module ID of another TDataSourceConfig. The {@link getDbConnection DbConnection}
+ * property of this configuration will equal to {@link getDbConnection DbConnection}
+ * of the given TDataSourceConfig module.
+ * @param string module ID.
+ */
+ public function setConnectionID($value)
+ {
+ $this->_connID=$value;
+ }
+
+ /**
+ * @return string connection module ID.
+ */
+ public function getConnectionID()
+ {
+ return $this->_connID;
+ }
+
+ /**
+ * Gets the TDbConnection from another module if {@link setConnectionID ConnectionID}
+ * is supplied and valid. Otherwise, a connection of type given by
+ * {@link setConnectionClass ConnectionClass} is created.
+ * @return TDbConnection database connection.
+ */
+ public function getDbConnection()
+ {
+ if(is_null($this->_conn))
+ {
+ if(!is_null($this->_connID))
+ $this->_conn = $this->findConnectionByID($this->getConnectionID());
+ else
+ $this->_conn = Prado::createComponent($this->getConnectionClass());
+ }
+ return $this->_conn;
+ }
+
+ /**
+ * @param string Database connection class name to be created.
+ */
+ public function getConnectionClass()
+ {
+ return $this->_connClass;
+ }
+
+ /**
+ * The database connection class name to be created when {@link getDbConnection}
+ * method is called <b>and</b> {@link setConnectionID ConnectionID} is null. The
+ * {@link setConnectionClass ConnectionClass} property must be set before
+ * calling {@link getDbConnection} if you wish to create the connection using the
+ * given class name.
+ * @param string Database connection class name.
+ * @throws TConfigurationException when database connection is already established.
+ */
+ public function setConnectionClass($value)
+ {
+ if(!is_null($this->_conn))
+ throw new TConfigurationException('datasource_dbconnection_exists', $value);
+ $this->_connClass=$value;
+ }
+
+ /**
+ * Finds the database connection instance from the Application modules.
+ * @param string Database connection module ID.
+ * @return TDbConnection database connection.
+ * @throws TConfigurationException when module is not of TDbConnection or TDataSourceConfig.
+ */
+ protected function findConnectionByID($id)
+ {
+ $conn = $this->getApplication()->getModule($id);
+ if($conn instanceof TDbConnection)
+ return $conn;
+ else if($conn instanceof TDataSourceConfig)
+ return $conn->_conn;
+ else
+ throw new TConfigurationException('datasource_dbconnection_invalid',$id);
+ }
+}
+ +?>
\ No newline at end of file |