This repository has been archived by the owner on Dec 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3742 from magento-thunder/MAGETWO-94241
Fixed issues: - MAGETWO-94241: Mysql reconnect does not work
- Loading branch information
Showing
3 changed files
with
192 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,21 +3,20 @@ | |
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Framework\DB\Statement\Pdo; | ||
|
||
use Magento\Framework\DB\Statement\Parameter; | ||
|
||
/** | ||
* Mysql DB Statement | ||
* | ||
* @author Magento Core Team <[email protected]> | ||
*/ | ||
namespace Magento\Framework\DB\Statement\Pdo; | ||
|
||
use Magento\Framework\DB\Statement\Parameter; | ||
|
||
class Mysql extends \Zend_Db_Statement_Pdo | ||
{ | ||
|
||
/** | ||
* Executes statement with binding values to it. | ||
* Allows transferring specific options to DB driver. | ||
* Executes statement with binding values to it. Allows transferring specific options to DB driver. | ||
* | ||
* @param array $params Array of values to bind to parameter placeholders. | ||
* @return bool | ||
|
@@ -61,11 +60,9 @@ public function _executeWithBinding(array $params) | |
$statement->bindParam($paramName, $bindValues[$name], $dataType, $length, $driverOptions); | ||
} | ||
|
||
try { | ||
return $this->tryExecute(function () use ($statement) { | ||
return $statement->execute(); | ||
} catch (\PDOException $e) { | ||
throw new \Zend_Db_Statement_Exception($e->getMessage(), (int)$e->getCode(), $e); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
|
@@ -90,7 +87,29 @@ public function _execute(array $params = null) | |
if ($specialExecute) { | ||
return $this->_executeWithBinding($params); | ||
} else { | ||
return parent::_execute($params); | ||
return $this->tryExecute(function () use ($params) { | ||
return $params !== null ? $this->_stmt->execute($params) : $this->_stmt->execute(); | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Executes query and avoid warnings. | ||
* | ||
* @param callable $callback | ||
* @return bool | ||
* @throws \Zend_Db_Statement_Exception | ||
*/ | ||
private function tryExecute($callback) | ||
{ | ||
$previousLevel = error_reporting(\E_ERROR); // disable warnings for PDO bugs #63812, #74401 | ||
try { | ||
return $callback(); | ||
} catch (\PDOException $e) { | ||
$message = sprintf('%s, query was: %s', $e->getMessage(), $this->_stmt->queryString); | ||
throw new \Zend_Db_Statement_Exception($message, (int)$e->getCode(), $e); | ||
} finally { | ||
error_reporting($previousLevel); | ||
} | ||
} | ||
} |
154 changes: 154 additions & 0 deletions
154
lib/internal/Magento/Framework/DB/Test/Unit/DB/Statement/MysqlTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Framework\DB\Test\Unit\DB\Statement; | ||
|
||
use Magento\Framework\DB\Statement\Parameter; | ||
use Magento\Framework\DB\Statement\Pdo\Mysql; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
class MysqlTest extends TestCase | ||
{ | ||
/** | ||
* @var \Zend_Db_Adapter_Abstract|MockObject | ||
*/ | ||
private $adapterMock; | ||
|
||
/** | ||
* @var \PDO|MockObject | ||
*/ | ||
private $pdoMock; | ||
|
||
/** | ||
* @var \Zend_Db_Profiler|MockObject | ||
*/ | ||
private $zendDbProfilerMock; | ||
|
||
/** | ||
* @var \PDOStatement|MockObject | ||
*/ | ||
private $pdoStatementMock; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function setUp() | ||
{ | ||
$this->adapterMock = $this->getMockForAbstractClass( | ||
\Zend_Db_Adapter_Abstract::class, | ||
[], | ||
'', | ||
false, | ||
true, | ||
true, | ||
['getConnection', 'getProfiler'] | ||
); | ||
$this->pdoMock = $this->createMock(\PDO::class); | ||
$this->adapterMock->expects($this->once()) | ||
->method('getConnection') | ||
->willReturn($this->pdoMock); | ||
$this->zendDbProfilerMock = $this->createMock(\Zend_Db_Profiler::class); | ||
$this->adapterMock->expects($this->once()) | ||
->method('getProfiler') | ||
->willReturn($this->zendDbProfilerMock); | ||
$this->pdoStatementMock = $this->createMock(\PDOStatement::class); | ||
} | ||
|
||
public function testExecuteWithoutParams() | ||
{ | ||
$query = 'SET @a=1;'; | ||
$this->pdoMock->expects($this->once()) | ||
->method('prepare') | ||
->with($query) | ||
->willReturn($this->pdoStatementMock); | ||
$this->pdoStatementMock->expects($this->once()) | ||
->method('execute'); | ||
(new Mysql($this->adapterMock, $query))->_execute(); | ||
} | ||
|
||
public function testExecuteWhenThrowPDOException() | ||
{ | ||
$this->expectException(\Zend_Db_Statement_Exception::class); | ||
$this->expectExceptionMessage('test message, query was:'); | ||
$errorReporting = error_reporting(); | ||
$query = 'SET @a=1;'; | ||
$this->pdoMock->expects($this->once()) | ||
->method('prepare') | ||
->with($query) | ||
->willReturn($this->pdoStatementMock); | ||
$this->pdoStatementMock->expects($this->once()) | ||
->method('execute') | ||
->willThrowException(new \PDOException('test message')); | ||
|
||
$this->assertEquals($errorReporting, error_reporting(), 'Error report level was\'t restored'); | ||
|
||
(new Mysql($this->adapterMock, $query))->_execute(); | ||
} | ||
|
||
public function testExecuteWhenParamsAsPrimitives() | ||
{ | ||
$params = [':param1' => 'value1', ':param2' => 'value2']; | ||
$query = 'UPDATE `some_table1` SET `col1`=\'val1\' WHERE `param1`=\':param1\' AND `param2`=\':param2\';'; | ||
$this->pdoMock->expects($this->once()) | ||
->method('prepare') | ||
->with($query) | ||
->willReturn($this->pdoStatementMock); | ||
$this->pdoStatementMock->expects($this->never()) | ||
->method('bindParam'); | ||
$this->pdoStatementMock->expects($this->once()) | ||
->method('execute') | ||
->with($params); | ||
|
||
(new Mysql($this->adapterMock, $query))->_execute($params); | ||
} | ||
|
||
public function testExecuteWhenParamsAsParameterObject() | ||
{ | ||
$param1 = $this->createMock(Parameter::class); | ||
$param1Value = 'SomeValue'; | ||
$param1DataType = 'dataType'; | ||
$param1Length = '9'; | ||
$param1DriverOptions = 'some driver options'; | ||
$param1->expects($this->once()) | ||
->method('getIsBlob') | ||
->willReturn(false); | ||
$param1->expects($this->once()) | ||
->method('getDataType') | ||
->willReturn($param1DataType); | ||
$param1->expects($this->once()) | ||
->method('getLength') | ||
->willReturn($param1Length); | ||
$param1->expects($this->once()) | ||
->method('getDriverOptions') | ||
->willReturn($param1DriverOptions); | ||
$param1->expects($this->once()) | ||
->method('getValue') | ||
->willReturn($param1Value); | ||
$params = [ | ||
':param1' => $param1, | ||
':param2' => 'value2', | ||
]; | ||
$query = 'UPDATE `some_table1` SET `col1`=\'val1\' WHERE `param1`=\':param1\' AND `param2`=\':param2\';'; | ||
$this->pdoMock->expects($this->once()) | ||
->method('prepare') | ||
->with($query) | ||
->willReturn($this->pdoStatementMock); | ||
$this->pdoStatementMock->expects($this->exactly(2)) | ||
->method('bindParam') | ||
->withConsecutive( | ||
[':param1', $param1Value, $param1DataType, $param1Length, $param1DriverOptions], | ||
[':param2', 'value2', \PDO::PARAM_STR, null, null] | ||
); | ||
$this->pdoStatementMock->expects($this->once()) | ||
->method('execute'); | ||
|
||
(new Mysql($this->adapterMock, $query))->_execute($params); | ||
} | ||
} |