summaryrefslogtreecommitdiff
path: root/lib/prado/framework/Data/SqlMap/TSqlMapManager.php
blob: 66b7b66d2f1a84f709dc683ebad0ea856a6b47e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
/**
 * TSqlMapManager class file.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @link https://github.com/pradosoft/prado
 * @copyright Copyright &copy; 2005-2016 The PRADO Group
 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
 * @package System.Data.SqlMap
 */

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.Configuration.TSqlMapStatement');
Prado::using('System.Data.SqlMap.Configuration.*');
Prado::using('System.Data.SqlMap.DataMapper.*');
Prado::using('System.Data.SqlMap.Statements.*');
Prado::using('System.Caching.TCache');


/**
 * TSqlMapManager class holds the sqlmap configuation result maps, statements
 * parameter maps and a type handler factory.
 *
 * Use {@link SqlMapGateway getSqlMapGateway()} property to obtain the gateway
 * instance used for querying statements defined in the SqlMap configuration files.
 *
 * <code>
 * $conn = new TDbConnection($dsn,$dbuser,$dbpass);
 * $manager = new TSqlMapManager($conn);
 * $manager->configureXml('mydb-sqlmap.xml');
 * $sqlmap = $manager->getSqlMapGateway();
 * $result = $sqlmap->queryForObject('Products');
 * </code>
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @package System.Data.SqlMap
 * @since 3.1
 */
class TSqlMapManager extends TComponent
{
	private $_mappedStatements;
	private $_resultMaps;
	private $_parameterMaps;
	private $_typeHandlers;
	private $_cacheModels;

	private $_connection;
	private $_gateway;
	private $_cacheDependencies;

	/**
	 * Constructor, create a new SqlMap manager.
	 * @param TDbConnection database connection
	 * @param string configuration file.
	 */
	public function __construct($connection=null)
	{
		$this->_connection=$connection;

		$this->_mappedStatements=new TMap;
		$this->_resultMaps=new TMap;
		$this->_parameterMaps=new TMap;
		$this->_cacheModels=new TMap;
	}

	/**
	 * @param TDbConnection default database connection
	 */
	public function setDbConnection($conn)
	{
		$this->_connection=$conn;
	}

	/**
	 * @return TDbConnection default database connection
	 */
	public function getDbConnection()
	{
		return $this->_connection;
	}

	/**
	 * @return TTypeHandlerFactory The TypeHandlerFactory
	 */
	public function getTypeHandlers()
	{
		if($this->_typeHandlers===null)
			$this->_typeHandlers= new TSqlMapTypeHandlerRegistry();
		return $this->_typeHandlers;
	}

	/**
	 * @return TSqlMapGateway SqlMap gateway.
	 */
	public function getSqlmapGateway()
	{
		if($this->_gateway===null)
			$this->_gateway=$this->createSqlMapGateway();
		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);
	}

	/**
	 * @return TChainedCacheDependency
	 * @since 3.1.5
	 */
	public function getCacheDependencies()
	{
		if($this->_cacheDependencies === null)
			$this->_cacheDependencies=new TChainedCacheDependency();

		return $this->_cacheDependencies;
	}

	/**
	 * Configures the current TSqlMapManager using the given xml configuration file
	 * defined in {@link ConfigFile setConfigFile()}.
	 * @return TSqlMapGateway create and configure a new TSqlMapGateway.
	 */
	protected function createSqlMapGateway()
	{
		return new TSqlMapGateway($this);
	}

	/**
	 * @return TMap mapped statements collection.
	 */
	public function getMappedStatements()
	{
		return $this->_mappedStatements;
	}

	/**
	 * Gets a MappedStatement by name.
	 * @param string The name of the statement.
	 * @return IMappedStatement The MappedStatement
	 * @throws TSqlMapUndefinedException
	 */
	public function getMappedStatement($name)
	{
		if($this->_mappedStatements->contains($name) == false)
			throw new TSqlMapUndefinedException('sqlmap_contains_no_statement', $name);
		return $this->_mappedStatements[$name];
	}

	/**
	 * Adds a (named) MappedStatement.
	 * @param string The key name
	 * @param IMappedStatement The statement to add
	 * @throws TSqlMapDuplicateException
	 */
	public function addMappedStatement(IMappedStatement $statement)
	{
		$key = $statement->getID();
		if($this->_mappedStatements->contains($key) == true)
			throw new TSqlMapDuplicateException('sqlmap_already_contains_statement', $key);
		$this->_mappedStatements->add($key, $statement);
	}

	/**
	 * @return TMap result maps collection.
	 */
	public function getResultMaps()
	{
		return $this->_resultMaps;
	}

	/**
	 * Gets a named result map
	 * @param string result name.
	 * @return TResultMap the result map.
	 * @throws TSqlMapUndefinedException
	 */
	public function getResultMap($name)
	{
		if($this->_resultMaps->contains($name) == false)
			throw new TSqlMapUndefinedException('sqlmap_contains_no_result_map', $name);
		return $this->_resultMaps[$name];
	}

	/**
	 * @param TResultMap add a new result map to this SQLMap
	 * @throws TSqlMapDuplicateException
	 */
	public function addResultMap(TResultMap $result)
	{
		$key = $result->getID();
		if($this->_resultMaps->contains($key) == true)
			throw new TSqlMapDuplicateException('sqlmap_already_contains_result_map', $key);
		$this->_resultMaps->add($key, $result);
	}

	/**
	 * @return TMap parameter maps collection.
	 */
	public function getParameterMaps()
	{
		return $this->_parameterMaps;
	}

	/**
	 * @param string parameter map ID name.
	 * @return TParameterMap the parameter with given ID.
	 * @throws TSqlMapUndefinedException
	 */
	public function getParameterMap($name)
	{
		if($this->_parameterMaps->contains($name) == false)
			throw new TSqlMapUndefinedException('sqlmap_contains_no_parameter_map', $name);
		return $this->_parameterMaps[$name];
	}

	/**
	 * @param TParameterMap add a new parameter map to this SQLMap.
	 * @throws TSqlMapDuplicateException
	 */
	public function addParameterMap(TParameterMap $parameter)
	{
		$key = $parameter->getID();
		if($this->_parameterMaps->contains($key) == true)
			throw new TSqlMapDuplicateException('sqlmap_already_contains_parameter_map', $key);
		$this->_parameterMaps->add($key, $parameter);
	}

	/**
	 * Adds a named cache.
	 * @param TSqlMapCacheModel the cache to add.
	 * @throws TSqlMapConfigurationException
	 */
	public function addCacheModel(TSqlMapCacheModel $cacheModel)
	{
		if($this->_cacheModels->contains($cacheModel->getID()))
			throw new TSqlMapConfigurationException('sqlmap_cache_model_already_exists', $cacheModel->getID());
		else
			$this->_cacheModels->add($cacheModel->getID(), $cacheModel);
	}

	/**
	 * Gets a cache by name
	 * @param string the name of the cache to get.
	 * @return TSqlMapCacheModel the cache object.
	 * @throws TSqlMapConfigurationException
	 */
	public function getCacheModel($name)
	{
		if(!$this->_cacheModels->contains($name))
			throw new TSqlMapConfigurationException('sqlmap_unable_to_find_cache_model', $name);
		return $this->_cacheModels[$name];
	}

	/**
	 * Flushes all cached objects that belong to this SqlMap
	 */
	public function flushCacheModels()
	{
		foreach($this->_cacheModels as $cache)
			$cache->flush();
	}
}