Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Zend\Db\TableGateway hotfix for MasterSlaveFeature #3597

Merged
merged 1 commit into from
Jan 29, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions library/Zend/Db/Adapter/AdapterInterface.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,12 +26,4 @@ public function getDriver();
*/ */
public function getPlatform(); public function getPlatform();


/**
* Create statement
*
* @param string $initialSql
* @param ParameterContainer $initialParameters
* @return Driver\StatementInterface
*/
public function createStatement($initialSql = null, $initialParameters = null);
} }
25 changes: 24 additions & 1 deletion library/Zend/Db/Sql/Sql.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@


class Sql class Sql
{ {
/** @var AdapterInterface */
protected $adapter = null; protected $adapter = null;

/** @var string */
protected $table = null; protected $table = null;

/** @var Platform\Platform */
protected $sqlPlatform = null; protected $sqlPlatform = null;


public function __construct(AdapterInterface $adapter, $table = null, Platform\AbstractPlatform $sqlPlatform = null) public function __construct(AdapterInterface $adapter, $table = null, Platform\AbstractPlatform $sqlPlatform = null)
Expand All @@ -28,6 +33,14 @@ public function __construct(AdapterInterface $adapter, $table = null, Platform\A
$this->sqlPlatform = ($sqlPlatform) ?: new Platform\Platform($adapter); $this->sqlPlatform = ($sqlPlatform) ?: new Platform\Platform($adapter);
} }


/**
* @return null|\Zend\Db\Adapter\AdapterInterface
*/
public function getAdapter()
{
return $this->adapter;
}

public function hasTable() public function hasTable()
{ {
return ($this->table != null); return ($this->table != null);
Expand All @@ -48,6 +61,11 @@ public function getTable()
return $this->table; return $this->table;
} }


public function getSqlPlatform()
{
return $this->sqlPlatform;
}

public function select($table = null) public function select($table = null)
{ {
if ($this->table !== null && $table !== null) { if ($this->table !== null && $table !== null) {
Expand Down Expand Up @@ -92,9 +110,14 @@ public function delete($table = null)
return new Delete(($table) ?: $this->table); return new Delete(($table) ?: $this->table);
} }


/**
* @param PreparableSqlInterface $sqlObject
* @param StatementInterface|null $statement
* @return StatementInterface
*/
public function prepareStatementForSqlObject(PreparableSqlInterface $sqlObject, StatementInterface $statement = null) public function prepareStatementForSqlObject(PreparableSqlInterface $sqlObject, StatementInterface $statement = null)
{ {
$statement = ($statement) ?: $this->adapter->createStatement(); $statement = ($statement) ?: $this->adapter->getDriver()->createStatement();


if ($this->sqlPlatform) { if ($this->sqlPlatform) {
$this->sqlPlatform->setSubject($sqlObject); $this->sqlPlatform->setSubject($sqlObject);
Expand Down
12 changes: 6 additions & 6 deletions library/Zend/Db/TableGateway/AbstractTableGateway.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


namespace Zend\Db\TableGateway; namespace Zend\Db\TableGateway;


use Zend\Db\Adapter\Adapter; use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\ResultSet\ResultSet; use Zend\Db\ResultSet\ResultSet;
use Zend\Db\ResultSet\ResultSetInterface; use Zend\Db\ResultSet\ResultSetInterface;
use Zend\Db\Sql\Delete; use Zend\Db\Sql\Delete;
Expand All @@ -22,7 +22,7 @@


/** /**
* *
* @property Adapter $adapter * @property AdapterInterface $adapter
* @property int $lastInsertValue * @property int $lastInsertValue
* @property string $table * @property string $table
*/ */
Expand All @@ -35,7 +35,7 @@ abstract class AbstractTableGateway implements TableGatewayInterface
protected $isInitialized = false; protected $isInitialized = false;


/** /**
* @var Adapter * @var AdapterInterface
*/ */
protected $adapter = null; protected $adapter = null;


Expand Down Expand Up @@ -64,7 +64,6 @@ abstract class AbstractTableGateway implements TableGatewayInterface
*/ */
protected $sql = null; protected $sql = null;



/** /**
* *
* @var integer * @var integer
Expand Down Expand Up @@ -98,7 +97,7 @@ public function initialize()
$this->featureSet->setTableGateway($this); $this->featureSet->setTableGateway($this);
$this->featureSet->apply('preInitialize', array()); $this->featureSet->apply('preInitialize', array());


if (!$this->adapter instanceof Adapter) { if (!$this->adapter instanceof AdapterInterface) {
throw new Exception\RuntimeException('This table does not have an Adapter setup'); throw new Exception\RuntimeException('This table does not have an Adapter setup');
} }


Expand Down Expand Up @@ -132,7 +131,7 @@ public function getTable()
/** /**
* Get adapter * Get adapter
* *
* @return Adapter * @return AdapterInterface
*/ */
public function getAdapter() public function getAdapter()
{ {
Expand Down Expand Up @@ -485,4 +484,5 @@ public function __clone()
$this->table = clone $this->table; $this->table = clone $this->table;
} }
} }

} }
48 changes: 39 additions & 9 deletions library/Zend/Db/TableGateway/Feature/MasterSlaveFeature.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,37 +9,66 @@


namespace Zend\Db\TableGateway\Feature; namespace Zend\Db\TableGateway\Feature;


use Zend\Db\Adapter\Adapter; use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\Sql\Sql;


class MasterSlaveFeature extends AbstractFeature class MasterSlaveFeature extends AbstractFeature
{ {


/** /**
* @var Adapter * @var AdapterInterface
*/ */
protected $masterAdapter = null; protected $slaveAdapter = null;


/** /**
* @var Adapter * @var Sql
*/ */
protected $slaveAdapter = null; protected $masterSql = null;

/**
* @var Sql
*/
protected $slaveSql = null;


/** /**
* Constructor * Constructor
* *
* @param Adapter $slaveAdapter * @param Adapter $slaveAdapter
*/ */
public function __construct(Adapter $slaveAdapter) public function __construct(AdapterInterface $slaveAdapter, Sql $slaveSql = null)
{ {
$this->slaveAdapter = $slaveAdapter; $this->slaveAdapter = $slaveAdapter;
if ($slaveSql) {
$this->slaveSql = $slaveSql;
}
}

public function getSlaveAdapter()
{
return $this->slaveAdapter;
}

/**
* @return Sql
*/
public function getSlaveSql()
{
return $this->slaveSql;
} }


/** /**
* after initialization, retrieve the original adapter as "master" * after initialization, retrieve the original adapter as "master"
*/ */
public function postInitialize() public function postInitialize()
{ {
$this->masterAdapter = $this->tableGateway->adapter; $this->masterSql = $this->tableGateway->sql;
if ($this->slaveSql == null) {
$this->slaveSql = new Sql(
$this->slaveAdapter,
$this->tableGateway->sql->getTable(),
$this->tableGateway->sql->getSqlPlatform()
);
}
} }


/** /**
Expand All @@ -48,7 +77,7 @@ public function postInitialize()
*/ */
public function preSelect() public function preSelect()
{ {
$this->tableGateway->adapter = $this->slaveAdapter; $this->tableGateway->sql = $this->slaveSql;
} }


/** /**
Expand All @@ -57,6 +86,7 @@ public function preSelect()
*/ */
public function postSelect() public function postSelect()
{ {
$this->tableGateway->adapter = $this->masterAdapter; $this->tableGateway->sql = $this->masterSql;
} }

} }
4 changes: 2 additions & 2 deletions library/Zend/Db/TableGateway/TableGateway.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


namespace Zend\Db\TableGateway; namespace Zend\Db\TableGateway;


use Zend\Db\Adapter\Adapter; use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\ResultSet\ResultSet; use Zend\Db\ResultSet\ResultSet;
use Zend\Db\ResultSet\ResultSetInterface; use Zend\Db\ResultSet\ResultSetInterface;
use Zend\Db\Sql\Sql; use Zend\Db\Sql\Sql;
Expand All @@ -28,7 +28,7 @@ class TableGateway extends AbstractTableGateway
* @param Sql $sql * @param Sql $sql
* @throws Exception\InvalidArgumentException * @throws Exception\InvalidArgumentException
*/ */
public function __construct($table, Adapter $adapter, $features = null, ResultSetInterface $resultSetPrototype = null, Sql $sql = null) public function __construct($table, AdapterInterface $adapter, $features = null, ResultSetInterface $resultSetPrototype = null, Sql $sql = null)
{ {
// table // table
if (!(is_string($table) || $table instanceof TableIdentifier)) { if (!(is_string($table) || $table instanceof TableIdentifier)) {
Expand Down
96 changes: 96 additions & 0 deletions tests/ZendTest/Db/TableGateway/Feature/MasterSlaveFeatureTest.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace ZendTest\Db\TableGateway\Feature;

use Zend\Db\TableGateway\Feature\MasterSlaveFeature;

class MasterSlaveFeatureTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Zend\Db\Adapter\AdapterInterface
*/
protected $mockMasterAdapter, $mockSlaveAdapter;

/**
* @var MasterSlaveFeature
*/
protected $feature = null;

/** @var \Zend\Db\TableGateway\TableGateway */
protected $table = null;

public function setup()
{
$this->mockMasterAdapter = $this->getMock(
'Zend\Db\Adapter\AdapterInterface',
array('getDriver', 'getPlatform')
);

$mockStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface');
$mockDriver = $this->getMock('Zend\Db\Adapter\Driver\DriverInterface');
$mockDriver->expects($this->any())->method('createStatement')->will($this->returnValue(
$mockStatement
));
$this->mockMasterAdapter->expects($this->any())->method('getDriver')->will($this->returnValue($mockDriver));
$this->mockMasterAdapter->expects($this->any())->method('getPlatform')->will($this->returnValue(new \Zend\Db\Adapter\Platform\Sql92()));

$this->mockSlaveAdapter = $this->getMock(
'Zend\Db\Adapter\AdapterInterface',
array('getDriver', 'getPlatform')
);

$mockStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface');
$mockDriver = $this->getMock('Zend\Db\Adapter\Driver\DriverInterface');
$mockDriver->expects($this->any())->method('createStatement')->will($this->returnValue(
$mockStatement
));
$this->mockSlaveAdapter->expects($this->any())->method('getDriver')->will($this->returnValue($mockDriver));
$this->mockSlaveAdapter->expects($this->any())->method('getPlatform')->will($this->returnValue(new \Zend\Db\Adapter\Platform\Sql92()));

$this->feature = new MasterSlaveFeature($this->mockSlaveAdapter);
}

public function testPostInitialize()
{
/** @var $table \Zend\Db\TableGateway\TableGateway */
$this->getMockForAbstractClass(
'Zend\Db\TableGateway\TableGateway',
array('foo', $this->mockMasterAdapter, $this->feature)
);
// postInitialize is run
$this->assertSame($this->mockSlaveAdapter, $this->feature->getSlaveSql()->getAdapter());
}

public function testPreSelect()
{
$table = $this->getMockForAbstractClass(
'Zend\Db\TableGateway\TableGateway',
array('foo', $this->mockMasterAdapter, $this->feature)
);

$this->mockSlaveAdapter->getDriver()->createStatement()
->expects($this->once())->method('execute')->will($this->returnValue(
$this->getMock('Zend\Db\ResultSet\ResultSet')
));
$table->select('foo = bar');
}

public function testPostSelect()
{
$table = $this->getMockForAbstractClass(
'Zend\Db\TableGateway\TableGateway',
array('foo', $this->mockMasterAdapter, $this->feature)
);
$this->mockSlaveAdapter->getDriver()->createStatement()
->expects($this->once())->method('execute')->will($this->returnValue(
$this->getMock('Zend\Db\ResultSet\ResultSet')
));

$masterSql = $table->getSql();
$table->select('foo = bar');

// test that the sql object is restored
$this->assertSame($masterSql, $table->getSql());
}

}