From 6e64cc101adcd46c4668f6c2927ac648238ab260 Mon Sep 17 00:00:00 2001 From: fabios Date: Mon, 3 Mar 2014 15:28:35 -0500 Subject: [PATCH] [SLC] Add query builder options --- lib/Doctrine/ORM/QueryBuilder.php | 132 +++++++++++++++++- tests/Doctrine/Tests/ORM/QueryBuilderTest.php | 52 ++++++- 2 files changed, 176 insertions(+), 8 deletions(-) diff --git a/lib/Doctrine/ORM/QueryBuilder.php b/lib/Doctrine/ORM/QueryBuilder.php index f8363fcea..ec908bb45 100644 --- a/lib/Doctrine/ORM/QueryBuilder.php +++ b/lib/Doctrine/ORM/QueryBuilder.php @@ -118,6 +118,32 @@ class QueryBuilder */ private $joinRootAliases = array(); + /** + * Whether to use second level cache, if available. + * + * @var boolean + */ + protected $cacheable = false; + + /** + * Second level cache region name. + * + * @var string|null + */ + protected $cacheRegion; + + /** + * Second level query cache mode. + * + * @var integer|null + */ + protected $cacheMode; + + /** + * @var integer + */ + protected $lifetime = 0; + /** * Initializes a new QueryBuilder that uses the given EntityManager. * @@ -151,6 +177,91 @@ class QueryBuilder return $this->_em->getExpressionBuilder(); } + /** + * + * Enable/disable second level query (result) caching for this query. + * + * @param boolean $cacheable + * + * @return \Doctrine\ORM\AbstractQuery This query instance. + */ + public function setCacheable($cacheable) + { + $this->cacheable = (boolean) $cacheable; + + return $this; + } + + /** + * @return boolean TRUE if the query results are enable for second level cache, FALSE otherwise. + */ + public function isCacheable() + { + return $this->cacheable; + } + + /** + * @param string $cacheRegion + * + * @return \Doctrine\ORM\AbstractQuery This query instance. + */ + public function setCacheRegion($cacheRegion) + { + $this->cacheRegion = (string) $cacheRegion; + + return $this; + } + + /** + * Obtain the name of the second level query cache region in which query results will be stored + * + * @return The cache region name; NULL indicates the default region. + */ + public function getCacheRegion() + { + return $this->cacheRegion; + } + + /** + * @return integer + */ + public function getLifetime() + { + return $this->lifetime; + } + + /** + * Sets the life-time for this query into second level cache. + * + * @param integer $lifetime + * @return \Doctrine\ORM\AbstractQuery This query instance. + */ + public function setLifetime($lifetime) + { + $this->lifetime = (integer) $lifetime; + + return $this; + } + + /** + * @return integer + */ + public function getCacheMode() + { + return $this->cacheMode; + } + + /** + * @param integer $cacheMode + * @return \Doctrine\ORM\AbstractQuery This query instance. + */ + public function setCacheMode($cacheMode) + { + $this->cacheMode = (integer) $cacheMode; + + return $this; + } + /** * Gets the type of the currently built query. * @@ -236,11 +347,28 @@ class QueryBuilder public function getQuery() { $parameters = clone $this->parameters; - - return $this->_em->createQuery($this->getDQL()) + $query = $this->_em->createQuery($this->getDQL()) ->setParameters($parameters) ->setFirstResult($this->_firstResult) ->setMaxResults($this->_maxResults); + + if ($this->lifetime) { + $query->setLifetime($this->lifetime); + } + + if ($this->cacheMode) { + $query->setCacheMode($this->cacheMode); + } + + if ($this->cacheable) { + $query->setCacheable($this->cacheable); + } + + if ($this->cacheRegion) { + $query->setCacheRegion($this->cacheRegion); + } + + return $query; } /** diff --git a/tests/Doctrine/Tests/ORM/QueryBuilderTest.php b/tests/Doctrine/Tests/ORM/QueryBuilderTest.php index 84d0c4172..0c38e4f6b 100644 --- a/tests/Doctrine/Tests/ORM/QueryBuilderTest.php +++ b/tests/Doctrine/Tests/ORM/QueryBuilderTest.php @@ -19,13 +19,14 @@ namespace Doctrine\Tests\ORM; -use Doctrine\Common\Collections\ArrayCollection, - Doctrine\Common\Collections\Criteria; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Criteria; +use Doctrine\ORM\Cache; -use Doctrine\ORM\QueryBuilder, - Doctrine\ORM\Query\Expr, - Doctrine\ORM\Query\Parameter, - Doctrine\ORM\Query\ParameterTypeInferer; +use Doctrine\ORM\QueryBuilder; +use Doctrine\ORM\Query\Expr; +use Doctrine\ORM\Query\Parameter; +use Doctrine\ORM\Query\ParameterTypeInferer; require_once __DIR__ . '/../TestInit.php'; @@ -978,4 +979,43 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase ->add('where', 'u.bar = ?2', true) ; } + + public function testSecondLevelCacheQueryBuilderOptions() + { + $defaultQueryBuilder = $this->_em->createQueryBuilder() + ->select('s') + ->from('Doctrine\Tests\Models\Cache\State', 's'); + + $this->assertFalse($defaultQueryBuilder->isCacheable()); + $this->assertEquals(0, $defaultQueryBuilder->getLifetime()); + $this->assertNull($defaultQueryBuilder->getCacheRegion()); + $this->assertNull($defaultQueryBuilder->getCacheMode()); + + $defaultQuery = $defaultQueryBuilder->getQuery(); + + $this->assertFalse($defaultQuery->isCacheable()); + $this->assertEquals(0, $defaultQuery->getLifetime()); + $this->assertNull($defaultQuery->getCacheRegion()); + $this->assertNull($defaultQuery->getCacheMode()); + + $builder = $this->_em->createQueryBuilder() + ->select('s') + ->setLifetime(123) + ->setCacheable(true) + ->setCacheRegion('foo_reg') + ->setCacheMode(Cache::MODE_REFRESH) + ->from('Doctrine\Tests\Models\Cache\State', 's'); + + $this->assertTrue($builder->isCacheable()); + $this->assertEquals(123, $builder->getLifetime()); + $this->assertEquals('foo_reg', $builder->getCacheRegion()); + $this->assertEquals(Cache::MODE_REFRESH, $builder->getCacheMode()); + + $query = $builder->getQuery(); + + $this->assertTrue($query->isCacheable()); + $this->assertEquals(123, $query->getLifetime()); + $this->assertEquals('foo_reg', $query->getCacheRegion()); + $this->assertEquals(Cache::MODE_REFRESH, $query->getCacheMode()); + } }