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 ...@@ -73,6 +73,11 @@ class ResultCacheStatement implements ResultStatement
*/ */
private $emptied = false; private $emptied = false;
/**
* @var array
*/
private $data;
/** /**
* @param Statement $stmt * @param Statement $stmt
* @param Cache $resultCache * @param Cache $resultCache
...@@ -97,7 +102,7 @@ class ResultCacheStatement implements ResultStatement ...@@ -97,7 +102,7 @@ class ResultCacheStatement implements ResultStatement
public function closeCursor() public function closeCursor()
{ {
$this->statement->closeCursor(); $this->statement->closeCursor();
if ($this->emptied && $this->data) { if ($this->emptied && $this->data !== null) {
$data = $this->resultCache->fetch($this->cacheKey); $data = $this->resultCache->fetch($this->cacheKey);
if (!$data) { if (!$data) {
$data = array(); $data = array();
...@@ -151,6 +156,10 @@ class ResultCacheStatement implements ResultStatement ...@@ -151,6 +156,10 @@ class ResultCacheStatement implements ResultStatement
*/ */
public function fetch($fetchStyle = PDO::FETCH_BOTH) public function fetch($fetchStyle = PDO::FETCH_BOTH)
{ {
if ($this->data === null) {
$this->data = array();
}
$row = $this->statement->fetch(PDO::FETCH_ASSOC); $row = $this->statement->fetch(PDO::FETCH_ASSOC);
if ($row) { if ($row) {
$this->data[] = $row; $this->data[] = $row;
......
...@@ -659,6 +659,8 @@ class Connection implements DriverConnection ...@@ -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? // 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]); 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()); return new ResultCacheStatement($this->executeQuery($query, $params, $types), $resultCache, $cacheKey, $realKey, $qcp->getLifetime());
......
...@@ -73,21 +73,13 @@ class ResultCacheTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -73,21 +73,13 @@ class ResultCacheTest extends \Doctrine\Tests\DbalFunctionalTestCase
} }
$stmt = $this->_conn->executeQuery("SELECT * FROM caching", array(), array(), new QueryCacheProfile(10, "testcachekey")); $stmt = $this->_conn->executeQuery("SELECT * FROM caching", array(), array(), new QueryCacheProfile(10, "testcachekey"));
$data = array(); $data = $this->hydrateStmt($stmt, \PDO::FETCH_ASSOC);
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
$data[] = $row;
}
$stmt->closeCursor();
$this->assertEquals($this->expectedResult, $data); $this->assertEquals($this->expectedResult, $data);
$stmt = $this->_conn->executeQuery("SELECT * FROM caching", array(), array(), new QueryCacheProfile(10, "testcachekey")); $stmt = $this->_conn->executeQuery("SELECT * FROM caching", array(), array(), new QueryCacheProfile(10, "testcachekey"));
$data = array(); $data = $this->hydrateStmt($stmt, \PDO::FETCH_NUM);
while ($row = $stmt->fetch(\PDO::FETCH_NUM)) {
$data[] = $row;
}
$stmt->closeCursor();
$this->assertEquals($numExpectedResult, $data); $this->assertEquals($numExpectedResult, $data);
} }
...@@ -120,45 +112,58 @@ class ResultCacheTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -120,45 +112,58 @@ class ResultCacheTest extends \Doctrine\Tests\DbalFunctionalTestCase
$stmt = $this->_conn->executeQuery("SELECT * FROM caching", array(), array(), new QueryCacheProfile(10, "testcachekey")); $stmt = $this->_conn->executeQuery("SELECT * FROM caching", array(), array(), new QueryCacheProfile(10, "testcachekey"));
$data = array(); $data = $this->hydrateStmt($stmt, \PDO::FETCH_NUM);
while ($row = $stmt->fetch(\PDO::FETCH_NUM)) {
$data[] = $row;
}
$stmt->closeCursor();
$this->assertEquals(2, count($this->sqlLogger->queries)); $this->assertEquals(2, count($this->sqlLogger->queries));
} }
public function assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, $fetchStyle) public function assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, $fetchStyle)
{ {
$s = microtime(true);
$stmt = $this->_conn->executeQuery("SELECT * FROM caching", array(), array(), new QueryCacheProfile(10, "testcachekey")); $stmt = $this->_conn->executeQuery("SELECT * FROM caching", array(), array(), new QueryCacheProfile(10, "testcachekey"));
$this->assertEquals(2, $stmt->columnCount()); $this->assertEquals(2, $stmt->columnCount());
$data = $this->hydrateStmt($stmt, $fetchStyle);
$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($expectedResult, $data);
$s = microtime(true);
$stmt = $this->_conn->executeQuery("SELECT * FROM caching", array(), array(), new QueryCacheProfile(10, "testcachekey")); $stmt = $this->_conn->executeQuery("SELECT * FROM caching", array(), array(), new QueryCacheProfile(10, "testcachekey"));
$this->assertEquals(2, $stmt->columnCount()); $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(); $data = array();
while ($row = $stmt->fetch($fetchStyle)) { while ($row = $stmt->fetch($fetchStyle)) {
$data[] = $row; $data[] = $row;
} }
$stmt->closeCursor(); $stmt->closeCursor();
#echo number_format(microtime(true)-$s, 6)."\n"; return $data;
$this->assertEquals($expectedResult, $data);
$this->assertEquals(1, count($this->sqlLogger->queries), "just one dbal hit");
} }
} }
\ 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