Skip to content

Loading…

Zend\Db\TableGateway hotfix for MasterSlaveFeature #3597

Merged
merged 1 commit into from

2 participants

@ralphschindler
Zend Framework member

Fixed MasterSlave feature, as reported in #3175

@ralphschindler ralphschindler Zend\Db\TableGateway
- Fixed MasterSlave feature
736bd64
@weierophinney weierophinney commented on the diff
library/Zend/Db/Adapter/AdapterInterface.php
@@ -26,12 +26,4 @@ public function getDriver();
*/
public function getPlatform();
- /**
- * Create statement
- *
- * @param string $initialSql
- * @param ParameterContainer $initialParameters
- * @return Driver\StatementInterface
- */
- public function createStatement($initialSql = null, $initialParameters = null);
@weierophinney Zend Framework member

Per @ralphschindler - As this is a driver level facility, it should not be in the adapter interface. Additionally, removing a method from an interface is always backwards compatible, so long as implementations do not rely on the existence of the method. Finally, as this only exists on the develop branch, not yet released, we are completely safe removing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@weierophinney weierophinney merged commit 736bd64 into zendframework:develop

1 check failed

Details default The Travis build could not complete due to an error
@ghost Unknown pushed a commit that referenced this pull request
@weierophinney weierophinney Merge branch 'hotfix/3597' into develop
Close #3597
827cabb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 29, 2013
  1. @ralphschindler

    Zend\Db\TableGateway

    ralphschindler committed
    - Fixed MasterSlave feature
View
8 library/Zend/Db/Adapter/AdapterInterface.php
@@ -26,12 +26,4 @@ public function getDriver();
*/
public function getPlatform();
- /**
- * Create statement
- *
- * @param string $initialSql
- * @param ParameterContainer $initialParameters
- * @return Driver\StatementInterface
- */
- public function createStatement($initialSql = null, $initialParameters = null);
@weierophinney Zend Framework member

Per @ralphschindler - As this is a driver level facility, it should not be in the adapter interface. Additionally, removing a method from an interface is always backwards compatible, so long as implementations do not rely on the existence of the method. Finally, as this only exists on the develop branch, not yet released, we are completely safe removing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
}
View
25 library/Zend/Db/Sql/Sql.php
@@ -15,8 +15,13 @@
class Sql
{
+ /** @var AdapterInterface */
protected $adapter = null;
+
+ /** @var string */
protected $table = null;
+
+ /** @var Platform\Platform */
protected $sqlPlatform = null;
public function __construct(AdapterInterface $adapter, $table = null, Platform\AbstractPlatform $sqlPlatform = null)
@@ -28,6 +33,14 @@ public function __construct(AdapterInterface $adapter, $table = null, Platform\A
$this->sqlPlatform = ($sqlPlatform) ?: new Platform\Platform($adapter);
}
+ /**
+ * @return null|\Zend\Db\Adapter\AdapterInterface
+ */
+ public function getAdapter()
+ {
+ return $this->adapter;
+ }
+
public function hasTable()
{
return ($this->table != null);
@@ -48,6 +61,11 @@ public function getTable()
return $this->table;
}
+ public function getSqlPlatform()
+ {
+ return $this->sqlPlatform;
+ }
+
public function select($table = null)
{
if ($this->table !== null && $table !== null) {
@@ -92,9 +110,14 @@ public function delete($table = null)
return new Delete(($table) ?: $this->table);
}
+ /**
+ * @param PreparableSqlInterface $sqlObject
+ * @param StatementInterface|null $statement
+ * @return StatementInterface
+ */
public function prepareStatementForSqlObject(PreparableSqlInterface $sqlObject, StatementInterface $statement = null)
{
- $statement = ($statement) ?: $this->adapter->createStatement();
+ $statement = ($statement) ?: $this->adapter->getDriver()->createStatement();
if ($this->sqlPlatform) {
$this->sqlPlatform->setSubject($sqlObject);
View
12 library/Zend/Db/TableGateway/AbstractTableGateway.php
@@ -9,7 +9,7 @@
namespace Zend\Db\TableGateway;
-use Zend\Db\Adapter\Adapter;
+use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\ResultSet\ResultSetInterface;
use Zend\Db\Sql\Delete;
@@ -22,7 +22,7 @@
/**
*
- * @property Adapter $adapter
+ * @property AdapterInterface $adapter
* @property int $lastInsertValue
* @property string $table
*/
@@ -35,7 +35,7 @@
protected $isInitialized = false;
/**
- * @var Adapter
+ * @var AdapterInterface
*/
protected $adapter = null;
@@ -64,7 +64,6 @@
*/
protected $sql = null;
-
/**
*
* @var integer
@@ -98,7 +97,7 @@ public function initialize()
$this->featureSet->setTableGateway($this);
$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');
}
@@ -132,7 +131,7 @@ public function getTable()
/**
* Get adapter
*
- * @return Adapter
+ * @return AdapterInterface
*/
public function getAdapter()
{
@@ -485,4 +484,5 @@ public function __clone()
$this->table = clone $this->table;
}
}
+
}
View
48 library/Zend/Db/TableGateway/Feature/MasterSlaveFeature.php
@@ -9,29 +9,51 @@
namespace Zend\Db\TableGateway\Feature;
-use Zend\Db\Adapter\Adapter;
+use Zend\Db\Adapter\AdapterInterface;
+use Zend\Db\Sql\Sql;
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
*
* @param Adapter $slaveAdapter
*/
- public function __construct(Adapter $slaveAdapter)
+ public function __construct(AdapterInterface $slaveAdapter, Sql $slaveSql = null)
{
$this->slaveAdapter = $slaveAdapter;
+ if ($slaveSql) {
+ $this->slaveSql = $slaveSql;
+ }
+ }
+
+ public function getSlaveAdapter()
+ {
+ return $this->slaveAdapter;
+ }
+
+ /**
+ * @return Sql
+ */
+ public function getSlaveSql()
+ {
+ return $this->slaveSql;
}
/**
@@ -39,7 +61,14 @@ public function __construct(Adapter $slaveAdapter)
*/
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()
+ );
+ }
}
/**
@@ -48,7 +77,7 @@ public function postInitialize()
*/
public function preSelect()
{
- $this->tableGateway->adapter = $this->slaveAdapter;
+ $this->tableGateway->sql = $this->slaveSql;
}
/**
@@ -57,6 +86,7 @@ public function preSelect()
*/
public function postSelect()
{
- $this->tableGateway->adapter = $this->masterAdapter;
+ $this->tableGateway->sql = $this->masterSql;
}
+
}
View
4 library/Zend/Db/TableGateway/TableGateway.php
@@ -9,7 +9,7 @@
namespace Zend\Db\TableGateway;
-use Zend\Db\Adapter\Adapter;
+use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\ResultSet\ResultSetInterface;
use Zend\Db\Sql\Sql;
@@ -28,7 +28,7 @@ class TableGateway extends AbstractTableGateway
* @param Sql $sql
* @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
if (!(is_string($table) || $table instanceof TableIdentifier)) {
View
96 tests/ZendTest/Db/TableGateway/Feature/MasterSlaveFeatureTest.php
@@ -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());
+ }
+
+}
Something went wrong with that request. Please try again.