Passed
Branch php-cs-fixer (b9836a)
by Fabio
15:02
created

TSqlMapManager::setDbConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * TSqlMapManager class file.
4
 *
5
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6
 * @link https://github.com/pradosoft/prado
7
 * @copyright Copyright &copy; 2005-2016 The PRADO Group
8
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
9
 * @package Prado\Data\SqlMap
10
 */
11
12
namespace Prado\Data\SqlMap;
13
14
use Prado\Caching\TChainedCacheDependency;
15
use Prado\Collections\TMap;
16
use Prado\Data\SqlMap\Configuration\TParameterMap;
17
use Prado\Data\SqlMap\Configuration\TResultMap;
18
use Prado\Data\SqlMap\Configuration\TSqlMapCacheModel;
19
use Prado\Data\SqlMap\Configuration\TSqlMapXmlConfiguration;
20
use Prado\Data\SqlMap\DataMapper\TSqlMapConfigurationException;
21
use Prado\Data\SqlMap\DataMapper\TSqlMapDuplicateException;
22
use Prado\Data\SqlMap\DataMapper\TSqlMapTypeHandlerRegistry;
23
use Prado\Data\SqlMap\DataMapper\TSqlMapUndefinedException;
24
use Prado\Data\SqlMap\Statements\IMappedStatement;
25
use Prado\Prado;
26
27
28
29
/**
30
 * TSqlMapManager class holds the sqlmap configuation result maps, statements
31
 * parameter maps and a type handler factory.
32
 *
33
 * Use {@link SqlMapGateway getSqlMapGateway()} property to obtain the gateway
34
 * instance used for querying statements defined in the SqlMap configuration files.
35
 *
36
 * <code>
37
 * $conn = new TDbConnection($dsn,$dbuser,$dbpass);
38
 * $manager = new TSqlMapManager($conn);
39
 * $manager->configureXml('mydb-sqlmap.xml');
40
 * $sqlmap = $manager->getSqlMapGateway();
41
 * $result = $sqlmap->queryForObject('Products');
42
 * </code>
43
 *
44
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
45
 * @package Prado\Data\SqlMap
46
 * @since 3.1
47
 */
48
class TSqlMapManager extends \Prado\TComponent
49
{
50
	private $_mappedStatements;
51
	private $_resultMaps;
52
	private $_parameterMaps;
53
	private $_typeHandlers;
54
	private $_cacheModels;
55
56
	private $_connection;
57
	private $_gateway;
58
	private $_cacheDependencies;
59
60
	/**
61
	 * Constructor, create a new SqlMap manager.
62
	 * @param TDbConnection database connection
0 ignored issues
show
Bug introduced by
The type Prado\Data\SqlMap\database was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
63
	 * @param string configuration file.
64
	 */
65
	public function __construct($connection = null)
66
	{
67
		$this->_connection = $connection;
68
69
		$this->_mappedStatements = new TMap;
70
		$this->_resultMaps = new TMap;
71
		$this->_parameterMaps = new TMap;
72
		$this->_cacheModels = new TMap;
73
	}
74
75
	/**
76
	 * @param TDbConnection default database connection
0 ignored issues
show
Bug introduced by
The type Prado\Data\SqlMap\default was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
77
	 */
78
	public function setDbConnection($conn)
79
	{
80
		$this->_connection = $conn;
81
	}
82
83
	/**
84
	 * @return TDbConnection default database connection
0 ignored issues
show
Bug introduced by
The type Prado\Data\SqlMap\TDbConnection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
85
	 */
86
	public function getDbConnection()
87
	{
88
		return $this->_connection;
89
	}
90
91
	/**
92
	 * @return TTypeHandlerFactory The TypeHandlerFactory
0 ignored issues
show
Bug introduced by
The type Prado\Data\SqlMap\TTypeHandlerFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
93
	 */
94
	public function getTypeHandlers()
95
	{
96
		if($this->_typeHandlers === null)
97
			$this->_typeHandlers = new TSqlMapTypeHandlerRegistry();
98
		return $this->_typeHandlers;
99
	}
100
101
	/**
102
	 * @return TSqlMapGateway SqlMap gateway.
103
	 */
104
	public function getSqlmapGateway()
105
	{
106
		if($this->_gateway === null)
107
			$this->_gateway = $this->createSqlMapGateway();
108
		return $this->_gateway;
109
	}
110
111
	/**
112
	 * Loads and parses the SqlMap configuration file.
113
	 * @param string xml configuration file.
0 ignored issues
show
Bug introduced by
The type Prado\Data\SqlMap\xml was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
114
	 */
115
	public function configureXml($file)
116
	{
117
		$config = new TSqlMapXmlConfiguration($this);
118
		$config->configure($file);
119
	}
120
121
	/**
122
	 * @return TChainedCacheDependency
123
	 * @since 3.1.5
124
	 */
125
	public function getCacheDependencies()
126
	{
127
		if($this->_cacheDependencies === null)
128
			$this->_cacheDependencies = new TChainedCacheDependency();
129
130
		return $this->_cacheDependencies;
131
	}
132
133
	/**
134
	 * Configures the current TSqlMapManager using the given xml configuration file
135
	 * defined in {@link ConfigFile setConfigFile()}.
136
	 * @return TSqlMapGateway create and configure a new TSqlMapGateway.
137
	 */
138
	protected function createSqlMapGateway()
139
	{
140
		return new TSqlMapGateway($this);
141
	}
142
143
	/**
144
	 * @return TMap mapped statements collection.
145
	 */
146
	public function getMappedStatements()
147
	{
148
		return $this->_mappedStatements;
149
	}
150
151
	/**
152
	 * Gets a MappedStatement by name.
153
	 * @param string The name of the statement.
0 ignored issues
show
Bug introduced by
The type Prado\Data\SqlMap\The was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
154
	 * @return IMappedStatement The MappedStatement
155
	 * @throws TSqlMapUndefinedException
156
	 */
157
	public function getMappedStatement($name)
158
	{
159
		if($this->_mappedStatements->contains($name) == false)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
160
			throw new TSqlMapUndefinedException('sqlmap_contains_no_statement', $name);
161
		return $this->_mappedStatements[$name];
162
	}
163
164
	/**
165
	 * Adds a (named) MappedStatement.
166
	 * @param string The key name
167
	 * @param IMappedStatement The statement to add
168
	 * @throws TSqlMapDuplicateException
169
	 */
170
	public function addMappedStatement(IMappedStatement $statement)
171
	{
172
		$key = $statement->getID();
173
		if($this->_mappedStatements->contains($key) == true)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
174
			throw new TSqlMapDuplicateException('sqlmap_already_contains_statement', $key);
175
		$this->_mappedStatements->add($key, $statement);
176
	}
177
178
	/**
179
	 * @return TMap result maps collection.
180
	 */
181
	public function getResultMaps()
182
	{
183
		return $this->_resultMaps;
184
	}
185
186
	/**
187
	 * Gets a named result map
0 ignored issues
show
Bug introduced by
The type Prado\Data\SqlMap\result was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
188
	 * @param string result name.
189
	 * @return TResultMap the result map.
190
	 * @throws TSqlMapUndefinedException
191
	 */
192
	public function getResultMap($name)
193
	{
194
		if($this->_resultMaps->contains($name) == false)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
195
			throw new TSqlMapUndefinedException('sqlmap_contains_no_result_map', $name);
196
		return $this->_resultMaps[$name];
197
	}
198
199
	/**
200
	 * @param TResultMap add a new result map to this SQLMap
0 ignored issues
show
Bug introduced by
The type Prado\Data\SqlMap\add was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
201
	 * @throws TSqlMapDuplicateException
202
	 */
203
	public function addResultMap(TResultMap $result)
204
	{
205
		$key = $result->getID();
206
		if($this->_resultMaps->contains($key) == true)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
207
			throw new TSqlMapDuplicateException('sqlmap_already_contains_result_map', $key);
208
		$this->_resultMaps->add($key, $result);
209
	}
210
211
	/**
212
	 * @return TMap parameter maps collection.
213
	 */
214
	public function getParameterMaps()
215
	{
216
		return $this->_parameterMaps;
217
	}
218
219
	/**
220
	 * @param string parameter map ID name.
0 ignored issues
show
Bug introduced by
The type Prado\Data\SqlMap\parameter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
221
	 * @return TParameterMap the parameter with given ID.
222
	 * @throws TSqlMapUndefinedException
223
	 */
224
	public function getParameterMap($name)
225
	{
226
		if($this->_parameterMaps->contains($name) == false)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
227
			throw new TSqlMapUndefinedException('sqlmap_contains_no_parameter_map', $name);
228
		return $this->_parameterMaps[$name];
229
	}
230
231
	/**
232
	 * @param TParameterMap add a new parameter map to this SQLMap.
233
	 * @throws TSqlMapDuplicateException
234
	 */
235
	public function addParameterMap(TParameterMap $parameter)
236
	{
237
		$key = $parameter->getID();
238
		if($this->_parameterMaps->contains($key) == true)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
239
			throw new TSqlMapDuplicateException('sqlmap_already_contains_parameter_map', $key);
240
		$this->_parameterMaps->add($key, $parameter);
241
	}
242
243
	/**
244
	 * Adds a named cache.
245
	 * @param TSqlMapCacheModel the cache to add.
0 ignored issues
show
Bug introduced by
The type Prado\Data\SqlMap\the was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
246
	 * @throws TSqlMapConfigurationException
247
	 */
248
	public function addCacheModel(TSqlMapCacheModel $cacheModel)
249
	{
250
		if($this->_cacheModels->contains($cacheModel->getID()))
251
			throw new TSqlMapConfigurationException('sqlmap_cache_model_already_exists', $cacheModel->getID());
252
		else
253
			$this->_cacheModels->add($cacheModel->getID(), $cacheModel);
254
	}
255
256
	/**
257
	 * Gets a cache by name
258
	 * @param string the name of the cache to get.
259
	 * @return TSqlMapCacheModel the cache object.
260
	 * @throws TSqlMapConfigurationException
261
	 */
262
	public function getCacheModel($name)
263
	{
264
		if(!$this->_cacheModels->contains($name))
265
			throw new TSqlMapConfigurationException('sqlmap_unable_to_find_cache_model', $name);
266
		return $this->_cacheModels[$name];
267
	}
268
269
	/**
270
	 * Flushes all cached objects that belong to this SqlMap
271
	 */
272
	public function flushCacheModels()
273
	{
274
		foreach($this->_cacheModels as $cache)
275
			$cache->flush();
276
	}
277
}
278
279