Skip to content

Commit

Permalink
fix fetching NULL values via fetchColumn()
Browse files Browse the repository at this point in the history
  • Loading branch information
deeky666 committed Nov 5, 2014
1 parent d126728 commit cc1aeec
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 12 deletions.
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
15 changes: 10 additions & 5 deletions lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,15 +239,20 @@ public function fetch($fetchMode = null)
$args = func_get_args();
$fetchMode = $fetchMode ?: $this->defaultFetchMode;
if (isset(self::$fetchMap[$fetchMode])) {
return sqlsrv_fetch_array($this->stmt, self::$fetchMap[$fetchMode]);
$rows = sqlsrv_fetch_array($this->stmt, self::$fetchMap[$fetchMode]);

return $rows ?: false;
} elseif ($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();
}
return sqlsrv_fetch_object($this->stmt, $className, $ctorArgs);

$rows = sqlsrv_fetch_object($this->stmt, $className, $ctorArgs);

return $rows ?: 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

0 comments on commit cc1aeec

Please sign in to comment.