Unverified Commit c08d3bc8 authored by Sergei Morozov's avatar Sergei Morozov Committed by GitHub

Merge pull request #4034 from morozov/deprecate-fetch-mode

Additional changes based on the discussion in #4007
parents ce218349 e74b97c2
......@@ -42,7 +42,7 @@ object is closed:
<?php
$stmt = $conn->executeCacheQuery($query, $params, $types, new QueryCacheProfile(0, "some key"));
$data = $stmt->fetchAll();
$data = $stmt->fetchAllAssociative();
$stmt->closeCursor(); // at this point the result is cached
.. warning::
......
......@@ -41,7 +41,7 @@ the query until there are no more rows:
<?php
while ($row = $stmt->fetch()) {
while (($row = $stmt->fetchAssociative()) !== false) {
echo $row['headline'];
}
......@@ -308,7 +308,7 @@ Prepare a given SQL statement and return the
<?php
$statement = $conn->prepare('SELECT * FROM user');
$statement->execute();
$users = $statement->fetchAll();
$users = $statement->fetchAllAssociative();
/*
array(
......@@ -346,7 +346,7 @@ parameters to the execute method, then returning the statement:
<?php
$statement = $conn->executeQuery('SELECT * FROM user WHERE username = ?', array('jwage'));
$user = $statement->fetch();
$user = $statement->fetchAssociative();
/*
array(
......@@ -360,15 +360,15 @@ to perform necessary type conversions between actual input
parameters and expected database values. See the
:ref:`Types <mappingMatrix>` section for more information.
fetchAll()
~~~~~~~~~~
fetchAllAssociative()
~~~~~~~~~~~~~~~~~~~~~
Execute the query and fetch all results into an array:
.. code-block:: php
<?php
$users = $conn->fetchAll('SELECT * FROM user');
$users = $conn->fetchAllAssociative('SELECT * FROM user');
/*
array(
......@@ -379,15 +379,15 @@ Execute the query and fetch all results into an array:
)
*/
fetchArray()
~~~~~~~~~~~~
fetchNumeric()
~~~~~~~~~~~~~~
Numeric index retrieval of first result row of the given query:
.. code-block:: php
<?php
$user = $conn->fetchArray('SELECT * FROM user WHERE username = ?', array('jwage'));
$user = $conn->fetchNumeric('SELECT * FROM user WHERE username = ?', array('jwage'));
/*
array(
......@@ -396,26 +396,26 @@ Numeric index retrieval of first result row of the given query:
)
*/
fetchColumn()
~~~~~~~~~~~~~
fetchOne()
~~~~~~~~~~
Retrieve only the given column of the first result row.
Retrieve only the value of the first column of the first result row.
.. code-block:: php
<?php
$username = $conn->fetchColumn('SELECT username FROM user WHERE id = ?', array(1), 0);
$username = $conn->fetchOne('SELECT username FROM user WHERE id = ?', array(1), 0);
echo $username; // jwage
fetchAssoc()
~~~~~~~~~~~~
fetchAssociative()
~~~~~~~~~~~~~~~~~~
Retrieve assoc row of the first result row.
Retrieve associative array of the first result row.
.. code-block:: php
<?php
$user = $conn->fetchAssoc('SELECT * FROM user WHERE username = ?', array('jwage'));
$user = $conn->fetchAssociative('SELECT * FROM user WHERE username = ?', array('jwage'));
/*
array(
'username' => 'jwage',
......
......@@ -13,6 +13,7 @@ use Doctrine\DBAL\ForwardCompatibility\Driver\ResultStatement as ForwardCompatib
use InvalidArgumentException;
use IteratorAggregate;
use PDO;
use function array_map;
use function array_merge;
use function array_values;
use function assert;
......@@ -55,7 +56,7 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement, Forwar
*/
private $emptied = false;
/** @var mixed[] */
/** @var array<int,array<string,mixed>> */
private $data;
/** @var int */
......@@ -179,18 +180,26 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement, Forwar
*/
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
{
$data = $this->statement->fetchAll($fetchMode, $fetchArgument, $ctorArgs);
if ($fetchMode === FetchMode::COLUMN) {
foreach ($data as $key => $value) {
$data[$key] = [$value];
}
}
$data = $this->statement->fetchAll(FetchMode::ASSOCIATIVE, $fetchArgument, $ctorArgs);
$this->data = $data;
$this->emptied = true;
return $this->data;
if ($fetchMode === FetchMode::NUMERIC) {
foreach ($data as $i => $row) {
$data[$i] = array_values($row);
}
} elseif ($fetchMode === FetchMode::MIXED) {
foreach ($data as $i => $row) {
$data[$i] = array_merge($row, array_values($row));
}
} elseif ($fetchMode === FetchMode::COLUMN) {
foreach ($data as $i => $row) {
$data[$i] = reset($row);
}
}
return $data;
}
/**
......@@ -247,7 +256,9 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement, Forwar
$data = $this->statement->fetchAll(FetchMode::ASSOCIATIVE);
}
return $this->store($data);
$this->store($data);
return array_map('array_values', $this->data);
}
/**
......@@ -261,7 +272,9 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement, Forwar
$data = $this->statement->fetchAll(FetchMode::ASSOCIATIVE);
}
return $this->store($data);
$this->store($data);
return $this->data;
}
/**
......@@ -311,19 +324,11 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement, Forwar
}
/**
* @param array<int,array<mixed>> $data
*
* @return array<int,array<mixed>>
* @param array<int,array<string,mixed>> $data
*/
private function store(array $data) : array
private function store(array $data) : void
{
foreach ($data as $key => $value) {
$data[$key] = [$value];
}
$this->data = $data;
$this->emptied = true;
return $this->data;
}
}
......@@ -102,13 +102,13 @@ class ResultCacheTest extends DbalFunctionalTestCase
$numExpectedResult[] = array_values($v);
}
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
$data = $this->hydrateStmt($stmt, FetchMode::ASSOCIATIVE);
self::assertEquals($this->expectedResult, $data);
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
$data = $this->hydrateStmt($stmt, FetchMode::NUMERIC);
......@@ -124,10 +124,10 @@ class ResultCacheTest extends DbalFunctionalTestCase
private function assertStandardAndIteratorFetchAreEqual(int $fetchMode) : void
{
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
$data = $this->hydrateStmt($stmt, $fetchMode);
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
$dataIterator = $this->hydrateStmtIterator($stmt, $fetchMode);
self::assertEquals($data, $dataIterator);
......@@ -135,7 +135,7 @@ class ResultCacheTest extends DbalFunctionalTestCase
public function testDontCloseNoCache() : void
{
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
$data = [];
......@@ -143,7 +143,7 @@ class ResultCacheTest extends DbalFunctionalTestCase
$data[] = $row;
}
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
$data = [];
......@@ -156,12 +156,12 @@ class ResultCacheTest extends DbalFunctionalTestCase
public function testDontFinishNoCache() : void
{
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
$stmt->fetch(FetchMode::ASSOCIATIVE);
$stmt->closeCursor();
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
$this->hydrateStmt($stmt, FetchMode::NUMERIC);
......@@ -171,7 +171,7 @@ class ResultCacheTest extends DbalFunctionalTestCase
public function testFetchAllAndFinishSavesCache() : void
{
$layerCache = new ArrayCache();
$stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(10, 'testcachekey', $layerCache));
$stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(0, 'testcachekey', $layerCache));
$stmt->fetchAll();
$stmt->closeCursor();
......@@ -199,13 +199,13 @@ class ResultCacheTest extends DbalFunctionalTestCase
*/
private function assertCacheNonCacheSelectSameFetchModeAreEqual(array $expectedResult, int $fetchMode) : void
{
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
self::assertEquals(2, $stmt->columnCount());
$data = $this->hydrateStmt($stmt, $fetchMode);
self::assertEquals($expectedResult, $data);
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
self::assertEquals(2, $stmt->columnCount());
$data = $this->hydrateStmt($stmt, $fetchMode);
......@@ -215,23 +215,24 @@ class ResultCacheTest extends DbalFunctionalTestCase
public function testEmptyResultCache() : void
{
$stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(10, 'emptycachekey'));
$data = $this->hydrateStmt($stmt);
$stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(0, 'emptycachekey'));
$this->hydrateStmt($stmt);
$stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(10, 'emptycachekey'));
$data = $this->hydrateStmt($stmt);
$stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(0, 'emptycachekey'));
$this->hydrateStmt($stmt);
self::assertCount(1, $this->sqlLogger->queries, 'just one dbal hit');
}
public function testChangeCacheImpl() : void
{
$stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(10, 'emptycachekey'));
$data = $this->hydrateStmt($stmt);
$stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(0, 'emptycachekey'));
$this->hydrateStmt($stmt);
$secondCache = new ArrayCache();
$stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(10, 'emptycachekey', $secondCache));
$data = $this->hydrateStmt($stmt);
$stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(0, 'emptycachekey', $secondCache));
$this->hydrateStmt($stmt);
self::assertCount(2, $this->sqlLogger->queries, 'two hits');
self::assertCount(1, $secondCache->fetch('emptycachekey'));
......@@ -243,7 +244,8 @@ class ResultCacheTest extends DbalFunctionalTestCase
private function hydrateStmt(ResultStatement $stmt, int $fetchMode = FetchMode::ASSOCIATIVE) : array
{
$data = [];
while ($row = $stmt->fetch($fetchMode)) {
foreach ($stmt->fetchAll($fetchMode) as $row) {
$data[] = is_array($row) ? array_change_key_case($row, CASE_LOWER) : $row;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment