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

[DBAL-1028] Fix fetching NULL values via fetchColumn() #709

Merged
merged 2 commits into from
Nov 5, 2014
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
7 changes: 4 additions & 3 deletions lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,12 @@ public function fetchAll($fetchMode = null)
public function fetchColumn($columnIndex = 0)
{
$row = $this->fetch(\PDO::FETCH_NUM);
if ($row && isset($row[$columnIndex])) {
return $row[$columnIndex];

if (false === $row) {
return false;
}

return false;
return isset($row[$columnIndex]) ? $row[$columnIndex] : null;
}

/**
Expand Down
6 changes: 5 additions & 1 deletion lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,11 @@ public function fetchColumn($columnIndex = 0)
{
$row = oci_fetch_array($this->_sth, OCI_NUM | OCI_RETURN_NULLS | OCI_RETURN_LOBS);

return isset($row[$columnIndex]) ? $row[$columnIndex] : false;
if (false === $row) {
return false;
}

return isset($row[$columnIndex]) ? $row[$columnIndex] : null;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,11 @@ public function fetchColumn($columnIndex = 0)
{
$row = $this->fetch(PDO::FETCH_NUM);

if ($row && isset($row[$columnIndex])) {
return $row[$columnIndex];
if (false === $row) {
return false;
}

return false;
return isset($row[$columnIndex]) ? $row[$columnIndex] : null;
}

/**
Expand Down
21 changes: 13 additions & 8 deletions lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,18 +236,23 @@ public function getIterator()
*/
public function fetch($fetchMode = null)
{
$args = func_get_args();
$args = func_get_args();
$fetchMode = $fetchMode ?: $this->defaultFetchMode;

if (isset(self::$fetchMap[$fetchMode])) {
return sqlsrv_fetch_array($this->stmt, self::$fetchMap[$fetchMode]);
} elseif ($fetchMode == PDO::FETCH_OBJ || $fetchMode == PDO::FETCH_CLASS) {
return sqlsrv_fetch_array($this->stmt, self::$fetchMap[$fetchMode]) ?: false;
}

if ($fetchMode == PDO::FETCH_OBJ || $fetchMode == PDO::FETCH_CLASS) {
$className = $this->defaultFetchClass;
$ctorArgs = $this->defaultFetchClassCtorArgs;

if (count($args) >= 2) {
$className = $args[1];
$ctorArgs = (isset($args[2])) ? $args[2] : array();
$ctorArgs = (isset($args[2])) ? $args[2] : array();
}
return sqlsrv_fetch_object($this->stmt, $className, $ctorArgs);

return sqlsrv_fetch_object($this->stmt, $className, $ctorArgs) ?: false;
}

throw new SQLSrvException("Fetch mode is not supported!");
Expand Down Expand Up @@ -287,11 +292,11 @@ public function fetchColumn($columnIndex = 0)
{
$row = $this->fetch(PDO::FETCH_NUM);

if ($row && isset($row[$columnIndex])) {
return $row[$columnIndex];
if (false === $row) {
return false;
}

return false;
return isset($row[$columnIndex]) ? $row[$columnIndex] : null;
}

/**
Expand Down
41 changes: 41 additions & 0 deletions tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,47 @@ public function testEmptyParameters()
$this->assertEquals(array(), $rows);
}

/**
* @group DBAL-1028
*/
public function testFetchColumnNullValue()
{
$this->_conn->executeUpdate(
'INSERT INTO fetch_table (test_int, test_string) VALUES (?, ?)',
array(1, 'foo')
);

$this->assertNull(
$this->_conn->fetchColumn('SELECT test_datetime FROM fetch_table WHERE test_int = ?', array(1))
);
}

/**
* @group DBAL-1028
*/
public function testFetchColumnNonExistingIndex()
{
if ($this->_conn->getDriver()->getName() === 'pdo_sqlsrv') {
$this->markTestSkipped(
'Test does not work for pdo_sqlsrv driver as it throws a fatal error for a non-existing column index.'
);
}

$this->assertNull(
$this->_conn->fetchColumn('SELECT test_int FROM fetch_table WHERE test_int = ?', array(1), 1)
);
}

/**
* @group DBAL-1028
*/
public function testFetchColumnNoResult()
{
$this->assertFalse(
$this->_conn->fetchColumn('SELECT test_int FROM fetch_table WHERE test_int = ?', array(-1))
);
}

private function setupFixture()
{
$this->_conn->executeQuery('DELETE FROM fetch_table')->execute();
Expand Down