Skip to content

Commit

Permalink
Merge branch '2.11.x' into 3.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed May 26, 2020
2 parents be4332a + 256cefa commit 179ab95
Show file tree
Hide file tree
Showing 41 changed files with 1,709 additions and 81 deletions.
10 changes: 10 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ Please use other database client applications for import, e.g.:

# Upgrade to 2.11

## Deprecated `FetchMode` and the corresponding methods

1. The `FetchMode` class and the `setFetchMode()` method of the `Connection` and `Statement` interfaces are deprecated.
2. The `Statement::fetch()` method is deprecated in favor of `fetchNumeric()`, `fetchAssociative()` and `fetchOne()`.
3. The `Statement::fetchAll()` method is deprecated in favor of `fetchAllNumeric()` and `fetchAllAssociative()`. There is no currently replacement for `Statement::fetchAll(FETCH_MODE::COLUMN)`. In a future major version, `fetchColumn()` will be used as a replacement.
4. The `Statement::fetchColumn()` method is deprecated in favor of `fetchOne()`.
5. The `Connection::fetchArray()` and `fetchAssoc()` method are deprecated in favor of `fetchNumeric()` and `fetchAssociative()` respectively.
6. The `StatementIterator` class and the usage of a `Statement` object as `Traversable` is deprecated in favor of `iterateNumeric()`, `iterateAssociative()` and `iterateColumn()`.
7. Fetching data in mixed mode (`FetchMode::MIXED`) is deprecated.

## Deprecated `Connection::project()`

The `Connection::project()` method is deprecated. Implement data transformation outside of DBAL.
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"phpstan/phpstan": "^0.12.18",
"phpstan/phpstan-strict-rules": "^0.12.2",
"phpunit/phpunit": "^9.1.1",
"slevomat/coding-standard": "^6.3.6",
"symfony/console": "^2.0.5|^3.0|^4.0|^5.0",
"vimeo/psalm": "^3.11"
},
Expand Down
16 changes: 8 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 77 additions & 1 deletion src/Cache/ArrayStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
namespace Doctrine\DBAL\Cache;

use ArrayIterator;
use Doctrine\DBAL\Driver\FetchUtils;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ForwardCompatibility\Driver\ResultStatement as ForwardCompatibleResultStatement;
use InvalidArgumentException;
use IteratorAggregate;
use function array_merge;
use function array_values;
use function count;
use function reset;

class ArrayStatement implements IteratorAggregate, ResultStatement
class ArrayStatement implements IteratorAggregate, ResultStatement, ForwardCompatibleResultStatement
{
/** @var mixed[] */
private $data;
Expand Down Expand Up @@ -59,6 +61,8 @@ public function columnCount()

/**
* {@inheritdoc}
*
* @deprecated Use one of the fetch- or iterate-related methods.
*/
public function setFetchMode($fetchMode)
{
Expand All @@ -69,6 +73,8 @@ public function setFetchMode($fetchMode)

/**
* {@inheritdoc}
*
* @deprecated Use iterateNumeric(), iterateAssociative() or iterateColumn() instead.
*/
public function getIterator()
{
Expand All @@ -79,6 +85,8 @@ public function getIterator()

/**
* {@inheritdoc}
*
* @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
*/
public function fetch($fetchMode = null)
{
Expand Down Expand Up @@ -110,6 +118,8 @@ public function fetch($fetchMode = null)

/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
*/
public function fetchAll($fetchMode = null)
{
Expand All @@ -123,6 +133,8 @@ public function fetchAll($fetchMode = null)

/**
* {@inheritdoc}
*
* @deprecated Use fetchOne() instead.
*/
public function fetchColumn()
{
Expand All @@ -131,4 +143,68 @@ public function fetchColumn()
// TODO: verify that return false is the correct behavior
return $row[0] ?? false;
}

/**
* {@inheritdoc}
*/
public function fetchNumeric()
{
$row = $this->doFetch();

if ($row === false) {
return false;
}

return array_values($row);
}

/**
* {@inheritdoc}
*/
public function fetchAssociative()
{
return $this->doFetch();
}

/**
* {@inheritdoc}
*/
public function fetchOne()
{
$row = $this->doFetch();

if ($row === false) {
return false;
}

return reset($row);
}

/**
* {@inheritdoc}
*/
public function fetchAllNumeric() : array
{
return FetchUtils::fetchAllNumeric($this);
}

/**
* {@inheritdoc}
*/
public function fetchAllAssociative() : array
{
return FetchUtils::fetchAllAssociative($this);
}

/**
* @return mixed|false
*/
private function doFetch()
{
if (! isset($this->data[$this->num])) {
return false;
}

return $this->data[$this->num++];
}
}
118 changes: 117 additions & 1 deletion src/Cache/ResultCacheStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

use ArrayIterator;
use Doctrine\Common\Cache\Cache;
use Doctrine\DBAL\Driver\DriverException;
use Doctrine\DBAL\Driver\FetchUtils;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ForwardCompatibility\Driver\ResultStatement as ForwardCompatibleResultStatement;
use InvalidArgumentException;
use IteratorAggregate;
use function array_merge;
Expand All @@ -27,7 +30,7 @@
* Also you have to realize that the cache will load the whole result into memory at once to ensure 2.
* This means that the memory usage for cached results might increase by using this feature.
*/
class ResultCacheStatement implements IteratorAggregate, ResultStatement
class ResultCacheStatement implements IteratorAggregate, ResultStatement, ForwardCompatibleResultStatement
{
/** @var Cache */
private $resultCache;
Expand Down Expand Up @@ -104,6 +107,8 @@ public function columnCount()

/**
* {@inheritdoc}
*
* @deprecated Use one of the fetch- or iterate-related methods.
*/
public function setFetchMode($fetchMode)
{
Expand All @@ -114,6 +119,8 @@ public function setFetchMode($fetchMode)

/**
* {@inheritdoc}
*
* @deprecated Use iterateNumeric(), iterateAssociative() or iterateColumn() instead.
*/
public function getIterator()
{
Expand All @@ -124,6 +131,8 @@ public function getIterator()

/**
* {@inheritdoc}
*
* @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
*/
public function fetch($fetchMode = null)
{
Expand Down Expand Up @@ -164,6 +173,8 @@ public function fetch($fetchMode = null)

/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
*/
public function fetchAll($fetchMode = null)
{
Expand All @@ -183,6 +194,8 @@ public function fetchAll($fetchMode = null)

/**
* {@inheritdoc}
*
* @deprecated Use fetchOne() instead.
*/
public function fetchColumn()
{
Expand All @@ -192,6 +205,64 @@ public function fetchColumn()
return $row[0] ?? false;
}

/**
* {@inheritdoc}
*/
public function fetchNumeric()
{
$row = $this->doFetch();

if ($row === false) {
return false;
}

return array_values($row);
}

/**
* {@inheritdoc}
*/
public function fetchAssociative()
{
return $this->doFetch();
}

/**
* {@inheritdoc}
*/
public function fetchOne()
{
return FetchUtils::fetchOne($this);
}

/**
* {@inheritdoc}
*/
public function fetchAllNumeric() : array
{
if ($this->statement instanceof ForwardCompatibleResultStatement) {
$data = $this->statement->fetchAllAssociative();
} else {
$data = $this->statement->fetchAll(FetchMode::ASSOCIATIVE);
}

return $this->store($data);
}

/**
* {@inheritdoc}
*/
public function fetchAllAssociative() : array
{
if ($this->statement instanceof ForwardCompatibleResultStatement) {
$data = $this->statement->fetchAllAssociative();
} else {
$data = $this->statement->fetchAll(FetchMode::ASSOCIATIVE);
}

return $this->store($data);
}

/**
* Returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement
* executed by the corresponding object.
Expand All @@ -209,4 +280,49 @@ public function rowCount() : int

return $this->statement->rowCount();
}

/**
* @return array<string,mixed>|false
*
* @throws DriverException
*/
private function doFetch()
{
if ($this->data === null) {
$this->data = [];
}

if ($this->statement instanceof ForwardCompatibleResultStatement) {
$row = $this->statement->fetchAssociative();
} else {
$row = $this->statement->fetch(FetchMode::ASSOCIATIVE);
}

if ($row !== false) {
$this->data[] = $row;

return $row;
}

$this->emptied = true;

return false;
}

/**
* @param array<int,array<mixed>> $data
*
* @return array<int,array<mixed>>
*/
private function store(array $data) : array
{
foreach ($data as $key => $value) {
$data[$key] = [$value];
}

$this->data = $data;
$this->emptied = true;

return $this->data;
}
}
Loading

0 comments on commit 179ab95

Please sign in to comment.