Commit e0d1cd21 authored by Nicolas Grekas's avatar Nicolas Grekas

add setFetchMode() at Connection level

parent d2655e48
...@@ -173,6 +173,8 @@ class Connection implements DriverConnection ...@@ -173,6 +173,8 @@ class Connection implements DriverConnection
*/ */
private $_isRollbackOnly = false; private $_isRollbackOnly = false;
private $_defaultFetchStyle = PDO::FETCH_ASSOC;
/** /**
* Initializes a new instance of the Connection class. * Initializes a new instance of the Connection class.
* *
...@@ -356,6 +358,16 @@ class Connection implements DriverConnection ...@@ -356,6 +358,16 @@ class Connection implements DriverConnection
return true; return true;
} }
/**
* setFetchMode
*
* @param integer $fetchStyle
*/
public function setFetchMode($fetchStyle)
{
$this->_defaultFetchStyle = $fetchStyle;
}
/** /**
* Prepares and executes an SQL query and returns the first row of the result * Prepares and executes an SQL query and returns the first row of the result
* as an associative array. * as an associative array.
...@@ -579,7 +591,7 @@ class Connection implements DriverConnection ...@@ -579,7 +591,7 @@ class Connection implements DriverConnection
*/ */
public function fetchAll($sql, array $params = array()) public function fetchAll($sql, array $params = array())
{ {
return $this->executeQuery($sql, $params)->fetchAll(PDO::FETCH_ASSOC); return $this->executeQuery($sql, $params)->fetchAll();
} }
/** /**
...@@ -592,7 +604,10 @@ class Connection implements DriverConnection ...@@ -592,7 +604,10 @@ class Connection implements DriverConnection
{ {
$this->connect(); $this->connect();
return new Statement($statement, $this); $stmt = new Statement($statement, $this);
$stmt->setFetchMode($this->_defaultFetchStyle);
return $stmt;
} }
/** /**
...@@ -635,6 +650,8 @@ class Connection implements DriverConnection ...@@ -635,6 +650,8 @@ class Connection implements DriverConnection
$stmt = $this->_conn->query($query); $stmt = $this->_conn->query($query);
} }
$stmt->setFetchMode($this->_defaultFetchStyle);
if ($logger) { if ($logger) {
$logger->stopQuery(); $logger->stopQuery();
} }
...@@ -664,12 +681,19 @@ class Connection implements DriverConnection ...@@ -664,12 +681,19 @@ class Connection implements DriverConnection
if ($data = $resultCache->fetch($cacheKey)) { if ($data = $resultCache->fetch($cacheKey)) {
// is the real key part of this row pointers map or is the cache only pointing to other cache keys? // is the real key part of this row pointers map or is the cache only pointing to other cache keys?
if (isset($data[$realKey])) { if (isset($data[$realKey])) {
return new ArrayStatement($data[$realKey]); $stmt = new ArrayStatement($data[$realKey]);
} else if (array_key_exists($realKey, $data)) { } else if (array_key_exists($realKey, $data)) {
return new ArrayStatement(array()); $stmt = new ArrayStatement(array());
} }
} }
return new ResultCacheStatement($this->executeQuery($query, $params, $types), $resultCache, $cacheKey, $realKey, $qcp->getLifetime());
if (!isset($stmt)) {
$stmt = new ResultCacheStatement($this->executeQuery($query, $params, $types), $resultCache, $cacheKey, $realKey, $qcp->getLifetime());
}
$stmt->setFetchMode($this->_defaultFetchStyle);
return $stmt;
} }
/** /**
...@@ -716,6 +740,7 @@ class Connection implements DriverConnection ...@@ -716,6 +740,7 @@ class Connection implements DriverConnection
} }
$statement = call_user_func_array(array($this->_conn, 'query'), $args); $statement = call_user_func_array(array($this->_conn, 'query'), $args);
$statement->setFetchMode($this->_defaultFetchStyle);
if ($logger) { if ($logger) {
$logger->stopQuery(); $logger->stopQuery();
......
...@@ -189,7 +189,7 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -189,7 +189,7 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase
public function testFetchBoth() public function testFetchBoth()
{ {
$sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?"; $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?";
$row = $this->_conn->executeQuery($sql, array(1, 'foo'))->fetch(); $row = $this->_conn->executeQuery($sql, array(1, 'foo'))->fetch(\PDO::FETCH_BOTH);
$this->assertTrue($row !== false); $this->assertTrue($row !== false);
......
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