3 * TSqlMapConfig class file.
5 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6 * @link http://www.pradosoft.com/
7 * @copyright Copyright © 2005-2014 PradoSoft
8 * @license http://www.pradosoft.com/license/
9 * @package System.Data.SqlMap
12 Prado::using('System.Data.TDataSourceConfig');
15 * TSqlMapConfig module configuration class.
17 * Database connection and TSqlMapManager configuration.
19 * @author Wei Zhuo <weizho[at]gmail[dot]com>
20 * @package System.Data.SqlMap
23 class TSqlMapConfig extends TDataSourceConfig
27 private $_enableCache=false;
30 * File extension of external configuration file
32 const CONFIG_FILE_EXT='.xml';
35 * @return string module ID + configuration file path.
37 private function getCacheKey()
39 return $this->getID().$this->getConfigFile();
43 * Deletes the configuration cache.
45 public function clearCache()
47 $cache = $this->getApplication()->getCache();
49 $cache->delete($this->getCacheKey());
54 * Create and configure the data mapper using sqlmap configuration file.
55 * Or if cache is enabled and manager already cached load from cache.
56 * If cache is enabled, the data mapper instance is cached.
58 * @return TSqlMapManager SqlMap manager instance
61 public function getSqlMapManager() {
62 Prado::using('System.Data.SqlMap.TSqlMapManager');
63 if(($manager = $this->loadCachedSqlMapManager())===null)
65 $manager = new TSqlMapManager($this->getDbConnection());
66 if(strlen($file=$this->getConfigFile()) > 0)
68 $manager->configureXml($file);
69 $this->cacheSqlMapManager($manager);
72 elseif($this->getConnectionID() !== '') {
73 $manager->setDbConnection($this->getDbConnection());
79 * Saves the current SqlMap manager to cache.
80 * @return boolean true if SqlMap manager was cached, false otherwise.
82 protected function cacheSqlMapManager($manager)
84 if($this->getEnableCache())
86 $cache = $this->getApplication()->getCache();
89 if($this->getApplication()->getMode() !== TApplicationMode::Performance)
90 $dependencies = $manager->getCacheDependencies();
91 return $cache->set($this->getCacheKey(), $manager, 0, $dependencies);
98 * Loads SqlMap manager from cache.
99 * @return TSqlMapManager SqlMap manager intance if load was successful, null otherwise.
101 protected function loadCachedSqlMapManager()
103 if($this->getEnableCache())
105 $cache = $this->getApplication()->getCache();
108 $manager = $cache->get($this->getCacheKey());
109 if($manager instanceof TSqlMapManager)
117 * @return string SqlMap configuration file.
119 public function getConfigFile()
121 return $this->_configFile;
125 * @param string external configuration file in namespace format. The file
126 * extension must be '.xml'.
127 * @throws TConfigurationException if the file is invalid.
129 public function setConfigFile($value)
132 $this->_configFile=$value;
135 $file = Prado::getPathOfNamespace($value,self::CONFIG_FILE_EXT);
136 if($file === null || !is_file($file))
137 throw new TConfigurationException('sqlmap_configfile_invalid',$value);
139 $this->_configFile = $file;
144 * Set true to cache sqlmap instances.
145 * @param boolean true to cache sqlmap instance.
147 public function setEnableCache($value)
149 $this->_enableCache = TPropertyValue::ensureBoolean($value);
153 * @return boolean true if configuration should be cached, false otherwise.
155 public function getEnableCache()
157 return $this->_enableCache;
161 * @return TSqlMapGateway SqlMap gateway instance.
163 protected function createSqlMapGateway()
165 return $this->getSqlMapManager()->getSqlmapGateway();
169 * Initialize the sqlmap if necessary, returns the TSqlMapGateway instance.
170 * @return TSqlMapGateway SqlMap gateway instance.
172 public function getClient()
174 if($this->_sqlmap===null )
175 $this->_sqlmap=$this->createSqlMapGateway();
176 return $this->_sqlmap;