From 2570226fbac3e26b1e94896b50d1db4bc1aa3308 Mon Sep 17 00:00:00 2001 From: wei <> Date: Sun, 17 Dec 2006 22:20:50 +0000 Subject: Add TDataSourceConfig, TSqlMapConfig, TActiveRecordConfig --- .../Data/SqlMap/Configuration/TSqlMapStatement.php | 28 ++-- .../Configuration/TSqlMapXmlConfiguration.php | 13 +- .../Data/SqlMap/Statements/TMappedStatement.php | 4 +- framework/Data/SqlMap/Statements/TSqlMapSelect.php | 29 ---- framework/Data/SqlMap/TSqlMapConfig.php | 163 +++++++++++++++++++++ framework/Data/SqlMap/TSqlMapGateway.php | 8 - framework/Data/SqlMap/TSqlMapManager.php | 25 ++-- 7 files changed, 207 insertions(+), 63 deletions(-) delete mode 100644 framework/Data/SqlMap/Statements/TSqlMapSelect.php create mode 100644 framework/Data/SqlMap/TSqlMapConfig.php (limited to 'framework/Data/SqlMap') 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 @@ -294,6 +294,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. * @@ -338,18 +354,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. * 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 @@ -55,14 +55,6 @@ class TSqlMapGateway extends TComponent return $this->getSqlMapManager()->getDbConnection(); } - /** - * @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; @@ -119,6 +118,16 @@ class TSqlMapManager extends TComponent return $this->_gateway; } + /** + * 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()}. @@ -126,8 +135,6 @@ class TSqlMapManager extends TComponent */ protected function createSqlMapGateway() { - $config = new TSqlMapXmlConfiguration($this); - $config->configure($this->getConfigFile()); return new TSqlMapGateway($this); } -- cgit v1.2.3