Skip to content

Commit

Permalink
Statement::fetchColumn() will throw an exception in the case of inval…
Browse files Browse the repository at this point in the history
…id index
  • Loading branch information
morozov committed Dec 1, 2018
1 parent 4d40426 commit 27ea5d3
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 9 deletions.
4 changes: 4 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Upgrade to 2.9

## MINOR BC BREAK `Statement::fetchColumn()`.

Similarly to `PDOStatement::fetchColumn()` DBAL statements will throw an exception in case of an invalid column index.

## Deprecated `Statement::fetchColumn()` with an invalid index

Calls to `Statement::fetchColumn()` with an invalid column index currently return `NULL`. In the future, such calls will result in a exception.
Expand Down
12 changes: 10 additions & 2 deletions lib/Doctrine/DBAL/Cache/ArrayStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Doctrine\DBAL\Cache;

use ArrayIterator;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\FetchMode;
use InvalidArgumentException;
Expand Down Expand Up @@ -131,7 +132,14 @@ public function fetchColumn($columnIndex = 0)
{
$row = $this->fetch(FetchMode::NUMERIC);

// TODO: verify that return false is the correct behavior
return $row[$columnIndex] ?? false;
if ($row === false) {
return false;
}

if ($columnIndex >= count($row)) {
throw DBALException::invalidColumnIndex($columnIndex, count($row));
}

return $row[$columnIndex];
}
}
13 changes: 11 additions & 2 deletions lib/Doctrine/DBAL/Cache/ResultCacheStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ArrayIterator;
use Doctrine\Common\Cache\Cache;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\FetchMode;
Expand All @@ -12,6 +13,7 @@
use PDO;
use function array_merge;
use function array_values;
use function count;
use function reset;

/**
Expand Down Expand Up @@ -176,8 +178,15 @@ public function fetchColumn($columnIndex = 0)
{
$row = $this->fetch(FetchMode::NUMERIC);

// TODO: verify that return false is the correct behavior
return $row[$columnIndex] ?? false;
if ($row === false) {
return false;
}

if ($columnIndex >= count($row)) {
throw DBALException::invalidColumnIndex($columnIndex, count($row));
}

return $row[$columnIndex];
}

/**
Expand Down
10 changes: 10 additions & 0 deletions lib/Doctrine/DBAL/DBALException.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,14 @@ public static function typeNotFound($name)
{
return new self('Type to be overwritten ' . $name . ' does not exist.');
}

public static function invalidColumnIndex(int $index, int $count) : self
{
return new self(sprintf(
'Invalid column index %d. The statement result contains %d column%s.',
$index,
$count,
$count === 1 ? '' : 's'
));
}
}
8 changes: 7 additions & 1 deletion lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\DBAL\Driver\IBMDB2;

use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Driver\StatementIterator;
use Doctrine\DBAL\FetchMode;
Expand All @@ -19,6 +20,7 @@
use const DB2_PARAM_FILE;
use const DB2_PARAM_IN;
use function array_change_key_case;
use function count;
use function db2_bind_param;
use function db2_execute;
use function db2_fetch_array;
Expand Down Expand Up @@ -347,7 +349,11 @@ public function fetchColumn($columnIndex = 0)
return false;
}

return $row[$columnIndex] ?? null;
if ($columnIndex >= count($row)) {
throw DBALException::invalidColumnIndex($columnIndex, count($row));
}

return $row[$columnIndex];
}

/**
Expand Down
7 changes: 6 additions & 1 deletion lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\DBAL\Driver\Mysqli;

use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Driver\StatementIterator;
use Doctrine\DBAL\Exception\InvalidArgumentException;
Expand Down Expand Up @@ -388,7 +389,11 @@ public function fetchColumn($columnIndex = 0)
return false;
}

return $row[$columnIndex] ?? null;
if ($columnIndex >= count($row)) {
throw DBALException::invalidColumnIndex($columnIndex, count($row));
}

return $row[$columnIndex];
}

/**
Expand Down
7 changes: 6 additions & 1 deletion lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\DBAL\Driver\OCI8;

use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Driver\StatementIterator;
use Doctrine\DBAL\FetchMode;
Expand Down Expand Up @@ -497,7 +498,11 @@ public function fetchColumn($columnIndex = 0)
return false;
}

return $row[$columnIndex] ?? null;
if ($columnIndex >= count($row)) {
throw DBALException::invalidColumnIndex($columnIndex, count($row));
}

return $row[$columnIndex];
}

/**
Expand Down
5 changes: 5 additions & 0 deletions lib/Doctrine/DBAL/Driver/PDOStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\DBAL\Driver;

use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType;
use PDO;
Expand Down Expand Up @@ -177,6 +178,10 @@ public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = n
*/
public function fetchColumn($columnIndex = 0)
{
if ($columnIndex >= $this->columnCount()) {
throw DBALException::invalidColumnIndex($columnIndex, $this->rowCount());
}

try {
return parent::fetchColumn($columnIndex);
} catch (\PDOException $exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\DBAL\Driver\SQLAnywhere;

use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Driver\StatementIterator;
use Doctrine\DBAL\FetchMode;
Expand All @@ -13,6 +14,7 @@
use stdClass;
use const SASQL_BOTH;
use function array_key_exists;
use function count;
use function func_get_args;
use function func_num_args;
use function gettype;
Expand Down Expand Up @@ -279,7 +281,9 @@ public function fetchColumn($columnIndex = 0)
return false;
}

return $row[$columnIndex] ?? null;
if ($columnIndex >= count($row)) {
throw DBALException::invalidColumnIndex($columnIndex, count($row));
}
}

/**
Expand Down
7 changes: 6 additions & 1 deletion lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\DBAL\Driver\SQLSrv;

use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Driver\StatementIterator;
use Doctrine\DBAL\FetchMode;
Expand Down Expand Up @@ -398,7 +399,11 @@ public function fetchColumn($columnIndex = 0)
return false;
}

return $row[$columnIndex] ?? null;
if ($columnIndex >= count($row)) {
throw DBALException::invalidColumnIndex($columnIndex, count($row));
}

return $row[$columnIndex];
}

/**
Expand Down
11 changes: 11 additions & 0 deletions tests/Doctrine/Tests/DBAL/Functional/StatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\Tests\DBAL\Functional;

use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType;
Expand Down Expand Up @@ -292,4 +293,14 @@ public function testFetchInColumnMode() : void

self::assertEquals(1, $result);
}

public function testFetchColumnNonExistingIndex() : void
{
$platform = $this->connection->getDatabasePlatform();
$query = $platform->getDummySelectSQL();
$stmt = $this->connection->query($query);

self::expectException(DBALException::class);
$stmt->fetchColumn(1);
}
}

0 comments on commit 27ea5d3

Please sign in to comment.