Unverified Commit 179ab95b authored by Sergei Morozov's avatar Sergei Morozov

Merge branch '2.11.x' into 3.0.x

parents be4332a6 256cefa6
......@@ -138,6 +138,16 @@ Please use other database client applications for import, e.g.:
# Upgrade to 2.11
## Deprecated `FetchMode` and the corresponding methods
1. The `FetchMode` class and the `setFetchMode()` method of the `Connection` and `Statement` interfaces are deprecated.
2. The `Statement::fetch()` method is deprecated in favor of `fetchNumeric()`, `fetchAssociative()` and `fetchOne()`.
3. The `Statement::fetchAll()` method is deprecated in favor of `fetchAllNumeric()` and `fetchAllAssociative()`. There is no currently replacement for `Statement::fetchAll(FETCH_MODE::COLUMN)`. In a future major version, `fetchColumn()` will be used as a replacement.
4. The `Statement::fetchColumn()` method is deprecated in favor of `fetchOne()`.
5. The `Connection::fetchArray()` and `fetchAssoc()` method are deprecated in favor of `fetchNumeric()` and `fetchAssociative()` respectively.
6. The `StatementIterator` class and the usage of a `Statement` object as `Traversable` is deprecated in favor of `iterateNumeric()`, `iterateAssociative()` and `iterateColumn()`.
7. Fetching data in mixed mode (`FetchMode::MIXED`) is deprecated.
## Deprecated `Connection::project()`
The `Connection::project()` method is deprecated. Implement data transformation outside of DBAL.
......
......@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "375d7b1967e204d71959234a23055326",
"content-hash": "869c59b61c7de15365fcbad3241d6837",
"packages": [
{
"name": "doctrine/cache",
......@@ -2553,16 +2553,16 @@
},
{
"name": "slevomat/coding-standard",
"version": "6.3.3",
"version": "6.3.7",
"source": {
"type": "git",
"url": "https://github.com/slevomat/coding-standard.git",
"reference": "b905a82255749de847fd4de607c7a4c8163f058d"
"reference": "1f7bd4b5c11cf4a164a400c937483785b0f4eb9e"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/slevomat/coding-standard/zipball/b905a82255749de847fd4de607c7a4c8163f058d",
"reference": "b905a82255749de847fd4de607c7a4c8163f058d",
"url": "https://api.github.com/repos/slevomat/coding-standard/zipball/1f7bd4b5c11cf4a164a400c937483785b0f4eb9e",
"reference": "1f7bd4b5c11cf4a164a400c937483785b0f4eb9e",
"shasum": ""
},
"require": {
......@@ -2606,7 +2606,7 @@
"type": "tidelift"
}
],
"time": "2020-04-28T07:15:08+00:00"
"time": "2020-05-25T13:39:15+00:00"
},
{
"name": "squizlabs/php_codesniffer",
......@@ -3019,8 +3019,8 @@
"authors": [
{
"name": "Arne Blankerts",
"email": "arne@blankerts.de",
"role": "Developer"
"role": "Developer",
"email": "arne@blankerts.de"
}
],
"description": "A small library for converting tokenized PHP source code into XML and potentially other formats",
......
......@@ -3,8 +3,10 @@
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;
......@@ -12,7 +14,7 @@ use function array_values;
use function count;
use function reset;
class ArrayStatement implements IteratorAggregate, ResultStatement
class ArrayStatement implements IteratorAggregate, ResultStatement, ForwardCompatibleResultStatement
{
/** @var mixed[] */
private $data;
......@@ -59,6 +61,8 @@ class ArrayStatement implements IteratorAggregate, ResultStatement
/**
* {@inheritdoc}
*
* @deprecated Use one of the fetch- or iterate-related methods.
*/
public function setFetchMode($fetchMode)
{
......@@ -69,6 +73,8 @@ class ArrayStatement implements IteratorAggregate, ResultStatement
/**
* {@inheritdoc}
*
* @deprecated Use iterateNumeric(), iterateAssociative() or iterateColumn() instead.
*/
public function getIterator()
{
......@@ -79,6 +85,8 @@ class ArrayStatement implements IteratorAggregate, ResultStatement
/**
* {@inheritdoc}
*
* @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
*/
public function fetch($fetchMode = null)
{
......@@ -110,6 +118,8 @@ class ArrayStatement implements IteratorAggregate, ResultStatement
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
*/
public function fetchAll($fetchMode = null)
{
......@@ -123,6 +133,8 @@ class ArrayStatement implements IteratorAggregate, ResultStatement
/**
* {@inheritdoc}
*
* @deprecated Use fetchOne() instead.
*/
public function fetchColumn()
{
......@@ -131,4 +143,68 @@ class ArrayStatement implements IteratorAggregate, ResultStatement
// TODO: verify that return false is the correct behavior
return $row[0] ?? false;
}
/**
* {@inheritdoc}
*/
public function fetchNumeric()
{
$row = $this->doFetch();
if ($row === false) {
return false;
}
return array_values($row);
}
/**
* {@inheritdoc}
*/
public function fetchAssociative()
{
return $this->doFetch();
}
/**
* {@inheritdoc}
*/
public function fetchOne()
{
$row = $this->doFetch();
if ($row === false) {
return false;
}
return reset($row);
}
/**
* {@inheritdoc}
*/
public function fetchAllNumeric() : array
{
return FetchUtils::fetchAllNumeric($this);
}
/**
* {@inheritdoc}
*/
public function fetchAllAssociative() : array
{
return FetchUtils::fetchAllAssociative($this);
}
/**
* @return mixed|false
*/
private function doFetch()
{
if (! isset($this->data[$this->num])) {
return false;
}
return $this->data[$this->num++];
}
}
......@@ -4,9 +4,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;
......@@ -27,7 +30,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
class ResultCacheStatement implements IteratorAggregate, ResultStatement, ForwardCompatibleResultStatement
{
/** @var Cache */
private $resultCache;
......@@ -104,6 +107,8 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement
/**
* {@inheritdoc}
*
* @deprecated Use one of the fetch- or iterate-related methods.
*/
public function setFetchMode($fetchMode)
{
......@@ -114,6 +119,8 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement
/**
* {@inheritdoc}
*
* @deprecated Use iterateNumeric(), iterateAssociative() or iterateColumn() instead.
*/
public function getIterator()
{
......@@ -124,6 +131,8 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement
/**
* {@inheritdoc}
*
* @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
*/
public function fetch($fetchMode = null)
{
......@@ -164,6 +173,8 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
*/
public function fetchAll($fetchMode = null)
{
......@@ -183,6 +194,8 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement
/**
* {@inheritdoc}
*
* @deprecated Use fetchOne() instead.
*/
public function fetchColumn()
{
......@@ -192,6 +205,64 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement
return $row[0] ?? false;
}
/**
* {@inheritdoc}
*/
public function fetchNumeric()
{
$row = $this->doFetch();
if ($row === false) {
return false;
}
return array_values($row);
}
/**
* {@inheritdoc}
*/
public function fetchAssociative()
{
return $this->doFetch();
}
/**
* {@inheritdoc}
*/
public function fetchOne()
{
return FetchUtils::fetchOne($this);
}
/**
* {@inheritdoc}
*/
public function fetchAllNumeric() : array
{
if ($this->statement instanceof ForwardCompatibleResultStatement) {
$data = $this->statement->fetchAllAssociative();
} else {
$data = $this->statement->fetchAll(FetchMode::ASSOCIATIVE);
}
return $this->store($data);
}
/**
* {@inheritdoc}
*/
public function fetchAllAssociative() : array
{
if ($this->statement instanceof ForwardCompatibleResultStatement) {
$data = $this->statement->fetchAllAssociative();
} else {
$data = $this->statement->fetchAll(FetchMode::ASSOCIATIVE);
}
return $this->store($data);
}
/**
* Returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement
* executed by the corresponding object.
......@@ -209,4 +280,49 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement
return $this->statement->rowCount();
}
/**
* @return array<string,mixed>|false
*
* @throws DriverException
*/
private function doFetch()
{
if ($this->data === null) {
$this->data = [];
}
if ($this->statement instanceof ForwardCompatibleResultStatement) {
$row = $this->statement->fetchAssociative();
} else {
$row = $this->statement->fetch(FetchMode::ASSOCIATIVE);
}
if ($row !== false) {
$this->data[] = $row;
return $row;
}
$this->emptied = true;
return false;
}
/**
* @param array<int,array<mixed>> $data
*
* @return array<int,array<mixed>>
*/
private function store(array $data) : array
{
foreach ($data as $key => $value) {
$data[$key] = [$value];
}
$this->data = $data;
$this->emptied = true;
return $this->data;
}
}
This diff is collapsed.
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Driver;
use Doctrine\DBAL\ForwardCompatibility\Driver\ResultStatement;
/**
* @internal
*/
final class FetchUtils
{
/**
* @return mixed|false
*
* @throws DriverException
*/
public static function fetchOne(ResultStatement $stmt)
{
$row = $stmt->fetchNumeric();
if ($row === false) {
return false;
}
return $row[0];
}
/**
* @return array<int,array<int,mixed>>
*
* @throws DriverException
*/
public static function fetchAllNumeric(ResultStatement $stmt) : array
{
$rows = [];
while (($row = $stmt->fetchNumeric()) !== false) {
$rows[] = $row;
}
return $rows;
}
/**
* @return array<int,array<string,mixed>>
*
* @throws DriverException
*/
public static function fetchAllAssociative(ResultStatement $stmt) : array
{
$rows = [];
while (($row = $stmt->fetchAssociative()) !== false) {
$rows[] = $row;
}
return $rows;
}
}
......@@ -221,6 +221,8 @@ class DB2Statement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*
* @deprecated Use one of the fetch- or iterate-related methods.
*/
public function setFetchMode($fetchMode)
{
......@@ -231,6 +233,8 @@ class DB2Statement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*
* @deprecated Use iterateNumeric(), iterateAssociative() or iterateColumn() instead.
*/
public function getIterator()
{
......@@ -239,6 +243,8 @@ class DB2Statement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*
* @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
*/
public function fetch($fetchMode = null)
{
......@@ -269,6 +275,8 @@ class DB2Statement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
*/
public function fetchAll($fetchMode = null)
{
......@@ -293,6 +301,8 @@ class DB2Statement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*
* @deprecated Use fetchOne() instead.
*/
public function fetchColumn()
{
......
......@@ -2,10 +2,12 @@
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;
......@@ -23,7 +25,7 @@ use function is_resource;
use function sprintf;
use function str_repeat;
class MysqliStatement implements IteratorAggregate, Statement
class MysqliStatement implements IteratorAggregate, Statement, ForwardCompatibleResultStatement
{
/** @var string[] */
protected static $_paramTypeMap = [
......@@ -306,6 +308,8 @@ class MysqliStatement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*
* @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
*/
public function fetch($fetchMode = null)
{
......@@ -353,6 +357,8 @@ class MysqliStatement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
*/
public function fetchAll($fetchMode = null)
{
......@@ -375,6 +381,8 @@ class MysqliStatement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*
* @deprecated Use fetchOne() instead.
*/
public function fetchColumn()
{
......@@ -392,6 +400,72 @@ class MysqliStatement implements IteratorAggregate, Statement
*
* @deprecated The error information is available via exceptions.
*/
public function fetchNumeric()
{
// 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;
}
$values = $this->_fetch();
if ($values === null) {
return false;
}
if ($values === false) {
throw new MysqliException($this->_stmt->error, $this->_stmt->sqlstate, $this->_stmt->errno);
}
return $values;
}
/**
* {@inheritDoc}
*/
public function fetchAssociative()
{
$values = $this->fetchNumeric();
if ($values === false) {
return false;
}
assert(is_array($this->_columnNames));
$row = array_combine($this->_columnNames, $values);
assert(is_array($row));
return $row;
}
/**
* {@inheritdoc}
*/
public function fetchOne()
{
return FetchUtils::fetchOne($this);
}
/**
* {@inheritdoc}
*/
public function fetchAllNumeric() : array
{
return FetchUtils::fetchAllNumeric($this);
}
/**
* {@inheritdoc}
*/
public function fetchAllAssociative() : array
{
return FetchUtils::fetchAllAssociative($this);
}
/**
* {@inheritdoc}
*/
public function errorCode()
{
return $this->_stmt->errno;
......@@ -437,6 +511,8 @@ class MysqliStatement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*
* @deprecated Use one of the fetch- or iterate-related methods.
*/
public function setFetchMode($fetchMode)
{
......@@ -447,6 +523,8 @@ class MysqliStatement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*
* @deprecated Use iterateNumeric(), iterateAssociative() or iterateColumn() instead.
*/
public function getIterator()
{
......
......@@ -2,9 +2,11 @@
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;
......@@ -45,7 +47,7 @@ use const SQLT_CHR;
/**
* The OCI8 implementation of the Statement interface.
*/
class OCI8Statement implements IteratorAggregate, Statement
class OCI8Statement implements IteratorAggregate, Statement, ForwardCompatibleResultStatement
{
/** @var resource */
protected $_dbh;
......@@ -413,6 +415,8 @@ class OCI8Statement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*
* @deprecated Use one of the fetch- or iterate-related methods.
*/
public function setFetchMode($fetchMode)
{
......@@ -423,6 +427,8 @@ class OCI8Statement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*
* @deprecated Use iterateNumeric(), iterateAssociative() or iterateColumn() instead.
*/
public function getIterator()
{
......@@ -431,6 +437,8 @@ class OCI8Statement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*
* @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
*/
public function fetch($fetchMode = null)
{
......@@ -458,6 +466,8 @@ class OCI8Statement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
*/
public function fetchAll($fetchMode = null)
{
......@@ -504,6 +514,8 @@ class OCI8Statement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*
* @deprecated Use fetchOne() instead.
*/
public function fetchColumn()
{
......@@ -532,4 +544,83 @@ class OCI8Statement implements IteratorAggregate, Statement
return 0;
}
/**
* {@inheritdoc}
*/
public function fetchNumeric()
{
return $this->doFetch(OCI_NUM);
}
/**
* {@inheritdoc}
*/
public function fetchAssociative()
{
return $this->doFetch(OCI_ASSOC);
}
/**
* {@inheritdoc}
*/
public function fetchOne()
{
return FetchUtils::fetchOne($this);
}
/**
* {@inheritdoc}
*/
public function fetchAllNumeric() : array
{
return $this->doFetchAll(OCI_NUM, OCI_FETCHSTATEMENT_BY_ROW);
}
/**
* {@inheritdoc}
*/
public function fetchAllAssociative() : array
{
return $this->doFetchAll(OCI_ASSOC, OCI_FETCHSTATEMENT_BY_ROW);
}
/**
* @return mixed|false
*/
private function doFetch(int $mode)
{
// 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;
}
return oci_fetch_array(
$this->_sth,
$mode | OCI_RETURN_NULLS | OCI_RETURN_LOBS
);
}
/**
* @return array<mixed>
*/
private function doFetchAll(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
if (! $this->result) {
return [];
}
oci_fetch_all(
$this->_sth,
$result,
0,
-1,
$mode | OCI_RETURN_NULLS | $fetchStructure | OCI_RETURN_LOBS
);
return $result;
}
}
......@@ -4,6 +4,7 @@ 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;
......@@ -25,6 +26,10 @@ 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();
}
......
......@@ -3,6 +3,7 @@
namespace Doctrine\DBAL\Driver;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ForwardCompatibility\Driver\ResultStatement as ForwardCompatibleResultStatement;
use Doctrine\DBAL\ParameterType;
use InvalidArgumentException;
use IteratorAggregate;
......@@ -16,7 +17,7 @@ use function is_array;
* The PDO implementation of the Statement interface.
* Used by all PDO-based drivers.
*/
class PDOStatement implements IteratorAggregate, Statement
class PDOStatement implements IteratorAggregate, Statement, ForwardCompatibleResultStatement
{
private const PARAM_TYPE_MAP = [
ParameterType::NULL => PDO::PARAM_NULL,
......@@ -44,6 +45,8 @@ class PDOStatement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*
* @deprecated Use one of the fetch- or iterate-related methods.
*/
public function setFetchMode($fetchMode)
{
......@@ -147,6 +150,8 @@ class PDOStatement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*
* @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
*/
public function fetch($fetchMode = null)
{
......@@ -165,6 +170,8 @@ class PDOStatement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
*/
public function fetchAll($fetchMode = null)
{
......@@ -187,6 +194,8 @@ class PDOStatement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*
* @deprecated Use fetchOne() instead.
*/
public function fetchColumn()
{
......@@ -197,6 +206,46 @@ class PDOStatement implements IteratorAggregate, Statement
}
}
/**
* {@inheritdoc}
*/
public function fetchNumeric()
{
return $this->fetch(PDO::FETCH_NUM);
}
/**
* {@inheritdoc}
*/
public function fetchAssociative()
{
return $this->fetch(PDO::FETCH_ASSOC);
}
/**
* {@inheritdoc}
*/
public function fetchOne()
{
return $this->fetch(PDO::FETCH_COLUMN);
}
/**
* {@inheritdoc}
*/
public function fetchAllNumeric() : array
{
return $this->fetchAll(PDO::FETCH_NUM);
}
/**
* {@inheritdoc}
*/
public function fetchAllAssociative() : array
{
return $this->fetchAll(PDO::FETCH_ASSOC);
}
/**
* Converts DBAL parameter type to PDO parameter type
*
......
......@@ -28,6 +28,8 @@ interface ResultStatement extends Traversable
/**
* Sets the fetch mode to use while iterating this statement.
*
* @deprecated Use one of the fetch- or iterate-related methods.
*
* @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.
*
......@@ -38,6 +40,8 @@ interface ResultStatement extends Traversable
/**
* Returns the next row of a result set.
*
* @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
*
* @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}.
......@@ -50,6 +54,8 @@ interface ResultStatement extends Traversable
/**
* Returns an array containing all of the result set rows.
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
*
* @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}.
......@@ -61,6 +67,8 @@ interface ResultStatement extends Traversable
/**
* Returns a single column from the next row of a result set or FALSE if there are no more rows.
*
* @deprecated Use fetchOne() instead.
*
* @return mixed|false A single column in the next row of a result set, or FALSE if there are no more rows.
*/
public function fetchColumn();
......
......@@ -5,6 +5,7 @@ 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;
......@@ -122,7 +123,13 @@ class SQLAnywhereConnection implements ServerInfoAwareConnection
*/
public function getServerVersion()
{
$version = $this->query("SELECT PROPERTY('ProductVersion')")->fetchColumn();
$stmt = $this->query("SELECT PROPERTY('ProductVersion')");
if ($stmt instanceof ForwardCompatibleResultStatement) {
$version = $stmt->fetchOne();
} else {
$version = $stmt->fetchColumn();
}
assert(is_string($version));
......@@ -138,7 +145,13 @@ class SQLAnywhereConnection implements ServerInfoAwareConnection
return sasql_insert_id($this->connection);
}
return $this->query('SELECT ' . $name . '.CURRVAL')->fetchColumn();
$stmt = $this->query('SELECT ' . $name . '.CURRVAL');
if ($stmt instanceof ForwardCompatibleResultStatement) {
return $stmt->fetchOne();
}
return $stmt->fetchColumn();
}
public function prepare(string $sql) : DriverStatement
......
......@@ -2,9 +2,12 @@
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;
......@@ -28,7 +31,7 @@ use const SASQL_BOTH;
/**
* SAP SQL Anywhere implementation of the Statement interface.
*/
class SQLAnywhereStatement implements IteratorAggregate, Statement
class SQLAnywhereStatement implements IteratorAggregate, Statement, ForwardCompatibleResultStatement
{
/** @var resource The connection resource. */
private $conn;
......@@ -186,6 +189,8 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*
* @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
*
* @throws SQLAnywhereException
*/
public function fetch($fetchMode = null)
......@@ -216,6 +221,8 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
*/
public function fetchAll($fetchMode = null)
{
......@@ -240,6 +247,8 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*
* @deprecated Use fetchOne() instead.
*/
public function fetchColumn()
{
......@@ -254,12 +263,68 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*
* @deprecated Use iterateNumeric(), iterateAssociative() or iterateColumn() instead.
*/
public function getIterator()
{
return new StatementIterator($this);
}
/**
* {@inheritDoc}
*/
public function fetchNumeric()
{
if (! is_resource($this->result)) {
return false;
}
return sasql_fetch_row($this->result);
}
/**
* {@inheritdoc}
*/
public function fetchAssociative()
{
if (! is_resource($this->result)) {
return false;
}
return sasql_fetch_assoc($this->result);
}
/**
* {@inheritdoc}
*
* @throws DriverException
*/
public function fetchOne()
{
return FetchUtils::fetchOne($this);
}
/**
* @return array<int,array<int,mixed>>
*
* @throws DriverException
*/
public function fetchAllNumeric() : array
{
return FetchUtils::fetchAllNumeric($this);
}
/**
* @return array<int,array<string,mixed>>
*
* @throws DriverException
*/
public function fetchAllAssociative() : array
{
return FetchUtils::fetchAllAssociative($this);
}
public function rowCount() : int
{
return sasql_stmt_affected_rows($this->stmt);
......@@ -267,6 +332,8 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*
* @deprecated Use one of the fetch- or iterate-related methods.
*/
public function setFetchMode($fetchMode)
{
......
......@@ -5,6 +5,7 @@ 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;
......@@ -130,6 +131,10 @@ class SQLSrvConnection implements ServerInfoAwareConnection
$stmt = $this->query('SELECT @@IDENTITY');
}
if ($stmt instanceof ForwardCompatibleResultStatement) {
return $stmt->fetchOne();
}
return $stmt->fetchColumn();
}
......
......@@ -2,9 +2,11 @@
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 array_key_exists;
......@@ -33,7 +35,7 @@ use const SQLSRV_PARAM_IN;
/**
* SQL Server Statement.
*/
class SQLSrvStatement implements IteratorAggregate, Statement
class SQLSrvStatement implements IteratorAggregate, Statement, ForwardCompatibleResultStatement
{
/**
* The SQLSRV Resource.
......@@ -308,6 +310,8 @@ class SQLSrvStatement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*
* @deprecated Use one of the fetch- or iterate-related methods.
*/
public function setFetchMode($fetchMode)
{
......@@ -318,6 +322,8 @@ class SQLSrvStatement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*
* @deprecated Use iterateNumeric(), iterateAssociative() or iterateColumn() instead.
*/
public function getIterator()
{
......@@ -327,6 +333,8 @@ class SQLSrvStatement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*
* @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
*
* @throws SQLSrvException
*/
public function fetch($fetchMode = null)
......@@ -352,6 +360,8 @@ class SQLSrvStatement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
*/
public function fetchAll($fetchMode = null)
{
......@@ -376,6 +386,8 @@ class SQLSrvStatement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*
* @deprecated Use fetchOne() instead.
*/
public function fetchColumn()
{
......@@ -388,6 +400,46 @@ class SQLSrvStatement implements IteratorAggregate, Statement
return $row[0] ?? null;
}
/**
* {@inheritdoc}
*/
public function fetchNumeric()
{
return $this->doFetch(SQLSRV_FETCH_NUMERIC);
}
/**
* {@inheritdoc}
*/
public function fetchAssociative()
{
return $this->doFetch(SQLSRV_FETCH_ASSOC);
}
/**
* {@inheritdoc}
*/
public function fetchOne()
{
return FetchUtils::fetchOne($this);
}
/**
* {@inheritdoc}
*/
public function fetchAllNumeric() : array
{
return FetchUtils::fetchAllNumeric($this);
}
/**
* {@inheritdoc}
*/
public function fetchAllAssociative() : array
{
return FetchUtils::fetchAllAssociative($this);
}
public function rowCount() : int
{
if ($this->stmt === null) {
......@@ -402,4 +454,18 @@ class SQLSrvStatement implements IteratorAggregate, Statement
return 0;
}
/**
* @return mixed|false
*/
private function doFetch(int $fetchType)
{
// do not try fetching from the statement if it's not expected to contain the result
// in order to prevent exceptional situation
if ($this->stmt === null || ! $this->result) {
return false;
}
return sqlsrv_fetch_array($this->stmt, $fetchType) ?? false;
}
}
......@@ -4,6 +4,9 @@ namespace Doctrine\DBAL\Driver;
use IteratorAggregate;
/**
* @deprecated Use iterateNumeric(), iterateAssociative() or iterateColumn().
*/
class StatementIterator implements IteratorAggregate
{
/** @var ResultStatement */
......
......@@ -4,6 +4,8 @@ namespace Doctrine\DBAL;
/**
* Contains statement fetch modes.
*
* @deprecated Use one of the fetch- or iterate-related methods on the Statement.
*/
final class FetchMode
{
......
<?php
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.
*/
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;
}
<?php
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.
*/
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;
}
......@@ -5,7 +5,6 @@ namespace Doctrine\DBAL\Id;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\LockMode;
use Throwable;
use function array_change_key_case;
......@@ -105,8 +104,7 @@ class TableGenerator
$sql = 'SELECT sequence_value, sequence_increment_by'
. ' FROM ' . $platform->appendLockHint($this->generatorTableName, LockMode::PESSIMISTIC_WRITE)
. ' WHERE sequence_name = ? ' . $platform->getWriteLockSQL();
$stmt = $this->conn->executeQuery($sql, [$sequenceName]);
$row = $stmt->fetch(FetchMode::ASSOCIATIVE);
$row = $this->conn->fetchAssociative($sql, [$sequenceName]);
if ($row !== false) {
$row = array_change_key_case($row, CASE_LOWER);
......
......@@ -6,6 +6,7 @@ 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;
......@@ -16,7 +17,7 @@ use function rtrim;
/**
* Portability wrapper for a Statement.
*/
class Statement implements IteratorAggregate, DriverStatement
class Statement implements IteratorAggregate, DriverStatement, ForwardCompatibleResultStatement
{
/** @var int */
private $portability;
......@@ -114,6 +115,8 @@ class Statement implements IteratorAggregate, DriverStatement
/**
* {@inheritdoc}
*
* @deprecated Use one of the fetch- or iterate-related methods.
*/
public function setFetchMode($fetchMode)
{
......@@ -124,6 +127,8 @@ class Statement implements IteratorAggregate, DriverStatement
/**
* {@inheritdoc}
*
* @deprecated Use iterateNumeric(), iterateAssociative() or iterateColumn() instead.
*/
public function getIterator()
{
......@@ -132,6 +137,8 @@ class Statement implements IteratorAggregate, DriverStatement
/**
* {@inheritdoc}
*
* @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
*/
public function fetch($fetchMode = null)
{
......@@ -151,6 +158,8 @@ class Statement implements IteratorAggregate, DriverStatement
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
*/
public function fetchAll($fetchMode = null)
{
......@@ -158,32 +167,133 @@ class Statement implements IteratorAggregate, DriverStatement
$rows = $this->stmt->fetchAll($fetchMode);
$iterateRow = ($this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM)) !== 0;
$fixCase = $this->case !== null
$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);
}
/**
* {@inheritdoc}
*/
public function fetchAssociative()
{
if ($this->stmt instanceof ForwardCompatibleResultStatement) {
$row = $this->stmt->fetchAssociative();
} else {
$row = $this->stmt->fetch(FetchMode::ASSOCIATIVE);
}
return $this->fixResult($row, true);
}
/**
* {@inheritdoc}
*/
public function fetchOne()
{
if ($this->stmt instanceof ForwardCompatibleResultStatement) {
$value = $this->stmt->fetchOne();
} else {
$value = $this->stmt->fetch(FetchMode::COLUMN);
}
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;
}
/**
* {@inheritdoc}
*/
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);
}
/**
* {@inheritdoc}
*/
public function fetchAllAssociative() : array
{
if ($this->stmt instanceof ForwardCompatibleResultStatement) {
$data = $this->stmt->fetchAllAssociative();
} else {
$data = $this->stmt->fetchAll(FetchMode::ASSOCIATIVE);
}
return $this->fixResultSet($data, true, true);
}
/**
* @param mixed $result
*
* @return mixed
*/
private function fixResult($result, bool $fixCase)
{
$iterateRow = ($this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM)) !== 0;
$fixCase = $fixCase && $this->case !== null && ($this->portability & Connection::PORTABILITY_FIX_CASE) !== 0;
return $this->fixRow($result, $iterateRow, $fixCase);
}
/**
* @param array<int,mixed> $resultSet
*
* @return array<int,mixed>
*/
private function fixResultSet(array $resultSet, bool $fixCase, bool $isArray) : array
{
$iterateRow = ($this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM)) !== 0;
$fixCase = $fixCase && $this->case !== null && ($this->portability & Connection::PORTABILITY_FIX_CASE) !== 0;
if (! $iterateRow && ! $fixCase) {
return $rows;
return $resultSet;
}
if ($fetchMode === FetchMode::COLUMN) {
foreach ($rows as $num => $row) {
$rows[$num] = [$row];
if (! $isArray) {
foreach ($resultSet as $num => $value) {
$resultSet[$num] = [$value];
}
}
foreach ($rows as $num => $row) {
$rows[$num] = $this->fixRow($row, $iterateRow, $fixCase);
foreach ($resultSet as $num => $row) {
$resultSet[$num] = $this->fixRow($row, $iterateRow, $fixCase);
}
if ($fetchMode === FetchMode::COLUMN) {
foreach ($rows as $num => $row) {
$rows[$num] = $row[0];
if (! $isArray) {
foreach ($resultSet as $num => $row) {
$resultSet[$num] = $row[0];
}
}
return $rows;
return $resultSet;
}
/**
......@@ -218,6 +328,8 @@ class Statement implements IteratorAggregate, DriverStatement
/**
* {@inheritdoc}
*
* @deprecated Use fetchOne() instead.
*/
public function fetchColumn()
{
......
......@@ -100,7 +100,7 @@ abstract class AbstractSchemaManager
{
$sql = $this->_platform->getListDatabasesSQL();
$databases = $this->_conn->fetchAll($sql);
$databases = $this->_conn->fetchAllAssociative($sql);
return $this->_getPortableDatabasesList($databases);
}
......@@ -114,7 +114,7 @@ abstract class AbstractSchemaManager
{
$sql = $this->_platform->getListNamespacesSQL();
$namespaces = $this->_conn->fetchAll($sql);
$namespaces = $this->_conn->fetchAllAssociative($sql);
return $this->getPortableNamespacesList($namespaces);
}
......@@ -134,7 +134,7 @@ abstract class AbstractSchemaManager
$sql = $this->_platform->getListSequencesSQL($database);
$sequences = $this->_conn->fetchAll($sql);
$sequences = $this->_conn->fetchAllAssociative($sql);
return $this->filterAssetNames($this->_getPortableSequencesList($sequences));
}
......@@ -162,7 +162,7 @@ abstract class AbstractSchemaManager
$sql = $this->_platform->getListTableColumnsSQL($table, $database);
$tableColumns = $this->_conn->fetchAll($sql);
$tableColumns = $this->_conn->fetchAllAssociative($sql);
return $this->_getPortableTableColumnList($table, $database, $tableColumns);
}
......@@ -180,7 +180,7 @@ abstract class AbstractSchemaManager
{
$sql = $this->_platform->getListTableIndexesSQL($table, $this->_conn->getDatabase());
$tableIndexes = $this->_conn->fetchAll($sql);
$tableIndexes = $this->_conn->fetchAllAssociative($sql);
return $this->_getPortableTableIndexesList($tableIndexes, $table);
}
......@@ -210,7 +210,7 @@ abstract class AbstractSchemaManager
{
$sql = $this->_platform->getListTablesSQL();
$tables = $this->_conn->fetchAll($sql);
$tables = $this->_conn->fetchAllAssociative($sql);
$tableNames = $this->_getPortableTablesList($tables);
return $this->filterAssetNames($tableNames);
......@@ -288,7 +288,7 @@ abstract class AbstractSchemaManager
{
$database = $this->_conn->getDatabase();
$sql = $this->_platform->getListViewsSQL($database);
$views = $this->_conn->fetchAll($sql);
$views = $this->_conn->fetchAllAssociative($sql);
return $this->_getPortableViewsList($views);
}
......@@ -308,7 +308,7 @@ abstract class AbstractSchemaManager
}
$sql = $this->_platform->getListTableForeignKeysSQL($table, $database);
$tableForeignKeys = $this->_conn->fetchAll($sql);
$tableForeignKeys = $this->_conn->fetchAllAssociative($sql);
return $this->_getPortableTableForeignKeysList($tableForeignKeys);
}
......
......@@ -29,7 +29,7 @@ class DB2SchemaManager extends AbstractSchemaManager
$sql = $this->_platform->getListTablesSQL();
$sql .= ' AND CREATOR = UPPER(' . $this->_conn->quote($this->_conn->getUsername()) . ')';
$tables = $this->_conn->fetchAll($sql);
$tables = $this->_conn->fetchAllAssociative($sql);
return $this->filterAssetNames($this->_getPortableTablesList($tables));
}
......@@ -227,7 +227,7 @@ class DB2SchemaManager extends AbstractSchemaManager
assert($platform instanceof DB2Platform);
$sql = $platform->getListTableCommentsSQL($tableName);
$tableOptions = $this->_conn->fetchAssoc($sql);
$tableOptions = $this->_conn->fetchAssociative($sql);
if ($tableOptions !== false) {
$table->addOption('comment', $tableOptions['REMARKS']);
......
......@@ -330,7 +330,7 @@ class MySqlSchemaManager extends AbstractSchemaManager
assert($platform instanceof MySqlPlatform);
$sql = $platform->getListTableMetadataSQL($tableName);
$tableOptions = $this->_conn->fetchAssoc($sql);
$tableOptions = $this->_conn->fetchAssociative($sql);
if ($tableOptions === false) {
return $table;
......
......@@ -380,7 +380,7 @@ WHERE
AND p.addr(+) = s.paddr
SQL;
$activeUserSessions = $this->_conn->fetchAll($sql, [strtoupper($user)]);
$activeUserSessions = $this->_conn->fetchAllAssociative($sql, [strtoupper($user)]);
foreach ($activeUserSessions as $activeUserSession) {
$activeUserSession = array_change_key_case($activeUserSession, CASE_LOWER);
......@@ -406,7 +406,7 @@ SQL;
assert($platform instanceof OraclePlatform);
$sql = $platform->getListTableCommentsSQL($tableName);
$tableOptions = $this->_conn->fetchAssoc($sql);
$tableOptions = $this->_conn->fetchAssociative($sql);
if ($tableOptions !== false) {
$table->addOption('comment', $tableOptions['COMMENTS']);
......
......@@ -233,8 +233,7 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
implode(' ,', $colNumbers)
);
$stmt = $this->_conn->executeQuery($columnNameSql);
$indexColumns = $stmt->fetchAll();
$indexColumns = $this->_conn->fetchAllAssociative($columnNameSql);
// required for getting the order of the columns right.
foreach ($colNumbers as $colNum) {
......
......@@ -250,7 +250,7 @@ class SQLServerSchemaManager extends AbstractSchemaManager
$sql = $this->_platform->getListTableIndexesSQL($table, $this->_conn->getDatabase());
try {
$tableIndexes = $this->_conn->fetchAll($sql);
$tableIndexes = $this->_conn->fetchAllAssociative($sql);
} catch (PDOException $e) {
if ($e->getCode() === 'IMSSP') {
return [];
......@@ -276,7 +276,7 @@ class SQLServerSchemaManager extends AbstractSchemaManager
if (count($tableDiff->removedColumns) > 0) {
foreach ($tableDiff->removedColumns as $col) {
$columnConstraintSql = $this->getColumnConstraintSQL($tableDiff->name, $col->getName());
foreach ($this->_conn->fetchAll($columnConstraintSql) as $constraint) {
foreach ($this->_conn->fetchAllAssociative($columnConstraintSql) as $constraint) {
$this->_conn->exec(
sprintf(
'ALTER TABLE %s DROP CONSTRAINT %s',
......@@ -342,7 +342,7 @@ class SQLServerSchemaManager extends AbstractSchemaManager
assert($platform instanceof SQLServer2012Platform);
$sql = $platform->getListTableMetadataSQL($tableName);
$tableOptions = $this->_conn->fetchAssoc($sql);
$tableOptions = $this->_conn->fetchAssociative($sql);
if ($tableOptions !== false) {
$table->addOption('comment', $tableOptions['table_comment']);
......
......@@ -4,7 +4,6 @@ namespace Doctrine\DBAL\Schema;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\Types\StringType;
use Doctrine\DBAL\Types\TextType;
use Doctrine\DBAL\Types\Type;
......@@ -115,7 +114,7 @@ class SqliteSchemaManager extends AbstractSchemaManager
}
$sql = $this->_platform->getListTableForeignKeysSQL($table, $database);
$tableForeignKeys = $this->_conn->fetchAll($sql);
$tableForeignKeys = $this->_conn->fetchAllAssociative($sql);
if (! empty($tableForeignKeys)) {
$createSql = $this->getCreateTableSQL($table);
......@@ -169,11 +168,10 @@ class SqliteSchemaManager extends AbstractSchemaManager
$indexBuffer = [];
// fetch primary
$stmt = $this->_conn->executeQuery(sprintf(
$indexArray = $this->_conn->fetchAllAssociative(sprintf(
'PRAGMA TABLE_INFO (%s)',
$this->_conn->quote($tableName)
));
$indexArray = $stmt->fetchAll(FetchMode::ASSOCIATIVE);
usort($indexArray, static function ($a, $b) {
if ($a['pk'] === $b['pk']) {
......@@ -208,11 +206,10 @@ class SqliteSchemaManager extends AbstractSchemaManager
$idx['primary'] = false;
$idx['non_unique'] = ! $tableIndex['unique'];
$stmt = $this->_conn->executeQuery(sprintf(
'PRAGMA INDEX_INFO (%s)',
$this->_conn->quote($keyName)
));
$indexArray = $stmt->fetchAll(FetchMode::ASSOCIATIVE);
$indexArray = $this->_conn->fetchAllAssociative(sprintf(
'PRAGMA INDEX_INFO (%s)',
$this->_conn->quote($keyName)
));
foreach ($indexArray as $indexColumnRow) {
$idx['column_name'] = $indexColumnRow['name'];
......
......@@ -2,18 +2,21 @@
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;
/**
* A thin wrapper around a Doctrine\DBAL\Driver\Statement that adds support
* for logging, DBAL mapping types, etc.
*/
class Statement implements IteratorAggregate, DriverStatement
class Statement implements IteratorAggregate, DriverStatement, ForwardCompatibleResultStatement
{
/**
* The SQL statement.
......@@ -217,6 +220,8 @@ class Statement implements IteratorAggregate, DriverStatement
/**
* {@inheritdoc}
*
* @deprecated Use one of the fetch- or iterate-related methods.
*/
public function setFetchMode($fetchMode)
{
......@@ -226,6 +231,8 @@ class Statement implements IteratorAggregate, DriverStatement
/**
* Required by interface IteratorAggregate.
*
* @deprecated Use iterateNumeric(), iterateAssociative() or iterateColumn() instead.
*
* {@inheritdoc}
*/
public function getIterator()
......@@ -235,6 +242,8 @@ class Statement implements IteratorAggregate, DriverStatement
/**
* {@inheritdoc}
*
* @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
*/
public function fetch($fetchMode = null)
{
......@@ -243,6 +252,8 @@ class Statement implements IteratorAggregate, DriverStatement
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
*/
public function fetchAll($fetchMode = null)
{
......@@ -251,12 +262,176 @@ class Statement implements IteratorAggregate, DriverStatement
/**
* {@inheritDoc}
*
* @deprecated Use fetchOne() instead.
*/
public function fetchColumn()
{
return $this->stmt->fetchColumn();
}
/**
* {@inheritdoc}
*
* @throws DBALException
*/
public function fetchNumeric()
{
try {
if ($this->stmt instanceof ForwardCompatibleResultStatement) {
return $this->stmt->fetchNumeric();
}
return $this->stmt->fetch(FetchMode::NUMERIC);
} catch (DriverException $e) {
throw DBALException::driverException($this->conn->getDriver(), $e);
}
}
/**
* {@inheritdoc}
*
* @throws DBALException
*/
public function fetchAssociative()
{
try {
if ($this->stmt instanceof ForwardCompatibleResultStatement) {
return $this->stmt->fetchAssociative();
}
return $this->stmt->fetch(FetchMode::ASSOCIATIVE);
} catch (DriverException $e) {
throw DBALException::driverException($this->conn->getDriver(), $e);
}
}
/**
* {@inheritDoc}
*
* @throws DBALException
*/
public function fetchOne()
{
try {
if ($this->stmt instanceof ForwardCompatibleResultStatement) {
return $this->stmt->fetchOne();
}
return $this->stmt->fetch(FetchMode::COLUMN);
} catch (DriverException $e) {
throw DBALException::driverException($this->conn->getDriver(), $e);
}
}
/**
* {@inheritdoc}
*
* @throws DBALException
*/
public function fetchAllNumeric() : array
{
try {
if ($this->stmt instanceof ForwardCompatibleResultStatement) {
return $this->stmt->fetchAllNumeric();
}
return $this->stmt->fetchAll(FetchMode::NUMERIC);
} catch (DriverException $e) {
throw DBALException::driverException($this->conn->getDriver(), $e);
}
}
/**
* {@inheritdoc}
*
* @throws DBALException
*/
public function fetchAllAssociative() : array
{
try {
if ($this->stmt instanceof ForwardCompatibleResultStatement) {
return $this->stmt->fetchAllAssociative();
}
return $this->stmt->fetchAll(FetchMode::ASSOCIATIVE);
} catch (DriverException $e) {
throw DBALException::driverException($this->conn->getDriver(), $e);
}
}
/**
* {@inheritDoc}
*
* @return Traversable<int,array<int,mixed>>
*
* @throws DBALException
*/
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;
}
}
} catch (DriverException $e) {
throw DBALException::driverException($this->conn->getDriver(), $e);
}
}
/**
* {@inheritDoc}
*
* @return Traversable<int,array<string,mixed>>
*
* @throws DBALException
*/
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;
}
}
} catch (DriverException $e) {
throw DBALException::driverException($this->conn->getDriver(), $e);
}
}
/**
* {@inheritDoc}
*
* @return Traversable<int,mixed>
*
* @throws DBALException
*/
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;
}
}
} catch (DriverException $e) {
throw DBALException::driverException($this->conn->getDriver(), $e);
}
}
/**
* Returns the number of rows affected by the last execution of this statement.
*
......
......@@ -87,7 +87,7 @@ EOT
assert(is_bool($forceFetch));
if (stripos($sql, 'select') === 0 || $forceFetch) {
$resultSet = $conn->fetchAll($sql);
$resultSet = $conn->fetchAllAssociative($sql);
} else {
$resultSet = $conn->executeUpdate($sql);
}
......
......@@ -97,8 +97,10 @@ class StatementIteratorTest extends TestCase
yield [PortabilityStatement::class];
yield [SQLAnywhereStatement::class];
if (extension_loaded('sqlsrv')) {
yield [SQLSrvStatement::class];
if (! extension_loaded('sqlsrv')) {
return;
}
yield [SQLSrvStatement::class];
}
}
<?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();
}
}
<?php
namespace Doctrine\DBAL\Tests\Functional\Connection;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Tests\TestUtil;
use function iterator_to_array;
class FetchTest extends FunctionalTestCase
{
/** @var string */
private $query;
public function setUp() : void
{
parent::setUp();
$this->query = TestUtil::generateResultSetQuery([
[
'a' => 'foo',
'b' => 1,
],
[
'a' => 'bar',
'b' => 2,
],
[
'a' => 'baz',
'b' => 3,
],
], $this->connection->getDatabasePlatform());
}
public function testFetchNumeric() : void
{
self::assertEquals(['foo', 1], $this->connection->fetchNumeric($this->query));
}
public function testFetchOne() : void
{
self::assertEquals('foo', $this->connection->fetchOne($this->query));
}
public function testFetchAssociative() : void
{
self::assertEquals([
'a' => 'foo',
'b' => 1,
], $this->connection->fetchAssociative($this->query));
}
public function testFetchAllNumeric() : void
{
self::assertEquals([
['foo', 1],
['bar', 2],
['baz', 3],
], $this->connection->fetchAllNumeric($this->query));
}
public function testFetchAllAssociative() : void
{
self::assertEquals([
[
'a' => 'foo',
'b' => 1,
],
[
'a' => 'bar',
'b' => 2,
],
[
'a' => 'baz',
'b' => 3,
],
], $this->connection->fetchAllAssociative($this->query));
}
public function testIterateNumeric() : void
{
self::assertEquals([
['foo', 1],
['bar', 2],
['baz', 3],
], iterator_to_array($this->connection->iterateNumeric($this->query)));
}
public function testIterateAssociative() : void
{
self::assertEquals([
[
'a' => 'foo',
'b' => 1,
],
[
'a' => 'bar',
'b' => 2,
],
[
'a' => 'baz',
'b' => 3,
],
], iterator_to_array($this->connection->iterateAssociative($this->query)));
}
public function testIterateColumn() : void
{
self::assertEquals([
'foo',
'bar',
'baz',
], iterator_to_array($this->connection->iterateColumn($this->query)));
}
}
......@@ -30,7 +30,7 @@ final class DB2SchemaManagerTest extends TestCase
$platform = $this->createMock(DB2Platform::class);
$this->conn = $this
->getMockBuilder(Connection::class)
->onlyMethods(['fetchAll', 'quote'])
->onlyMethods(['fetchAllAssociative', 'quote'])
->setConstructorArgs([['platform' => $platform], $driverMock, new Configuration(), $eventManager])
->getMock();
$this->manager = new DB2SchemaManager($this->conn);
......@@ -44,7 +44,7 @@ final class DB2SchemaManagerTest extends TestCase
public function testListTableNamesFiltersAssetNamesCorrectly() : void
{
$this->conn->getConfiguration()->setFilterSchemaAssetsExpression('/^(?!T_)/');
$this->conn->expects(self::once())->method('fetchAll')->will(self::returnValue([
$this->conn->expects(self::once())->method('fetchAllAssociative')->will(self::returnValue([
['name' => 'FOO'],
['name' => 'T_FOO'],
['name' => 'BAR'],
......@@ -67,7 +67,7 @@ final class DB2SchemaManagerTest extends TestCase
{
$filterExpression = '/^(?!T_)/';
$this->conn->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression);
$this->conn->expects(self::once())->method('fetchAll')->will(self::returnValue([
$this->conn->expects(self::once())->method('fetchAllAssociative')->will(self::returnValue([
['name' => 'FOO'],
['name' => 'T_FOO'],
['name' => 'BAR'],
......@@ -96,7 +96,7 @@ final class DB2SchemaManagerTest extends TestCase
return in_array($assetName, $accepted, true);
});
$this->conn->expects(self::any())->method('quote');
$this->conn->expects(self::once())->method('fetchAll')->will(self::returnValue([
$this->conn->expects(self::once())->method('fetchAllAssociative')->will(self::returnValue([
['name' => 'FOO'],
['name' => 'T_FOO'],
['name' => 'BAR'],
......@@ -121,7 +121,7 @@ final class DB2SchemaManagerTest extends TestCase
return in_array($assetName, $accepted);
});
$this->conn->expects(self::any())->method('quote');
$this->conn->expects($this->atLeastOnce())->method('fetchAll')->will(self::returnValue([
$this->conn->expects($this->atLeastOnce())->method('fetchAllAssociative')->will(self::returnValue([
['name' => 'FOO'],
['name' => 'T_FOO'],
['name' => 'BAR'],
......@@ -156,7 +156,7 @@ final class DB2SchemaManagerTest extends TestCase
$filterExpression = '/^(?!T_)/';
$this->conn->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression);
$this->conn->expects(self::exactly(2))->method('fetchAll')->will(self::returnValue([
$this->conn->expects(self::exactly(2))->method('fetchAllAssociative')->will(self::returnValue([
['name' => 'FOO'],
['name' => 'T_FOO'],
['name' => 'BAR'],
......
......@@ -23,11 +23,15 @@ class MySqlSchemaManagerTest extends TestCase
protected function setUp() : void
{
$eventManager = new EventManager();
$driverMock = $this->createMock(Driver::class);
$platform = $this->createMock(MySqlPlatform::class);
$eventManager = new EventManager();
$driverMock = $this->createMock(Driver::class);
$platform = $this->createMock(MySqlPlatform::class);
$platform->method('getListTableForeignKeysSQL')
->willReturn('');
$this->conn = $this->getMockBuilder(Connection::class)
->onlyMethods(['fetchAll'])
->onlyMethods(['fetchAllAssociative'])
->setConstructorArgs([['platform' => $platform], $driverMock, new Configuration(), $eventManager])
->getMock();
$this->manager = new MySqlSchemaManager($this->conn);
......@@ -35,7 +39,7 @@ class MySqlSchemaManagerTest extends TestCase
public function testCompositeForeignKeys() : void
{
$this->conn->expects(self::once())->method('fetchAll')->will(self::returnValue($this->getFKDefinition()));
$this->conn->expects(self::once())->method('fetchAllAssociative')->will(self::returnValue($this->getFKDefinition()));
$fkeys = $this->manager->listTableForeignKeys('dummy');
self::assertCount(1, $fkeys, 'Table has to have one foreign key.');
......
......@@ -4,9 +4,15 @@ namespace Doctrine\DBAL\Tests;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use PHPUnit\Framework\Assert;
use function array_keys;
use function array_map;
use function array_values;
use function explode;
use function extension_loaded;
use function implode;
use function is_string;
use function unlink;
/**
......@@ -207,4 +213,24 @@ class TestUtil
{
return DriverManager::getConnection(self::getParamsForTemporaryConnection());
}
/**
* Generates a query that will return the given rows without the need to create a temporary table.
*
* @param array<int,array<string,mixed>> $rows
*/
public static function generateResultSetQuery(array $rows, AbstractPlatform $platform) : string
{
return implode(' UNION ALL ', array_map(static function (array $row) use ($platform) : string {
return $platform->getDummySelectSQL(
implode(', ', array_map(static function (string $column, $value) use ($platform) : string {
if (is_string($value)) {
$value = $platform->quoteStringLiteral($value);
}
return $value . ' ' . $platform->quoteIdentifier($column);
}, array_keys($row), array_values($row)))
);
}, $rows));
}
}
......@@ -30,7 +30,7 @@ class RunSqlCommandTest extends TestCase
$this->commandTester = new CommandTester($this->command);
$this->connectionMock = $this->createMock(Connection::class);
$this->connectionMock->method('fetchAll')
$this->connectionMock->method('fetchAllAssociative')
->willReturn([[1]]);
$this->connectionMock->method('executeUpdate')
->willReturn(42);
......@@ -68,7 +68,7 @@ class RunSqlCommandTest extends TestCase
public function testSelectStatementsPrintsResult() : void
{
$this->expectConnectionFetchAll();
$this->expectConnectionFetchAllAssociative();
$exitCode = $this->commandTester->execute([
'command' => $this->command->getName(),
......@@ -100,22 +100,22 @@ class RunSqlCommandTest extends TestCase
->method('executeUpdate');
$this->connectionMock
->expects(self::exactly(0))
->method('fetchAll');
->method('fetchAllAssociative');
}
private function expectConnectionFetchAll() : void
private function expectConnectionFetchAllAssociative() : void
{
$this->connectionMock
->expects(self::exactly(0))
->method('executeUpdate');
$this->connectionMock
->expects(self::exactly(1))
->method('fetchAll');
->method('fetchAllAssociative');
}
public function testStatementsWithFetchResultPrintsResult() : void
{
$this->expectConnectionFetchAll();
$this->expectConnectionFetchAllAssociative();
$this->commandTester->execute([
'command' => $this->command->getName(),
......
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