Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TDataSourceConfig class file.
4
 *
5
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6
 * @link http://www.pradosoft.com/
7
 * @copyright Copyright &copy; 2005-2008 PradoSoft
8
 * @license http://www.pradosoft.com/license/
9
 * @version $Id: TDataSourceConfig.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Data
11
 */
12
 
13
Prado::using('System.Data.TDbConnection');
14
 
15
/**
16
 * TDataSourceConfig module class provides <module> configuration for database connections.
17
 *
18
 * Example usage: mysql connection
19
 * <code>
20
 * <modules>
21
 * 	<module id="db1">
22
 * 		<database ConnectionString="mysqli:host=localhost;dbname=test"
23
 * 			username="dbuser" password="dbpass" />
24
 * 	</module>
25
 * </modules>
26
 * </code>
27
 *
28
 * Usage in php:
29
 * <code>
30
 * class Home extends TPage
31
 * {
32
 * 		function onLoad($param)
33
 * 		{
34
 * 			$db = $this->Application->Modules['db1']->DbConnection;
35
 * 			$db->createCommand('...'); //...
36
 * 		}
37
 * }
38
 * </code>
39
 *
40
 * The properties of <connection> are those of the class TDbConnection.
41
 * Set {@link setConnectionClass} attribute for a custom database connection class
42
 * that extends the TDbConnection class.
43
 *
44
 * @author Wei Zhuo <weizho[at]gmail[dot]com>
45
 * @version $Id: TDataSourceConfig.php 2541 2008-10-21 15:05:13Z qiang.xue $
46
 * @package System.Data
47
 * @since 3.1
48
 */
49
class TDataSourceConfig extends TModule
50
{
51
	private $_connID='';
52
	private $_conn;
53
	private $_connClass='System.Data.TDbConnection';
54
 
55
	/**
56
	 * Initalize the database connection properties from attributes in <database> tag.
57
	 * @param TXmlDocument xml configuration.
58
	 */
59
	public function init($xml)
60
	{
61
		if($prop=$xml->getElementByTagName('database'))
62
		{
63
			$db=$this->getDbConnection();
64
			foreach($prop->getAttributes() as $name=>$value)
65
				$db->setSubproperty($name,$value);
66
		}
67
	}
68
 
69
	/**
70
	 * The module ID of another TDataSourceConfig. The {@link getDbConnection DbConnection}
71
	 * property of this configuration will equal to {@link getDbConnection DbConnection}
72
	 * of the given TDataSourceConfig module.
73
	 * @param string module ID.
74
	 */
75
	public function setConnectionID($value)
76
	{
77
		$this->_connID=$value;
78
	}
79
 
80
	/**
81
	 * @return string connection module ID.
82
	 */
83
	public function getConnectionID()
84
	{
85
		return $this->_connID;
86
	}
87
 
88
	/**
89
	 * Gets the TDbConnection from another module if {@link setConnectionID ConnectionID}
90
	 * is supplied and valid. Otherwise, a connection of type given by
91
	 * {@link setConnectionClass ConnectionClass} is created.
92
	 * @return TDbConnection database connection.
93
	 */
94
	public function getDbConnection()
95
	{
96
		if($this->_conn===null)
97
		{
98
			if($this->_connID!=='')
99
				$this->_conn = $this->findConnectionByID($this->getConnectionID());
100
			else
101
				$this->_conn = Prado::createComponent($this->getConnectionClass());
102
		}
103
		return $this->_conn;
104
	}
105
 
106
	/**
107
	 * Alias for getDbConnection().
108
	 * @return TDbConnection database connection.
109
	 */
110
	public function getDatabase()
111
	{
112
		return $this->getDbConnection();
113
	}
114
 
115
	/**
116
	 * @param string Database connection class name to be created.
117
	 */
118
	public function getConnectionClass()
119
	{
120
		return $this->_connClass;
121
	}
122
 
123
	/**
124
	 * The database connection class name to be created when {@link getDbConnection}
125
	 * method is called <b>and</b> {@link setConnectionID ConnectionID} is null. The
126
	 * {@link setConnectionClass ConnectionClass} property must be set before
127
	 * calling {@link getDbConnection} if you wish to create the connection using the
128
	 * given class name.
129
	 * @param string Database connection class name.
130
	 * @throws TConfigurationException when database connection is already established.
131
	 */
132
	public function setConnectionClass($value)
133
	{
134
		if(!is_null($this->_conn))
135
			throw new TConfigurationException('datasource_dbconnection_exists', $value);
136
		$this->_connClass=$value;
137
	}
138
 
139
	/**
140
	 * Finds the database connection instance from the Application modules.
141
	 * @param string Database connection module ID.
142
	 * @return TDbConnection database connection.
143
	 * @throws TConfigurationException when module is not of TDbConnection or TDataSourceConfig.
144
	 */
145
	protected function findConnectionByID($id)
146
	{
147
		$conn = $this->getApplication()->getModule($id);
148
		if($conn instanceof TDbConnection)
149
			return $conn;
150
		else if($conn instanceof TDataSourceConfig)
151
			return $conn->_conn;
152
		else
153
			throw new TConfigurationException('datasource_dbconnection_invalid',$id);
154
	}
155
}
156