Commit 0b3e0043 authored by Marco Pivetta's avatar Marco Pivetta

Merge pull request #709 from deeky666/DBAL-1028

[DBAL-1028] Fix fetching NULL values via fetchColumn()
parents c6887c12 857b9500
...@@ -262,13 +262,14 @@ class DB2Statement implements \IteratorAggregate, Statement ...@@ -262,13 +262,14 @@ class DB2Statement implements \IteratorAggregate, Statement
public function fetchColumn($columnIndex = 0) public function fetchColumn($columnIndex = 0)
{ {
$row = $this->fetch(\PDO::FETCH_NUM); $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;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
...@@ -289,7 +289,11 @@ class OCI8Statement implements \IteratorAggregate, Statement ...@@ -289,7 +289,11 @@ class OCI8Statement implements \IteratorAggregate, Statement
{ {
$row = oci_fetch_array($this->_sth, OCI_NUM | OCI_RETURN_NULLS | OCI_RETURN_LOBS); $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 ...@@ -266,11 +266,11 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement
{ {
$row = $this->fetch(PDO::FETCH_NUM); $row = $this->fetch(PDO::FETCH_NUM);
if ($row && isset($row[$columnIndex])) { if (false === $row) {
return $row[$columnIndex]; return false;
} }
return false; return isset($row[$columnIndex]) ? $row[$columnIndex] : null;
} }
/** /**
......
...@@ -238,16 +238,21 @@ class SQLSrvStatement implements IteratorAggregate, Statement ...@@ -238,16 +238,21 @@ class SQLSrvStatement implements IteratorAggregate, Statement
{ {
$args = func_get_args(); $args = func_get_args();
$fetchMode = $fetchMode ?: $this->defaultFetchMode; $fetchMode = $fetchMode ?: $this->defaultFetchMode;
if (isset(self::$fetchMap[$fetchMode])) { if (isset(self::$fetchMap[$fetchMode])) {
return sqlsrv_fetch_array($this->stmt, self::$fetchMap[$fetchMode]); return sqlsrv_fetch_array($this->stmt, self::$fetchMap[$fetchMode]) ?: false;
} elseif ($fetchMode == PDO::FETCH_OBJ || $fetchMode == PDO::FETCH_CLASS) { }
if ($fetchMode == PDO::FETCH_OBJ || $fetchMode == PDO::FETCH_CLASS) {
$className = $this->defaultFetchClass; $className = $this->defaultFetchClass;
$ctorArgs = $this->defaultFetchClassCtorArgs; $ctorArgs = $this->defaultFetchClassCtorArgs;
if (count($args) >= 2) { if (count($args) >= 2) {
$className = $args[1]; $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!"); throw new SQLSrvException("Fetch mode is not supported!");
...@@ -287,11 +292,11 @@ class SQLSrvStatement implements IteratorAggregate, Statement ...@@ -287,11 +292,11 @@ class SQLSrvStatement implements IteratorAggregate, Statement
{ {
$row = $this->fetch(PDO::FETCH_NUM); $row = $this->fetch(PDO::FETCH_NUM);
if ($row && isset($row[$columnIndex])) { if (false === $row) {
return $row[$columnIndex]; return false;
} }
return false; return isset($row[$columnIndex]) ? $row[$columnIndex] : null;
} }
/** /**
......
...@@ -767,6 +767,47 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -767,6 +767,47 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase
$this->assertEquals(array(), $rows); $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() private function setupFixture()
{ {
$this->_conn->executeQuery('DELETE FROM fetch_table')->execute(); $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