Unverified Commit b0ca9434 authored by Grégoire Paris's avatar Grégoire Paris

Merge remote-tracking branch 'origin/3.0.x' into 3.0.x

parents a04c608d 72e219a7
# Upgrade to 3.0
## Removed `FetchMode` and the corresponding methods
1. The `FetchMode` class and the `setFetchMode()` method of the `Connection` and `Statement` interfaces are removed.
2. The `Statement::fetch()` method is replaced with `fetchNumeric()`, `fetchAssociative()` and `fetchOne()`.
3. The `Statement::fetchAll()` method is replaced with `fetchAllNumeric()`, `fetchAllAssociative()` and `fechColumn()`.
4. The `Statement::fetchColumn()` method is replaced with `fetchOne()`.
5. The `Connection::fetchArray()` and `fetchAssoc()` methods are replaced with `fetchNumeric()` and `fetchAssociative()` respectively.
6. The `StatementIterator` class is removed. The usage of a `Statement` object as `Traversable` is no longer possible. Use `iterateNumeric()`, `iterateAssociative()` and `iterateColumn()` instead.
7. Fetching data in mixed mode (former `FetchMode::MIXED`) is no longer possible.
## BC BREAK: Dropped handling of one-based numeric arrays of parameters in `Statement::execute()`
The statement implementations no longer detect whether `$params` is a zero- or one-based array. A zero-based numeric array is expected.
......
......@@ -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',
......
......@@ -2,19 +2,13 @@
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, ForwardCompatibleResultStatement
class ArrayStatement implements ResultStatement
{
/** @var mixed[] */
private $data;
......@@ -25,9 +19,6 @@ class ArrayStatement implements IteratorAggregate, ResultStatement, ForwardCompa
/** @var int */
private $num = 0;
/** @var int */
private $defaultFetchMode = FetchMode::MIXED;
/**
* @param mixed[] $data
*/
......@@ -59,97 +50,12 @@ class ArrayStatement implements IteratorAggregate, ResultStatement, ForwardCompa
return $this->columnCount;
}
/**
* {@inheritdoc}
*
* @deprecated Use one of the fetch- or iterate-related methods.
*/
public function setFetchMode($fetchMode)
{
$this->defaultFetchMode = $fetchMode;
return true;
}
/**
* {@inheritdoc}
*
* @deprecated Use iterateNumeric(), iterateAssociative() or iterateColumn() instead.
*/
public function getIterator()
{
$data = $this->fetchAll();
return new ArrayIterator($data);
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
*/
public function fetch($fetchMode = null)
{
if (! isset($this->data[$this->num])) {
return false;
}
$row = $this->data[$this->num++];
$fetchMode = $fetchMode ?? $this->defaultFetchMode;
if ($fetchMode === FetchMode::ASSOCIATIVE) {
return $row;
}
if ($fetchMode === FetchMode::NUMERIC) {
return array_values($row);
}
if ($fetchMode === FetchMode::MIXED) {
return array_merge($row, array_values($row));
}
if ($fetchMode === FetchMode::COLUMN) {
return reset($row);
}
throw new InvalidArgumentException('Invalid fetch-style given for fetching result.');
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
*/
public function fetchAll($fetchMode = null)
{
$rows = [];
while ($row = $this->fetch($fetchMode)) {
$rows[] = $row;
}
return $rows;
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchOne() instead.
*/
public function fetchColumn()
{
$row = $this->fetch(FetchMode::NUMERIC);
// TODO: verify that return false is the correct behavior
return $row[0] ?? false;
}
/**
* {@inheritdoc}
*/
public function fetchNumeric()
{
$row = $this->doFetch();
$row = $this->fetch();
if ($row === false) {
return false;
......@@ -163,7 +69,7 @@ class ArrayStatement implements IteratorAggregate, ResultStatement, ForwardCompa
*/
public function fetchAssociative()
{
return $this->doFetch();
return $this->fetch();
}
/**
......@@ -171,7 +77,7 @@ class ArrayStatement implements IteratorAggregate, ResultStatement, ForwardCompa
*/
public function fetchOne()
{
$row = $this->doFetch();
$row = $this->fetch();
if ($row === false) {
return false;
......@@ -196,10 +102,18 @@ class ArrayStatement implements IteratorAggregate, ResultStatement, ForwardCompa
return FetchUtils::fetchAllAssociative($this);
}
/**
* {@inheritdoc}
*/
public function fetchColumn() : array
{
return FetchUtils::fetchColumn($this);
}
/**
* @return mixed|false
*/
private function doFetch()
private function fetch()
{
if (! isset($this->data[$this->num])) {
return false;
......
......@@ -2,20 +2,12 @@
namespace Doctrine\DBAL\Cache;
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;
use function array_map;
use function array_values;
use function assert;
use function reset;
/**
* Cache statement for SQL results.
......@@ -30,7 +22,7 @@ use function reset;
* 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, ForwardCompatibleResultStatement
class ResultCacheStatement implements ResultStatement
{
/** @var Cache */
private $resultCache;
......@@ -54,12 +46,9 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement, Forwar
*/
private $emptied = false;
/** @var mixed[] */
/** @var array<int,array<string,mixed>> */
private $data;
/** @var int */
private $defaultFetchMode = FetchMode::MIXED;
/**
* @param string $cacheKey
* @param string $realKey
......@@ -105,112 +94,12 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement, Forwar
return $this->statement->columnCount();
}
/**
* {@inheritdoc}
*
* @deprecated Use one of the fetch- or iterate-related methods.
*/
public function setFetchMode($fetchMode)
{
$this->defaultFetchMode = $fetchMode;
return true;
}
/**
* {@inheritdoc}
*
* @deprecated Use iterateNumeric(), iterateAssociative() or iterateColumn() instead.
*/
public function getIterator()
{
$data = $this->fetchAll();
return new ArrayIterator($data);
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
*/
public function fetch($fetchMode = null)
{
if ($this->data === null) {
$this->data = [];
}
$row = $this->statement->fetch(FetchMode::ASSOCIATIVE);
if ($row !== false) {
$this->data[] = $row;
$fetchMode = $fetchMode ?? $this->defaultFetchMode;
if ($fetchMode === FetchMode::ASSOCIATIVE) {
return $row;
}
if ($fetchMode === FetchMode::NUMERIC) {
return array_values($row);
}
if ($fetchMode === FetchMode::MIXED) {
return array_merge($row, array_values($row));
}
if ($fetchMode === FetchMode::COLUMN) {
return reset($row);
}
throw new InvalidArgumentException('Invalid fetch-style given for caching result.');
}
$this->emptied = true;
return false;
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
*/
public function fetchAll($fetchMode = null)
{
$data = $this->statement->fetchAll($fetchMode);
if ($fetchMode === FetchMode::COLUMN) {
foreach ($data as $key => $value) {
$data[$key] = [$value];
}
}
$this->data = $data;
$this->emptied = true;
return $this->data;
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchOne() instead.
*/
public function fetchColumn()
{
$row = $this->fetch(FetchMode::NUMERIC);
// TODO: verify that return false is the correct behavior
return $row[0] ?? false;
}
/**
* {@inheritdoc}
*/
public function fetchNumeric()
{
$row = $this->doFetch();
$row = $this->fetch();
if ($row === false) {
return false;
......@@ -224,7 +113,7 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement, Forwar
*/
public function fetchAssociative()
{
return $this->doFetch();
return $this->fetch();
}
/**
......@@ -240,13 +129,11 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement, Forwar
*/
public function fetchAllNumeric() : array
{
if ($this->statement instanceof ForwardCompatibleResultStatement) {
$data = $this->statement->fetchAllAssociative();
} else {
$data = $this->statement->fetchAll(FetchMode::ASSOCIATIVE);
}
$this->store(
$this->statement->fetchAllAssociative()
);
return $this->store($data);
return array_map('array_values', $this->data);
}
/**
......@@ -254,31 +141,19 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement, Forwar
*/
public function fetchAllAssociative() : array
{
if ($this->statement instanceof ForwardCompatibleResultStatement) {
$data = $this->statement->fetchAllAssociative();
} else {
$data = $this->statement->fetchAll(FetchMode::ASSOCIATIVE);
}
$this->store(
$this->statement->fetchAllAssociative()
);
return $this->store($data);
return $this->data;
}
/**
* Returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement
* executed by the corresponding object.
*
* If the last SQL statement executed by the associated Statement object was a SELECT statement,
* some databases may return the number of rows returned by that statement. However,
* this behaviour is not guaranteed for all databases and should not be
* relied on for portable applications.
*
* @return int The number of rows.
* {@inheritdoc}
*/
public function rowCount() : int
public function fetchColumn() : array
{
assert($this->statement instanceof Statement);
return $this->statement->rowCount();
return FetchUtils::fetchColumn($this);
}
/**
......@@ -286,17 +161,13 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement, Forwar
*
* @throws DriverException
*/
private function doFetch()
private function fetch()
{
if ($this->data === null) {
$this->data = [];
}
if ($this->statement instanceof ForwardCompatibleResultStatement) {
$row = $this->statement->fetchAssociative();
} else {
$row = $this->statement->fetch(FetchMode::ASSOCIATIVE);
}
$row = $this->statement->fetchAssociative();
if ($row !== false) {
$this->data[] = $row;
......@@ -310,19 +181,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;
}
}
......@@ -14,7 +14,6 @@ use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\Exception\InvalidArgumentException;
use Doctrine\DBAL\ForwardCompatibility\ResultStatement as ForwardCompatibleResultStatement;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
use Doctrine\DBAL\Query\QueryBuilder;
......@@ -168,9 +167,6 @@ class Connection implements DriverConnection
*/
private $isRollbackOnly = false;
/** @var int */
protected $defaultFetchMode = FetchMode::ASSOCIATIVE;
/**
* Initializes a new instance of the Connection class.
*
......@@ -522,75 +518,6 @@ class Connection implements DriverConnection
$this->commitAll();
}
/**
* Sets the fetch mode.
*
* @deprecated Use one of the fetch- or iterate-related methods.
*
* @param int $fetchMode
*
* @return void
*/
public function setFetchMode($fetchMode)
{
$this->defaultFetchMode = $fetchMode;
}
/**
* Prepares and executes an SQL query and returns the first row of the result
* as an associative array.
*
* @deprecated Use fetchAllAssociative()
*
* @param string $statement The SQL query.
* @param mixed[] $params The query parameters.
* @param int[]|string[] $types The query parameter types.
*
* @return mixed[]|false False is returned if no rows are found.
*
* @throws DBALException
*/
public function fetchAssoc($statement, array $params = [], array $types = [])
{
return $this->executeQuery($statement, $params, $types)->fetch(FetchMode::ASSOCIATIVE);
}
/**
* Prepares and executes an SQL query and returns the first row of the result
* as a numerically indexed array.
*
* @deprecated Use fetchAllNumeric()
*
* @param string $statement The SQL query to be executed.
* @param mixed[] $params The prepared statement params.
* @param int[]|string[] $types The query parameter types.
*
* @return mixed[]|false False is returned if no rows are found.
*/
public function fetchArray($statement, array $params = [], array $types = [])
{
return $this->executeQuery($statement, $params, $types)->fetch(FetchMode::NUMERIC);
}
/**
* Prepares and executes an SQL query and returns the value of a single column
* of the first row of the result.
*
* @deprecated Use fetchOne() instead.
*
* @param string $statement The SQL query to be executed.
* @param mixed[] $params The prepared statement params.
* @param int[]|string[] $types The query parameter types.
*
* @return mixed|false False is returned if no rows are found.
*
* @throws DBALException
*/
public function fetchColumn($statement, array $params = [], array $types = [])
{
return $this->executeQuery($statement, $params, $types)->fetchColumn();
}
/**
* Prepares and executes an SQL query and returns the first row of the result
* as an associative array.
......@@ -606,13 +533,7 @@ class Connection implements DriverConnection
public function fetchAssociative(string $query, array $params = [], array $types = [])
{
try {
$stmt = $this->executeQuery($query, $params, $types);
if ($stmt instanceof ForwardCompatibleResultStatement) {
return $stmt->fetchAssociative();
}
return $stmt->fetch(FetchMode::ASSOCIATIVE);
return $this->executeQuery($query, $params, $types)->fetchAssociative();
} catch (Throwable $e) {
throw DBALException::driverExceptionDuringQuery($this->_driver, $e, $query);
}
......@@ -633,13 +554,7 @@ class Connection implements DriverConnection
public function fetchNumeric(string $query, array $params = [], array $types = [])
{
try {
$stmt = $this->executeQuery($query, $params, $types);
if ($stmt instanceof ForwardCompatibleResultStatement) {
return $stmt->fetchNumeric();
}
return $stmt->fetch(FetchMode::NUMERIC);
return $this->executeQuery($query, $params, $types)->fetchNumeric();
} catch (Throwable $e) {
throw DBALException::driverExceptionDuringQuery($this->_driver, $e, $query);
}
......@@ -660,13 +575,7 @@ class Connection implements DriverConnection
public function fetchOne(string $query, array $params = [], array $types = [])
{
try {
$stmt = $this->executeQuery($query, $params, $types);
if ($stmt instanceof ForwardCompatibleResultStatement) {
return $stmt->fetchOne();
}
return $stmt->fetch(FetchMode::COLUMN);
return $this->executeQuery($query, $params, $types)->fetchOne();
} catch (Throwable $e) {
throw DBALException::driverExceptionDuringQuery($this->_driver, $e, $query);
}
......@@ -917,68 +826,60 @@ class Connection implements DriverConnection
}
/**
* Prepares and executes an SQL query and returns the result as an associative array.
* Prepares and executes an SQL query and returns the result as an array of numeric arrays.
*
* @deprecated Use fetchAllAssociative()
* @param string $query The SQL query.
* @param array<int, mixed>|array<string, mixed> $params The query parameters.
* @param array<int, int|string>|array<string, int|string> $types The query parameter types.
*
* @param string $sql The SQL query.
* @param mixed[] $params The query parameters.
* @param int[]|string[] $types The query parameter types.
* @return array<int,array<int,mixed>>
*
* @return mixed[]
* @throws DBALException
*/
public function fetchAll($sql, array $params = [], $types = [])
public function fetchAllNumeric(string $query, array $params = [], array $types = []) : array
{
return $this->executeQuery($sql, $params, $types)->fetchAll();
try {
return $this->executeQuery($query, $params, $types)->fetchAllNumeric();
} catch (Throwable $e) {
throw DBALException::driverExceptionDuringQuery($this->_driver, $e, $query);
}
}
/**
* Prepares and executes an SQL query and returns the result as an array of numeric arrays.
* Prepares and executes an SQL query and returns the result as an array of associative arrays.
*
* @param string $query The SQL query.
* @param array<int, mixed>|array<string, mixed> $params The query parameters.
* @param array<int, int|string>|array<string, int|string> $types The query parameter types.
*
* @return array<int,array<int,mixed>>
* @return array<int,array<string,mixed>>
*
* @throws DBALException
*/
public function fetchAllNumeric(string $query, array $params = [], array $types = []) : array
public function fetchAllAssociative(string $query, array $params = [], array $types = []) : array
{
try {
$stmt = $this->executeQuery($query, $params, $types);
if ($stmt instanceof ForwardCompatibleResultStatement) {
return $stmt->fetchAllNumeric();
}
return $stmt->fetchAll(FetchMode::NUMERIC);
return $this->executeQuery($query, $params, $types)->fetchAllAssociative();
} catch (Throwable $e) {
throw DBALException::driverExceptionDuringQuery($this->_driver, $e, $query);
}
}
/**
* Prepares and executes an SQL query and returns the result as an array of associative arrays.
* Prepares and executes an SQL query and returns the result as an array of the first column values.
*
* @param string $query The SQL query.
* @param array<int, mixed>|array<string, mixed> $params The query parameters.
* @param array<int, int|string>|array<string, int|string> $types The query parameter types.
*
* @return array<int,array<string,mixed>>
* @return array<int,mixed>
*
* @throws DBALException
*/
public function fetchAllAssociative(string $query, array $params = [], array $types = []) : array
public function fetchColumn(string $query, array $params = [], array $types = []) : array
{
try {
$stmt = $this->executeQuery($query, $params, $types);
if ($stmt instanceof ForwardCompatibleResultStatement) {
return $stmt->fetchAllAssociative();
}
return $stmt->fetchAll(FetchMode::ASSOCIATIVE);
return $this->executeQuery($query, $params, $types)->fetchColumn();
} catch (Throwable $e) {
throw DBALException::driverExceptionDuringQuery($this->_driver, $e, $query);
}
......@@ -1000,12 +901,8 @@ class Connection implements DriverConnection
try {
$stmt = $this->executeQuery($query, $params, $types);
if ($stmt instanceof ForwardCompatibleResultStatement) {
yield from $stmt->iterateNumeric();
} else {
while (($row = $stmt->fetch(FetchMode::NUMERIC)) !== false) {
yield $row;
}
while (($row = $stmt->fetchNumeric()) !== false) {
yield $row;
}
} catch (Throwable $e) {
throw DBALException::driverExceptionDuringQuery($this->_driver, $e, $query);
......@@ -1028,12 +925,8 @@ class Connection implements DriverConnection
try {
$stmt = $this->executeQuery($query, $params, $types);
if ($stmt instanceof ForwardCompatibleResultStatement) {
yield from $stmt->iterateAssociative();
} else {
while (($row = $stmt->fetch(FetchMode::ASSOCIATIVE)) !== false) {
yield $row;
}
while (($row = $stmt->fetchAssociative()) !== false) {
yield $row;
}
} catch (Throwable $e) {
throw DBALException::driverExceptionDuringQuery($this->_driver, $e, $query);
......@@ -1056,12 +949,8 @@ class Connection implements DriverConnection
try {
$stmt = $this->executeQuery($query, $params, $types);
if ($stmt instanceof ForwardCompatibleResultStatement) {
yield from $stmt->iterateColumn();
} else {
while (($value = $stmt->fetch(FetchMode::COLUMN)) !== false) {
yield $value;
}
while (($value = $stmt->fetchOne()) !== false) {
yield $value;
}
} catch (Throwable $e) {
throw DBALException::driverExceptionDuringQuery($this->_driver, $e, $query);
......@@ -1078,14 +967,10 @@ class Connection implements DriverConnection
public function prepare(string $sql) : DriverStatement
{
try {
$stmt = new Statement($sql, $this);
return new Statement($sql, $this);
} catch (Throwable $ex) {
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $sql);
}
$stmt->setFetchMode($this->defaultFetchMode);
return $stmt;
}
/**
......@@ -1134,8 +1019,6 @@ class Connection implements DriverConnection
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $query, $this->resolveParams($params, $types));
}
$stmt->setFetchMode($this->defaultFetchMode);
if ($logger !== null) {
$logger->stopQuery();
}
......@@ -1182,8 +1065,6 @@ class Connection implements DriverConnection
$stmt = new ResultCacheStatement($this->executeQuery($query, $params, $types), $resultCache, $cacheKey, $realKey, $qcp->getLifetime());
}
$stmt->setFetchMode($this->defaultFetchMode);
return $stmt;
}
......@@ -1202,8 +1083,6 @@ class Connection implements DriverConnection
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $sql);
}
$statement->setFetchMode($this->defaultFetchMode);
if ($logger !== null) {
$logger->stopQuery();
}
......
......@@ -352,8 +352,6 @@ class MasterSlaveConnection extends Connection
$statement = $this->_conn->query($sql);
$statement->setFetchMode($this->defaultFetchMode);
if ($logger !== null) {
$logger->stopQuery();
}
......
......@@ -196,7 +196,7 @@ abstract class AbstractMySQLDriver implements ExceptionConverterDriver, VersionA
{
$params = $conn->getParams();
return $params['dbname'] ?? $conn->query('SELECT DATABASE()')->fetchColumn();
return $params['dbname'] ?? $conn->query('SELECT DATABASE()')->fetchOne();
}
/**
......
......@@ -108,7 +108,7 @@ abstract class AbstractPostgreSQLDriver implements ExceptionConverterDriver, Ver
{
$params = $conn->getParams();
return $params['dbname'] ?? $conn->query('SELECT CURRENT_DATABASE()')->fetchColumn();
return $params['dbname'] ?? $conn->query('SELECT CURRENT_DATABASE()')->fetchOne();
}
/**
......
......@@ -105,7 +105,7 @@ abstract class AbstractSQLAnywhereDriver implements ExceptionConverterDriver, Ve
{
$params = $conn->getParams();
return $params['dbname'] ?? $conn->query('SELECT DB_NAME()')->fetchColumn();
return $params['dbname'] ?? $conn->query('SELECT DB_NAME()')->fetchOne();
}
/**
......
......@@ -19,7 +19,7 @@ abstract class AbstractSQLServerDriver implements Driver
{
$params = $conn->getParams();
return $params['dbname'] ?? $conn->query('SELECT DB_NAME()')->fetchColumn();
return $params['dbname'] ?? $conn->query('SELECT DB_NAME()')->fetchOne();
}
/**
......
......@@ -4,8 +4,6 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Driver;
use Doctrine\DBAL\ForwardCompatibility\Driver\ResultStatement;
/**
* @internal
*/
......@@ -58,4 +56,20 @@ final class FetchUtils
return $rows;
}
/**
* @return array<int,mixed>
*
* @throws DriverException
*/
public static function fetchColumn(ResultStatement $stmt) : array
{
$rows = [];
while (($row = $stmt->fetchOne()) !== false) {
$rows[] = $row;
}
return $rows;
}
}
......@@ -2,17 +2,14 @@
namespace Doctrine\DBAL\Driver\IBMDB2;
use Doctrine\DBAL\Driver\FetchUtils;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Driver\StatementIterator;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType;
use IteratorAggregate;
use function assert;
use function db2_bind_param;
use function db2_execute;
use function db2_fetch_array;
use function db2_fetch_assoc;
use function db2_fetch_both;
use function db2_free_result;
use function db2_num_fields;
use function db2_num_rows;
......@@ -32,7 +29,7 @@ use const DB2_LONG;
use const DB2_PARAM_FILE;
use const DB2_PARAM_IN;
class DB2Statement implements IteratorAggregate, Statement
class DB2Statement implements Statement
{
/** @var resource */
private $stmt;
......@@ -48,9 +45,6 @@ class DB2Statement implements IteratorAggregate, Statement
*/
private $lobs = [];
/** @var int */
private $defaultFetchMode = FetchMode::MIXED;
/**
* Indicates whether the statement is in the state when fetching results is possible
*
......@@ -196,99 +190,61 @@ class DB2Statement implements IteratorAggregate, Statement
}
/**
* {@inheritdoc}
*
* @deprecated Use one of the fetch- or iterate-related methods.
* {@inheritDoc}
*/
public function setFetchMode($fetchMode)
public function fetchNumeric()
{
$this->defaultFetchMode = $fetchMode;
return true;
}
if (! $this->result) {
return false;
}
/**
* {@inheritdoc}
*
* @deprecated Use iterateNumeric(), iterateAssociative() or iterateColumn() instead.
*/
public function getIterator()
{
return new StatementIterator($this);
return db2_fetch_array($this->stmt);
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
*/
public function fetch($fetchMode = null)
public function fetchAssociative()
{
// do not try fetching from the statement if it's not expected to contain result
// do not try fetching from the statement if it's not expected to contain the result
// in order to prevent exceptional situation
if (! $this->result) {
return false;
}
$fetchMode = $fetchMode ?? $this->defaultFetchMode;
switch ($fetchMode) {
case FetchMode::COLUMN:
return $this->fetchColumn();
case FetchMode::MIXED:
return db2_fetch_both($this->stmt);
case FetchMode::ASSOCIATIVE:
return db2_fetch_assoc($this->stmt);
case FetchMode::NUMERIC:
return db2_fetch_array($this->stmt);
default:
throw new DB2Exception('Given Fetch-Style ' . $fetchMode . ' is not supported.');
}
return db2_fetch_assoc($this->stmt);
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
*/
public function fetchAll($fetchMode = null)
public function fetchOne()
{
$rows = [];
switch ($fetchMode) {
case FetchMode::COLUMN:
while (($row = $this->fetchColumn()) !== false) {
$rows[] = $row;
}
break;
default:
while (($row = $this->fetch($fetchMode)) !== false) {
$rows[] = $row;
}
}
return $rows;
return FetchUtils::fetchOne($this);
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchOne() instead.
*/
public function fetchColumn()
public function fetchAllNumeric() : array
{
$row = $this->fetch(FetchMode::NUMERIC);
return FetchUtils::fetchAllNumeric($this);
}
if ($row === false) {
return false;
}
/**
* {@inheritdoc}
*/
public function fetchAllAssociative() : array
{
return FetchUtils::fetchAllAssociative($this);
}
return $row[0] ?? null;
/**
* {@inheritdoc}
*/
public function fetchColumn() : array
{
return FetchUtils::fetchColumn($this);
}
public function rowCount() : int
......
......@@ -4,12 +4,8 @@ namespace Doctrine\DBAL\Driver\Mysqli;
use Doctrine\DBAL\Driver\FetchUtils;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Driver\StatementIterator;
use Doctrine\DBAL\Exception\InvalidArgumentException;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ForwardCompatibility\Driver\ResultStatement as ForwardCompatibleResultStatement;
use Doctrine\DBAL\ParameterType;
use IteratorAggregate;
use mysqli;
use mysqli_stmt;
use function array_combine;
......@@ -25,7 +21,7 @@ use function is_resource;
use function sprintf;
use function str_repeat;
class MysqliStatement implements IteratorAggregate, Statement, ForwardCompatibleResultStatement
class MysqliStatement implements Statement
{
/** @var string[] */
protected static $_paramTypeMap = [
......@@ -62,9 +58,6 @@ class MysqliStatement implements IteratorAggregate, Statement, ForwardCompatible
*/
protected $_values = [];
/** @var int */
protected $_defaultFetchMode = FetchMode::MIXED;
/**
* Indicates whether the statement is in the state when fetching results is possible
*
......@@ -308,97 +301,6 @@ class MysqliStatement implements IteratorAggregate, Statement, ForwardCompatible
/**
* {@inheritdoc}
*
* @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
*/
public function fetch($fetchMode = null)
{
// do not try fetching from the statement if it's not expected to contain result
// in order to prevent exceptional situation
if (! $this->result) {
return false;
}
$fetchMode = $fetchMode ?? $this->_defaultFetchMode;
if ($fetchMode === FetchMode::COLUMN) {
return $this->fetchColumn();
}
$values = $this->_fetch();
if ($values === null) {
return false;
}
if ($values === false) {
throw new MysqliException($this->_stmt->error, $this->_stmt->sqlstate, $this->_stmt->errno);
}
if ($fetchMode === FetchMode::NUMERIC) {
return $values;
}
assert(is_array($this->_columnNames));
$assoc = array_combine($this->_columnNames, $values);
assert(is_array($assoc));
switch ($fetchMode) {
case FetchMode::ASSOCIATIVE:
return $assoc;
case FetchMode::MIXED:
return $assoc + $values;
default:
throw new MysqliException(sprintf("Unknown fetch type '%s'", $fetchMode));
}
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
*/
public function fetchAll($fetchMode = null)
{
$fetchMode = $fetchMode ?? $this->_defaultFetchMode;
$rows = [];
if ($fetchMode === FetchMode::COLUMN) {
while (($row = $this->fetchColumn()) !== false) {
$rows[] = $row;
}
} else {
while (($row = $this->fetch($fetchMode)) !== false) {
$rows[] = $row;
}
}
return $rows;
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchOne() instead.
*/
public function fetchColumn()
{
$row = $this->fetch(FetchMode::NUMERIC);
if ($row === false) {
return false;
}
return $row[0] ?? null;
}
/**
* {@inheritdoc}
*
* @deprecated The error information is available via exceptions.
*/
public function fetchNumeric()
{
......@@ -463,6 +365,14 @@ class MysqliStatement implements IteratorAggregate, Statement, ForwardCompatible
return FetchUtils::fetchAllAssociative($this);
}
/**
* {@inheritdoc}
*/
public function fetchColumn() : array
{
return FetchUtils::fetchColumn($this);
}
/**
* {@inheritdoc}
*/
......@@ -490,26 +400,4 @@ class MysqliStatement implements IteratorAggregate, Statement, ForwardCompatible
{
return $this->_stmt->field_count;
}
/**
* {@inheritdoc}
*
* @deprecated Use one of the fetch- or iterate-related methods.
*/
public function setFetchMode($fetchMode)
{
$this->_defaultFetchMode = $fetchMode;
return true;
}
/**
* {@inheritdoc}
*
* @deprecated Use iterateNumeric(), iterateAssociative() or iterateColumn() instead.
*/
public function getIterator()
{
return new StatementIterator($this);
}
}
......@@ -146,9 +146,7 @@ class OCI8Connection implements Connection, ServerInfoAwareConnection
return false;
}
$sql = 'SELECT ' . $name . '.CURRVAL FROM DUAL';
$stmt = $this->query($sql);
$result = $stmt->fetchColumn();
$result = $this->query('SELECT ' . $name . '.CURRVAL FROM DUAL')->fetchOne();
if ($result === false) {
throw new OCI8Exception('lastInsertId failed: Query was executed but no result was returned.');
......
......@@ -4,12 +4,7 @@ namespace Doctrine\DBAL\Driver\OCI8;
use Doctrine\DBAL\Driver\FetchUtils;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Driver\StatementIterator;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ForwardCompatibility\Driver\ResultStatement as ForwardCompatibleResultStatement;
use Doctrine\DBAL\ParameterType;
use InvalidArgumentException;
use IteratorAggregate;
use function assert;
use function count;
use function implode;
......@@ -32,7 +27,6 @@ use function substr;
use const OCI_ASSOC;
use const OCI_B_BIN;
use const OCI_B_BLOB;
use const OCI_BOTH;
use const OCI_D_LOB;
use const OCI_FETCHSTATEMENT_BY_COLUMN;
use const OCI_FETCHSTATEMENT_BY_ROW;
......@@ -46,7 +40,7 @@ use const SQLT_CHR;
/**
* The OCI8 implementation of the Statement interface.
*/
class OCI8Statement implements IteratorAggregate, Statement, ForwardCompatibleResultStatement
class OCI8Statement implements Statement
{
/** @var resource */
protected $_dbh;
......@@ -64,17 +58,6 @@ class OCI8Statement implements IteratorAggregate, Statement, ForwardCompatibleRe
*/
protected static $_PARAM = ':param';
/** @var int[] */
protected static $fetchModeMap = [
FetchMode::MIXED => OCI_BOTH,
FetchMode::ASSOCIATIVE => OCI_ASSOC,
FetchMode::NUMERIC => OCI_NUM,
FetchMode::COLUMN => OCI_NUM,
];
/** @var int */
protected $_defaultFetchMode = FetchMode::MIXED;
/** @var string[] */
protected $_paramMap = [];
......@@ -380,127 +363,6 @@ class OCI8Statement implements IteratorAggregate, Statement, ForwardCompatibleRe
return $ret;
}
/**
* {@inheritdoc}
*
* @deprecated Use one of the fetch- or iterate-related methods.
*/
public function setFetchMode($fetchMode)
{
$this->_defaultFetchMode = $fetchMode;
return true;
}
/**
* {@inheritdoc}
*
* @deprecated Use iterateNumeric(), iterateAssociative() or iterateColumn() instead.
*/
public function getIterator()
{
return new StatementIterator($this);
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
*/
public function fetch($fetchMode = null)
{
// do not try fetching from the statement if it's not expected to contain result
// in order to prevent exceptional situation
if (! $this->result) {
return false;
}
$fetchMode = $fetchMode ?? $this->_defaultFetchMode;
if ($fetchMode === FetchMode::COLUMN) {
return $this->fetchColumn();
}
if (! isset(self::$fetchModeMap[$fetchMode])) {
throw new InvalidArgumentException('Invalid fetch style: ' . $fetchMode);
}
return oci_fetch_array(
$this->_sth,
self::$fetchModeMap[$fetchMode] | OCI_RETURN_NULLS | OCI_RETURN_LOBS
);
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
*/
public function fetchAll($fetchMode = null)
{
$fetchMode = $fetchMode ?? $this->_defaultFetchMode;
$result = [];
if (! isset(self::$fetchModeMap[$fetchMode])) {
throw new InvalidArgumentException('Invalid fetch style: ' . $fetchMode);
}
if (self::$fetchModeMap[$fetchMode] === OCI_BOTH) {
while ($row = $this->fetch($fetchMode)) {
$result[] = $row;
}
} else {
$fetchStructure = OCI_FETCHSTATEMENT_BY_ROW;
if ($fetchMode === FetchMode::COLUMN) {
$fetchStructure = OCI_FETCHSTATEMENT_BY_COLUMN;
}
// do not try fetching from the statement if it's not expected to contain result
// in order to prevent exceptional situation
if (! $this->result) {
return [];
}
oci_fetch_all(
$this->_sth,
$result,
0,
-1,
self::$fetchModeMap[$fetchMode] | OCI_RETURN_NULLS | $fetchStructure | OCI_RETURN_LOBS
);
if ($fetchMode === FetchMode::COLUMN) {
$result = $result[0];
}
}
return $result;
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchOne() instead.
*/
public function fetchColumn()
{
// do not try fetching from the statement if it's not expected to contain result
// in order to prevent exceptional situation
if (! $this->result) {
return false;
}
$row = oci_fetch_array($this->_sth, OCI_NUM | OCI_RETURN_NULLS | OCI_RETURN_LOBS);
if ($row === false) {
return false;
}
return $row[0] ?? null;
}
public function rowCount() : int
{
$count = oci_num_rows($this->_sth);
......@@ -517,7 +379,7 @@ class OCI8Statement implements IteratorAggregate, Statement, ForwardCompatibleRe
*/
public function fetchNumeric()
{
return $this->doFetch(OCI_NUM);
return $this->fetch(OCI_NUM);
}
/**
......@@ -525,7 +387,7 @@ class OCI8Statement implements IteratorAggregate, Statement, ForwardCompatibleRe
*/
public function fetchAssociative()
{
return $this->doFetch(OCI_ASSOC);
return $this->fetch(OCI_ASSOC);
}
/**
......@@ -541,7 +403,7 @@ class OCI8Statement implements IteratorAggregate, Statement, ForwardCompatibleRe
*/
public function fetchAllNumeric() : array
{
return $this->doFetchAll(OCI_NUM, OCI_FETCHSTATEMENT_BY_ROW);
return $this->fetchAll(OCI_NUM, OCI_FETCHSTATEMENT_BY_ROW);
}
/**
......@@ -549,13 +411,21 @@ class OCI8Statement implements IteratorAggregate, Statement, ForwardCompatibleRe
*/
public function fetchAllAssociative() : array
{
return $this->doFetchAll(OCI_ASSOC, OCI_FETCHSTATEMENT_BY_ROW);
return $this->fetchAll(OCI_ASSOC, OCI_FETCHSTATEMENT_BY_ROW);
}
/**
* {@inheritdoc}
*/
public function fetchColumn() : array
{
return $this->fetchAll(OCI_NUM, OCI_FETCHSTATEMENT_BY_COLUMN)[0];
}
/**
* @return mixed|false
*/
private function doFetch(int $mode)
private function fetch(int $mode)
{
// do not try fetching from the statement if it's not expected to contain the result
// in order to prevent exceptional situation
......@@ -572,7 +442,7 @@ class OCI8Statement implements IteratorAggregate, Statement, ForwardCompatibleRe
/**
* @return array<mixed>
*/
private function doFetchAll(int $mode, int $fetchStructure) : array
private function fetchAll(int $mode, int $fetchStructure) : array
{
// do not try fetching from the statement if it's not expected to contain the result
// in order to prevent exceptional situation
......
......@@ -4,7 +4,6 @@ namespace Doctrine\DBAL\Driver\PDOSqlsrv;
use Doctrine\DBAL\Driver\PDOConnection;
use Doctrine\DBAL\Driver\PDOStatement;
use Doctrine\DBAL\ForwardCompatibility\Driver\ResultStatement as ForwardCompatibleResultStatement;
use Doctrine\DBAL\ParameterType;
use function strpos;
use function substr;
......@@ -26,11 +25,7 @@ class Connection extends PDOConnection
$stmt = $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?');
$stmt->execute([$name]);
if ($stmt instanceof ForwardCompatibleResultStatement) {
return $stmt->fetchOne();
}
return $stmt->fetchColumn();
return $stmt->fetchOne();
}
/**
......
......@@ -2,11 +2,8 @@
namespace Doctrine\DBAL\Driver;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ForwardCompatibility\Driver\ResultStatement as ForwardCompatibleResultStatement;
use Doctrine\DBAL\ParameterType;
use InvalidArgumentException;
use IteratorAggregate;
use PDO;
use function array_slice;
use function assert;
......@@ -17,7 +14,7 @@ use function is_array;
* The PDO implementation of the Statement interface.
* Used by all PDO-based drivers.
*/
class PDOStatement implements IteratorAggregate, Statement, ForwardCompatibleResultStatement
class PDOStatement implements Statement
{
private const PARAM_TYPE_MAP = [
ParameterType::NULL => PDO::PARAM_NULL,
......@@ -28,13 +25,6 @@ class PDOStatement implements IteratorAggregate, Statement, ForwardCompatibleRes
ParameterType::BOOLEAN => PDO::PARAM_BOOL,
];
private const FETCH_MODE_MAP = [
FetchMode::ASSOCIATIVE => PDO::FETCH_ASSOC,
FetchMode::NUMERIC => PDO::FETCH_NUM,
FetchMode::MIXED => PDO::FETCH_BOTH,
FetchMode::COLUMN => PDO::FETCH_COLUMN,
];
/** @var \PDOStatement */
private $stmt;
......@@ -43,22 +33,6 @@ class PDOStatement implements IteratorAggregate, Statement, ForwardCompatibleRes
$this->stmt = $stmt;
}
/**
* {@inheritdoc}
*
* @deprecated Use one of the fetch- or iterate-related methods.
*/
public function setFetchMode($fetchMode)
{
$fetchMode = $this->convertFetchMode($fetchMode);
try {
return $this->stmt->setFetchMode($fetchMode);
} catch (\PDOException $exception) {
throw new PDOException($exception);
}
}
/**
* {@inheritdoc}
*/
......@@ -132,64 +106,6 @@ class PDOStatement implements IteratorAggregate, Statement, ForwardCompatibleRes
return $this->stmt->rowCount();
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
*/
public function fetch($fetchMode = null)
{
try {
if ($fetchMode === null) {
return $this->stmt->fetch();
}
return $this->stmt->fetch(
$this->convertFetchMode($fetchMode)
);
} catch (\PDOException $exception) {
throw new PDOException($exception);
}
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
*/
public function fetchAll($fetchMode = null)
{
try {
if ($fetchMode === null) {
$data = $this->stmt->fetchAll();
} else {
$data = $this->stmt->fetchAll(
$this->convertFetchMode($fetchMode)
);
}
} catch (\PDOException $exception) {
throw new PDOException($exception);
}
assert(is_array($data));
return $data;
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchOne() instead.
*/
public function fetchColumn()
{
try {
return $this->stmt->fetchColumn();
} catch (\PDOException $exception) {
throw new PDOException($exception);
}
}
/**
* {@inheritdoc}
*/
......@@ -231,38 +147,56 @@ class PDOStatement implements IteratorAggregate, Statement, ForwardCompatibleRes
}
/**
* Converts DBAL parameter type to PDO parameter type
* {@inheritdoc}
*/
public function fetchColumn() : array
{
return $this->fetchAll(PDO::FETCH_COLUMN);
}
/**
* @return mixed|false
*
* @param int $type Parameter type
* @throws PDOException
*/
private function convertParamType(int $type) : int
private function fetch(int $mode)
{
if (! isset(self::PARAM_TYPE_MAP[$type])) {
throw new InvalidArgumentException('Invalid parameter type: ' . $type);
try {
return $this->stmt->fetch($mode);
} catch (\PDOException $exception) {
throw new PDOException($exception);
}
return self::PARAM_TYPE_MAP[$type];
}
/**
* Converts DBAL fetch mode to PDO fetch mode
* @return array<int,mixed>
*
* @param int $fetchMode Fetch mode
* @throws PDOException
*/
private function convertFetchMode(int $fetchMode) : int
private function fetchAll(int $mode) : array
{
if (! isset(self::FETCH_MODE_MAP[$fetchMode])) {
throw new InvalidArgumentException('Invalid fetch mode: ' . $fetchMode);
try {
$data = $this->stmt->fetchAll($mode);
} catch (\PDOException $exception) {
throw new PDOException($exception);
}
return self::FETCH_MODE_MAP[$fetchMode];
assert(is_array($data));
return $data;
}
/**
* {@inheritdoc}
* Converts DBAL parameter type to PDO parameter type
*
* @param int $type Parameter type
*/
public function getIterator()
private function convertParamType(int $type) : int
{
yield from $this->stmt;
if (! isset(self::PARAM_TYPE_MAP[$type])) {
throw new InvalidArgumentException('Invalid parameter type: ' . $type);
}
return self::PARAM_TYPE_MAP[$type];
}
}
<?php
namespace Doctrine\DBAL\Driver;
declare(strict_types=1);
use Traversable;
namespace Doctrine\DBAL\Driver;
/**
* Interface for the reading part of a prepare statement only.
*/
interface ResultStatement extends Traversable
interface ResultStatement
{
/**
* Closes the cursor, enabling the statement to be executed again.
......@@ -20,56 +20,62 @@ interface ResultStatement extends Traversable
* Returns the number of columns in the result set
*
* @return int The number of columns in the result set represented
* by the PDOStatement object. If there is no result set,
* this method should return 0.
* by the statement. If there is no result set,
* this method should return 0.
*/
public function columnCount();
/**
* Sets the fetch mode to use while iterating this statement.
*
* @deprecated Use one of the fetch- or iterate-related methods.
* Returns the next row of a result set as a numeric array or FALSE if there are no more rows.
*
* @param int $fetchMode Controls how the next row will be returned to the caller.
* The value must be one of the {@link \Doctrine\DBAL\FetchMode} constants.
* @return array<int,mixed>|false
*
* @return bool
* @throws DriverException
*/
public function setFetchMode($fetchMode);
public function fetchNumeric();
/**
* Returns the next row of a result set.
* Returns the next row of a result set as an associative array or FALSE if there are no more rows.
*
* @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
* @return array<string,mixed>|false
*
* @param int|null $fetchMode Controls how the next row will be returned to the caller.
* The value must be one of the {@link \Doctrine\DBAL\FetchMode} constants,
* defaulting to {@link \Doctrine\DBAL\FetchMode::MIXED}.
* @throws DriverException
*/
public function fetchAssociative();
/**
* Returns the first value of the next row of a result set or FALSE if there are no more rows.
*
* @return mixed The return value of this method on success depends on the fetch mode. In all cases, FALSE is
* returned on failure.
* @return mixed|false
*
* @throws DriverException
*/
public function fetch($fetchMode = null);
public function fetchOne();
/**
* Returns an array containing all of the result set rows.
* Returns an array containing all of the result set rows represented as numeric arrays.
*
* @return array<int,array<int,mixed>>
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
* @throws DriverException
*/
public function fetchAllNumeric() : array;
/**
* Returns an array containing all of the result set rows represented as associative arrays.
*
* @param int|null $fetchMode Controls how the next row will be returned to the caller.
* The value must be one of the {@link \Doctrine\DBAL\FetchMode} constants,
* defaulting to {@link \Doctrine\DBAL\FetchMode::MIXED}.
* @return array<int,array<string,mixed>>
*
* @return mixed[]
* @throws DriverException
*/
public function fetchAll($fetchMode = null);
public function fetchAllAssociative() : array;
/**
* Returns a single column from the next row of a result set or FALSE if there are no more rows.
* Returns an array containing the values of the first column of the result set.
*
* @deprecated Use fetchOne() instead.
* @return array<int,mixed>
*
* @return mixed|false A single column in the next row of a result set, or FALSE if there are no more rows.
* @throws DriverException
*/
public function fetchColumn();
public function fetchColumn() : array;
}
......@@ -5,7 +5,6 @@ namespace Doctrine\DBAL\Driver\SQLAnywhere;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\ForwardCompatibility\Driver\ResultStatement as ForwardCompatibleResultStatement;
use Doctrine\DBAL\ParameterType;
use function assert;
use function is_float;
......@@ -101,14 +100,7 @@ class SQLAnywhereConnection implements ServerInfoAwareConnection
*/
public function getServerVersion()
{
$stmt = $this->query("SELECT PROPERTY('ProductVersion')");
if ($stmt instanceof ForwardCompatibleResultStatement) {
$version = $stmt->fetchOne();
} else {
$version = $stmt->fetchColumn();
}
$version = $this->query("SELECT PROPERTY('ProductVersion')")->fetchOne();
assert(is_string($version));
return $version;
......@@ -123,13 +115,7 @@ class SQLAnywhereConnection implements ServerInfoAwareConnection
return sasql_insert_id($this->connection);
}
$stmt = $this->query('SELECT ' . $name . '.CURRVAL');
if ($stmt instanceof ForwardCompatibleResultStatement) {
return $stmt->fetchOne();
}
return $stmt->fetchColumn();
return $this->query('SELECT ' . $name . '.CURRVAL')->fetchOne();
}
public function prepare(string $sql) : DriverStatement
......
......@@ -5,16 +5,11 @@ namespace Doctrine\DBAL\Driver\SQLAnywhere;
use Doctrine\DBAL\Driver\DriverException;
use Doctrine\DBAL\Driver\FetchUtils;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Driver\StatementIterator;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ForwardCompatibility\Driver\ResultStatement as ForwardCompatibleResultStatement;
use Doctrine\DBAL\ParameterType;
use IteratorAggregate;
use function array_key_exists;
use function assert;
use function is_int;
use function is_resource;
use function sasql_fetch_array;
use function sasql_fetch_assoc;
use function sasql_fetch_row;
use function sasql_prepare;
......@@ -24,19 +19,15 @@ use function sasql_stmt_execute;
use function sasql_stmt_field_count;
use function sasql_stmt_reset;
use function sasql_stmt_result_metadata;
use const SASQL_BOTH;
/**
* SAP SQL Anywhere implementation of the Statement interface.
*/
class SQLAnywhereStatement implements IteratorAggregate, Statement, ForwardCompatibleResultStatement
class SQLAnywhereStatement implements Statement
{
/** @var resource The connection resource. */
private $conn;
/** @var int Default fetch mode to use. */
private $defaultFetchMode = FetchMode::MIXED;
/** @var resource|null The result set resource to fetch. */
private $result;
......@@ -167,91 +158,8 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement, ForwardCompa
/**
* {@inheritdoc}
*
* @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
*
* @throws SQLAnywhereException
*/
public function fetch($fetchMode = null)
{
if (! is_resource($this->result)) {
return false;
}
$fetchMode = $fetchMode ?? $this->defaultFetchMode;
switch ($fetchMode) {
case FetchMode::COLUMN:
return $this->fetchColumn();
case FetchMode::ASSOCIATIVE:
return sasql_fetch_assoc($this->result);
case FetchMode::MIXED:
return sasql_fetch_array($this->result, SASQL_BOTH);
case FetchMode::NUMERIC:
return sasql_fetch_row($this->result);
default:
throw new SQLAnywhereException('Fetch mode is not supported: ' . $fetchMode);
}
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
*/
public function fetchAll($fetchMode = null)
{
$rows = [];
switch ($fetchMode) {
case FetchMode::COLUMN:
while (($row = $this->fetchColumn()) !== false) {
$rows[] = $row;
}
break;
default:
while (($row = $this->fetch($fetchMode)) !== false) {
$rows[] = $row;
}
}
return $rows;
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchOne() instead.
*/
public function fetchColumn()
{
$row = $this->fetch(FetchMode::NUMERIC);
if ($row === false) {
return false;
}
return $row[0] ?? null;
}
/**
* {@inheritdoc}
*
* @deprecated Use iterateNumeric(), iterateAssociative() or iterateColumn() instead.
*/
public function getIterator()
{
return new StatementIterator($this);
}
/**
* {@inheritDoc}
*/
public function fetchNumeric()
{
if (! is_resource($this->result)) {
......@@ -303,20 +211,18 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement, ForwardCompa
return FetchUtils::fetchAllAssociative($this);
}
public function rowCount() : int
{
return sasql_stmt_affected_rows($this->stmt);
}
/**
* {@inheritdoc}
* @return array<int,mixed>
*
* @deprecated Use one of the fetch- or iterate-related methods.
* @throws DriverException
*/
public function setFetchMode($fetchMode)
public function fetchColumn() : array
{
$this->defaultFetchMode = $fetchMode;
return FetchUtils::fetchColumn($this);
}
return true;
public function rowCount() : int
{
return sasql_stmt_affected_rows($this->stmt);
}
}
......@@ -5,7 +5,6 @@ namespace Doctrine\DBAL\Driver\SQLSrv;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\ForwardCompatibility\Driver\ResultStatement as ForwardCompatibleResultStatement;
use Doctrine\DBAL\ParameterType;
use function is_float;
use function is_int;
......@@ -129,11 +128,7 @@ class SQLSrvConnection implements ServerInfoAwareConnection
$stmt = $this->query('SELECT @@IDENTITY');
}
if ($stmt instanceof ForwardCompatibleResultStatement) {
return $stmt->fetchOne();
}
return $stmt->fetchColumn();
return $stmt->fetchOne();
}
/**
......
......@@ -4,11 +4,7 @@ namespace Doctrine\DBAL\Driver\SQLSrv;
use Doctrine\DBAL\Driver\FetchUtils;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Driver\StatementIterator;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ForwardCompatibility\Driver\ResultStatement as ForwardCompatibleResultStatement;
use Doctrine\DBAL\ParameterType;
use IteratorAggregate;
use function is_int;
use function is_numeric;
use function sqlsrv_execute;
......@@ -25,14 +21,13 @@ use function SQLSRV_SQLTYPE_VARBINARY;
use function stripos;
use const SQLSRV_ENC_BINARY;
use const SQLSRV_FETCH_ASSOC;
use const SQLSRV_FETCH_BOTH;
use const SQLSRV_FETCH_NUMERIC;
use const SQLSRV_PARAM_IN;
/**
* SQL Server Statement.
*/
class SQLSrvStatement implements IteratorAggregate, Statement, ForwardCompatibleResultStatement
final class SQLSrvStatement implements Statement
{
/**
* The SQLSRV Resource.
......@@ -69,24 +64,6 @@ class SQLSrvStatement implements IteratorAggregate, Statement, ForwardCompatible
*/
private $types = [];
/**
* Translations.
*
* @var int[]
*/
private static $fetchMap = [
FetchMode::MIXED => SQLSRV_FETCH_BOTH,
FetchMode::ASSOCIATIVE => SQLSRV_FETCH_ASSOC,
FetchMode::NUMERIC => SQLSRV_FETCH_NUMERIC,
];
/**
* The fetch style.
*
* @var int
*/
private $defaultFetchMode = FetchMode::MIXED;
/**
* The last insert ID.
*
......@@ -279,104 +256,12 @@ class SQLSrvStatement implements IteratorAggregate, Statement, ForwardCompatible
return $stmt;
}
/**
* {@inheritdoc}
*
* @deprecated Use one of the fetch- or iterate-related methods.
*/
public function setFetchMode($fetchMode)
{
$this->defaultFetchMode = $fetchMode;
return true;
}
/**
* {@inheritdoc}
*
* @deprecated Use iterateNumeric(), iterateAssociative() or iterateColumn() instead.
*/
public function getIterator()
{
return new StatementIterator($this);
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
*
* @throws SQLSrvException
*/
public function fetch($fetchMode = null)
{
// do not try fetching from the statement if it's not expected to contain result
// in order to prevent exceptional situation
if ($this->stmt === null || ! $this->result) {
return false;
}
$fetchMode = $fetchMode ?? $this->defaultFetchMode;
if ($fetchMode === FetchMode::COLUMN) {
return $this->fetchColumn();
}
if (isset(self::$fetchMap[$fetchMode])) {
return sqlsrv_fetch_array($this->stmt, self::$fetchMap[$fetchMode]) ?? false;
}
throw new SQLSrvException('Fetch mode is not supported!');
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
*/
public function fetchAll($fetchMode = null)
{
$rows = [];
switch ($fetchMode) {
case FetchMode::COLUMN:
while (($row = $this->fetchColumn()) !== false) {
$rows[] = $row;
}
break;
default:
while (($row = $this->fetch($fetchMode)) !== false) {
$rows[] = $row;
}
}
return $rows;
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchOne() instead.
*/
public function fetchColumn()
{
$row = $this->fetch(FetchMode::NUMERIC);
if ($row === false) {
return false;
}
return $row[0] ?? null;
}
/**
* {@inheritdoc}
*/
public function fetchNumeric()
{
return $this->doFetch(SQLSRV_FETCH_NUMERIC);
return $this->fetch(SQLSRV_FETCH_NUMERIC);
}
/**
......@@ -384,7 +269,7 @@ class SQLSrvStatement implements IteratorAggregate, Statement, ForwardCompatible
*/
public function fetchAssociative()
{
return $this->doFetch(SQLSRV_FETCH_ASSOC);
return $this->fetch(SQLSRV_FETCH_ASSOC);
}
/**
......@@ -411,6 +296,14 @@ class SQLSrvStatement implements IteratorAggregate, Statement, ForwardCompatible
return FetchUtils::fetchAllAssociative($this);
}
/**
* {@inheritdoc}
*/
public function fetchColumn() : array
{
return FetchUtils::fetchColumn($this);
}
public function rowCount() : int
{
if ($this->stmt === null) {
......@@ -429,7 +322,7 @@ class SQLSrvStatement implements IteratorAggregate, Statement, ForwardCompatible
/**
* @return mixed|false
*/
private function doFetch(int $fetchType)
private function fetch(int $fetchType)
{
// do not try fetching from the statement if it's not expected to contain the result
// in order to prevent exceptional situation
......
<?php
namespace Doctrine\DBAL\Driver;
use IteratorAggregate;
/**
* @deprecated Use iterateNumeric(), iterateAssociative() or iterateColumn().
*/
class StatementIterator implements IteratorAggregate
{
/** @var ResultStatement */
private $statement;
public function __construct(ResultStatement $statement)
{
$this->statement = $statement;
}
/**
* {@inheritdoc}
*/
public function getIterator()
{
while (($result = $this->statement->fetch()) !== false) {
yield $result;
}
}
}
<?php
namespace Doctrine\DBAL;
/**
* Contains statement fetch modes.
*
* @deprecated Use one of the fetch- or iterate-related methods on the Statement.
*/
final class FetchMode
{
/**
* Specifies that the fetch method shall return each row as an array indexed
* by column name as returned in the corresponding result set. If the result
* set contains multiple columns with the same name, the statement returns
* only a single value per column name.
*
* @see \PDO::FETCH_ASSOC
*/
public const ASSOCIATIVE = 2;
/**
* Specifies that the fetch method shall return each row as an array indexed
* by column number as returned in the corresponding result set, starting at
* column 0.
*
* @see \PDO::FETCH_NUM
*/
public const NUMERIC = 3;
/**
* Specifies that the fetch method shall return each row as an array indexed
* by both column name and number as returned in the corresponding result set,
* starting at column 0.
*
* @see \PDO::FETCH_BOTH
*/
public const MIXED = 4;
/**
* Specifies that the fetch method shall return only a single requested
* column from the next row in the result set.
*
* @see \PDO::FETCH_COLUMN
*/
public const COLUMN = 7;
/**
* This class cannot be instantiated.
*/
private function __construct()
{
}
}
......@@ -4,56 +4,13 @@ declare(strict_types=1);
namespace Doctrine\DBAL\ForwardCompatibility\Driver;
use Doctrine\DBAL\Driver\DriverException;
use Doctrine\DBAL\Driver\ResultStatement as BaseResultStatement;
/**
* Forward compatibility extension for the ResultStatement interface.
*
* @deprecated
*/
interface ResultStatement extends BaseResultStatement
{
/**
* Returns the next row of a result set as a numeric array or FALSE if there are no more rows.
*
* @return array<int,mixed>|false
*
* @throws DriverException
*/
public function fetchNumeric();
/**
* Returns the next row of a result set as an associative array or FALSE if there are no more rows.
*
* @return array<string,mixed>|false
*
* @throws DriverException
*/
public function fetchAssociative();
/**
* Returns the first value of the next row of a result set or FALSE if there are no more rows.
*
* @return mixed|false
*
* @throws DriverException
*/
public function fetchOne();
/**
* Returns an array containing all of the result set rows represented as numeric arrays.
*
* @return array<int,array<int,mixed>>
*
* @throws DriverException
*/
public function fetchAllNumeric() : array;
/**
* Returns an array containing all of the result set rows represented as associative arrays.
*
* @return array<int,array<string,mixed>>
*
* @throws DriverException
*/
public function fetchAllAssociative() : array;
}
......@@ -4,39 +4,13 @@ declare(strict_types=1);
namespace Doctrine\DBAL\ForwardCompatibility;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\ForwardCompatibility\Driver\ResultStatement as BaseResultStatement;
use Traversable;
/**
* Forward compatibility extension for the DBAL ResultStatement interface.
*
* @deprecated
*/
interface ResultStatement extends BaseResultStatement
{
/**
* Returns an iterator over the result set rows represented as numeric arrays.
*
* @return Traversable<int,array<int,mixed>>
*
* @throws DBALException
*/
public function iterateNumeric() : Traversable;
/**
* Returns an iterator over the result set rows represented as associative arrays.
*
* @return Traversable<int,array<string,mixed>>
*
* @throws DBALException
*/
public function iterateAssociative() : Traversable;
/**
* Returns an iterator over the values of the first column of the result set.
*
* @return Traversable<int,mixed>
*
* @throws DBALException
*/
public function iterateColumn() : Traversable;
}
......@@ -98,28 +98,20 @@ class Connection extends \Doctrine\DBAL\Connection
*/
public function executeQuery(string $query, array $params = [], $types = [], ?QueryCacheProfile $qcp = null) : ResultStatement
{
$stmt = new Statement(parent::executeQuery($query, $params, $types, $qcp), $this);
$stmt->setFetchMode($this->defaultFetchMode);
return $stmt;
return new Statement(parent::executeQuery($query, $params, $types, $qcp), $this);
}
public function prepare(string $sql) : DriverStatement
{
$stmt = new Statement(parent::prepare($sql), $this);
$stmt->setFetchMode($this->defaultFetchMode);
return $stmt;
return new Statement(parent::prepare($sql), $this);
}
public function query(string $sql) : ResultStatement
{
$connection = $this->getWrappedConnection();
$stmt = $connection->query($sql);
$stmt = new Statement($stmt, $this);
$stmt->setFetchMode($this->defaultFetchMode);
return $stmt;
return new Statement(
$this->getWrappedConnection()
->query($sql),
$this
);
}
}
......@@ -4,11 +4,7 @@ namespace Doctrine\DBAL\Portability;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\Driver\StatementIterator;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ForwardCompatibility\Driver\ResultStatement as ForwardCompatibleResultStatement;
use Doctrine\DBAL\ParameterType;
use IteratorAggregate;
use function array_change_key_case;
use function assert;
use function is_string;
......@@ -17,7 +13,7 @@ use function rtrim;
/**
* Portability wrapper for a Statement.
*/
class Statement implements IteratorAggregate, DriverStatement, ForwardCompatibleResultStatement
class Statement implements DriverStatement
{
/** @var int */
private $portability;
......@@ -28,9 +24,6 @@ class Statement implements IteratorAggregate, DriverStatement, ForwardCompatible
/** @var int */
private $case;
/** @var int */
private $defaultFetchMode = FetchMode::MIXED;
/**
* Wraps <tt>Statement</tt> and applies portability measures.
*
......@@ -89,79 +82,15 @@ class Statement implements IteratorAggregate, DriverStatement, ForwardCompatible
return $this->stmt->execute($params);
}
/**
* {@inheritdoc}
*
* @deprecated Use one of the fetch- or iterate-related methods.
*/
public function setFetchMode($fetchMode)
{
$this->defaultFetchMode = $fetchMode;
return $this->stmt->setFetchMode($fetchMode);
}
/**
* {@inheritdoc}
*
* @deprecated Use iterateNumeric(), iterateAssociative() or iterateColumn() instead.
*/
public function getIterator()
{
return new StatementIterator($this);
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
*/
public function fetch($fetchMode = null)
{
$fetchMode = $fetchMode ?? $this->defaultFetchMode;
$row = $this->stmt->fetch($fetchMode);
$iterateRow = ($this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM)) !== 0;
$fixCase = $this->case !== null
&& ($fetchMode === FetchMode::ASSOCIATIVE || $fetchMode === FetchMode::MIXED)
&& ($this->portability & Connection::PORTABILITY_FIX_CASE) !== 0;
$row = $this->fixRow($row, $iterateRow, $fixCase);
return $row;
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
*/
public function fetchAll($fetchMode = null)
{
$fetchMode = $fetchMode ?? $this->defaultFetchMode;
$rows = $this->stmt->fetchAll($fetchMode);
$fixCase = $this->case !== null
&& ($fetchMode === FetchMode::ASSOCIATIVE || $fetchMode === FetchMode::MIXED)
&& ($this->portability & Connection::PORTABILITY_FIX_CASE) !== 0;
return $this->fixResultSet($rows, $fixCase, $fetchMode !== FetchMode::COLUMN);
}
/**
* {@inheritdoc}
*/
public function fetchNumeric()
{
if ($this->stmt instanceof ForwardCompatibleResultStatement) {
$row = $this->stmt->fetchNumeric();
} else {
$row = $this->stmt->fetch(FetchMode::NUMERIC);
}
return $this->fixResult($row, false);
return $this->fixResult(
$this->stmt->fetchAssociative(),
false
);
}
/**
......@@ -169,13 +98,10 @@ class Statement implements IteratorAggregate, DriverStatement, ForwardCompatible
*/
public function fetchAssociative()
{
if ($this->stmt instanceof ForwardCompatibleResultStatement) {
$row = $this->stmt->fetchAssociative();
} else {
$row = $this->stmt->fetch(FetchMode::ASSOCIATIVE);
}
return $this->fixResult($row, true);
return $this->fixResult(
$this->stmt->fetchAssociative(),
true
);
}
/**
......@@ -183,11 +109,7 @@ class Statement implements IteratorAggregate, DriverStatement, ForwardCompatible
*/
public function fetchOne()
{
if ($this->stmt instanceof ForwardCompatibleResultStatement) {
$value = $this->stmt->fetchOne();
} else {
$value = $this->stmt->fetch(FetchMode::COLUMN);
}
$value = $this->stmt->fetchOne();
if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) !== 0 && $value === '') {
$value = null;
......@@ -203,13 +125,11 @@ class Statement implements IteratorAggregate, DriverStatement, ForwardCompatible
*/
public function fetchAllNumeric() : array
{
if ($this->stmt instanceof ForwardCompatibleResultStatement) {
$data = $this->stmt->fetchAllNumeric();
} else {
$data = $this->stmt->fetchAll(FetchMode::NUMERIC);
}
return $this->fixResultSet($data, false, true);
return $this->fixResultSet(
$this->stmt->fetchAllNumeric(),
false,
true
);
}
/**
......@@ -217,13 +137,23 @@ class Statement implements IteratorAggregate, DriverStatement, ForwardCompatible
*/
public function fetchAllAssociative() : array
{
if ($this->stmt instanceof ForwardCompatibleResultStatement) {
$data = $this->stmt->fetchAllAssociative();
} else {
$data = $this->stmt->fetchAll(FetchMode::ASSOCIATIVE);
}
return $this->fixResultSet(
$this->stmt->fetchAllAssociative(),
true,
true
);
}
return $this->fixResultSet($data, true, true);
/**
* {@inheritdoc}
*/
public function fetchColumn() : array
{
return $this->fixResultSet(
$this->stmt->fetchColumn(),
true,
false
);
}
/**
......@@ -302,24 +232,6 @@ class Statement implements IteratorAggregate, DriverStatement, ForwardCompatible
return $row;
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchOne() instead.
*/
public function fetchColumn()
{
$value = $this->stmt->fetchColumn();
if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) !== 0 && $value === '') {
$value = null;
} elseif (($this->portability & Connection::PORTABILITY_RTRIM) !== 0 && is_string($value)) {
$value = rtrim($value);
}
return $value;
}
public function rowCount() : int
{
assert($this->stmt instanceof DriverStatement);
......
<?php
declare(strict_types=1);
namespace Doctrine\DBAL;
use Doctrine\DBAL\Driver\ResultStatement as DriverResultStatement;
use Traversable;
/**
* DBAL-level ResultStatement interface.
*/
interface ResultStatement extends DriverResultStatement
{
/**
* Returns an iterator over the result set rows represented as numeric arrays.
*
* @return Traversable<int,array<int,mixed>>
*
* @throws DBALException
*/
public function iterateNumeric() : Traversable;
/**
* Returns an iterator over the result set rows represented as associative arrays.
*
* @return Traversable<int,array<string,mixed>>
*
* @throws DBALException
*/
public function iterateAssociative() : Traversable;
/**
* Returns an iterator over the values of the first column of the result set.
*
* @return Traversable<int,mixed>
*
* @throws DBALException
*/
public function iterateColumn() : Traversable;
}
......@@ -640,7 +640,7 @@ abstract class AbstractSchemaManager
/**
* Converts a list of namespace names from the native DBMS data definition to a portable Doctrine definition.
*
* @param mixed[][] $namespaces The list of namespace names in the native DBMS data definition.
* @param array<int, array<string, mixed>> $namespaces The list of namespace names in the native DBMS data definition.
*
* @return string[]
*/
......@@ -668,7 +668,7 @@ abstract class AbstractSchemaManager
/**
* Converts a namespace definition from the native DBMS data definition to a portable Doctrine definition.
*
* @param mixed[] $namespace The native DBMS namespace definition.
* @param array<string, mixed> $namespace The native DBMS namespace definition.
*
* @return mixed
*/
......
......@@ -3,7 +3,6 @@
namespace Doctrine\DBAL\Schema;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
......@@ -41,9 +40,7 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
*/
public function getSchemaNames()
{
$statement = $this->_conn->executeQuery("SELECT nspname FROM pg_namespace WHERE nspname !~ '^pg_.*' AND nspname != 'information_schema'");
return $statement->fetchAll(FetchMode::COLUMN);
return $this->_conn->fetchColumn("SELECT nspname FROM pg_namespace WHERE nspname !~ '^pg_.*' AND nspname != 'information_schema'");
}
/**
......@@ -56,7 +53,7 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
public function getSchemaSearchPaths()
{
$params = $this->_conn->getParams();
$schema = explode(',', $this->_conn->fetchColumn('SHOW search_path'));
$schema = explode(',', $this->_conn->fetchOne('SHOW search_path'));
if (isset($params['user'])) {
$schema = str_replace('"$user"', $params['user'], $schema);
......@@ -311,7 +308,7 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
if (! isset($sequence['increment_by'], $sequence['min_value'])) {
/** @var string[] $data */
$data = $this->_conn->fetchAssoc('SELECT min_value, increment_by FROM ' . $this->_platform->quoteIdentifier($sequenceName));
$data = $this->_conn->fetchAssociative('SELECT min_value, increment_by FROM ' . $this->_platform->quoteIdentifier($sequenceName));
$sequence += $data;
}
......@@ -532,7 +529,7 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
assert($platform instanceof PostgreSQL94Platform);
$sql = $platform->getListTableMetadataSQL($tableName);
$tableOptions = $this->_conn->fetchAssoc($sql);
$tableOptions = $this->_conn->fetchAssociative($sql);
if ($tableOptions !== false) {
$table->addOption('comment', $tableOptions['table_comment']);
......
......@@ -514,7 +514,7 @@ CREATE\sTABLE # Match "CREATE TABLE"
private function getCreateTableSQL(string $table) : string
{
$sql = $this->_conn->fetchColumn(
$sql = $this->_conn->fetchOne(
<<<'SQL'
SELECT sql
FROM (
......
......@@ -4,10 +4,8 @@ namespace Doctrine\DBAL;
use Doctrine\DBAL\Driver\DriverException;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\ForwardCompatibility\ResultStatement as ForwardCompatibleResultStatement;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use IteratorAggregate;
use Throwable;
use Traversable;
use function is_string;
......@@ -16,7 +14,7 @@ use function is_string;
* A thin wrapper around a Doctrine\DBAL\Driver\Statement that adds support
* for logging, DBAL mapping types, etc.
*/
class Statement implements IteratorAggregate, DriverStatement, ForwardCompatibleResultStatement
class Statement implements DriverStatement, ResultStatement
{
/**
* The SQL statement.
......@@ -199,53 +197,15 @@ class Statement implements IteratorAggregate, DriverStatement, ForwardCompatible
/**
* {@inheritdoc}
*
* @deprecated Use one of the fetch- or iterate-related methods.
*/
public function setFetchMode($fetchMode)
{
return $this->stmt->setFetchMode($fetchMode);
}
/**
* Required by interface IteratorAggregate.
*
* @deprecated Use iterateNumeric(), iterateAssociative() or iterateColumn() instead.
*
* {@inheritdoc}
*/
public function getIterator()
{
return $this->stmt;
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
*/
public function fetch($fetchMode = null)
{
return $this->stmt->fetch($fetchMode);
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
*/
public function fetchAll($fetchMode = null)
{
return $this->stmt->fetchAll($fetchMode);
}
/**
* {@inheritDoc}
*
* @deprecated Use fetchOne() instead.
* @throws DBALException
*/
public function fetchColumn()
public function fetchNumeric()
{
return $this->stmt->fetchColumn();
try {
return $this->stmt->fetchNumeric();
} catch (DriverException $e) {
throw DBALException::driverException($this->conn->getDriver(), $e);
}
}
/**
......@@ -253,14 +213,10 @@ class Statement implements IteratorAggregate, DriverStatement, ForwardCompatible
*
* @throws DBALException
*/
public function fetchNumeric()
public function fetchAssociative()
{
try {
if ($this->stmt instanceof ForwardCompatibleResultStatement) {
return $this->stmt->fetchNumeric();
}
return $this->stmt->fetch(FetchMode::NUMERIC);
return $this->stmt->fetchAssociative();
} catch (DriverException $e) {
throw DBALException::driverException($this->conn->getDriver(), $e);
}
......@@ -268,35 +224,25 @@ class Statement implements IteratorAggregate, DriverStatement, ForwardCompatible
/**
* {@inheritdoc}
*
* @throws DBALException
*/
public function fetchAssociative()
public function fetchOne()
{
try {
if ($this->stmt instanceof ForwardCompatibleResultStatement) {
return $this->stmt->fetchAssociative();
}
return $this->stmt->fetch(FetchMode::ASSOCIATIVE);
return $this->stmt->fetchOne();
} catch (DriverException $e) {
throw DBALException::driverException($this->conn->getDriver(), $e);
}
}
/**
* {@inheritDoc}
* {@inheritdoc}
*
* @throws DBALException
*/
public function fetchOne()
public function fetchAllNumeric() : array
{
try {
if ($this->stmt instanceof ForwardCompatibleResultStatement) {
return $this->stmt->fetchOne();
}
return $this->stmt->fetch(FetchMode::COLUMN);
return $this->stmt->fetchAllNumeric();
} catch (DriverException $e) {
throw DBALException::driverException($this->conn->getDriver(), $e);
}
......@@ -307,14 +253,10 @@ class Statement implements IteratorAggregate, DriverStatement, ForwardCompatible
*
* @throws DBALException
*/
public function fetchAllNumeric() : array
public function fetchAllAssociative() : array
{
try {
if ($this->stmt instanceof ForwardCompatibleResultStatement) {
return $this->stmt->fetchAllNumeric();
}
return $this->stmt->fetchAll(FetchMode::NUMERIC);
return $this->stmt->fetchAllAssociative();
} catch (DriverException $e) {
throw DBALException::driverException($this->conn->getDriver(), $e);
}
......@@ -325,14 +267,10 @@ class Statement implements IteratorAggregate, DriverStatement, ForwardCompatible
*
* @throws DBALException
*/
public function fetchAllAssociative() : array
public function fetchColumn() : array
{
try {
if ($this->stmt instanceof ForwardCompatibleResultStatement) {
return $this->stmt->fetchAllAssociative();
}
return $this->stmt->fetchAll(FetchMode::ASSOCIATIVE);
return $this->stmt->fetchColumn();
} catch (DriverException $e) {
throw DBALException::driverException($this->conn->getDriver(), $e);
}
......@@ -348,14 +286,8 @@ class Statement implements IteratorAggregate, DriverStatement, ForwardCompatible
public function iterateNumeric() : Traversable
{
try {
if ($this->stmt instanceof ForwardCompatibleResultStatement) {
while (($row = $this->stmt->fetchNumeric()) !== false) {
yield $row;
}
} else {
while (($row = $this->stmt->fetch(FetchMode::NUMERIC)) !== false) {
yield $row;
}
while (($row = $this->stmt->fetchNumeric()) !== false) {
yield $row;
}
} catch (DriverException $e) {
throw DBALException::driverException($this->conn->getDriver(), $e);
......@@ -372,14 +304,8 @@ class Statement implements IteratorAggregate, DriverStatement, ForwardCompatible
public function iterateAssociative() : Traversable
{
try {
if ($this->stmt instanceof ForwardCompatibleResultStatement) {
while (($row = $this->stmt->fetchAssociative()) !== false) {
yield $row;
}
} else {
while (($row = $this->stmt->fetch(FetchMode::ASSOCIATIVE)) !== false) {
yield $row;
}
while (($row = $this->stmt->fetchAssociative()) !== false) {
yield $row;
}
} catch (DriverException $e) {
throw DBALException::driverException($this->conn->getDriver(), $e);
......@@ -396,14 +322,8 @@ class Statement implements IteratorAggregate, DriverStatement, ForwardCompatible
public function iterateColumn() : Traversable
{
try {
if ($this->stmt instanceof ForwardCompatibleResultStatement) {
while (($value = $this->stmt->fetchOne()) !== false) {
yield $value;
}
} else {
while (($value = $this->stmt->fetch(FetchMode::COLUMN)) !== false) {
yield $value;
}
while (($value = $this->stmt->fetchOne()) !== false) {
yield $value;
}
} catch (DriverException $e) {
throw DBALException::driverException($this->conn->getDriver(), $e);
......
......@@ -17,7 +17,6 @@ use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Events;
use Doctrine\DBAL\Exception\InvalidArgumentException;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\Logging\DebugStack;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
......@@ -546,7 +545,7 @@ class ConnectionTest extends TestCase
);
}
public function testFetchAssoc() : void
public function testFetchAssociative() : void
{
$statement = 'SELECT * FROM foo WHERE bar = ?';
$params = [666];
......@@ -564,8 +563,7 @@ class ConnectionTest extends TestCase
$driverStatementMock = $this->createMock(Statement::class);
$driverStatementMock->expects(self::once())
->method('fetch')
->with(FetchMode::ASSOCIATIVE)
->method('fetchAssociative')
->will(self::returnValue($result));
$conn = $this->getMockBuilder(Connection::class)
......@@ -578,10 +576,10 @@ class ConnectionTest extends TestCase
->with($statement, $params, $types)
->will(self::returnValue($driverStatementMock));
self::assertSame($result, $conn->fetchAssoc($statement, $params, $types));
self::assertSame($result, $conn->fetchAssociative($statement, $params, $types));
}
public function testFetchArray() : void
public function testFetchNumeric() : void
{
$statement = 'SELECT * FROM foo WHERE bar = ?';
$params = [666];
......@@ -599,8 +597,7 @@ class ConnectionTest extends TestCase
$driverStatementMock = $this->createMock(Statement::class);
$driverStatementMock->expects(self::once())
->method('fetch')
->with(FetchMode::NUMERIC)
->method('fetchNumeric')
->will(self::returnValue($result));
$conn = $this->getMockBuilder(Connection::class)
......@@ -613,10 +610,10 @@ class ConnectionTest extends TestCase
->with($statement, $params, $types)
->will(self::returnValue($driverStatementMock));
self::assertSame($result, $conn->fetchArray($statement, $params, $types));
self::assertSame($result, $conn->fetchNumeric($statement, $params, $types));
}
public function testFetchColumn() : void
public function testFetchOne() : void
{
$statement = 'SELECT * FROM foo WHERE bar = ?';
$params = [666];
......@@ -634,7 +631,7 @@ class ConnectionTest extends TestCase
$driverStatementMock = $this->createMock(Statement::class);
$driverStatementMock->expects(self::once())
->method('fetchColumn')
->method('fetchOne')
->will(self::returnValue($result));
$conn = $this->getMockBuilder(Connection::class)
......@@ -647,10 +644,10 @@ class ConnectionTest extends TestCase
->with($statement, $params, $types)
->will(self::returnValue($driverStatementMock));
self::assertSame($result, $conn->fetchColumn($statement, $params, $types));
self::assertSame($result, $conn->fetchOne($statement, $params, $types));
}
public function testFetchAll() : void
public function testFetchAllAssociative() : void
{
$statement = 'SELECT * FROM foo WHERE bar = ?';
$params = [666];
......@@ -668,7 +665,7 @@ class ConnectionTest extends TestCase
$driverStatementMock = $this->createMock(Statement::class);
$driverStatementMock->expects(self::once())
->method('fetchAll')
->method('fetchAllAssociative')
->will(self::returnValue($result));
$conn = $this->getMockBuilder(Connection::class)
......@@ -681,7 +678,7 @@ class ConnectionTest extends TestCase
->with($statement, $params, $types)
->will(self::returnValue($driverStatementMock));
self::assertSame($result, $conn->fetchAll($statement, $params, $types));
self::assertSame($result, $conn->fetchAllAssociative($statement, $params, $types));
}
public function testCallingDeleteWithNoDeletionCriteriaResultsInInvalidArgumentException() : void
......
......@@ -29,7 +29,7 @@ class AbstractMySQLDriverTest extends AbstractDriverTest
$statement = $this->createMock(ResultStatement::class);
$statement->expects(self::once())
->method('fetchColumn')
->method('fetchOne')
->will(self::returnValue($database));
$connection = $this->getConnectionMock();
......
......@@ -27,7 +27,7 @@ class AbstractPostgreSQLDriverTest extends AbstractDriverTest
$statement = $this->createMock(ResultStatement::class);
$statement->expects(self::once())
->method('fetchColumn')
->method('fetchOne')
->will(self::returnValue($database));
$connection = $this->getConnectionMock();
......
<?php
namespace Doctrine\DBAL\Tests\Driver;
use Doctrine\DBAL\Driver\IBMDB2\DB2Statement;
use Doctrine\DBAL\Driver\Mysqli\MysqliStatement;
use Doctrine\DBAL\Driver\OCI8\OCI8Statement;
use Doctrine\DBAL\Driver\SQLAnywhere\SQLAnywhereStatement;
use Doctrine\DBAL\Driver\SQLSrv\SQLSrvStatement;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Driver\StatementIterator;
use Doctrine\DBAL\Portability\Statement as PortabilityStatement;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Traversable;
use function extension_loaded;
class StatementIteratorTest extends TestCase
{
/**
* @dataProvider statementProvider()
*/
public function testGettingIteratorDoesNotCallFetch(string $class) : void
{
$stmt = $this->createPartialMock($class, ['fetch', 'fetchAll', 'fetchColumn']);
$stmt->expects(self::never())->method('fetch');
$stmt->expects(self::never())->method('fetchAll');
$stmt->expects(self::never())->method('fetchColumn');
$stmt->getIterator();
}
public function testIteratorIterationCallsFetchOncePerStep() : void
{
$stmt = $this->createMock(Statement::class);
$calls = 0;
$this->configureStatement($stmt, $calls);
$stmtIterator = new StatementIterator($stmt);
$this->assertIterationCallsFetchOncePerStep($stmtIterator, $calls);
}
/**
* @param class-string<Statement> $class
*
* @dataProvider statementProvider()
*/
public function testStatementIterationCallsFetchOncePerStep(string $class) : void
{
$stmt = $this->createPartialMock($class, ['fetch']);
$calls = 0;
$this->configureStatement($stmt, $calls);
$this->assertIterationCallsFetchOncePerStep($stmt, $calls);
}
private function configureStatement(MockObject $stmt, int &$calls) : void
{
$values = ['foo', '', 'bar', '0', 'baz', 0, 'qux', null, 'quz', false, 'impossible'];
$calls = 0;
$stmt->expects(self::exactly(10))
->method('fetch')
->willReturnCallback(static function () use ($values, &$calls) {
$value = $values[$calls];
$calls++;
return $value;
});
}
/**
* @param Traversable<int, mixed> $iterator
*/
private function assertIterationCallsFetchOncePerStep(Traversable $iterator, int &$calls) : void
{
foreach ($iterator as $i => $_) {
self::assertEquals($i + 1, $calls);
}
}
/**
* @return iterable<array{0: class-string<Statement>}>
*/
public static function statementProvider() : iterable
{
if (extension_loaded('ibm_db2')) {
yield [DB2Statement::class];
}
yield [MysqliStatement::class];
if (extension_loaded('oci8')) {
yield [OCI8Statement::class];
}
yield [PortabilityStatement::class];
yield [SQLAnywhereStatement::class];
if (! extension_loaded('sqlsrv')) {
return;
}
yield [SQLSrvStatement::class];
}
}
......@@ -4,7 +4,6 @@ namespace Doctrine\DBAL\Tests\Functional;
use Doctrine\DBAL\Driver\OCI8\Driver as OCI8Driver;
use Doctrine\DBAL\Driver\PDOOracle\Driver as PDOOracleDriver;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
......@@ -158,7 +157,7 @@ class BlobTest extends FunctionalTestCase
private function assertBlobContains(string $text) : void
{
$rows = $this->connection->query('SELECT blobfield FROM blob_table')->fetchAll(FetchMode::COLUMN);
$rows = $this->connection->query('SELECT blobfield FROM blob_table')->fetchColumn();
self::assertCount(1, $rows);
......
<?php
namespace Doctrine\DBAL\Tests\Functional\Connection\BackwardCompatibility;
use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Connection as BaseConnection;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use function func_get_args;
/**
* Wraps statements in a non-forward-compatible wrapper.
*/
class Connection extends BaseConnection
{
/**
* {@inheritdoc}
*/
public function executeQuery(string $query, array $params = [], $types = [], ?QueryCacheProfile $qcp = null) : ResultStatement
{
return new Statement(parent::executeQuery($query, $params, $types, $qcp));
}
public function prepare(string $sql) : DriverStatement
{
return new Statement(parent::prepare($sql));
}
public function query(string $sql) : ResultStatement
{
return new Statement(parent::query(...func_get_args()));
}
}
<?php
namespace Doctrine\DBAL\Tests\Functional\Connection\BackwardCompatibility;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Tests\Functional\Connection\FetchTest as BaseFetchTest;
use function array_merge;
class FetchTest extends BaseFetchTest
{
public function setUp() : void
{
parent::setUp();
$this->connection = DriverManager::getConnection(
array_merge($this->connection->getParams(), [
'wrapperClass' => Connection::class,
]),
$this->connection->getConfiguration(),
$this->connection->getEventManager()
);
}
}
<?php
namespace Doctrine\DBAL\Tests\Functional\Connection\BackwardCompatibility;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\Driver\StatementIterator;
use Doctrine\DBAL\ParameterType;
use IteratorAggregate;
use function assert;
/**
* A wrapper that does not implement the forward-compatible statement interface.
*/
class Statement implements IteratorAggregate, DriverStatement
{
/** @var DriverStatement|ResultStatement */
private $stmt;
/**
* @param DriverStatement|ResultStatement $stmt
*/
public function __construct($stmt)
{
$this->stmt = $stmt;
}
/**
* {@inheritdoc}
*/
public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null)
{
assert($this->stmt instanceof DriverStatement);
return $this->stmt->bindParam($column, $variable, $type, $length);
}
/**
* {@inheritdoc}
*/
public function bindValue($param, $value, $type = ParameterType::STRING)
{
assert($this->stmt instanceof DriverStatement);
return $this->stmt->bindValue($param, $value, $type);
}
/**
* {@inheritdoc}
*/
public function closeCursor()
{
return $this->stmt->closeCursor();
}
/**
* {@inheritdoc}
*/
public function columnCount()
{
return $this->stmt->columnCount();
}
/**
* {@inheritdoc}
*
* @deprecated The error information is available via exceptions.
*/
public function errorCode()
{
assert($this->stmt instanceof DriverStatement);
return $this->stmt->errorCode();
}
/**
* {@inheritdoc}
*
* @deprecated The error information is available via exceptions.
*/
public function errorInfo()
{
assert($this->stmt instanceof DriverStatement);
return $this->stmt->errorInfo();
}
/**
* {@inheritdoc}
*/
public function execute($params = null)
{
assert($this->stmt instanceof DriverStatement);
return $this->stmt->execute($params);
}
/**
* {@inheritdoc}
*
* @deprecated Use one of the fetch- or iterate-related methods.
*/
public function setFetchMode($fetchMode)
{
return $this->stmt->setFetchMode($fetchMode);
}
/**
* {@inheritdoc}
*
* @deprecated Use iterateNumeric(), iterateAssociative() or iterateColumn() instead.
*/
public function getIterator()
{
return new StatementIterator($this);
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
*/
public function fetch($fetchMode = null)
{
return $this->stmt->fetch($fetchMode);
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
*/
public function fetchAll($fetchMode = null)
{
return $this->stmt->fetchAll($fetchMode);
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchOne() instead.
*/
public function fetchColumn()
{
return $this->stmt->fetchColumn();
}
public function rowCount() : int
{
assert($this->stmt instanceof DriverStatement);
return $this->stmt->rowCount();
}
}
......@@ -116,7 +116,7 @@ class ConnectionTest extends FunctionalTestCase
$connection->executeQuery('insert into test_nesting values (33)');
$connection->rollBack();
self::assertEquals(0, $connection->fetchColumn('select count(*) from test_nesting'));
self::assertEquals(0, $connection->fetchOne('select count(*) from test_nesting'));
}
public function testTransactionNestingBehaviorWithSavepoints() : void
......
......@@ -7,7 +7,6 @@ use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\Mysqli\Driver as MySQLiDriver;
use Doctrine\DBAL\Driver\SQLSrv\Driver as SQLSrvDriver;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Platforms\TrimMode;
......@@ -16,11 +15,8 @@ use Doctrine\DBAL\Statement;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Types\Types;
use function array_change_key_case;
use function array_filter;
use function array_keys;
use function count;
use function date;
use function is_numeric;
use function json_encode;
use function sprintf;
use function strtotime;
......@@ -62,7 +58,8 @@ class DataAccessTest extends FunctionalTestCase
$stmt->bindValue(2, 'foo');
$stmt->execute();
$row = $stmt->fetch(FetchMode::ASSOCIATIVE);
$row = $stmt->fetchAssociative();
self::assertIsArray($row);
$row = array_change_key_case($row, CASE_LOWER);
self::assertEquals(['test_int' => 1, 'test_string' => 'foo'], $row);
}
......@@ -80,12 +77,13 @@ class DataAccessTest extends FunctionalTestCase
$stmt->bindParam(2, $paramStr);
$stmt->execute();
$row = $stmt->fetch(FetchMode::ASSOCIATIVE);
$row = $stmt->fetchAssociative();
self::assertIsArray($row);
$row = array_change_key_case($row, CASE_LOWER);
self::assertEquals(['test_int' => 1, 'test_string' => 'foo'], $row);
}
public function testPrepareWithFetchAll() : void
public function testPrepareWithFetchAllAssociative() : void
{
$paramInt = 1;
$paramStr = 'foo';
......@@ -98,33 +96,12 @@ class DataAccessTest extends FunctionalTestCase
$stmt->bindParam(2, $paramStr);
$stmt->execute();
$rows = $stmt->fetchAll(FetchMode::ASSOCIATIVE);
$rows = $stmt->fetchAllAssociative();
$rows[0] = array_change_key_case($rows[0], CASE_LOWER);
self::assertEquals(['test_int' => 1, 'test_string' => 'foo'], $rows[0]);
}
/**
* @group DBAL-228
*/
public function testPrepareWithFetchAllBoth() : void
{
$paramInt = 1;
$paramStr = 'foo';
$sql = 'SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?';
$stmt = $this->connection->prepare($sql);
self::assertInstanceOf(Statement::class, $stmt);
$stmt->bindParam(1, $paramInt);
$stmt->bindParam(2, $paramStr);
$stmt->execute();
$rows = $stmt->fetchAll(FetchMode::MIXED);
$rows[0] = array_change_key_case($rows[0], CASE_LOWER);
self::assertEquals(['test_int' => 1, 'test_string' => 'foo', 0 => 1, 1 => 'foo'], $rows[0]);
}
public function testPrepareWithFetchColumn() : void
public function testPrepareWithFetchOne() : void
{
$paramInt = 1;
$paramStr = 'foo';
......@@ -137,7 +114,7 @@ class DataAccessTest extends FunctionalTestCase
$stmt->bindParam(2, $paramStr);
$stmt->execute();
$column = $stmt->fetchColumn();
$column = $stmt->fetchOne();
self::assertEquals(1, $column);
}
......@@ -155,8 +132,8 @@ class DataAccessTest extends FunctionalTestCase
$stmt->execute();
$rows = [];
$stmt->setFetchMode(FetchMode::ASSOCIATIVE);
foreach ($stmt as $row) {
foreach ($stmt->iterateAssociative() as $row) {
$rows[] = array_change_key_case($row, CASE_LOWER);
}
......@@ -188,16 +165,16 @@ class DataAccessTest extends FunctionalTestCase
self::assertInstanceOf(Statement::class, $stmt);
$stmt->execute([$paramInt, $paramStr]);
$row = $stmt->fetch(FetchMode::ASSOCIATIVE);
$row = $stmt->fetchAssociative();
self::assertNotFalse($row);
$row = array_change_key_case($row, CASE_LOWER);
self::assertEquals(['test_int' => 1, 'test_string' => 'foo'], $row);
}
public function testFetchAll() : void
public function testFetchAllAssociative() : void
{
$sql = 'SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?';
$data = $this->connection->fetchAll($sql, [1, 'foo']);
$data = $this->connection->fetchAllAssociative($sql, [1, 'foo']);
self::assertCount(1, $data);
......@@ -218,7 +195,7 @@ class DataAccessTest extends FunctionalTestCase
$datetime = new DateTime($datetimeString);
$sql = 'SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?';
$data = $this->connection->fetchAll(
$data = $this->connection->fetchAllAssociative(
$sql,
[1, $datetime],
[ParameterType::STRING, Types::DATETIME_MUTABLE]
......@@ -250,35 +227,20 @@ class DataAccessTest extends FunctionalTestCase
$this->expectException(DBALException::class);
$this->connection->fetchAll($sql, [1, $datetime]);
}
public function testFetchBoth() : void
{
$sql = 'SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?';
$row = $this->connection->executeQuery($sql, [1, 'foo'])->fetch(FetchMode::MIXED);
self::assertNotFalse($row);
$row = array_change_key_case($row, CASE_LOWER);
self::assertEquals(1, $row['test_int']);
self::assertEquals('foo', $row['test_string']);
self::assertEquals(1, $row[0]);
self::assertEquals('foo', $row[1]);
$this->connection->fetchAllAssociative($sql, [1, $datetime]);
}
public function testFetchNoResult() : void
{
self::assertFalse(
$this->connection->executeQuery('SELECT test_int FROM fetch_table WHERE test_int = ?', [-1])->fetch()
$this->connection->executeQuery('SELECT test_int FROM fetch_table WHERE test_int = ?', [-1])->fetchAssociative()
);
}
public function testFetchAssoc() : void
public function testFetchAssociative() : void
{
$sql = 'SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?';
$row = $this->connection->fetchAssoc($sql, [1, 'foo']);
$row = $this->connection->fetchAssociative($sql, [1, 'foo']);
self::assertNotFalse($row);
......@@ -294,7 +256,7 @@ class DataAccessTest extends FunctionalTestCase
$datetime = new DateTime($datetimeString);
$sql = 'SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?';
$row = $this->connection->fetchAssoc(
$row = $this->connection->fetchAssociative(
$sql,
[1, $datetime],
[ParameterType::STRING, Types::DATETIME_MUTABLE]
......@@ -321,13 +283,13 @@ class DataAccessTest extends FunctionalTestCase
$this->expectException(DBALException::class);
$this->connection->fetchAssoc($sql, [1, $datetime]);
$this->connection->fetchAssociative($sql, [1, $datetime]);
}
public function testFetchArray() : void
{
$sql = 'SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?';
$row = $this->connection->fetchArray($sql, [1, 'foo']);
$row = $this->connection->fetchNumeric($sql, [1, 'foo']);
self::assertEquals(1, $row[0]);
self::assertEquals('foo', $row[1]);
......@@ -339,7 +301,7 @@ class DataAccessTest extends FunctionalTestCase
$datetime = new DateTime($datetimeString);
$sql = 'SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?';
$row = $this->connection->fetchArray(
$row = $this->connection->fetchNumeric(
$sql,
[1, $datetime],
[ParameterType::STRING, Types::DATETIME_MUTABLE]
......@@ -366,29 +328,29 @@ class DataAccessTest extends FunctionalTestCase
$this->expectException(DBALException::class);
$this->connection->fetchArray($sql, [1, $datetime]);
$this->connection->fetchNumeric($sql, [1, $datetime]);
}
public function testFetchColumn() : void
public function testFetchOne() : void
{
$sql = 'SELECT test_int FROM fetch_table WHERE test_int = ? AND test_string = ?';
$testInt = $this->connection->fetchColumn($sql, [1, 'foo']);
$testInt = $this->connection->fetchOne($sql, [1, 'foo']);
self::assertEquals(1, $testInt);
$sql = 'SELECT test_string FROM fetch_table WHERE test_int = ? AND test_string = ?';
$testString = $this->connection->fetchColumn($sql, [1, 'foo']);
$testString = $this->connection->fetchOne($sql, [1, 'foo']);
self::assertEquals('foo', $testString);
}
public function testFetchColumnWithTypes() : void
public function testFetchOneWithTypes() : void
{
$datetimeString = '2010-01-01 10:10:10';
$datetime = new DateTime($datetimeString);
$sql = 'SELECT test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?';
$column = $this->connection->fetchColumn(
$column = $this->connection->fetchOne(
$sql,
[1, $datetime],
[ParameterType::STRING, Types::DATETIME_MUTABLE]
......@@ -399,7 +361,7 @@ class DataAccessTest extends FunctionalTestCase
self::assertStringStartsWith($datetimeString, $column);
}
public function testFetchColumnWithMissingTypes() : void
public function testFetchOneWithMissingTypes() : void
{
if ($this->connection->getDriver() instanceof MySQLiDriver ||
$this->connection->getDriver() instanceof SQLSrvDriver) {
......@@ -412,7 +374,7 @@ class DataAccessTest extends FunctionalTestCase
$this->expectException(DBALException::class);
$this->connection->fetchColumn($sql, [1, $datetime]);
$this->connection->fetchOne($sql, [1, $datetime]);
}
/**
......@@ -427,7 +389,7 @@ class DataAccessTest extends FunctionalTestCase
[1 => Types::DATETIME_MUTABLE]
);
self::assertEquals(1, $stmt->fetchColumn());
self::assertEquals(1, $stmt->fetchOne());
}
/**
......@@ -453,7 +415,7 @@ class DataAccessTest extends FunctionalTestCase
'SELECT count(*) AS c FROM fetch_table WHERE test_datetime = ?',
[1 => $datetime],
[1 => Types::DATETIME_MUTABLE]
)->fetchColumn());
)->fetchOne());
}
/**
......@@ -466,7 +428,7 @@ class DataAccessTest extends FunctionalTestCase
$stmt->bindValue(1, new DateTime('2010-01-01 10:10:10'), Types::DATETIME_MUTABLE);
$stmt->execute();
self::assertEquals(1, $stmt->fetchColumn());
self::assertEquals(1, $stmt->fetchOne());
}
/**
......@@ -484,7 +446,7 @@ class DataAccessTest extends FunctionalTestCase
[Connection::PARAM_INT_ARRAY]
);
$data = $stmt->fetchAll(FetchMode::NUMERIC);
$data = $stmt->fetchAllNumeric();
self::assertCount(5, $data);
self::assertEquals([[100], [101], [102], [103], [104]], $data);
......@@ -494,7 +456,7 @@ class DataAccessTest extends FunctionalTestCase
[Connection::PARAM_STR_ARRAY]
);
$data = $stmt->fetchAll(FetchMode::NUMERIC);
$data = $stmt->fetchAllNumeric();
self::assertCount(5, $data);
self::assertEquals([[100], [101], [102], [103], [104]], $data);
}
......@@ -510,7 +472,7 @@ class DataAccessTest extends FunctionalTestCase
$this->connection->getDatabasePlatform()->getTrimExpression($value, $position, $char) . ' AS trimmed ' .
'FROM fetch_table';
$row = $this->connection->fetchAssoc($sql);
$row = $this->connection->fetchAssociative($sql);
$row = array_change_key_case($row, CASE_LOWER);
self::assertEquals($expectedResult, $row['trimmed']);
......@@ -586,7 +548,7 @@ class DataAccessTest extends FunctionalTestCase
$sql .= $p->getDateSubYearsExpression('test_datetime', 6) . ' AS sub_years ';
$sql .= 'FROM fetch_table';
$row = $this->connection->fetchAssoc($sql);
$row = $this->connection->fetchAssociative($sql);
$row = array_change_key_case($row, CASE_LOWER);
self::assertEquals('2010-01-01 10:10:11', date('Y-m-d H:i:s', strtotime($row['add_seconds'])), 'Adding second should end up on 2010-01-01 10:10:11');
......@@ -629,7 +591,7 @@ class DataAccessTest extends FunctionalTestCase
$sql = 'SELECT COUNT(*) FROM fetch_table_date_math WHERE ';
$sql .= $platform->getDateSubDaysExpression('test_date', 'test_days') . " < '2010-05-12'";
$rowCount = $this->connection->fetchColumn($sql);
$rowCount = $this->connection->fetchOne($sql);
self::assertEquals(1, $rowCount);
}
......@@ -650,7 +612,7 @@ class DataAccessTest extends FunctionalTestCase
$sql .= $platform->getLocateExpression('test_string', "'oo'", 3) . ' AS locate9 ';
$sql .= 'FROM fetch_table';
$row = $this->connection->fetchAssoc($sql);
$row = $this->connection->fetchAssociative($sql);
$row = array_change_key_case($row, CASE_LOWER);
self::assertEquals(2, $row['locate1']);
......@@ -667,7 +629,7 @@ class DataAccessTest extends FunctionalTestCase
public function testQuoteSQLInjection() : void
{
$sql = 'SELECT * FROM fetch_table WHERE test_string = ' . $this->connection->quote("bar' OR '1'='1");
$rows = $this->connection->fetchAll($sql);
$rows = $this->connection->fetchAllAssociative($sql);
self::assertCount(0, $rows, 'no result should be returned, otherwise SQL injection is possible');
}
......@@ -699,7 +661,7 @@ class DataAccessTest extends FunctionalTestCase
. ' FROM fetch_table';
$stmt = $this->connection->executeQuery($sql);
$data = $stmt->fetchAll(FetchMode::ASSOCIATIVE);
$data = $stmt->fetchAllAssociative();
self::assertCount(4, $data);
self::assertEquals(count($bitmap), count($data));
......@@ -721,17 +683,6 @@ class DataAccessTest extends FunctionalTestCase
}
}
public function testSetDefaultFetchMode() : void
{
$stmt = $this->connection->query('SELECT * FROM fetch_table');
$stmt->setFetchMode(FetchMode::NUMERIC);
$row = array_keys($stmt->fetch());
self::assertCount(0, array_filter($row, static function ($v) : bool {
return ! is_numeric($v);
}), 'should be no non-numerical elements in the result.');
}
/**
* @group DBAL-241
*/
......@@ -743,40 +694,24 @@ class DataAccessTest extends FunctionalTestCase
$this->connection->insert('fetch_table', ['test_int' => 1, 'test_string' => 'foo']);
$this->connection->insert('fetch_table', ['test_int' => 10, 'test_string' => 'foo']);
$sql = 'SELECT test_int FROM fetch_table';
$rows = $this->connection->query($sql)->fetchAll(FetchMode::COLUMN);
$sql = 'SELECT test_int FROM fetch_table';
$values = $this->connection->query($sql)->fetchColumn();
self::assertEquals([1, 10], $rows);
self::assertEquals([1, 10], $values);
}
/**
* @group DBAL-257
*/
public function testEmptyFetchColumnReturnsFalse() : void
public function testEmptyFetchOneReturnsFalse() : void
{
$this->connection->beginTransaction();
$this->connection->exec('DELETE FROM fetch_table');
self::assertFalse($this->connection->fetchColumn('SELECT test_int FROM fetch_table'));
self::assertFalse($this->connection->query('SELECT test_int FROM fetch_table')->fetchColumn());
self::assertFalse($this->connection->fetchOne('SELECT test_int FROM fetch_table'));
self::assertFalse($this->connection->query('SELECT test_int FROM fetch_table')->fetchOne());
$this->connection->rollBack();
}
/**
* @group DBAL-339
*/
public function testSetFetchModeOnDbalStatement() : void
{
$sql = 'SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?';
$stmt = $this->connection->executeQuery($sql, [1, 'foo']);
$stmt->setFetchMode(FetchMode::NUMERIC);
$row = $stmt->fetch();
self::assertArrayHasKey(0, $row);
self::assertArrayHasKey(1, $row);
self::assertFalse($stmt->fetch());
}
/**
* @group DBAL-435
*/
......@@ -784,7 +719,7 @@ class DataAccessTest extends FunctionalTestCase
{
$sql = 'SELECT * FROM fetch_table WHERE test_int IN (?)';
$stmt = $this->connection->executeQuery($sql, [[]], [Connection::PARAM_INT_ARRAY]);
$rows = $stmt->fetchAll();
$rows = $stmt->fetchAllAssociative();
self::assertEquals([], $rows);
}
......@@ -792,10 +727,10 @@ class DataAccessTest extends FunctionalTestCase
/**
* @group DBAL-1028
*/
public function testFetchColumnNoResult() : void
public function testFetchOneNoResult() : void
{
self::assertFalse(
$this->connection->fetchColumn('SELECT test_int FROM fetch_table WHERE test_int = ?', [-1])
$this->connection->fetchOne('SELECT test_int FROM fetch_table WHERE test_int = ?', [-1])
);
}
}
......@@ -33,7 +33,7 @@ class StatementTest extends FunctionalTestCase
{
self::assertEquals(
$expected,
$this->connection->executeQuery($query, $params)->fetch()
$this->connection->executeQuery($query, $params)->fetchAssociative()
);
}
......@@ -52,7 +52,7 @@ class StatementTest extends FunctionalTestCase
self::assertEquals(
$expected,
$stmt->fetch()
$stmt->fetchAssociative()
);
}
......
......@@ -85,7 +85,7 @@ class DriverTest extends AbstractDriverTest
$hash = microtime(true); // required to identify the record in the results uniquely
$sql = sprintf('SELECT * FROM pg_stat_activity WHERE %d = %d', $hash, $hash);
$statement = $connection->query($sql);
$records = $statement->fetchAll();
$records = $statement->fetchAllAssociative();
foreach ($records as $record) {
// The query column is named "current_query" on PostgreSQL < 9.2
......
......@@ -3,7 +3,6 @@
namespace Doctrine\DBAL\Tests\Functional\Driver;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use function extension_loaded;
......@@ -44,7 +43,7 @@ class PDOPgsqlConnectionTest extends FunctionalTestCase
self::assertEquals(
$charset,
$connection->query('SHOW client_encoding')
->fetch(FetchMode::COLUMN)
->fetchOne()
);
}
......
......@@ -57,7 +57,7 @@ class DriverTest extends AbstractDriverTest
public function testConnectionOptions() : void
{
$connection = $this->getConnection(['APP' => 'APP_NAME']);
$result = $connection->query('SELECT APP_NAME()')->fetchColumn();
$result = $connection->query('SELECT APP_NAME()')->fetchOne();
self::assertSame('APP_NAME', $result);
}
......
......@@ -23,6 +23,6 @@ final class LikeWildcardsEscapingTest extends FunctionalTestCase
)
);
$stmt->execute();
self::assertTrue((bool) $stmt->fetchColumn());
self::assertTrue((bool) $stmt->fetchOne());
}
}
......@@ -89,7 +89,7 @@ class MasterSlaveConnectionTest extends FunctionalTestCase
self::assertFalse($conn->isConnectedToMaster());
$clientCharset = $conn->fetchColumn('select @@character_set_client as c');
$clientCharset = $conn->fetchOne('select @@character_set_client as c');
self::assertSame(
$charset,
......@@ -114,7 +114,7 @@ class MasterSlaveConnectionTest extends FunctionalTestCase
$conn = $this->createMasterSlaveConnection();
$sql = 'SELECT count(*) as num FROM master_slave_table';
$data = $conn->fetchAll($sql);
$data = $conn->fetchAllAssociative($sql);
$data[0] = array_change_key_case($data[0], CASE_LOWER);
self::assertEquals(1, $data[0]['num']);
......@@ -129,7 +129,7 @@ class MasterSlaveConnectionTest extends FunctionalTestCase
self::assertTrue($conn->isConnectedToMaster());
$sql = 'SELECT count(*) as num FROM master_slave_table';
$data = $conn->fetchAll($sql);
$data = $conn->fetchAllAssociative($sql);
$data[0] = array_change_key_case($data[0], CASE_LOWER);
self::assertEquals(2, $data[0]['num']);
......@@ -202,9 +202,8 @@ class MasterSlaveConnectionTest extends FunctionalTestCase
//Query must be executed only on Master
self::assertTrue($conn->isConnectedToMaster());
$data = $statement->fetchAll();
$data = $statement->fetchAllAssociative();
//Default fetchmode is FetchMode::ASSOCIATIVE
self::assertArrayHasKey(0, $data);
self::assertArrayHasKey('num', $data[0]);
......@@ -227,9 +226,8 @@ class MasterSlaveConnectionTest extends FunctionalTestCase
//Query must be executed only on Master, even when we connect to the slave
self::assertTrue($conn->isConnectedToMaster());
$data = $statement->fetchAll();
$data = $statement->fetchAllAssociative();
//Default fetchmode is FetchMode::ASSOCIATIVE
self::assertArrayHasKey(0, $data);
self::assertArrayHasKey('num', $data[0]);
......
......@@ -169,7 +169,7 @@ SQL;
{
$p = $this->connection->getDatabasePlatform();
$data = [];
foreach ($this->connection->fetchAll($p->modifyLimitQuery($sql, $limit, $offset)) as $row) {
foreach ($this->connection->fetchAllAssociative($p->modifyLimitQuery($sql, $limit, $offset)) as $row) {
$row = array_change_key_case($row, CASE_LOWER);
$data[] = $row['test_int'];
}
......
......@@ -3,7 +3,6 @@
namespace Doctrine\DBAL\Tests\Functional;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
......@@ -213,7 +212,7 @@ class NamedParametersTest extends FunctionalTestCase
public function testTicket(string $query, array $params, array $types, array $expected) : void
{
$stmt = $this->connection->executeQuery($query, $params, $types);
$result = $stmt->fetchAll(FetchMode::ASSOCIATIVE);
$result = $stmt->fetchAllAssociative();
foreach ($result as $k => $v) {
$result[$k] = array_change_key_case($v, CASE_LOWER);
......
......@@ -26,7 +26,7 @@ class DateExpressionTest extends FunctionalTestCase
$platform = $this->connection->getDatabasePlatform();
$sql = sprintf('SELECT %s FROM date_expr_test', $platform->getDateDiffExpression('date1', 'date2'));
$diff = $this->connection->query($sql)->fetchColumn();
$diff = $this->connection->query($sql)->fetchOne();
self::assertEquals($expected, $diff);
}
......
......@@ -4,7 +4,6 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Tests\Functional\Platform;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Platforms\OraclePlatform;
......@@ -71,7 +70,7 @@ class DefaultExpressionTest extends FunctionalTestCase
[$actualValue, $defaultValue] = $this->connection->query(
'SELECT default_value, actual_value FROM default_expr_test'
)->fetch(FetchMode::NUMERIC);
)->fetchNumeric();
self::assertEquals($actualValue, $defaultValue);
}
......
......@@ -16,7 +16,7 @@ class QuotingTest extends FunctionalTestCase
$platform->quoteStringLiteral($string)
);
self::assertSame($string, $this->connection->fetchColumn($query));
self::assertSame($string, $this->connection->fetchOne($query));
}
/**
......
......@@ -5,7 +5,6 @@ namespace Doctrine\DBAL\Tests\Functional;
use Doctrine\DBAL\ColumnCase;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\Portability\Connection as ConnectionPortability;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
......@@ -63,26 +62,19 @@ class PortabilityTest extends FunctionalTestCase
public function testFullFetchMode() : void
{
$rows = $this->getPortableConnection()->fetchAll('SELECT * FROM portability_table');
$rows = $this->getPortableConnection()->fetchAllAssociative('SELECT * FROM portability_table');
$this->assertFetchResultRows($rows);
$stmt = $this->getPortableConnection()->query('SELECT * FROM portability_table');
$stmt->setFetchMode(FetchMode::ASSOCIATIVE);
foreach ($stmt as $row) {
$this->assertFetchResultRow($row);
}
$stmt = $this->getPortableConnection()->query('SELECT * FROM portability_table');
while (($row = $stmt->fetch(FetchMode::ASSOCIATIVE))) {
while (($row = $stmt->fetchAssociative())) {
$this->assertFetchResultRow($row);
}
$stmt = $this->getPortableConnection()->prepare('SELECT * FROM portability_table');
$stmt->execute();
while (($row = $stmt->fetch(FetchMode::ASSOCIATIVE))) {
while (($row = $stmt->fetchAssociative())) {
$this->assertFetchResultRow($row);
}
}
......@@ -90,24 +82,18 @@ class PortabilityTest extends FunctionalTestCase
public function testConnFetchMode() : void
{
$conn = $this->getPortableConnection();
$conn->setFetchMode(FetchMode::ASSOCIATIVE);
$rows = $conn->fetchAll('SELECT * FROM portability_table');
$rows = $conn->fetchAllAssociative('SELECT * FROM portability_table');
$this->assertFetchResultRows($rows);
$stmt = $conn->query('SELECT * FROM portability_table');
foreach ($stmt as $row) {
$this->assertFetchResultRow($row);
}
$stmt = $conn->query('SELECT * FROM portability_table');
while (($row = $stmt->fetch())) {
while (($row = $stmt->fetchAssociative())) {
$this->assertFetchResultRow($row);
}
$stmt = $conn->prepare('SELECT * FROM portability_table');
$stmt->execute();
while (($row = $stmt->fetch())) {
while (($row = $stmt->fetchAssociative())) {
$this->assertFetchResultRow($row);
}
}
......@@ -142,21 +128,21 @@ class PortabilityTest extends FunctionalTestCase
/**
* @param mixed[] $expected
*
* @dataProvider fetchAllColumnProvider
* @dataProvider fetchColumnProvider
*/
public function testFetchAllColumn(string $field, array $expected) : void
public function testfetchColumn(string $field, array $expected) : void
{
$conn = $this->getPortableConnection();
$stmt = $conn->query('SELECT ' . $field . ' FROM portability_table');
$column = $stmt->fetchAll(FetchMode::COLUMN);
$column = $stmt->fetchColumn();
self::assertEquals($expected, $column);
}
/**
* @return iterable<string, array<int, mixed>>
*/
public static function fetchAllColumnProvider() : iterable
public static function fetchColumnProvider() : iterable
{
return [
'int' => [
......@@ -175,7 +161,7 @@ class PortabilityTest extends FunctionalTestCase
$conn = $this->getPortableConnection();
$stmt = $conn->query('SELECT Test_Null FROM portability_table');
$column = $stmt->fetchAll(FetchMode::COLUMN);
$column = $stmt->fetchColumn();
self::assertSame([null, null], $column);
}
}
......@@ -5,12 +5,10 @@ namespace Doctrine\DBAL\Tests\Functional;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\Logging\DebugStack;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use function array_change_key_case;
use function array_merge;
use function array_shift;
use function array_values;
use function is_array;
......@@ -57,42 +55,44 @@ class ResultCacheTest extends FunctionalTestCase
parent::tearDown();
}
public function testCacheFetchAssoc() : void
public function testCacheFetchAssociative() : void
{
$this->assertCacheNonCacheSelectSameFetchModeAreEqual(
$this->expectedResult,
FetchMode::ASSOCIATIVE
static function (ResultStatement $stmt) {
return $stmt->fetchAssociative();
}
);
}
public function testFetchNum() : void
public function testFetchNumeric() : void
{
$expectedResult = [];
foreach ($this->expectedResult as $v) {
$expectedResult[] = array_values($v);
}
$this->assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, FetchMode::NUMERIC);
}
public function testFetchBoth() : void
{
$expectedResult = [];
foreach ($this->expectedResult as $v) {
$expectedResult[] = array_merge($v, array_values($v));
}
$this->assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, FetchMode::MIXED);
$this->assertCacheNonCacheSelectSameFetchModeAreEqual(
$expectedResult,
static function (ResultStatement $stmt) {
return $stmt->fetchNumeric();
}
);
}
public function testFetchColumn() : void
public function testFetchOne() : void
{
$expectedResult = [];
foreach ($this->expectedResult as $v) {
$expectedResult[] = array_shift($v);
}
$this->assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, FetchMode::COLUMN);
$this->assertCacheNonCacheSelectSameFetchModeAreEqual(
$expectedResult,
static function (ResultStatement $stmt) {
return $stmt->fetchOne();
}
);
}
public function testMixingFetch() : void
......@@ -102,52 +102,52 @@ class ResultCacheTest extends FunctionalTestCase
$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);
$data = $this->hydrateViaFetchAll($stmt, static function (ResultStatement $stmt) {
return $stmt->fetchAllAssociative();
});
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);
$data = $this->hydrateViaFetchAll($stmt, static function (ResultStatement $stmt) {
return $stmt->fetchAllNumeric();
});
self::assertEquals($numExpectedResult, $data);
}
public function testIteratorFetch() : void
{
self::assertStandardAndIteratorFetchAreEqual(FetchMode::MIXED);
self::assertStandardAndIteratorFetchAreEqual(FetchMode::ASSOCIATIVE);
self::assertStandardAndIteratorFetchAreEqual(FetchMode::NUMERIC);
}
private function assertStandardAndIteratorFetchAreEqual(int $fetchMode) : void
/**
* @dataProvider fetchProvider
*/
public function testFetchViaIteration(callable $fetch, callable $fetchAll) : void
{
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
$data = $this->hydrateStmt($stmt, $fetchMode);
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
$data = $this->hydrateViaFetchAll($stmt, $fetchAll);
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
$dataIterator = $this->hydrateStmtIterator($stmt, $fetchMode);
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
$dataIterator = $this->hydrateViaIteration($stmt, $fetch);
self::assertEquals($data, $dataIterator);
}
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 = [];
while ($row = $stmt->fetch(FetchMode::ASSOCIATIVE)) {
while (($row = $stmt->fetchAssociative()) !== false) {
$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 = [];
while ($row = $stmt->fetch(FetchMode::NUMERIC)) {
while (($row = $stmt->fetchNumeric()) !== false) {
$data[] = $row;
}
......@@ -156,14 +156,16 @@ class ResultCacheTest extends FunctionalTestCase
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->fetchAssociative();
$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);
$this->hydrateViaIteration($stmt, static function (ResultStatement $stmt) {
return $stmt->fetchNumeric();
});
self::assertCount(2, $this->sqlLogger->queries);
}
......@@ -171,14 +173,14 @@ class ResultCacheTest extends FunctionalTestCase
public function testFetchAllAndFinishSavesCache() : void
{
$layerCache = new ArrayCache();
$stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(10, 'testcachekey', $layerCache));
$stmt->fetchAll();
$stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(0, 'testcachekey', $layerCache));
$stmt->fetchAllAssociative();
$stmt->closeCursor();
self::assertCount(1, $layerCache->fetch('testcachekey'));
}
public function testFetchAllColumn() : void
public function testFetchColumn() : void
{
$query = $this->connection->getDatabasePlatform()
->getDummySelectSQL('1');
......@@ -186,64 +188,107 @@ class ResultCacheTest extends FunctionalTestCase
$qcp = new QueryCacheProfile(0, 0, new ArrayCache());
$stmt = $this->connection->executeCacheQuery($query, [], [], $qcp);
$stmt->fetchAll(FetchMode::COLUMN);
$stmt->fetchColumn();
$stmt->closeCursor();
$stmt = $this->connection->executeCacheQuery($query, [], [], $qcp);
self::assertEquals([1], $stmt->fetchAll(FetchMode::COLUMN));
self::assertEquals([1], $stmt->fetchColumn());
}
/**
* @param array<int, array<int, int|string>>|list<int> $expectedResult
*/
private function assertCacheNonCacheSelectSameFetchModeAreEqual(array $expectedResult, int $fetchMode) : void
private function assertCacheNonCacheSelectSameFetchModeAreEqual(array $expectedResult, callable $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);
$data = $this->hydrateViaIteration($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);
$data = $this->hydrateViaIteration($stmt, $fetchMode);
self::assertEquals($expectedResult, $data);
self::assertCount(1, $this->sqlLogger->queries, 'just one dbal hit');
}
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->hydrateViaIteration($stmt, static function (ResultStatement $stmt) {
return $stmt->fetchAssociative();
});
$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->hydrateViaIteration($stmt, static function (ResultStatement $stmt) {
return $stmt->fetchAssociative();
});
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->hydrateViaIteration($stmt, static function (ResultStatement $stmt) {
return $stmt->fetchAssociative();
});
$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->hydrateViaIteration($stmt, static function (ResultStatement $stmt) {
return $stmt->fetchAssociative();
});
self::assertCount(2, $this->sqlLogger->queries, 'two hits');
self::assertCount(1, $secondCache->fetch('emptycachekey'));
}
/**
* @return iterable<string,array<int,mixed>>
*/
public static function fetchProvider() : iterable
{
yield 'associative' => [
static function (ResultStatement $stmt) {
return $stmt->fetchAssociative();
},
static function (ResultStatement $stmt) {
return $stmt->fetchAllAssociative();
},
];
yield 'numeric' => [
static function (ResultStatement $stmt) {
return $stmt->fetchNumeric();
},
static function (ResultStatement $stmt) {
return $stmt->fetchAllNumeric();
},
];
yield 'column' => [
static function (ResultStatement $stmt) {
return $stmt->fetchOne();
},
static function (ResultStatement $stmt) {
return $stmt->fetchColumn();
},
];
}
/**
* @return array<int, mixed>
*/
private function hydrateStmt(ResultStatement $stmt, int $fetchMode = FetchMode::ASSOCIATIVE) : array
private function hydrateViaFetchAll(ResultStatement $stmt, callable $fetchAll) : array
{
$data = [];
while ($row = $stmt->fetch($fetchMode)) {
foreach ($fetchAll($stmt) as $row) {
$data[] = is_array($row) ? array_change_key_case($row, CASE_LOWER) : $row;
}
......@@ -255,11 +300,11 @@ class ResultCacheTest extends FunctionalTestCase
/**
* @return array<int, mixed>
*/
private function hydrateStmtIterator(ResultStatement $stmt, int $fetchMode = FetchMode::ASSOCIATIVE) : array
private function hydrateViaIteration(ResultStatement $stmt, callable $fetch) : array
{
$data = [];
$stmt->setFetchMode($fetchMode);
foreach ($stmt as $row) {
while (($row = $fetch($stmt)) !== false) {
$data[] = is_array($row) ? array_change_key_case($row, CASE_LOWER) : $row;
}
......
......@@ -63,7 +63,7 @@ class DefaultValueTest extends FunctionalTestCase
*/
public function testEscapedDefaultValueCanBeInserted(string $name, ?string $expectedDefault) : void
{
$value = $this->connection->fetchColumn(
$value = $this->connection->fetchOne(
sprintf('SELECT %s FROM default_value', $name)
);
......
......@@ -462,7 +462,7 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
'INSERT INTO test_column_defaults_are_valid () VALUES()'
);
$row = $this->connection->fetchAssoc(
$row = $this->connection->fetchAssociative(
'SELECT *, DATEDIFF(CURRENT_TIMESTAMP(), col_datetime) as diff_seconds FROM test_column_defaults_are_valid'
);
......
......@@ -1557,7 +1557,7 @@ abstract class SchemaManagerFunctionalTestCase extends FunctionalTestCase
$query = $this->connection->query('SELECT id FROM test_pk_auto_increment WHERE text = \'1\'');
$query->execute();
$lastUsedIdBeforeDelete = (int) $query->fetchColumn();
$lastUsedIdBeforeDelete = (int) $query->fetchOne();
$this->connection->query('DELETE FROM test_pk_auto_increment');
......@@ -1565,7 +1565,7 @@ abstract class SchemaManagerFunctionalTestCase extends FunctionalTestCase
$query = $this->connection->query('SELECT id FROM test_pk_auto_increment WHERE text = \'2\'');
$query->execute();
$lastUsedIdAfterDelete = (int) $query->fetchColumn();
$lastUsedIdAfterDelete = (int) $query->fetchOne();
self::assertGreaterThan($lastUsedIdBeforeDelete, $lastUsedIdAfterDelete);
}
......
......@@ -274,7 +274,7 @@ SQL;
$query = $this->connection->query('SELECT id FROM test_pk_auto_increment WHERE text = "2"');
$query->execute();
$lastUsedIdAfterDelete = (int) $query->fetchColumn();
$lastUsedIdAfterDelete = (int) $query->fetchOne();
// with an empty table, non autoincrement rowid is always 1
self::assertEquals(1, $lastUsedIdAfterDelete);
......
......@@ -4,7 +4,6 @@ namespace Doctrine\DBAL\Tests\Functional;
use Doctrine\DBAL\Driver\PDOOracle\Driver as PDOOracleDriver;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
......@@ -37,15 +36,15 @@ class StatementTest extends FunctionalTestCase
$stmt->execute();
$id = $stmt->fetchColumn();
$id = $stmt->fetchOne();
self::assertEquals(1, $id);
$stmt->closeCursor();
$stmt->execute();
$id = $stmt->fetchColumn();
$id = $stmt->fetchOne();
self::assertEquals(1, $id);
$id = $stmt->fetchColumn();
$id = $stmt->fetchOne();
self::assertEquals(2, $id);
}
......@@ -71,7 +70,7 @@ class StatementTest extends FunctionalTestCase
$stmt->execute();
self::assertEquals([
['param1', 'X'],
], $stmt->fetchAll(FetchMode::NUMERIC));
], $stmt->fetchAllNumeric());
$row2 = [
'param' => 'param2',
......@@ -83,7 +82,7 @@ class StatementTest extends FunctionalTestCase
self::assertEquals([
['param1', 'X'],
['param2', 'A bit longer value'],
], $stmt->fetchAll(FetchMode::NUMERIC));
], $stmt->fetchAllNumeric());
}
public function testFetchLongBlob() : void
......@@ -127,7 +126,7 @@ EOF
$stream = Type::getType('blob')
->convertToPHPValue(
$stmt->fetchColumn(),
$stmt->fetchOne(),
$this->connection->getDatabasePlatform()
);
......@@ -141,14 +140,14 @@ EOF
$stmt1 = $this->connection->prepare('SELECT id FROM stmt_test');
$stmt1->execute();
$stmt1->fetch();
$stmt1->fetchAssociative();
$stmt1->execute();
// fetching only one record out of two
$stmt1->fetch();
$stmt1->fetchAssociative();
$stmt2 = $this->connection->prepare('SELECT id FROM stmt_test WHERE id = ?');
$stmt2->execute([1]);
self::assertEquals(1, $stmt2->fetchColumn());
self::assertEquals(1, $stmt2->fetchOne());
}
public function testReuseStatementAfterClosingCursor() : void
......@@ -163,13 +162,13 @@ EOF
$stmt = $this->connection->prepare('SELECT id FROM stmt_test WHERE id = ?');
$stmt->execute([1]);
$id = $stmt->fetchColumn();
$id = $stmt->fetchOne();
self::assertEquals(1, $id);
$stmt->closeCursor();
$stmt->execute([2]);
$id = $stmt->fetchColumn();
$id = $stmt->fetchOne();
self::assertEquals(2, $id);
}
......@@ -183,11 +182,11 @@ EOF
$id = 1;
$stmt->execute();
self::assertEquals(1, $stmt->fetchColumn());
self::assertEquals(1, $stmt->fetchOne());
$id = 2;
$stmt->execute();
self::assertEquals(2, $stmt->fetchColumn());
self::assertEquals(2, $stmt->fetchOne());
}
public function testReuseStatementWithReboundValue() : void
......@@ -199,11 +198,11 @@ EOF
$stmt->bindValue(1, 1);
$stmt->execute();
self::assertEquals(1, $stmt->fetchColumn());
self::assertEquals(1, $stmt->fetchOne());
$stmt->bindValue(1, 2);
$stmt->execute();
self::assertEquals(2, $stmt->fetchColumn());
self::assertEquals(2, $stmt->fetchOne());
}
public function testReuseStatementWithReboundParam() : void
......@@ -216,12 +215,12 @@ EOF
$x = 1;
$stmt->bindParam(1, $x);
$stmt->execute();
self::assertEquals(1, $stmt->fetchColumn());
self::assertEquals(1, $stmt->fetchOne());
$y = 2;
$stmt->bindParam(1, $y);
$stmt->execute();
self::assertEquals(2, $stmt->fetchColumn());
self::assertEquals(2, $stmt->fetchOne());
}
/**
......@@ -250,7 +249,7 @@ EOF
$stmt = $this->connection->prepare('SELECT name FROM stmt_test');
$stmt->execute();
$stmt->fetch();
$stmt->fetchAssociative();
self::assertTrue($stmt->closeCursor());
}
......@@ -292,19 +291,19 @@ EOF
return [
'fetch' => [
static function (Statement $stmt) {
return $stmt->fetch();
return $stmt->fetchAssociative();
},
false,
],
'fetch-column' => [
static function (Statement $stmt) {
return $stmt->fetchColumn();
return $stmt->fetchOne();
},
false,
],
'fetch-all' => [
static function (Statement $stmt) : array {
return $stmt->fetchAll();
return $stmt->fetchAllAssociative();
},
[],
],
......@@ -315,7 +314,7 @@ EOF
{
$platform = $this->connection->getDatabasePlatform();
$query = $platform->getDummySelectSQL();
$result = $this->connection->executeQuery($query)->fetch(FetchMode::COLUMN);
$result = $this->connection->executeQuery($query)->fetchOne();
self::assertEquals(1, $result);
}
......
......@@ -62,7 +62,7 @@ class TemporaryTableTest extends FunctionalTestCase
$this->connection->rollBack();
$rows = $this->connection->fetchAll('SELECT * FROM nontemporary');
$rows = $this->connection->fetchAllAssociative('SELECT * FROM nontemporary');
self::assertEquals([], $rows, 'In an event of an error this result has one row, because of an implicit commit.');
}
......@@ -102,7 +102,7 @@ class TemporaryTableTest extends FunctionalTestCase
} catch (Throwable $e) {
}
$rows = $this->connection->fetchAll('SELECT * FROM nontemporary');
$rows = $this->connection->fetchAllAssociative('SELECT * FROM nontemporary');
self::assertEquals([], $rows, 'In an event of an error this result has one row, because of an implicit commit.');
}
}
......@@ -36,7 +36,7 @@ class DBAL202Test extends FunctionalTestCase
$stmt->execute();
$this->connection->rollBack();
self::assertEquals(0, $this->connection->query('SELECT COUNT(1) FROM DBAL202')->fetchColumn());
self::assertEquals(0, $this->connection->query('SELECT COUNT(1) FROM DBAL202')->fetchOne());
}
public function testStatementCommit() : void
......@@ -46,6 +46,6 @@ class DBAL202Test extends FunctionalTestCase
$stmt->execute();
$this->connection->commit();
self::assertEquals(1, $this->connection->query('SELECT COUNT(1) FROM DBAL202')->fetchColumn());
self::assertEquals(1, $this->connection->query('SELECT COUNT(1) FROM DBAL202')->fetchOne());
}
}
......@@ -25,7 +25,7 @@ class DBAL421Test extends FunctionalTestCase
public function testGuidShouldMatchPattern() : void
{
$guid = $this->connection->query($this->getSelectGuidSql())->fetchColumn();
$guid = $this->connection->query($this->getSelectGuidSql())->fetchOne();
$pattern = '/[0-9A-F]{8}\-[0-9A-F]{4}\-[0-9A-F]{4}\-[8-9A-B][0-9A-F]{3}\-[0-9A-F]{12}/i';
self::assertEquals(1, preg_match($pattern, $guid), 'GUID does not match pattern');
}
......
......@@ -50,7 +50,7 @@ class DBAL630Test extends FunctionalTestCase
$id = $this->connection->lastInsertId('dbal630_id_seq');
self::assertNotEmpty($id);
$row = $this->connection->fetchAssoc('SELECT bool_col FROM dbal630 WHERE id = ?', [$id]);
$row = $this->connection->fetchAssociative('SELECT bool_col FROM dbal630 WHERE id = ?', [$id]);
self::assertFalse($row['bool_col']);
}
......@@ -65,7 +65,7 @@ class DBAL630Test extends FunctionalTestCase
$id = $this->connection->lastInsertId('dbal630_id_seq');
self::assertNotEmpty($id);
$row = $this->connection->fetchAssoc('SELECT bool_col FROM dbal630 WHERE id = ?', [$id]);
$row = $this->connection->fetchAssociative('SELECT bool_col FROM dbal630 WHERE id = ?', [$id]);
self::assertFalse($row['bool_col']);
}
......@@ -86,7 +86,7 @@ class DBAL630Test extends FunctionalTestCase
self::assertNotEmpty($id);
$row = $this->connection->fetchAssoc('SELECT bool_col FROM dbal630 WHERE id = ?', [$id]);
$row = $this->connection->fetchAssociative('SELECT bool_col FROM dbal630 WHERE id = ?', [$id]);
self::assertFalse($row['bool_col']);
}
......@@ -112,7 +112,7 @@ class DBAL630Test extends FunctionalTestCase
self::assertNotEmpty($id);
$row = $this->connection->fetchAssoc('SELECT bool_col FROM dbal630_allow_nulls WHERE id = ?', [$id]);
$row = $this->connection->fetchAssociative('SELECT bool_col FROM dbal630_allow_nulls WHERE id = ?', [$id]);
self::assertSame($databaseConvertedValue, $row['bool_col']);
}
......@@ -142,7 +142,7 @@ class DBAL630Test extends FunctionalTestCase
self::assertNotEmpty($id);
$row = $this->connection->fetchAssoc('SELECT bool_col FROM dbal630_allow_nulls WHERE id = ?', [$id]);
$row = $this->connection->fetchAssociative('SELECT bool_col FROM dbal630_allow_nulls WHERE id = ?', [$id]);
self::assertSame($databaseConvertedValue, $row['bool_col']);
}
......
......@@ -244,7 +244,7 @@ class TypeConversionTest extends FunctionalTestCase
$sql = 'SELECT ' . $columnName . ' FROM type_conversion WHERE id = ' . self::$typeCounter;
return $typeInstance->convertToPHPValue(
$this->connection->fetchColumn($sql),
$this->connection->fetchOne($sql),
$this->connection->getDatabasePlatform()
);
}
......
......@@ -75,7 +75,7 @@ class BinaryTest extends FunctionalTestCase
*/
private function select(string $id)
{
$value = $this->connection->fetchColumn(
$value = $this->connection->fetchOne(
'SELECT val FROM binary_table WHERE id = ?',
[$id],
[ParameterType::BINARY]
......
......@@ -132,10 +132,10 @@ class WriteTest extends FunctionalTestCase
$this->insertRows();
self::assertEquals(1, $this->connection->delete('write_table', ['test_int' => 2]));
self::assertCount(1, $this->connection->fetchAll('SELECT * FROM write_table'));
self::assertCount(1, $this->connection->fetchAllAssociative('SELECT * FROM write_table'));
self::assertEquals(1, $this->connection->delete('write_table', ['test_int' => 1]));
self::assertCount(0, $this->connection->fetchAll('SELECT * FROM write_table'));
self::assertCount(0, $this->connection->fetchAllAssociative('SELECT * FROM write_table'));
}
public function testUpdate() : void
......@@ -178,7 +178,7 @@ class WriteTest extends FunctionalTestCase
}));
$stmt = $this->connection->query($this->connection->getDatabasePlatform()->getSequenceNextValSQL('write_table_id_seq'));
$nextSequenceVal = $stmt->fetchColumn();
$nextSequenceVal = $stmt->fetchOne();
$lastInsertId = $this->lastInsertId('write_table_id_seq');
......@@ -208,7 +208,7 @@ class WriteTest extends FunctionalTestCase
['test_string' => 'datetime', 'test_int' => 'integer']
);
$data = $this->connection->fetchColumn('SELECT test_string FROM write_table WHERE test_int = 30');
$data = $this->connection->fetchOne('SELECT test_string FROM write_table WHERE test_int = 30');
self::assertEquals($testString->format($this->connection->getDatabasePlatform()->getDateTimeFormatString()), $data);
}
......@@ -235,7 +235,7 @@ class WriteTest extends FunctionalTestCase
['test_string' => 'datetime', 'test_int' => 'integer']
);
$data = $this->connection->fetchColumn('SELECT test_string FROM write_table WHERE test_int = 30');
$data = $this->connection->fetchOne('SELECT test_string FROM write_table WHERE test_int = 30');
self::assertEquals($testString->format($this->connection->getDatabasePlatform()->getDateTimeFormatString()), $data);
}
......@@ -254,7 +254,7 @@ class WriteTest extends FunctionalTestCase
$this->connection->delete('write_table', ['test_int' => 30, 'test_string' => $val], ['test_string' => 'datetime', 'test_int' => 'integer']);
$data = $this->connection->fetchColumn('SELECT test_string FROM write_table WHERE test_int = 30');
$data = $this->connection->fetchOne('SELECT test_string FROM write_table WHERE test_int = 30');
self::assertFalse($data);
}
......@@ -310,13 +310,13 @@ class WriteTest extends FunctionalTestCase
['test_string' => 'string', 'test_int' => 'integer']
);
$data = $this->connection->fetchAll('SELECT * FROM write_table WHERE test_int = 30');
$data = $this->connection->fetchAllAssociative('SELECT * FROM write_table WHERE test_int = 30');
self::assertCount(1, $data);
$this->connection->update('write_table', ['test_int' => 10], ['test_string' => null], ['test_string' => 'string', 'test_int' => 'integer']);
$data = $this->connection->fetchAll('SELECT * FROM write_table WHERE test_int = 30');
$data = $this->connection->fetchAllAssociative('SELECT * FROM write_table WHERE test_int = 30');
self::assertCount(0, $data);
}
......@@ -329,13 +329,13 @@ class WriteTest extends FunctionalTestCase
['test_string' => 'string', 'test_int' => 'integer']
);
$data = $this->connection->fetchAll('SELECT * FROM write_table WHERE test_int = 30');
$data = $this->connection->fetchAllAssociative('SELECT * FROM write_table WHERE test_int = 30');
self::assertCount(1, $data);
$this->connection->delete('write_table', ['test_string' => null], ['test_string' => 'string']);
$data = $this->connection->fetchAll('SELECT * FROM write_table WHERE test_int = 30');
$data = $this->connection->fetchAllAssociative('SELECT * FROM write_table WHERE test_int = 30');
self::assertCount(0, $data);
}
......
......@@ -8,7 +8,6 @@ use Doctrine\DBAL\Portability\Connection;
use Doctrine\DBAL\Portability\Statement;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use function iterator_to_array;
class StatementTest extends TestCase
{
......@@ -95,15 +94,6 @@ class StatementTest extends TestCase
self::assertTrue($this->stmt->execute($params));
}
public function testGetIterator() : void
{
$this->wrappedStmt->expects(self::exactly(3))
->method('fetch')
->willReturnOnConsecutiveCalls('foo', 'bar', false);
self::assertSame(['foo', 'bar'], iterator_to_array($this->stmt->getIterator()));
}
public function testRowCount() : void
{
$rowCount = 666;
......
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