123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- <?php
-
-
- namespace Doctrine\DBAL\Connections;
-
-
- use Doctrine\DBAL\Connection,
- Doctrine\DBAL\Driver,
- Doctrine\DBAL\Configuration,
- Doctrine\Common\EventManager,
- Doctrine\DBAL\Event\ConnectionEventArgs,
- Doctrine\DBAL\Events;
-
-
- class MasterSlaveConnection extends Connection
- {
-
-
- protected $connections = array('master' => null, 'slave' => null);
-
-
-
- protected $keepSlave = false;
-
-
-
- public function __construct(array $params, Driver $driver, Configuration $config = null, EventManager $eventManager = null)
- {
- if ( !isset($params['slaves']) || !isset($params['master']) ) {
- throw new \InvalidArgumentException('master or slaves configuration missing');
- }
- if ( count($params['slaves']) == 0 ) {
- throw new \InvalidArgumentException('You have to configure at least one slaves.');
- }
-
- $params['master']['driver'] = $params['driver'];
- foreach ($params['slaves'] as $slaveKey => $slave) {
- $params['slaves'][$slaveKey]['driver'] = $params['driver'];
- }
-
- $this->keepSlave = isset($params['keepSlave']) ? (bool)$params['keepSlave'] : false;
-
- parent::__construct($params, $driver, $config, $eventManager);
- }
-
-
-
- public function isConnectedToMaster()
- {
- return $this->_conn !== null && $this->_conn === $this->connections['master'];
- }
-
-
-
- public function connect($connectionName = null)
- {
- $requestedConnectionChange = ($connectionName !== null);
- $connectionName = $connectionName ?: 'slave';
-
- if ( $connectionName !== 'slave' && $connectionName !== 'master' ) {
- throw new \InvalidArgumentException("Invalid option to connect(), only master or slave allowed.");
- }
-
-
-
-
- if ($this->_conn && !$requestedConnectionChange) {
- return false;
- }
-
- $forceMasterAsSlave = false;
-
- if ($this->getTransactionNestingLevel() > 0) {
- $connectionName = 'master';
- $forceMasterAsSlave = true;
- }
-
- if ($this->connections[$connectionName]) {
- if ($forceMasterAsSlave) {
- $this->connections['slave'] = $this->_conn = $this->connections['master'];
- } else {
- $this->_conn = $this->connections[$connectionName];
- }
- return false;
- }
-
- if ($connectionName === 'master') {
-
- if ($this->connections['slave'] && ! $this->keepSlave) {
- unset($this->connections['slave']);
- }
-
- $this->connections['master'] = $this->_conn = $this->connectTo($connectionName);
-
- if ( ! $this->keepSlave) {
- $this->connections['slave'] = $this->connections['master'];
- }
- } else {
- $this->connections['slave'] = $this->_conn = $this->connectTo($connectionName);
- }
-
- if ($this->_eventManager->hasListeners(Events::postConnect)) {
- $eventArgs = new ConnectionEventArgs($this);
- $this->_eventManager->dispatchEvent(Events::postConnect, $eventArgs);
- }
-
- return true;
- }
-
-
-
- protected function connectTo($connectionName)
- {
- $params = $this->getParams();
-
- $driverOptions = isset($params['driverOptions']) ? $params['driverOptions'] : array();
-
- $connectionParams = $this->chooseConnectionConfiguration($connectionName, $params);
-
- $user = isset($connectionParams['user']) ? $connectionParams['user'] : null;
- $password = isset($connectionParams['password']) ? $connectionParams['password'] : null;
-
- return $this->_driver->connect($connectionParams, $user, $password, $driverOptions);
- }
-
- protected function chooseConnectionConfiguration($connectionName, $params)
- {
- if ($connectionName === 'master') {
- return $params['master'];
- }
-
- return $params['slaves'][array_rand($params['slaves'])];
- }
-
-
-
- public function executeUpdate($query, array $params = array(), array $types = array())
- {
- $this->connect('master');
- return parent::executeUpdate($query, $params, $types);
- }
-
-
-
- public function beginTransaction()
- {
- $this->connect('master');
- return parent::beginTransaction();
- }
-
-
-
- public function commit()
- {
- $this->connect('master');
- return parent::commit();
- }
-
-
-
- public function rollBack()
- {
- $this->connect('master');
- return parent::rollBack();
- }
-
-
-
- public function delete($tableName, array $identifier)
- {
- $this->connect('master');
- return parent::delete($tableName, $identifier);
- }
-
-
-
- public function update($tableName, array $data, array $identifier, array $types = array())
- {
- $this->connect('master');
- return parent::update($tableName, $data, $identifier, $types);
- }
-
-
-
- public function insert($tableName, array $data, array $types = array())
- {
- $this->connect('master');
- return parent::insert($tableName, $data, $types);
- }
-
-
-
- public function exec($statement)
- {
- $this->connect('master');
- return parent::exec($statement);
- }
-
-
-
- public function createSavepoint($savepoint)
- {
- $this->connect('master');
-
- return parent::createSavepoint($savepoint);
- }
-
-
-
- public function releaseSavepoint($savepoint)
- {
- $this->connect('master');
-
- return parent::releaseSavepoint($savepoint);
- }
-
-
-
- public function rollbackSavepoint($savepoint)
- {
- $this->connect('master');
-
- return parent::rollbackSavepoint($savepoint);
- }
-
- public function query()
- {
- $this->connect('master');
-
- $args = func_get_args();
-
- $logger = $this->getConfiguration()->getSQLLogger();
- if ($logger) {
- $logger->startQuery($args[0]);
- }
-
- $statement = call_user_func_array(array($this->_conn, 'query'), $args);
-
- if ($logger) {
- $logger->stopQuery();
- }
-
- return $statement;
- }
-
- public function prepare($statement)
- {
- $this->connect('master');
-
- return parent::prepare($statement);
- }
- }
|