| 1 |
lars |
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 © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id: TSqlMapManager.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
10 |
* @package System.Data.SqlMap
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
Prado::using('System.Data.SqlMap.TSqlMapGateway');
|
|
|
14 |
Prado::using('System.Data.SqlMap.DataMapper.TSqlMapException');
|
|
|
15 |
Prado::using('System.Data.SqlMap.DataMapper.TSqlMapTypeHandlerRegistry');
|
|
|
16 |
Prado::using('System.Data.SqlMap.DataMapper.TSqlMapCache');
|
|
|
17 |
Prado::using('System.Data.SqlMap.Configuration.TSqlMapStatement');
|
|
|
18 |
Prado::using('System.Data.SqlMap.Configuration.*');
|
|
|
19 |
Prado::using('System.Data.SqlMap.DataMapper.*');
|
|
|
20 |
Prado::using('System.Data.SqlMap.Statements.*');
|
|
|
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 |
* @version $Id: TSqlMapManager.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
40 |
* @package System.Data.SqlMap
|
|
|
41 |
* @since 3.1
|
|
|
42 |
*/
|
|
|
43 |
class TSqlMapManager extends TComponent
|
|
|
44 |
{
|
|
|
45 |
private $_mappedStatements;
|
|
|
46 |
private $_resultMaps;
|
|
|
47 |
private $_parameterMaps;
|
|
|
48 |
private $_typeHandlers;
|
|
|
49 |
private $_cacheModels;
|
|
|
50 |
|
|
|
51 |
private $_connection;
|
|
|
52 |
private $_gateway;
|
|
|
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 |
* Configures the current TSqlMapManager using the given xml configuration file
|
|
|
117 |
* defined in {@link ConfigFile setConfigFile()}.
|
|
|
118 |
* @return TSqlMapGateway create and configure a new TSqlMapGateway.
|
|
|
119 |
*/
|
|
|
120 |
protected function createSqlMapGateway()
|
|
|
121 |
{
|
|
|
122 |
return new TSqlMapGateway($this);
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
/**
|
|
|
126 |
* @return TMap mapped statements collection.
|
|
|
127 |
*/
|
|
|
128 |
public function getMappedStatements()
|
|
|
129 |
{
|
|
|
130 |
return $this->_mappedStatements;
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
/**
|
|
|
134 |
* Gets a MappedStatement by name.
|
|
|
135 |
* @param string The name of the statement.
|
|
|
136 |
* @return IMappedStatement The MappedStatement
|
|
|
137 |
* @throws TSqlMapUndefinedException
|
|
|
138 |
*/
|
|
|
139 |
public function getMappedStatement($name)
|
|
|
140 |
{
|
|
|
141 |
if($this->_mappedStatements->contains($name) == false)
|
|
|
142 |
throw new TSqlMapUndefinedException('sqlmap_contains_no_statement', $name);
|
|
|
143 |
return $this->_mappedStatements[$name];
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
/**
|
|
|
147 |
* Adds a (named) MappedStatement.
|
|
|
148 |
* @param string The key name
|
|
|
149 |
* @param IMappedStatement The statement to add
|
|
|
150 |
* @throws TSqlMapDuplicateException
|
|
|
151 |
*/
|
|
|
152 |
public function addMappedStatement(IMappedStatement $statement)
|
|
|
153 |
{
|
|
|
154 |
$key = $statement->getID();
|
|
|
155 |
if($this->_mappedStatements->contains($key) == true)
|
|
|
156 |
throw new TSqlMapDuplicateException('sqlmap_already_contains_statement', $key);
|
|
|
157 |
$this->_mappedStatements->add($key, $statement);
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
/**
|
|
|
161 |
* @return TMap result maps collection.
|
|
|
162 |
*/
|
|
|
163 |
public function getResultMaps()
|
|
|
164 |
{
|
|
|
165 |
return $this->_resultMaps;
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
/**
|
|
|
169 |
* Gets a named result map
|
|
|
170 |
* @param string result name.
|
|
|
171 |
* @return TResultMap the result map.
|
|
|
172 |
* @throws TSqlMapUndefinedException
|
|
|
173 |
*/
|
|
|
174 |
public function getResultMap($name)
|
|
|
175 |
{
|
|
|
176 |
if($this->_resultMaps->contains($name) == false)
|
|
|
177 |
throw new TSqlMapUndefinedException('sqlmap_contains_no_result_map', $name);
|
|
|
178 |
return $this->_resultMaps[$name];
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
/**
|
|
|
182 |
* @param TResultMap add a new result map to this SQLMap
|
|
|
183 |
* @throws TSqlMapDuplicateException
|
|
|
184 |
*/
|
|
|
185 |
public function addResultMap(TResultMap $result)
|
|
|
186 |
{
|
|
|
187 |
$key = $result->getID();
|
|
|
188 |
if($this->_resultMaps->contains($key) == true)
|
|
|
189 |
throw new TSqlMapDuplicateException('sqlmap_already_contains_result_map', $key);
|
|
|
190 |
$this->_resultMaps->add($key, $result);
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
/**
|
|
|
194 |
* @return TMap parameter maps collection.
|
|
|
195 |
*/
|
|
|
196 |
public function getParameterMaps()
|
|
|
197 |
{
|
|
|
198 |
return $this->_parameterMaps;
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
/**
|
|
|
202 |
* @param string parameter map ID name.
|
|
|
203 |
* @return TParameterMap the parameter with given ID.
|
|
|
204 |
* @throws TSqlMapUndefinedException
|
|
|
205 |
*/
|
|
|
206 |
public function getParameterMap($name)
|
|
|
207 |
{
|
|
|
208 |
if($this->_parameterMaps->contains($name) == false)
|
|
|
209 |
throw new TSqlMapUndefinedException('sqlmap_contains_no_parameter_map', $name);
|
|
|
210 |
return $this->_parameterMaps[$name];
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
/**
|
|
|
214 |
* @param TParameterMap add a new parameter map to this SQLMap.
|
|
|
215 |
* @throws TSqlMapDuplicateException
|
|
|
216 |
*/
|
|
|
217 |
public function addParameterMap(TParameterMap $parameter)
|
|
|
218 |
{
|
|
|
219 |
$key = $parameter->getID();
|
|
|
220 |
if($this->_parameterMaps->contains($key) == true)
|
|
|
221 |
throw new TSqlMapDuplicateException('sqlmap_already_contains_parameter_map', $key);
|
|
|
222 |
$this->_parameterMaps->add($key, $parameter);
|
|
|
223 |
}
|
|
|
224 |
|
|
|
225 |
/**
|
|
|
226 |
* Adds a named cache.
|
|
|
227 |
* @param TSqlMapCacheModel the cache to add.
|
|
|
228 |
* @throws TSqlMapConfigurationException
|
|
|
229 |
*/
|
|
|
230 |
public function addCacheModel(TSqlMapCacheModel $cacheModel)
|
|
|
231 |
{
|
|
|
232 |
if($this->_cacheModels->contains($cacheModel->getID()))
|
|
|
233 |
throw new TSqlMapConfigurationException('sqlmap_cache_model_already_exists', $cacheModel->getID());
|
|
|
234 |
else
|
|
|
235 |
$this->_cacheModels->add($cacheModel->getID(), $cacheModel);
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
/**
|
|
|
239 |
* Gets a cache by name
|
|
|
240 |
* @param string the name of the cache to get.
|
|
|
241 |
* @return TSqlMapCacheModel the cache object.
|
|
|
242 |
* @throws TSqlMapConfigurationException
|
|
|
243 |
*/
|
|
|
244 |
public function getCacheModel($name)
|
|
|
245 |
{
|
|
|
246 |
if(!$this->_cacheModels->contains($name))
|
|
|
247 |
throw new TSqlMapConfigurationException('sqlmap_unable_to_find_cache_model', $name);
|
|
|
248 |
return $this->_cacheModels[$name];
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
/**
|
|
|
252 |
* Flushes all cached objects that belong to this SqlMap
|
|
|
253 |
*/
|
|
|
254 |
public function flushCacheModels()
|
|
|
255 |
{
|
|
|
256 |
foreach($this->_cacheModels as $cache)
|
|
|
257 |
$cache->flush();
|
|
|
258 |
}
|
|
|
259 |
}
|
|
|
260 |
|