3 * TSqlMapManager 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.SqlMap.TSqlMapGateway');
13 Prado::using('System.Data.SqlMap.DataMapper.TSqlMapException');
14 Prado::using('System.Data.SqlMap.DataMapper.TSqlMapTypeHandlerRegistry');
15 Prado::using('System.Data.SqlMap.DataMapper.TSqlMapCache');
16 Prado::using('System.Data.SqlMap.Configuration.TSqlMapStatement');
17 Prado::using('System.Data.SqlMap.Configuration.*');
18 Prado::using('System.Data.SqlMap.DataMapper.*');
19 Prado::using('System.Data.SqlMap.Statements.*');
20 Prado::using('System.Caching.TCache');
24 * TSqlMapManager class holds the sqlmap configuation result maps, statements
25 * parameter maps and a type handler factory.
27 * Use {@link SqlMapGateway getSqlMapGateway()} property to obtain the gateway
28 * instance used for querying statements defined in the SqlMap configuration files.
31 * $conn = new TDbConnection($dsn,$dbuser,$dbpass);
32 * $manager = new TSqlMapManager($conn);
33 * $manager->configureXml('mydb-sqlmap.xml');
34 * $sqlmap = $manager->getSqlMapGateway();
35 * $result = $sqlmap->queryForObject('Products');
38 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
39 * @package System.Data.SqlMap
42 class TSqlMapManager extends TComponent
44 private $_mappedStatements;
46 private $_parameterMaps;
47 private $_typeHandlers;
48 private $_cacheModels;
52 private $_cacheDependencies;
55 * Constructor, create a new SqlMap manager.
56 * @param TDbConnection database connection
57 * @param string configuration file.
59 public function __construct($connection=null)
61 $this->_connection=$connection;
63 $this->_mappedStatements=new TMap;
64 $this->_resultMaps=new TMap;
65 $this->_parameterMaps=new TMap;
66 $this->_cacheModels=new TMap;
70 * @param TDbConnection default database connection
72 public function setDbConnection($conn)
74 $this->_connection=$conn;
78 * @return TDbConnection default database connection
80 public function getDbConnection()
82 return $this->_connection;
86 * @return TTypeHandlerFactory The TypeHandlerFactory
88 public function getTypeHandlers()
90 if($this->_typeHandlers===null)
91 $this->_typeHandlers= new TSqlMapTypeHandlerRegistry();
92 return $this->_typeHandlers;
96 * @return TSqlMapGateway SqlMap gateway.
98 public function getSqlmapGateway()
100 if($this->_gateway===null)
101 $this->_gateway=$this->createSqlMapGateway();
102 return $this->_gateway;
106 * Loads and parses the SqlMap configuration file.
107 * @param string xml configuration file.
109 public function configureXml($file)
111 $config = new TSqlMapXmlConfiguration($this);
112 $config->configure($file);
116 * @return TChainedCacheDependency
119 public function getCacheDependencies()
121 if($this->_cacheDependencies === null)
122 $this->_cacheDependencies=new TChainedCacheDependency();
124 return $this->_cacheDependencies;
128 * Configures the current TSqlMapManager using the given xml configuration file
129 * defined in {@link ConfigFile setConfigFile()}.
130 * @return TSqlMapGateway create and configure a new TSqlMapGateway.
132 protected function createSqlMapGateway()
134 return new TSqlMapGateway($this);
138 * @return TMap mapped statements collection.
140 public function getMappedStatements()
142 return $this->_mappedStatements;
146 * Gets a MappedStatement by name.
147 * @param string The name of the statement.
148 * @return IMappedStatement The MappedStatement
149 * @throws TSqlMapUndefinedException
151 public function getMappedStatement($name)
153 if($this->_mappedStatements->contains($name) == false)
154 throw new TSqlMapUndefinedException('sqlmap_contains_no_statement', $name);
155 return $this->_mappedStatements[$name];
159 * Adds a (named) MappedStatement.
160 * @param string The key name
161 * @param IMappedStatement The statement to add
162 * @throws TSqlMapDuplicateException
164 public function addMappedStatement(IMappedStatement $statement)
166 $key = $statement->getID();
167 if($this->_mappedStatements->contains($key) == true)
168 throw new TSqlMapDuplicateException('sqlmap_already_contains_statement', $key);
169 $this->_mappedStatements->add($key, $statement);
173 * @return TMap result maps collection.
175 public function getResultMaps()
177 return $this->_resultMaps;
181 * Gets a named result map
182 * @param string result name.
183 * @return TResultMap the result map.
184 * @throws TSqlMapUndefinedException
186 public function getResultMap($name)
188 if($this->_resultMaps->contains($name) == false)
189 throw new TSqlMapUndefinedException('sqlmap_contains_no_result_map', $name);
190 return $this->_resultMaps[$name];
194 * @param TResultMap add a new result map to this SQLMap
195 * @throws TSqlMapDuplicateException
197 public function addResultMap(TResultMap $result)
199 $key = $result->getID();
200 if($this->_resultMaps->contains($key) == true)
201 throw new TSqlMapDuplicateException('sqlmap_already_contains_result_map', $key);
202 $this->_resultMaps->add($key, $result);
206 * @return TMap parameter maps collection.
208 public function getParameterMaps()
210 return $this->_parameterMaps;
214 * @param string parameter map ID name.
215 * @return TParameterMap the parameter with given ID.
216 * @throws TSqlMapUndefinedException
218 public function getParameterMap($name)
220 if($this->_parameterMaps->contains($name) == false)
221 throw new TSqlMapUndefinedException('sqlmap_contains_no_parameter_map', $name);
222 return $this->_parameterMaps[$name];
226 * @param TParameterMap add a new parameter map to this SQLMap.
227 * @throws TSqlMapDuplicateException
229 public function addParameterMap(TParameterMap $parameter)
231 $key = $parameter->getID();
232 if($this->_parameterMaps->contains($key) == true)
233 throw new TSqlMapDuplicateException('sqlmap_already_contains_parameter_map', $key);
234 $this->_parameterMaps->add($key, $parameter);
238 * Adds a named cache.
239 * @param TSqlMapCacheModel the cache to add.
240 * @throws TSqlMapConfigurationException
242 public function addCacheModel(TSqlMapCacheModel $cacheModel)
244 if($this->_cacheModels->contains($cacheModel->getID()))
245 throw new TSqlMapConfigurationException('sqlmap_cache_model_already_exists', $cacheModel->getID());
247 $this->_cacheModels->add($cacheModel->getID(), $cacheModel);
251 * Gets a cache by name
252 * @param string the name of the cache to get.
253 * @return TSqlMapCacheModel the cache object.
254 * @throws TSqlMapConfigurationException
256 public function getCacheModel($name)
258 if(!$this->_cacheModels->contains($name))
259 throw new TSqlMapConfigurationException('sqlmap_unable_to_find_cache_model', $name);
260 return $this->_cacheModels[$name];
264 * Flushes all cached objects that belong to this SqlMap
266 public function flushCacheModels()
268 foreach($this->_cacheModels as $cache)