Skip to content

Commit

Permalink
Document result for Connection execute
Browse files Browse the repository at this point in the history
Document result for Connection execute to resolve users's static
analysis.
  • Loading branch information
mdumoulin committed Mar 13, 2021
1 parent db66bf3 commit 9868993
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 69 deletions.
108 changes: 41 additions & 67 deletions lib/Doctrine/DBAL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@

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;
use Doctrine\DBAL\Cache\ResultCacheStatement;
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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -1237,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 @@ -1293,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 @@ -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);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/Doctrine/DBAL/Portability/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Connection as BaseConnection;
use Doctrine\DBAL\ForwardCompatibility;

use function func_get_args;

Expand All @@ -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))
);
}

/**
Expand Down

0 comments on commit 9868993

Please sign in to comment.