blob: ee486fe8d1f403fd3f8160b4954990d92f708d60 (
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
|
<?php
/**
* TMasterSlaveDataSourceConfig class file.
*
* @author Yves Berkholz <godzilla80[at]gmx[dot]net>
* @link http://www.pradosoft.com/
* @copyright Copyright © 2005-2010 PradoSoft
* @license http://www.pradosoft.com/license/
* @version $Id$
* @package System.Testing.Data.Distributed.MasterSlave
*/
Prado::using('System.Testing.Data.Distributed.TDistributedDataSourceConfig');
Prado::using('System.Testing.Data.Distributed.MasterSlave.TMasterSlaveDbConnection');
/**
* TMasterSlaveDataSourceConfig module class provides <module> configuration for database connections in master/slave senario.
*
* IMPORTANT!!!
* BETA Version - Use with care and NOT in production environment (only tested with MySql)
*
* Example usage: mysql connection
* <code>
* <modules>
* <module id="db1" class="System.Testing.Data.Distributed.MasterSlave.TMasterSlaveDataSourceConfig"
* ConnectionClass="System.Testing.Data.Distributed.MasterSlave.TMasterSlaveDbConnection"
* DistributedConnectionClass="System.Testing.Data.Distributed.MasterSlave.TSlaveDbConnection"
* DbConnection.StatementAnalyserClass="System.Testing.Data.Analysis.TSimpleDbStatementAnalysis">
* <database ConnectionString="mysql:host=127.0.0.1;port=3306;dbname=mydatabase" Username="dbuser" Password="dbpass" />
* <slave ConnectionString="mysql:host=127.0.0.1;port=3307;dbname=mydatabase" Username="dbuser" Password="dbpass" Weight="3" />
* <slave ConnectionString="mysql:host=127.0.0.1;port=3308;dbname=mydatabase" Username="dbuser" Password="dbpass" Weight="2" />
* <slave ConnectionString="mysql:host=127.0.0.1;port=3309;dbname=mydatabase" Username="dbuser" Password="dbpass" Weight="5" />
* </module>
* </modules>
* </code>
*
* @author Yves Berkholz <godzilla80[at]gmx[dot]net>
* @version $Id$
* @package System.Testing.Data.Distributed.MasterSlave
* @since 4.0
*/
class TMasterSlaveDataSourceConfig extends TDistributedDataSourceConfig
{
/**
* @var boolean
*/
private $_bMasterInitialized = false;
/**
* @var boolean
*/
private $_bSlaveInitialized = false;
/**
* Constructor
*/
public function __construct()
{
$this->setConnectionClass('System.Testing.Data.Distributed.MasterSlave.TMasterSlaveDbConnection');
$this->setDistributedConnectionClass('System.Testing.Data.Distributed.MasterSlave.TSlaveDbConnection');
}
/**
* Initalize the database connection properties from attributes in slave tag.
* @param TXmlDocument xml configuration.
*/
protected function initChildConnectionData($xml)
{
parent::initChildConnectionData($xml, 'slave');
}
/**
* @return IMasterSlaveDbConnection
*/
public function getDbConnection() {
$id = $this->getID();
static $result = array();
if(!isset($result[$id]))
$result[$id] = parent::getDbConnection();
if(!$this->bInitialized)
return $result[$id];
if($this->_bMasterInitialized)
return $result[$id];
$this->_bMasterInitialized = true;
if(!$result[$id] instanceof IMasterSlaveDbConnection)
return $result[$id];
$slave = parent::getDistributedDbConnection();
if($slave instanceof ISlaveDbConnection && $slave->getMasterConnection()===null)
$slave->setMasterConnection($result[$id]);
if($result[$id]->getSlaveConnection()===null)
$result[$id]->setSlaveConnection($slave);
return $result[$id];
}
/**
* @return ISlaveDbConnection
*/
public function getDistributedDbConnection() {
$id = $this->getID();
static $result = array();
if(!isset($result[$id]))
$result[$id] = parent::getDistributedDbConnection();
if(!$this->bInitialized)
return $result[$id];
if($this->_bSlaveInitialized)
return $result[$id];
$this->_bSlaveInitialized = true;
if(!$result[$id] instanceof ISlaveDbConnection)
return $result[$id];
$master = parent::getDbConnection();
if($master instanceof IMasterSlaveDbConnection && ($master->getSlaveConnection()===null))
$master->setSlaveConnection($result[$id]);
if($result[$id]->getMasterConnection()===null)
$result[$id]->setMasterConnection($master);
return $result[$id];
}
/**
* Alias for getDbConnection().
* @return IMasterSlaveDbConnection database connection.
*/
public function getMasterDbConnection()
{
return $this->getDbConnection();
}
/**
* Alias for getDistributedDbConnection().
* @return ISlaveDbConnection database connection.
*/
public function getSlaveDbConnection()
{
return $this->getDistributedDbConnection();
}
}
?>
|