summaryrefslogtreecommitdiff
path: root/framework/DataAccess/TSQLMap.php
blob: 27973047a2c2479f913b14b712c1afe37758d7d9 (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
<?php

class TSQLMap extends TModule
{
	private $_configFile;
	private $_sqlmap;
	private $_enableCache=false;

	/**
	 * File extension of external configuration file
	 */
	const CONFIG_FILE_EXT='.xml';
	
	/**
	 * Saves the current sqlmap instance to cache.
	 * @return boolean true if sqlmap was cached, false otherwise.
	 */
	protected function cacheSqlMap()
	{
		if($this->getEnableConfigCache())
		{
			$cache = $this->getApplication()->getCache();
			if(!is_null($cache))
				return $cache->add($this->getID(), $this->_sqlmap);
		}
		return false;
	}

	/**
	 * Loads sqlmap data mapper instance from cache.
	 * @return boolean true if load was successful, false otherwise.
	 */
	protected function loadSqlMapCache()
	{
		if($this->getEnableConfigCache())
		{
			$cache = $this->getApplication()->getCache();			
			Prado::using('System.DataAccess.SQLMap.TSqlMapper');		
			if(!is_null($cache))
				$this->_sqlmap = $cache->get($this->getID());
			return $this->_sqlmap instanceof TSqlMapper;
		}
		return false;
	}
	
	/**
	 * @return string sqlmap configuration file.
	 */
	public function getConfigFile()
	{
		return $this->_configFile;
	}	

	/**
	 * @param string external configuration file in namespace format. The file
	 * must be suffixed with '.xml'.
	 * @throws TInvalidDataValueException if the file is invalid.
	 */
	public function setConfigFile($value)
	{
		$file = Prado::getPathOfNamespace($value,self::CONFIG_FILE_EXT);
		if(is_null($file))
			throw new TConfigurationException('sqlmap_configfile_invalid',$value);
		else
			$this->_configFile = $file;
	}
	
	/**
	 * Set true to cache sqlmap instances. 
	 * @param boolean true to cache sqlmap instance.
	 */
	public function setEnableConfigCache($value)
	{
		$this->_enableCache = TPropertyValue::ensureBoolean($value, false);
	}
	
	/**
	 * @return boolean true if configuration should be cached, false otherwise.
	 */
	public function getEnableConfigCache()
	{
		return $this->_enableCache;
	}

	/**
	 * Configure the data mapper using sqlmap configuration file.
	 * If cache is enabled, the data mapper instance is cached.
	 * @param string sqlmap configuration file.
	 * @return TSqlMapper sqlmap instance.
	 */
	protected function configure($configFile)
	{
		Prado::using('System.DataAccess.SQLMap.TSqlMapper');
		$builder = new TDomSqlMapBuilder();
		$this->_sqlmap = $builder->configure($configFile);
		$this->cacheSqlMap();
		return $this->_sqlmap;			
	}

	/**
	 * Initialize the sqlmap if necessary, returns the TSqlMapper instance.
	 * @return TSqlMapper data mapper for this module.
	 */
	public function getClient()
	{
		if(is_null($this->_sqlmap) && !$this->loadSqlMapCache())
			$this->configure($this->getConfigFile());
		return $this->_sqlmap;
	}
	
	/**
	 * This magic method allows this TSQLMap module to be treated like
	 * TSqlMapper instance.
	 * @param string calling method name
	 * @param array calling parameters
	 * @return mixed data obtained from TSqlMapper method call.
	 */
	public function __call($method, $params)
	{
		$client = $this->getClient();
		return call_user_func_array(array($client,$method),$params);
	}	
}

?>