Skip to content

Commit

Permalink
Merge pull request #4529 from mdumoulin/improve-v2-forward-compatibility
Browse files Browse the repository at this point in the history
Improve forward compatibility between 2.x and 3.0
  • Loading branch information
morozov authored Mar 17, 2021
2 parents 70ab840 + 4297f50 commit b49127c
Show file tree
Hide file tree
Showing 8 changed files with 746 additions and 80 deletions.
118 changes: 53 additions & 65 deletions lib/Doctrine/DBAL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Closure;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Abstraction\Result;
use Doctrine\DBAL\Cache\ArrayStatement;
use Doctrine\DBAL\Cache\CacheException;
use Doctrine\DBAL\Cache\QueryCacheProfile;
Expand All @@ -19,6 +18,7 @@
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 Throwable;
Expand Down Expand Up @@ -616,13 +616,11 @@ public function fetchColumn($sql, array $params = [], $column = 0, array $types
public function fetchAssociative(string $query, array $params = [], array $types = [])
{
try {
$stmt = $this->executeQuery($query, $params, $types);

if ($stmt instanceof Result) {
return $stmt->fetchAssociative();
}
$stmt = $this->ensureForwardCompatibilityStatement(
$this->executeQuery($query, $params, $types)
);

return $stmt->fetch(FetchMode::ASSOCIATIVE);
return $stmt->fetchAssociative();
} catch (Throwable $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types);
}
Expand All @@ -643,13 +641,11 @@ public function fetchAssociative(string $query, array $params = [], array $types
public function fetchNumeric(string $query, array $params = [], array $types = [])
{
try {
$stmt = $this->executeQuery($query, $params, $types);

if ($stmt instanceof Result) {
return $stmt->fetchNumeric();
}
$stmt = $this->ensureForwardCompatibilityStatement(
$this->executeQuery($query, $params, $types)
);

return $stmt->fetch(FetchMode::NUMERIC);
return $stmt->fetchNumeric();
} catch (Throwable $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types);
}
Expand All @@ -670,13 +666,11 @@ public function fetchNumeric(string $query, array $params = [], array $types = [
public function fetchOne(string $query, array $params = [], array $types = [])
{
try {
$stmt = $this->executeQuery($query, $params, $types);

if ($stmt instanceof Result) {
return $stmt->fetchOne();
}
$stmt = $this->ensureForwardCompatibilityStatement(
$this->executeQuery($query, $params, $types)
);

return $stmt->fetch(FetchMode::COLUMN);
return $stmt->fetchOne();
} catch (Throwable $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types);
}
Expand Down Expand Up @@ -956,13 +950,11 @@ public function fetchAll($sql, array $params = [], $types = [])
public function fetchAllNumeric(string $query, array $params = [], array $types = []): array
{
try {
$stmt = $this->executeQuery($query, $params, $types);

if ($stmt instanceof Result) {
return $stmt->fetchAllNumeric();
}
$stmt = $this->ensureForwardCompatibilityStatement(
$this->executeQuery($query, $params, $types)
);

return $stmt->fetchAll(FetchMode::NUMERIC);
return $stmt->fetchAllNumeric();
} catch (Throwable $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types);
}
Expand All @@ -982,13 +974,11 @@ public function fetchAllNumeric(string $query, array $params = [], array $types
public function fetchAllAssociative(string $query, array $params = [], array $types = []): array
{
try {
$stmt = $this->executeQuery($query, $params, $types);

if ($stmt instanceof Result) {
return $stmt->fetchAllAssociative();
}
$stmt = $this->ensureForwardCompatibilityStatement(
$this->executeQuery($query, $params, $types)
);

return $stmt->fetchAll(FetchMode::ASSOCIATIVE);
return $stmt->fetchAllAssociative();
} catch (Throwable $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types);
}
Expand Down Expand Up @@ -1061,13 +1051,11 @@ public function fetchAllAssociativeIndexed(string $query, array $params = [], ar
public function fetchFirstColumn(string $query, array $params = [], array $types = []): array
{
try {
$stmt = $this->executeQuery($query, $params, $types);

if ($stmt instanceof Result) {
return $stmt->fetchFirstColumn();
}
$stmt = $this->ensureForwardCompatibilityStatement(
$this->executeQuery($query, $params, $types)
);

return $stmt->fetchAll(FetchMode::COLUMN);
return $stmt->fetchFirstColumn();
} catch (Throwable $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types);
}
Expand All @@ -1087,15 +1075,11 @@ public function fetchFirstColumn(string $query, array $params = [], array $types
public function iterateNumeric(string $query, array $params = [], array $types = []): Traversable
{
try {
$stmt = $this->executeQuery($query, $params, $types);
$stmt = $this->ensureForwardCompatibilityStatement(
$this->executeQuery($query, $params, $types)
);

if ($stmt instanceof Result) {
yield from $stmt->iterateNumeric();
} else {
while (($row = $stmt->fetch(FetchMode::NUMERIC)) !== false) {
yield $row;
}
}
yield from $stmt->iterateNumeric();
} catch (Throwable $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types);
}
Expand All @@ -1116,15 +1100,11 @@ public function iterateNumeric(string $query, array $params = [], array $types =
public function iterateAssociative(string $query, array $params = [], array $types = []): Traversable
{
try {
$stmt = $this->executeQuery($query, $params, $types);
$stmt = $this->ensureForwardCompatibilityStatement(
$this->executeQuery($query, $params, $types)
);

if ($stmt instanceof Result) {
yield from $stmt->iterateAssociative();
} else {
while (($row = $stmt->fetch(FetchMode::ASSOCIATIVE)) !== false) {
yield $row;
}
}
yield from $stmt->iterateAssociative();
} catch (Throwable $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types);
}
Expand Down Expand Up @@ -1189,15 +1169,11 @@ public function iterateAssociativeIndexed(string $query, array $params = [], arr
public function iterateColumn(string $query, array $params = [], array $types = []): Traversable
{
try {
$stmt = $this->executeQuery($query, $params, $types);
$stmt = $this->ensureForwardCompatibilityStatement(
$this->executeQuery($query, $params, $types)
);

if ($stmt instanceof Result) {
yield from $stmt->iterateColumn();
} else {
while (($value = $stmt->fetch(FetchMode::COLUMN)) !== false) {
yield $value;
}
}
yield from $stmt->iterateColumn();
} catch (Throwable $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types);
}
Expand Down Expand Up @@ -1235,7 +1211,7 @@ 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 The executed statement.
* @return ResultStatement&BaseResult The executed statement.
*
* @throws Exception
*/
Expand Down Expand Up @@ -1281,7 +1257,7 @@ public function executeQuery($sql, array $params = [], $types = [], ?QueryCacheP
$logger->stopQuery();
}

return $stmt;
return $this->ensureForwardCompatibilityStatement($stmt);
}

/**
Expand All @@ -1291,7 +1267,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
* @return ResultStatement&BaseResult
*
* @throws CacheException
*/
Expand Down Expand Up @@ -1332,7 +1308,19 @@ public function executeCacheQuery($sql, $params, $types, QueryCacheProfile $qcp)

$stmt->setFetchMode($this->defaultFetchMode);

return $stmt;
return $this->ensureForwardCompatibilityStatement($stmt);
}

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

return new ForwardCompatibility\Result($stmt);
}

/**
Expand Down
Loading

0 comments on commit b49127c

Please sign in to comment.