Skip to content

Commit

Permalink
Merge pull request #4596 from mdumoulin/fix_issue_4591
Browse files Browse the repository at this point in the history
Fix BC break on QueryBuilder::execute()
  • Loading branch information
morozov committed Apr 15, 2021
2 parents 68a6f8e + 1372a42 commit f4a801b
Show file tree
Hide file tree
Showing 8 changed files with 352 additions and 24 deletions.
15 changes: 6 additions & 9 deletions lib/Doctrine/DBAL/Connection.php
Expand Up @@ -18,7 +18,6 @@
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Result as BaseResult;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Types\Type;
use Doctrine\Deprecations\Deprecation;
Expand Down Expand Up @@ -1264,7 +1263,9 @@ public function prepare($sql)
* @param array<int, mixed>|array<string, mixed> $params Query parameters
* @param array<int, int|string|Type|null>|array<string, int|string|Type|null> $types Parameter types
*
* @return ResultStatement&BaseResult The executed statement.
* @return ForwardCompatibility\DriverStatement|ForwardCompatibility\DriverResultStatement
*
* The executed statement or the cached result statement if a query cache profile is used
*
* @throws Exception
*/
Expand Down Expand Up @@ -1320,7 +1321,7 @@ public function executeQuery($sql, array $params = [], $types = [], ?QueryCacheP
* @param array<int, mixed>|array<string, mixed> $params Query parameters
* @param array<int, int|string|Type|null>|array<string, int|string|Type|null> $types Parameter types
*
* @return ResultStatement&BaseResult
* @return ForwardCompatibility\DriverResultStatement
*
* @throws CacheException
*/
Expand Down Expand Up @@ -1365,15 +1366,11 @@ public function executeCacheQuery($sql, $params, $types, QueryCacheProfile $qcp)
}

/**
* @return ResultStatement&BaseResult
* @return ForwardCompatibility\Result
*/
private function ensureForwardCompatibilityStatement(ResultStatement $stmt)
{
if ($stmt instanceof BaseResult) {
return $stmt;
}

return new ForwardCompatibility\Result($stmt);
return ForwardCompatibility\Result::ensure($stmt);
}

/**
Expand Down
@@ -0,0 +1,9 @@
<?php

namespace Doctrine\DBAL\ForwardCompatibility;

use Doctrine\DBAL;

interface DriverResultStatement extends DBAL\Driver\ResultStatement, DBAL\Result
{
}
9 changes: 9 additions & 0 deletions lib/Doctrine/DBAL/ForwardCompatibility/DriverStatement.php
@@ -0,0 +1,9 @@
<?php

namespace Doctrine\DBAL\ForwardCompatibility;

use Doctrine\DBAL;

interface DriverStatement extends DBAL\Driver\Statement, DBAL\Result
{
}
121 changes: 115 additions & 6 deletions lib/Doctrine/DBAL/ForwardCompatibility/Result.php
Expand Up @@ -2,10 +2,10 @@

namespace Doctrine\DBAL\ForwardCompatibility;

use Doctrine\DBAL\Driver\ResultStatement as DriverResultStatement;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Exception\NoKeyValue;
use Doctrine\DBAL\Result as BaseResult;
use Doctrine\DBAL\ParameterType;
use Doctrine\Deprecations\Deprecation;
use IteratorAggregate;
use PDO;
Expand All @@ -18,18 +18,27 @@
* A wrapper around a Doctrine\DBAL\Driver\ResultStatement that adds 3.0 features
* defined in Result interface
*/
class Result implements IteratorAggregate, DriverResultStatement, BaseResult
class Result implements IteratorAggregate, DriverStatement, DriverResultStatement
{
/** @var DriverResultStatement */
/** @var Driver\ResultStatement */
private $stmt;

public function __construct(DriverResultStatement $stmt)
public static function ensure(Driver\ResultStatement $stmt): Result
{
if ($stmt instanceof Result) {
return $stmt;
}

return new Result($stmt);
}

public function __construct(Driver\ResultStatement $stmt)
{
$this->stmt = $stmt;
}

/**
* @return DriverResultStatement
* @return Driver\ResultStatement
*/
public function getIterator()
{
Expand Down Expand Up @@ -301,4 +310,104 @@ private function ensureHasKeyValue(): void
throw NoKeyValue::fromColumnCount($columnCount);
}
}

/**
* {@inheritDoc}
*
* @deprecated This feature will no longer be available on Result object in 3.0.x version.
*/
public function bindValue($param, $value, $type = ParameterType::STRING)
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/4019',
'Result::bindValue() is deprecated, no replacement.'
);

if ($this->stmt instanceof Driver\Statement) {
return $this->stmt->bindValue($param, $value, $type);
}

throw Exception::notSupported('bindValue');
}

/**
* {@inheritDoc}
*
* @deprecated This feature will no longer be available on Result object in 3.0.x version.
*/
public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null)
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/4019',
'Result::bindParam() is deprecated, no replacement.'
);

if ($this->stmt instanceof Driver\Statement) {
return $this->stmt->bindParam($param, $variable, $type, $length);
}

throw Exception::notSupported('bindParam');
}

/**
* {@inheritDoc}
*
* @deprecated The error information is available via exceptions.
*/
public function errorCode()
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/4019',
'Result::errorCode() is deprecated, the error information is available via exceptions.'
);

if ($this->stmt instanceof Driver\Statement) {
return $this->stmt->errorCode();
}

throw Exception::notSupported('errorCode');
}

/**
* {@inheritDoc}
*
* @deprecated The error information is available via exceptions.
*/
public function errorInfo()
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/4019',
'Result::errorInfo() is deprecated, the error information is available via exceptions.'
);

if ($this->stmt instanceof Driver\Statement) {
return $this->stmt->errorInfo();
}

throw Exception::notSupported('errorInfo');
}

/**
* {@inheritDoc}
*
* @deprecated This feature will no longer be available on Result object in 3.0.x version.
*/
public function execute($params = null)
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/4019',
'Result::execute() is deprecated, no replacement.'
);

if ($this->stmt instanceof Driver\Statement) {
return $this->stmt->execute($params);
}

throw Exception::notSupported('execute');
}
}
8 changes: 5 additions & 3 deletions lib/Doctrine/DBAL/Query/QueryBuilder.php
Expand Up @@ -3,8 +3,8 @@
namespace Doctrine\DBAL\Query;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\ForwardCompatibility;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Query\Expression\CompositeExpression;
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
Expand Down Expand Up @@ -201,14 +201,16 @@ public function getState()
/**
* Executes this query using the bound parameters and their types.
*
* @return ResultStatement|int
* @return ForwardCompatibility\DriverStatement|int
*
* @throws Exception
*/
public function execute()
{
if ($this->type === self::SELECT) {
return $this->connection->executeQuery($this->getSQL(), $this->params, $this->paramTypes);
return ForwardCompatibility\Result::ensure(
$this->connection->executeQuery($this->getSQL(), $this->params, $this->paramTypes)
);
}

return $this->connection->executeStatement($this->getSQL(), $this->params, $this->paramTypes);
Expand Down

0 comments on commit f4a801b

Please sign in to comment.