Commit dea79e7b authored by Benjamin Eberlei's avatar Benjamin Eberlei

DDC-217 - More tests and fixes for some edge cases

parent 3932a5a7
......@@ -73,6 +73,11 @@ class ResultCacheStatement implements ResultStatement
*/
private $emptied = false;
/**
* @var array
*/
private $data;
/**
* @param Statement $stmt
* @param Cache $resultCache
......@@ -97,7 +102,7 @@ class ResultCacheStatement implements ResultStatement
public function closeCursor()
{
$this->statement->closeCursor();
if ($this->emptied && $this->data) {
if ($this->emptied && $this->data !== null) {
$data = $this->resultCache->fetch($this->cacheKey);
if (!$data) {
$data = array();
......@@ -151,6 +156,10 @@ class ResultCacheStatement implements ResultStatement
*/
public function fetch($fetchStyle = PDO::FETCH_BOTH)
{
if ($this->data === null) {
$this->data = array();
}
$row = $this->statement->fetch(PDO::FETCH_ASSOC);
if ($row) {
$this->data[] = $row;
......
......@@ -659,6 +659,8 @@ class Connection implements DriverConnection
// is the real key part of this row pointers map or is the cache only pointing to other cache keys?
if (isset($data[$realKey])) {
return new ArrayStatement($data[$realKey]);
} else if (array_key_exists($realKey, $data)) {
return new ArrayStatement(array());
}
}
return new ResultCacheStatement($this->executeQuery($query, $params, $types), $resultCache, $cacheKey, $realKey, $qcp->getLifetime());
......
......@@ -73,21 +73,13 @@ class ResultCacheTest extends \Doctrine\Tests\DbalFunctionalTestCase
}
$stmt = $this->_conn->executeQuery("SELECT * FROM caching", array(), array(), new QueryCacheProfile(10, "testcachekey"));
$data = array();
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
$data[] = $row;
}
$stmt->closeCursor();
$data = $this->hydrateStmt($stmt, \PDO::FETCH_ASSOC);
$this->assertEquals($this->expectedResult, $data);
$stmt = $this->_conn->executeQuery("SELECT * FROM caching", array(), array(), new QueryCacheProfile(10, "testcachekey"));
$data = array();
while ($row = $stmt->fetch(\PDO::FETCH_NUM)) {
$data[] = $row;
}
$stmt->closeCursor();
$data = $this->hydrateStmt($stmt, \PDO::FETCH_NUM);
$this->assertEquals($numExpectedResult, $data);
}
......@@ -120,45 +112,58 @@ class ResultCacheTest extends \Doctrine\Tests\DbalFunctionalTestCase
$stmt = $this->_conn->executeQuery("SELECT * FROM caching", array(), array(), new QueryCacheProfile(10, "testcachekey"));
$data = array();
while ($row = $stmt->fetch(\PDO::FETCH_NUM)) {
$data[] = $row;
}
$stmt->closeCursor();
$data = $this->hydrateStmt($stmt, \PDO::FETCH_NUM);
$this->assertEquals(2, count($this->sqlLogger->queries));
}
public function assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, $fetchStyle)
{
$s = microtime(true);
$stmt = $this->_conn->executeQuery("SELECT * FROM caching", array(), array(), new QueryCacheProfile(10, "testcachekey"));
$this->assertEquals(2, $stmt->columnCount());
$data = array();
while ($row = $stmt->fetch($fetchStyle)) {
$data[] = $row;
}
$stmt->closeCursor();
#echo number_format(microtime(true)-$s, 6)."\n";
$data = $this->hydrateStmt($stmt, $fetchStyle);
$this->assertEquals($expectedResult, $data);
$s = microtime(true);
$stmt = $this->_conn->executeQuery("SELECT * FROM caching", array(), array(), new QueryCacheProfile(10, "testcachekey"));
$this->assertEquals(2, $stmt->columnCount());
$data = $this->hydrateStmt($stmt, $fetchStyle);
$this->assertEquals($expectedResult, $data);
$this->assertEquals(1, count($this->sqlLogger->queries), "just one dbal hit");
}
public function testEmptyResultCache()
{
$stmt = $this->_conn->executeQuery("SELECT * FROM caching WHERE test_int > 500", array(), array(), new QueryCacheProfile(10, "emptycachekey"));
$data = $this->hydrateStmt($stmt);
$stmt = $this->_conn->executeQuery("SELECT * FROM caching WHERE test_int > 500", array(), array(), new QueryCacheProfile(10, "emptycachekey"));
$data = $this->hydrateStmt($stmt);
$this->assertEquals(1, count($this->sqlLogger->queries), "just one dbal hit");
}
public function testChangeCacheImpl()
{
$stmt = $this->_conn->executeQuery("SELECT * FROM caching WHERE test_int > 500", array(), array(), new QueryCacheProfile(10, "emptycachekey"));
$data = $this->hydrateStmt($stmt);
$secondCache = new \Doctrine\Common\Cache\ArrayCache;
$stmt = $this->_conn->executeQuery("SELECT * FROM caching WHERE test_int > 500", array(), array(), new QueryCacheProfile(10, "emptycachekey", $secondCache));
$data = $this->hydrateStmt($stmt);
$this->assertEquals(2, count($this->sqlLogger->queries), "two hits");
$this->assertEquals(1, count($secondCache->fetch("emptycachekey")));
}
private function hydrateStmt($stmt, $fetchStyle = \PDO::FETCH_ASSOC)
{
$data = array();
while ($row = $stmt->fetch($fetchStyle)) {
$data[] = $row;
}
$stmt->closeCursor();
#echo number_format(microtime(true)-$s, 6)."\n";
$this->assertEquals($expectedResult, $data);
$this->assertEquals(1, count($this->sqlLogger->queries), "just one dbal hit");
return $data;
}
}
\ No newline at end of file
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