Commit 1f7ef808 authored by Benjamin Eberlei's avatar Benjamin Eberlei

Merge branch 'MasterSlave'

parents a9eb7b48 e7d6fc2c
This diff is collapsed.
......@@ -56,37 +56,40 @@ final class DriverManager
* driver connection.
*
* $params must contain at least one of the following.
*
*
* Either 'driver' with one of the following values:
* pdo_mysql
* pdo_sqlite
* pdo_pgsql
* pdo_oracle
* pdo_sqlsrv
*
*
* OR 'driverClass' that contains the full class name (with namespace) of the
* driver class to instantiate.
*
*
* Other (optional) parameters:
*
*
* <b>user (string)</b>:
* The username to use when connecting.
*
* The username to use when connecting.
*
* <b>password (string)</b>:
* The password to use when connecting.
*
*
* <b>driverOptions (array)</b>:
* Any additional driver-specific options for the driver. These are just passed
* through to the driver.
*
*
* <b>pdo</b>:
* You can pass an existing PDO instance through this parameter. The PDO
* instance will be wrapped in a Doctrine\DBAL\Connection.
*
*
* <b>wrapperClass</b>:
* You may specify a custom wrapper class through the 'wrapperClass'
* parameter but this class MUST inherit from Doctrine\DBAL\Connection.
*
*
* <b>driverClass</b>:
* The driver class to use.
*
* @param array $params The parameters.
* @param Doctrine\DBAL\Configuration The configuration to use.
* @param Doctrine\Common\EventManager The event manager to use.
......@@ -104,7 +107,7 @@ final class DriverManager
if ( ! $eventManager) {
$eventManager = new EventManager();
}
// check for existing pdo object
if (isset($params['pdo']) && ! $params['pdo'] instanceof \PDO) {
throw DBALException::invalidPdoInstance();
......@@ -119,9 +122,9 @@ final class DriverManager
} else {
$className = self::$_driverMap[$params['driver']];
}
$driver = new $className();
$wrapperClass = 'Doctrine\DBAL\Connection';
if (isset($params['wrapperClass'])) {
if (is_subclass_of($params['wrapperClass'], $wrapperClass)) {
......@@ -130,7 +133,7 @@ final class DriverManager
throw DBALException::invalidWrapperClass($params['wrapperClass']);
}
}
return new $wrapperClass($params, $driver, $config, $eventManager);
}
......@@ -140,16 +143,16 @@ final class DriverManager
* @param array $params
*/
private static function _checkParams(array $params)
{
{
// check existance of mandatory parameters
// driver
if ( ! isset($params['driver']) && ! isset($params['driverClass'])) {
throw DBALException::driverRequired();
}
// check validity of parameters
// driver
if ( isset($params['driver']) && ! isset(self::$_driverMap[$params['driver']])) {
throw DBALException::unknownDriver($params['driver'], array_keys(self::$_driverMap));
......@@ -159,4 +162,4 @@ final class DriverManager
throw DBALException::invalidDriverClass($params['driverClass']);
}
}
}
\ No newline at end of file
}
......@@ -2,8 +2,6 @@
namespace Doctrine\Tests\DBAL\Functional;
require_once __DIR__ . '/../../TestInit.php';
class LoggingTest extends \Doctrine\Tests\DbalFunctionalTestCase
{
public function testLogExecuteQuery()
......
<?php
namespace Doctrine\Tests\DBAL\Functional;
use Doctrine\Tests\DbalFunctionalTestCase;
use Doctrine\DBAL\DriverManager;
/**
* @group DBAL-20
*/
class MasterSlaveConnectionTest extends DbalFunctionalTestCase
{
public function setUp()
{
parent::setUp();
if ($this->_conn->getDatabasePlatform()->getName() == "sqlite") {
$this->markTestSkipped('Test does not work on sqlite.');
}
try {
/* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
$table = new \Doctrine\DBAL\Schema\Table("master_slave_table");
$table->addColumn('test_int', 'integer');
$table->setPrimaryKey(array('test_int'));
$sm = $this->_conn->getSchemaManager();
$sm->createTable($table);
$this->_conn->insert('master_slave_table', array('test_int' => 1));
} catch(\Exception $e) {
}
}
public function createMasterSlaveConnection()
{
$params = $this->_conn->getParams();
$params['master'] = $params;
$params['slaves'] = array($params, $params);
$params['wrapperClass'] = 'Doctrine\DBAL\Connections\MasterSlaveConnection';
return DriverManager::getConnection($params);
}
public function testMasterOnConnect()
{
$conn = $this->createMasterSlaveConnection();
$this->assertFalse($conn->isConnectedToMaster());
$conn->connect('slave');
$this->assertFalse($conn->isConnectedToMaster());
$conn->connect('master');
$this->assertTrue($conn->isConnectedToMaster());
}
public function testNoMasterOnExecuteQuery()
{
$conn = $this->createMasterSlaveConnection();
$sql = "SELECT count(*) as num FROM master_slave_table";
$data = $conn->fetchAll($sql);
$data[0] = array_change_key_case($data[0], CASE_LOWER);
$this->assertEquals(1, $data[0]['num']);
$this->assertFalse($conn->isConnectedToMaster());
}
public function testMasterOnWriteOperation()
{
$conn = $this->createMasterSlaveConnection();
$conn->insert('master_slave_table', array('test_int' => 30));
$this->assertTrue($conn->isConnectedToMaster());
$sql = "SELECT count(*) as num FROM master_slave_table";
$data = $conn->fetchAll($sql);
$data[0] = array_change_key_case($data[0], CASE_LOWER);
$this->assertEquals(2, $data[0]['num']);
$this->assertTrue($conn->isConnectedToMaster());
}
}
<?php
namespace Doctrine\Tests\DBAL\Functional;
class MasterSlaveTest extends \Doctrine\Tests\DbalFunctionalTestCase
{
public function test()
{
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment