Commit 9b604859 authored by Steve Müller's avatar Steve Müller

fix fetching NULL values via fetchColumn()

parent 7710d546
......@@ -262,11 +262,12 @@ class DB2Statement implements \IteratorAggregate, Statement
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;
}
/**
......
......@@ -289,7 +289,11 @@ class OCI8Statement implements \IteratorAggregate, Statement
{
$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;
}
/**
......
......@@ -266,11 +266,11 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement
{
$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;
}
/**
......
......@@ -239,7 +239,9 @@ class SQLSrvStatement implements IteratorAggregate, Statement
$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;
......@@ -247,7 +249,10 @@ class SQLSrvStatement implements IteratorAggregate, Statement
$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!");
......@@ -287,11 +292,11 @@ class SQLSrvStatement implements IteratorAggregate, Statement
{
$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;
}
/**
......
......@@ -767,6 +767,47 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase
$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();
......
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