summaryrefslogtreecommitdiff
path: root/framework/Testing/Data/Distributed/TDistributedDataSourceConfig.php
blob: b8c4e5a96f9db458f9cc6517716565b9077dfb79 (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
<?php
/**
 * TDistributedDataSourceConfig class file.
 *
 * @author Yves Berkholz <godzilla80[at]gmx[dot]net>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005-2010 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @version $Id$
 * @package System.Testing.Data.Distributed
 */

	Prado::using('System.Data.TDataSourceConfig');
	Prado::using('System.Testing.Data.Distributed.TDistributedDbConnection');

	/**
	 * IDistributedDataSourceConfig module interface provides <module> configuration for database connections.
	 *
	 * @author Yves Berkholz <godzilla80[at]gmx[dot]net>
	 * @version $Id$
	 * @package System.Testing.Data.Distributed
	 * @since 4.0
	 */
	interface IDistributedDataSourceConfig /*extends IDataSourceConfig*/ {
		/**
		 * @return string Database connection class name to be created for child connection.
		 */
		public function getDistributedConnectionClass();

		/**
		 * @param string Database connection class name to be created for child connection.
		 */
		public function setDistributedConnectionClass($value);
	}

	/**
	 * TDistributedDataSourceConfig module class provides <module> configuration for database connections.
	 *
	 * @author Yves Berkholz <godzilla80[at]gmx[dot]net>
	 * @version $Id$
	 * @package System.Testing.Data.Distributed
	 * @since 4.0
	 */
	class TDistributedDataSourceConfig extends TDataSourceConfig implements IDistributedDataSourceConfig
	{
		/**
		 * @var array
		 */
		private $_childConnectionAttributes = array();

		/**
		 * @var string
		 */
		private $_connDistributedClass = 'System.Data.TDbConnection';

		/**
		 * @var IDistributedDbConnection
		 */
		private $_connDistributed = null;

		/**
		 * @var boolean
		 */
		protected $bInitialized = false;

		/**
		 * @var boolean
		 */
		private $_hasDistributedConnectionData = false;

		/**
		 * Initalize the database connection properties from attributes in <database> tag.
		 * @param TXmlDocument xml configuration.
		 */
		public function init($xml)
		{
			parent::init($xml);
			$this->initChildConnectionData($xml);
			$this->bInitialized = true;
		}

		/**
		 * Initalize the database connection properties from attributes in $tagName tag.
		 * @param TXmlDocument xml configuration.
		 * @param string Tagnames to parse. Defaults to 'child'
		 */
		protected function initChildConnectionData($xml, $tagName='child')
		{
			$c = 0;
			foreach($xml->getElementsByTagName($tagName) as $item)
			{
				++$c;
				$this->_childConnectionAttributes[] = $item->getAttributes();
			}

			if($c===0)
				throw new TConfigurationException('distributeddatasource_child_required', get_class($this), $tagName);
		}

		/**
		 * @return string Database connection class name to be created for child connection.
		 */
		public function getDistributedConnectionClass()
		{
			return $this->_connDistributedClass;
		}

		/**
		 * @param string Database connection class name to be created for child connection.
		 */
		public function setDistributedConnectionClass($value)
		{
			$this->_connDistributedClass=$value;
		}

		/**
		 * @return IDistributedDbConnection
		 */
		public function getDistributedDbConnection()
		{
			$this->_hasDistributedConnectionData = false;
			if($this->_connDistributed===null)
				$this->_connDistributed = Prado::createComponent($this->getDistributedConnectionClass());

			if($this->_hasDistributedConnectionData)
				return $this->_connDistributed;

			$attribs = $this->getDistributedDbConnectionAttributes();

			if($attribs===null)
				return $this->_connDistributed;

			foreach($attribs as $name => $value)
				$this->_connDistributed->setSubproperty($name, $value);

			$this->_hasDistributedConnectionData = true;

			return $this->_connDistributed;
		}

		/**
		 * @return TMap
		 */
		protected function getDistributedDbConnectionAttributes()
		{
			$index = 0;
			$c = count($this->_childConnectionAttributes);

			if($c > 1) {
				$aSrc = array();
				$aTmp = array();

				foreach($this->_childConnectionAttributes as $k => $item)
				{
					$weight = 1;
					if( isset($item['Weight']) )
						$weight = $item['Weight'];
					$aSrc[$k] = $weight;
				}

				asort($aSrc);

				foreach($aSrc as $idx => $weight)
					$aTmp = array_merge($aTmp, array_pad(array(), $weight*5, $idx));

				$min	= 0;
				$max	= count($aTmp)-1;
				$factor = array_sum($aSrc) / count($aSrc);
				$wrand	= round($min + (pow(rand(0, $max) / $max, $factor) * ($max - $min)));
				$index	= $aTmp[$wrand];
			}

			$result = $this->_childConnectionAttributes[$index];

			if( isset($result['Weight']) )
				unset($result['Weight']);

			return $result;
		}
	}
?>