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

class TSQLMap extends TModule
{
	private $_SQLMapLibrary='';
	private $_configFile;
	private $_sqlmap;
	private $_provider;

	/**
	 * File extension of external configuration file
	 */
	const CONFIG_FILE_EXT='.config';

	public function getSQLMapLibrary()
	{
		if(strlen($this->_SQLMapLibrary) < 1)
			return dirname(__FILE__).'/SQLMap';
		else
			return $this->_SQLMapLibrary;
	}

	public function setSQLMapLibrary($path)
	{
		$this->_SQLMapLibrary = Prado::getPathOfNamespace($path);
	}

	/**
	 * @return string external configuration file. Defaults to null.
	 */
	public function getConfigFile()
	{
		return $this->_configFile;
	}

	public function init($xml)
	{
		$config = $xml->getElementByTagName('provider');
		$class = $config->getAttribute('class');
		$provider = Prado::createComponent($class);
		$datasource = $config->getElementByTagName('datasource');
		$properties = $datasource->getAttributes();
		foreach($properties as $name=>$value)
			$provider->setSubproperty($name,$value);
		$this->_provider = $provider;
	}

	/**
	 * @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)
	{
		if(($this->_configFile=Prado::getPathOfNamespace(
					$value,self::CONFIG_FILE_EXT))===null)
			throw new TConfigurationException('sqlmap_configfile_invalid',$value);
	}

	protected function configure($configFile)
	{
		include($this->getSQLMapLibrary().'/TSqlMapper.php');
		$builder = new TDomSqlMapBuilder();
		$this->_sqlmap = $builder->configure($configFile);
		if(!is_null($this->_provider))
			$this->_sqlmap->setDataProvider($this->_provider);
	}

	public function getClient()
	{
		if(is_null($this->_sqlmap))
			$this->configure($this->getConfigFile());
		return $this->_sqlmap;
	}
}

?>