Commit f855a02d authored by Benjamin Eberlei's avatar Benjamin Eberlei

DDC-217 - Move factory method into Connection

parent 120172a7
...@@ -19,9 +19,11 @@ ...@@ -19,9 +19,11 @@
namespace Doctrine\DBAL\Cache; namespace Doctrine\DBAL\Cache;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Driver\ResultStatement; use Doctrine\DBAL\Driver\ResultStatement;
use PDO;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\Common\Cache\Cache;
use PDO;
/** /**
* Cache statement for SQL results. * Cache statement for SQL results.
...@@ -99,13 +101,13 @@ class ResultCacheStatement implements ResultStatement ...@@ -99,13 +101,13 @@ class ResultCacheStatement implements ResultStatement
} }
/** /**
*
* @param Statement $stmt * @param Statement $stmt
* @param Cache $resultCache * @param Cache $resultCache
* @param string $cacheKey * @param string $cacheKey
* @param string $realKey
* @param int $lifetime * @param int $lifetime
*/ */
private function __construct($stmt, $resultCache, $cacheKey, $realKey, $lifetime) public function __construct(Statement $stmt, Cache $resultCache, $cacheKey, $realKey, $lifetime)
{ {
$this->statement = $stmt; $this->statement = $stmt;
$this->resultCache = $resultCache; $this->resultCache = $resultCache;
......
...@@ -25,7 +25,8 @@ use PDO, Closure, Exception, ...@@ -25,7 +25,8 @@ use PDO, Closure, Exception,
Doctrine\Common\EventManager, Doctrine\Common\EventManager,
Doctrine\DBAL\DBALException, Doctrine\DBAL\DBALException,
Doctrine\DBAL\Cache\ResultCacheStatement, Doctrine\DBAL\Cache\ResultCacheStatement,
Doctrine\DBAL\Cache\QueryCacheProfile; Doctrine\DBAL\Cache\QueryCacheProfile,
Doctrine\DBAL\Cache\ArrayStatement;
/** /**
* A wrapper around a Doctrine\DBAL\Driver\Connection that adds features like * A wrapper around a Doctrine\DBAL\Driver\Connection that adds features like
...@@ -603,7 +604,7 @@ class Connection implements DriverConnection ...@@ -603,7 +604,7 @@ class Connection implements DriverConnection
public function executeQuery($query, array $params = array(), $types = array(), QueryCacheProfile $qcp = null) public function executeQuery($query, array $params = array(), $types = array(), QueryCacheProfile $qcp = null)
{ {
if ($qcp !== null) { if ($qcp !== null) {
return ResultCacheStatement::create($this, $query, $params, $types, $qcp); return $this->executeCacheQuery($query, $params, $types, $qcp);
} }
$this->connect(); $this->connect();
...@@ -634,6 +635,34 @@ class Connection implements DriverConnection ...@@ -634,6 +635,34 @@ class Connection implements DriverConnection
return $stmt; return $stmt;
} }
/**
* Execute a caching query and
*
* @param string $query
* @param array $params
* @param array $types
* @param QueryCacheProfile $qcp
* @return \Doctrine\DBAL\Driver\ResultStatement
*/
public function executeCacheQuery($query, $params, $types, QueryCacheProfile $qcp)
{
$resultCache = $qcp->getResultCacheDriver() ?: $this->_config->getResultCacheImpl();
if (!$resultCache) {
throw CacheException::noResultDriverConfigured();
}
list($cacheKey, $realKey) = $qcp->generateCacheKeys($query, $params, $types);
// fetch the row pointers entry
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?
if (isset($data[$realKey])) {
return new ArrayStatement($data[$realKey]);
}
}
return new ResultCacheStatement($this->executeQuery($query, $params, $types), $resultCache, $cacheKey, $realKey, $qcp->getLifetime());
}
/** /**
* Executes an, optionally parameterized, SQL query and returns the result, * Executes an, optionally parameterized, SQL query and returns the result,
* applying a given projection/transformation function on each row of the result. * applying a given projection/transformation function on each row of the result.
......
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