1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Connection; |
4
|
|
|
|
5
|
|
|
use Bdf\Prime\ConnectionManager; |
|
|
|
|
6
|
|
|
use Doctrine\Common\EventManager; |
7
|
|
|
use Doctrine\DBAL\Cache\QueryCacheProfile; |
8
|
|
|
use Doctrine\DBAL\Configuration; |
9
|
|
|
use Doctrine\DBAL\Driver; |
10
|
|
|
use LogicException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* MasterSlaveConnection |
14
|
|
|
* |
15
|
|
|
* The master / slave connection is a connection to a master server with a connection wrapper to a slave server. |
16
|
|
|
* Only method SimpleConnection#executeQuery will be redirect to the salve. |
17
|
|
|
* |
18
|
|
|
* SimpleConnection#quote also use slave. |
19
|
|
|
* |
20
|
|
|
* Becareful those methods are used on master: |
21
|
|
|
* |
22
|
|
|
* SimpleConnection#prepare |
23
|
|
|
* SimpleConnection#query |
24
|
|
|
* |
25
|
|
|
* @package Bdf\Prime\Connection |
|
|
|
|
26
|
|
|
*/ |
27
|
|
|
class MasterSlaveConnection extends SimpleConnection implements SubConnectionManagerInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* The connection specifically for read operations |
31
|
|
|
* |
32
|
|
|
* This connection is used only for the method SimpleConnection#executeQuery |
|
|
|
|
33
|
|
|
* |
34
|
|
|
* @var SimpleConnection |
35
|
|
|
*/ |
36
|
|
|
private $readConnection; |
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Force the read on master |
40
|
|
|
* |
41
|
|
|
* @var boolean |
|
|
|
|
42
|
|
|
*/ |
43
|
|
|
private $force = false; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Initializes a new instance of the Connection class. |
47
|
|
|
* |
48
|
|
|
* Here's a read connection configuration |
|
|
|
|
49
|
|
|
* |
50
|
|
|
* @example |
51
|
|
|
* |
52
|
|
|
* $conn = DriverManager::getConnection([ |
53
|
|
|
* 'driver' => 'pdo_mysql', |
54
|
|
|
* 'user' => '', |
55
|
|
|
* 'password' => '', |
56
|
|
|
* 'host' => '', |
57
|
|
|
* 'dbname' => '', |
58
|
|
|
* 'read' => [ |
59
|
|
|
* 'user' => 'slave', |
60
|
|
|
* 'password' => '', |
61
|
|
|
* 'host' => '', |
62
|
|
|
* 'dbname' => '', |
63
|
|
|
* ] |
64
|
|
|
* ]); |
65
|
|
|
* |
66
|
|
|
* @param array $params The connection parameters. |
|
|
|
|
67
|
|
|
* @param \Doctrine\DBAL\Driver $driver The driver to use. |
68
|
|
|
* @param \Doctrine\DBAL\Configuration|null $config The configuration, optional. |
69
|
|
|
* @param \Doctrine\Common\EventManager|null $eventManager The event manager, optional. |
70
|
|
|
*/ |
|
|
|
|
71
|
8 |
|
public function __construct(array $params, Driver $driver, Configuration $config = null, EventManager $eventManager = null) |
72
|
|
|
{ |
73
|
8 |
|
if (!isset($params['read'])) { |
74
|
|
|
throw new LogicException('Master/slave connection needs readable connection in parameters'); |
75
|
|
|
} |
76
|
|
|
|
77
|
8 |
|
$this->readConnection = $params['read']; |
78
|
|
|
|
79
|
8 |
|
parent::__construct($params, $driver, $config, $eventManager); |
80
|
8 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the read connection |
84
|
|
|
* |
85
|
|
|
* @return SimpleConnection |
86
|
|
|
*/ |
87
|
8 |
|
public function getReadConnection() |
88
|
|
|
{ |
89
|
8 |
|
return $this->readConnection; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
|
|
|
|
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
|
|
|
|
95
|
3 |
|
public function getConnection($name) |
96
|
|
|
{ |
97
|
3 |
|
if ($name === 'read') { |
98
|
2 |
|
return $this->readConnection; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// Force the read on master if it is the awaiting connection |
102
|
3 |
|
if ($name === 'master') { |
103
|
2 |
|
return $this->force(); |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
throw new LogicException('The sub connection "'.$name.'" is unknown in the master / slave connection'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Force next read on master connection once |
111
|
|
|
* This flag will change after the execution of method executeQuery |
|
|
|
|
112
|
|
|
* |
113
|
|
|
* @return $this |
114
|
|
|
*/ |
115
|
3 |
|
public function force() |
116
|
|
|
{ |
117
|
3 |
|
$this->force = true; |
118
|
|
|
|
119
|
3 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
|
|
|
|
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
8 |
|
public function executeQuery($query, array $params = [], $types = [], QueryCacheProfile $qcp = null) |
126
|
|
|
{ |
127
|
8 |
|
if ($this->getTransactionNestingLevel() <= 0 && $this->force !== true) { |
128
|
8 |
|
return $this->readConnection->executeQuery($query, $params, $types, $qcp); |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
$this->force = false; |
132
|
|
|
|
133
|
1 |
|
return parent::executeQuery($query, $params, $types, $qcp); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
|
|
|
|
137
|
|
|
* {@inheritdoc} |
138
|
|
|
*/ |
139
|
1 |
|
public function quote($input, $type = null) |
140
|
|
|
{ |
141
|
1 |
|
return $this->readConnection->quote($input, $type); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
|
|
public function close() |
148
|
|
|
{ |
149
|
|
|
parent::close(); |
150
|
|
|
|
151
|
|
|
$this->readConnection->close(); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|