]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Data/SqlMap/TSqlMapManager.php
baculum: Update PRADO framework from v3.2.3 to v3.2.4
[bacula/bacula] / gui / baculum / framework / Data / SqlMap / TSqlMapManager.php
1 <?php
2 /**
3  * TSqlMapManager class file.
4  *
5  * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6  * @link http://www.pradosoft.com/
7  * @copyright Copyright &copy; 2005-2014 PradoSoft
8  * @license http://www.pradosoft.com/license/
9  * @package System.Data.SqlMap
10  */
11
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');
21
22
23 /**
24  * TSqlMapManager class holds the sqlmap configuation result maps, statements
25  * parameter maps and a type handler factory.
26  *
27  * Use {@link SqlMapGateway getSqlMapGateway()} property to obtain the gateway
28  * instance used for querying statements defined in the SqlMap configuration files.
29  *
30  * <code>
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');
36  * </code>
37  *
38  * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
39  * @package System.Data.SqlMap
40  * @since 3.1
41  */
42 class TSqlMapManager extends TComponent
43 {
44         private $_mappedStatements;
45         private $_resultMaps;
46         private $_parameterMaps;
47         private $_typeHandlers;
48         private $_cacheModels;
49
50         private $_connection;
51         private $_gateway;
52         private $_cacheDependencies;
53
54         /**
55          * Constructor, create a new SqlMap manager.
56          * @param TDbConnection database connection
57          * @param string configuration file.
58          */
59         public function __construct($connection=null)
60         {
61                 $this->_connection=$connection;
62
63                 $this->_mappedStatements=new TMap;
64                 $this->_resultMaps=new TMap;
65                 $this->_parameterMaps=new TMap;
66                 $this->_cacheModels=new TMap;
67         }
68
69         /**
70          * @param TDbConnection default database connection
71          */
72         public function setDbConnection($conn)
73         {
74                 $this->_connection=$conn;
75         }
76
77         /**
78          * @return TDbConnection default database connection
79          */
80         public function getDbConnection()
81         {
82                 return $this->_connection;
83         }
84
85         /**
86          * @return TTypeHandlerFactory The TypeHandlerFactory
87          */
88         public function getTypeHandlers()
89         {
90                 if($this->_typeHandlers===null)
91                         $this->_typeHandlers= new TSqlMapTypeHandlerRegistry();
92                 return $this->_typeHandlers;
93         }
94
95         /**
96          * @return TSqlMapGateway SqlMap gateway.
97          */
98         public function getSqlmapGateway()
99         {
100                 if($this->_gateway===null)
101                         $this->_gateway=$this->createSqlMapGateway();
102                 return $this->_gateway;
103         }
104
105         /**
106          * Loads and parses the SqlMap configuration file.
107          * @param string xml configuration file.
108          */
109         public function configureXml($file)
110         {
111                 $config = new TSqlMapXmlConfiguration($this);
112                 $config->configure($file);
113         }
114
115         /**
116          * @return TChainedCacheDependency
117          * @since 3.1.5
118          */
119         public function getCacheDependencies()
120         {
121                 if($this->_cacheDependencies === null)
122                         $this->_cacheDependencies=new TChainedCacheDependency();
123
124                 return $this->_cacheDependencies;
125         }
126
127         /**
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.
131          */
132         protected function createSqlMapGateway()
133         {
134                 return new TSqlMapGateway($this);
135         }
136
137         /**
138          * @return TMap mapped statements collection.
139          */
140         public function getMappedStatements()
141         {
142                 return $this->_mappedStatements;
143         }
144
145         /**
146          * Gets a MappedStatement by name.
147          * @param string The name of the statement.
148          * @return IMappedStatement The MappedStatement
149          * @throws TSqlMapUndefinedException
150          */
151         public function getMappedStatement($name)
152         {
153                 if($this->_mappedStatements->contains($name) == false)
154                         throw new TSqlMapUndefinedException('sqlmap_contains_no_statement', $name);
155                 return $this->_mappedStatements[$name];
156         }
157
158         /**
159          * Adds a (named) MappedStatement.
160          * @param string The key name
161          * @param IMappedStatement The statement to add
162          * @throws TSqlMapDuplicateException
163          */
164         public function addMappedStatement(IMappedStatement $statement)
165         {
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);
170         }
171
172         /**
173          * @return TMap result maps collection.
174          */
175         public function getResultMaps()
176         {
177                 return $this->_resultMaps;
178         }
179
180         /**
181          * Gets a named result map
182          * @param string result name.
183          * @return TResultMap the result map.
184          * @throws TSqlMapUndefinedException
185          */
186         public function getResultMap($name)
187         {
188                 if($this->_resultMaps->contains($name) == false)
189                         throw new TSqlMapUndefinedException('sqlmap_contains_no_result_map', $name);
190                 return $this->_resultMaps[$name];
191         }
192
193         /**
194          * @param TResultMap add a new result map to this SQLMap
195          * @throws TSqlMapDuplicateException
196          */
197         public function addResultMap(TResultMap $result)
198         {
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);
203         }
204
205         /**
206          * @return TMap parameter maps collection.
207          */
208         public function getParameterMaps()
209         {
210                 return $this->_parameterMaps;
211         }
212
213         /**
214          * @param string parameter map ID name.
215          * @return TParameterMap the parameter with given ID.
216          * @throws TSqlMapUndefinedException
217          */
218         public function getParameterMap($name)
219         {
220                 if($this->_parameterMaps->contains($name) == false)
221                         throw new TSqlMapUndefinedException('sqlmap_contains_no_parameter_map', $name);
222                 return $this->_parameterMaps[$name];
223         }
224
225         /**
226          * @param TParameterMap add a new parameter map to this SQLMap.
227          * @throws TSqlMapDuplicateException
228          */
229         public function addParameterMap(TParameterMap $parameter)
230         {
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);
235         }
236
237         /**
238          * Adds a named cache.
239          * @param TSqlMapCacheModel the cache to add.
240          * @throws TSqlMapConfigurationException
241          */
242         public function addCacheModel(TSqlMapCacheModel $cacheModel)
243         {
244                 if($this->_cacheModels->contains($cacheModel->getID()))
245                         throw new TSqlMapConfigurationException('sqlmap_cache_model_already_exists', $cacheModel->getID());
246                 else
247                         $this->_cacheModels->add($cacheModel->getID(), $cacheModel);
248         }
249
250         /**
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
255          */
256         public function getCacheModel($name)
257         {
258                 if(!$this->_cacheModels->contains($name))
259                         throw new TSqlMapConfigurationException('sqlmap_unable_to_find_cache_model', $name);
260                 return $this->_cacheModels[$name];
261         }
262
263         /**
264          * Flushes all cached objects that belong to this SqlMap
265          */
266         public function flushCacheModels()
267         {
268                 foreach($this->_cacheModels as $cache)
269                         $cache->flush();
270         }
271 }
272