diff --git a/lib/Doctrine/DBAL/Connection.php b/lib/Doctrine/DBAL/Connection.php index 81bd4bf8dc2..a7136044a7d 100644 --- a/lib/Doctrine/DBAL/Connection.php +++ b/lib/Doctrine/DBAL/Connection.php @@ -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; @@ -12,7 +11,6 @@ use Doctrine\DBAL\Driver\Connection as DriverConnection; use Doctrine\DBAL\Driver\PingableConnection; use Doctrine\DBAL\Driver\ResultStatement; -use Doctrine\DBAL\Driver\ResultStatement as DriverResultStatement; use Doctrine\DBAL\Driver\ServerInfoAwareConnection; use Doctrine\DBAL\Exception\ConnectionLost; use Doctrine\DBAL\Exception\InvalidArgumentException; @@ -618,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); } @@ -645,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); } @@ -672,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); } @@ -958,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); } @@ -984,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); } @@ -1063,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); } @@ -1089,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); } @@ -1118,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); } @@ -1191,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); } @@ -1237,7 +1211,7 @@ public function prepare($sql) * @param array|array $params Query parameters * @param array|array $types Parameter types * - * @return ResultStatement The executed statement. + * @return ResultStatement&BaseResult The executed statement. * * @throws Exception */ @@ -1293,7 +1267,7 @@ public function executeQuery($sql, array $params = [], $types = [], ?QueryCacheP * @param array|array $params Query parameters * @param array|array $types Parameter types * - * @return ResultStatement + * @return ResultStatement&BaseResult * * @throws CacheException */ @@ -1338,15 +1312,15 @@ public function executeCacheQuery($sql, $params, $types, QueryCacheProfile $qcp) } /** - * @return DriverResultStatement&BaseResult + * @return ResultStatement&BaseResult */ - private function ensureForwardCompatibilityStatement(DriverResultStatement $stmt) + private function ensureForwardCompatibilityStatement(ResultStatement $stmt) { if ($stmt instanceof BaseResult) { return $stmt; } - return new \Doctrine\DBAL\ForwardCompatibility\Result($stmt); + return new ForwardCompatibility\Result($stmt); } /** diff --git a/lib/Doctrine/DBAL/Portability/Connection.php b/lib/Doctrine/DBAL/Portability/Connection.php index eb7af86be81..bf973830d35 100644 --- a/lib/Doctrine/DBAL/Portability/Connection.php +++ b/lib/Doctrine/DBAL/Portability/Connection.php @@ -6,6 +6,7 @@ use Doctrine\DBAL\ColumnCase; use Doctrine\DBAL\Connection as BaseConnection; use Doctrine\DBAL\Driver\PDO\Connection as PDOConnection; +use Doctrine\DBAL\ForwardCompatibility; use PDO; use function func_get_args; @@ -96,7 +97,7 @@ public function executeQuery($sql, array $params = [], $types = [], ?QueryCacheP $stmt = new Statement(parent::executeQuery($sql, $params, $types, $qcp), $this); $stmt->setFetchMode($this->defaultFetchMode); - return $stmt; + return new ForwardCompatibility\Result($stmt); } /** diff --git a/tests/Doctrine/Tests/DBAL/Functional/Connection/BackwardCompatibility/Connection.php b/tests/Doctrine/Tests/DBAL/Functional/Connection/BackwardCompatibility/Connection.php index b864a794236..aa0496fee89 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/Connection/BackwardCompatibility/Connection.php +++ b/tests/Doctrine/Tests/DBAL/Functional/Connection/BackwardCompatibility/Connection.php @@ -4,6 +4,7 @@ use Doctrine\DBAL\Cache\QueryCacheProfile; use Doctrine\DBAL\Connection as BaseConnection; +use Doctrine\DBAL\ForwardCompatibility; use function func_get_args; @@ -17,7 +18,9 @@ class Connection extends BaseConnection */ public function executeQuery($sql, array $params = [], $types = [], ?QueryCacheProfile $qcp = null) { - return new Statement(parent::executeQuery($sql, $params, $types, $qcp)); + return new ForwardCompatibility\Result( + new Statement(parent::executeQuery($sql, $params, $types, $qcp)) + ); } /**