Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Statement::fetchFirstColumn() #4037

Merged
merged 3 commits into from
May 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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.
3. The `Statement::fetchAll()` method is deprecated in favor of `fetchAllNumeric()`, `fetchAllAssociative()` and `fetchFirstColumn()`.
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()`.
Expand Down
10 changes: 9 additions & 1 deletion lib/Doctrine/DBAL/Cache/ArrayStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEX
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchFirstColumn() instead.
*/
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
{
Expand Down Expand Up @@ -201,6 +201,14 @@ public function fetchAllAssociative() : array
return FetchUtils::fetchAllAssociative($this);
}

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

/**
* @return mixed|false
*/
Expand Down
10 changes: 9 additions & 1 deletion lib/Doctrine/DBAL/Cache/ResultCacheStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEX
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchFirstColumn() instead.
*/
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
{
Expand Down Expand Up @@ -277,6 +277,14 @@ public function fetchAllAssociative() : array
return $this->data;
}

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

/**
* Returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement
* executed by the corresponding object.
Expand Down
26 changes: 26 additions & 0 deletions lib/Doctrine/DBAL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,32 @@ public function fetchAllAssociative(string $query, array $params = [], array $ty
}
}

/**
* 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,mixed>
*
* @throws DBALException
*/
public function fetchFirstColumn(string $query, array $params = [], array $types = []) : array
{
try {
$stmt = $this->executeQuery($query, $params, $types);

if ($stmt instanceof ForwardCompatibleResultStatement) {
return $stmt->fetchFirstColumn();
}

return $stmt->fetchAll(FetchMode::COLUMN);
} catch (Throwable $e) {
throw DBALException::driverExceptionDuringQuery($this->_driver, $e, $query);
}
}

/**
* Prepares and executes an SQL query and returns the result as an iterator over rows represented as numeric arrays.
*
Expand Down
16 changes: 16 additions & 0 deletions lib/Doctrine/DBAL/Driver/FetchUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,20 @@ public static function fetchAllAssociative(ResultStatement $stmt) : array

return $rows;
}

/**
* @return array<int,mixed>
*
* @throws DriverException
*/
public static function fetchFirstColumn(ResultStatement $stmt) : array
{
$rows = [];

while (($row = $stmt->fetchOne()) !== false) {
$rows[] = $row;
}

return $rows;
}
}
64 changes: 62 additions & 2 deletions lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

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\ForwardCompatibility\Driver\ResultStatement as ForwardCompatibleResultStatement;
use Doctrine\DBAL\ParameterType;
use IteratorAggregate;
use PDO;
Expand Down Expand Up @@ -46,7 +48,7 @@
use const DB2_PARAM_FILE;
use const DB2_PARAM_IN;

class DB2Statement implements IteratorAggregate, Statement
class DB2Statement implements IteratorAggregate, Statement, ForwardCompatibleResultStatement
{
/** @var resource */
private $stmt;
Expand Down Expand Up @@ -310,7 +312,7 @@ public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEX
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchFirstColumn() instead.
*/
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
{
Expand Down Expand Up @@ -356,6 +358,64 @@ public function fetchColumn($columnIndex = 0)
return $row[$columnIndex] ?? null;
}

/**
* {@inheritDoc}
*/
public function fetchNumeric()
{
if (! $this->result) {
return false;
}

return db2_fetch_array($this->stmt);
}

/**
* {@inheritdoc}
*/
public function fetchAssociative()
{
// 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 db2_fetch_assoc($this->stmt);
}

/**
* {@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 fetchFirstColumn() : array
{
return FetchUtils::fetchFirstColumn($this);
}

/**
* {@inheritdoc}
*/
Expand Down
10 changes: 9 additions & 1 deletion lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEX
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchFirstColumn() instead.
*/
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
{
Expand Down Expand Up @@ -467,6 +467,14 @@ public function fetchAllAssociative() : array
return FetchUtils::fetchAllAssociative($this);
}

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

/**
* {@inheritdoc}
*/
Expand Down
10 changes: 9 additions & 1 deletion lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEX
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchFirstColumn() instead.
*/
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
{
Expand Down Expand Up @@ -591,6 +591,14 @@ public function fetchAllAssociative() : array
return $this->doFetchAll(OCI_ASSOC, OCI_FETCHSTATEMENT_BY_ROW);
}

/**
* {@inheritdoc}
*/
public function fetchFirstColumn() : array
{
return $this->doFetchAll(OCI_NUM, OCI_FETCHSTATEMENT_BY_COLUMN)[0];
}

/**
* @return mixed|false
*/
Expand Down
10 changes: 9 additions & 1 deletion lib/Doctrine/DBAL/Driver/PDOStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEX
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchFirstColumn() instead.
*/
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
{
Expand Down Expand Up @@ -240,6 +240,14 @@ public function fetchAllAssociative() : array
return $this->fetchAll(PDO::FETCH_ASSOC);
}

/**
* {@inheritdoc}
*/
public function fetchFirstColumn() : array
{
return $this->fetchAll(PDO::FETCH_COLUMN);
}

/**
* Converts DBAL parameter type to PDO parameter type
*
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Driver/ResultStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEX
/**
* Returns an array containing all of the result set rows.
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchFirstColumn() 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,
Expand Down
12 changes: 11 additions & 1 deletion lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEX
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchFirstColumn() instead.
*/
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
{
Expand Down Expand Up @@ -368,6 +368,16 @@ public function fetchAllAssociative() : array
return FetchUtils::fetchAllAssociative($this);
}

/**
* @return array<int,mixed>
*
* @throws DriverException
*/
public function fetchFirstColumn() : array
{
return FetchUtils::fetchFirstColumn($this);
}

/**
* {@inheritdoc}
*/
Expand Down
10 changes: 9 additions & 1 deletion lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEX
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchFirstColumn() instead.
*/
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
{
Expand Down Expand Up @@ -476,6 +476,14 @@ public function fetchAllAssociative() : array
return FetchUtils::fetchAllAssociative($this);
}

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

/**
* {@inheritdoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,13 @@ public function fetchAllNumeric() : array;
* @throws DriverException
*/
public function fetchAllAssociative() : array;

/**
* Returns an array containing the values of the first column of the result set.
*
* @return array<int,mixed>
*
* @throws DriverException
*/
public function fetchFirstColumn() : array;
}
16 changes: 15 additions & 1 deletion lib/Doctrine/DBAL/Portability/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEX
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchFirstColumn() instead.
*/
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
{
Expand Down Expand Up @@ -255,6 +255,20 @@ public function fetchAllAssociative() : array
return $this->fixResultSet($data, true, true);
}

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

return $this->fixResultSet($data, true, false);
}

/**
* @param mixed $result
*
Expand Down
Loading