Commit ba8b4337 authored by jwage's avatar jwage

[2.0] Cleanup on QueryBuilder and Expr from discussion with roman

parent f608dd8a
......@@ -262,11 +262,13 @@ class EntityManager
}
/**
* @todo Implementation.
* Create a QueryBuilder instance
*
* @return QueryBuilder $qb
*/
public function createQueryBuilder()
{
//...
return new QueryBuilder($this);
}
/**
......
......@@ -291,7 +291,6 @@ final class Query extends AbstractQuery
*/
public function setDql($dqlQuery)
{
$this->free();
if ($dqlQuery !== null) {
$this->_dql = $dqlQuery;
$this->_state = self::STATE_DIRTY;
......
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\ORM\Query\Expr;
/**
* Expression class for building DQL Having parts
*
* @author Jonathan H. Wage <jonwage@gmail.com>
* @author Guilherme Blanco <guilhermeblanco@gmail.com>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://www.phpdoctrine.org
* @since 2.0
* @version $Revision$
*/
class Having extends Base
{
protected $_preSeparator = '';
protected $_postSeparator = '';
protected $_allowedClasses = array(
'Doctrine\ORM\Query\Expr\Comparison',
'Doctrine\ORM\Query\Expr\Orx',
'Doctrine\ORM\Query\Expr\Func'
);
}
\ No newline at end of file
......@@ -79,28 +79,14 @@ class QueryBuilder
private $_dql;
/**
* @var array $params Parameters of this query.
* @var Query $q The Query instance used for this QueryBuilder
*/
private $_params = array();
/**
* @var integer The first result to return (the "offset").
*/
private $_firstResult = null;
/**
* @var integer The maximum number of results to return (the "limit").
*/
private $_maxResults = null;
private $_q;
public function __construct(EntityManager $entityManager)
{
$this->_em = $entityManager;
}
public static function create(EntityManager $entityManager)
{
return new self($entityManager);
$this->_q = $entityManager->createQuery();
}
public function getType()
......@@ -148,18 +134,9 @@ class QueryBuilder
public function getQuery()
{
$q = new Query($this->_em);
$q->setDql($this->getDql());
$q->setParameters($this->getParameters());
$q->setFirstResult($this->getFirstResult());
$q->setMaxResults($this->getMaxResults());
return $q;
}
$this->_q->setDql($this->getDql());
public function execute($params = array(), $hydrationMode = null)
{
return $this->getQuery()->execute($params, $hydrationMode);
return $this->_q;
}
/**
......@@ -170,7 +147,7 @@ class QueryBuilder
*/
public function setParameter($key, $value)
{
$this->_params[$key] = $value;
$this->_q->setParameter($key, $value);
return $this;
}
......@@ -182,9 +159,7 @@ class QueryBuilder
*/
public function setParameters(array $params)
{
foreach ($params as $key => $value) {
$this->setParameter($key, $value);
}
$this->_q->setParameters($params);
return $this;
}
......@@ -196,10 +171,7 @@ class QueryBuilder
*/
public function getParameters($params = array())
{
if ($params) {
return array_merge($this->_params, $params);
}
return $this->_params;
return $this->_q->getParameters($params);
}
/**
......@@ -210,7 +182,7 @@ class QueryBuilder
*/
public function getParameter($key)
{
return isset($this->_params[$key]) ? $this->_params[$key] : null;
return $this->_q->getParameter($key);
}
/**
......@@ -400,52 +372,6 @@ class QueryBuilder
return $this->add('orderBy', Expr::orderBy($sort, $order), true);
}
/**
* Sets the position of the first result to retrieve (the "offset").
*
* @param integer $firstResult The first result to return.
* @return Query This query object.
*/
public function setFirstResult($firstResult)
{
$this->_firstResult = $firstResult;
return $this;
}
/**
* Gets the position of the first result the query object was set to retrieve (the "offset").
* Returns NULL if {@link setFirstResult} was not applied to this query.
*
* @return integer The position of the first result.
*/
public function getFirstResult()
{
return $this->_firstResult;
}
/**
* Sets the maximum number of results to retrieve (the "limit").
*
* @param integer $maxResults
* @return Query This query object.
*/
public function setMaxResults($maxResults)
{
$this->_maxResults = $maxResults;
return $this;
}
/**
* Gets the maximum number of results the query object was set to retrieve (the "limit").
* Returns NULL if {@link setMaxResults} was not applied to this query.
*
* @return integer Maximum number of results.
*/
public function getMaxResults()
{
return $this->_maxResults;
}
/**
* Get the DQL query string for DELETE queries
*
......
......@@ -34,7 +34,6 @@ class AllTests
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\OneToOneSelfReferentialAssociationTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\OneToManySelfReferentialAssociationTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\ManyToManySelfReferentialAssociationTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\QueryBuilderTest');
return $suite;
}
......
<?php
namespace Doctrine\Tests\ORM\Functional;
use Doctrine\Tests\Models\CMS\CmsUser;
use Doctrine\Tests\Models\CMS\CmsArticle;
use Doctrine\ORM\QueryBuilder;
require_once __DIR__ . '/../../TestInit.php';
class QueryBuilderTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
protected function setUp()
{
$this->useModelSet('cms');
parent::setUp();
}
public function testExecute()
{
$qb = QueryBuilder::create($this->_em)
->select('u')
->from('Doctrine\Tests\Models\CMS\CmsUser', 'u');
$results = $qb->execute();
$this->assertEquals('Doctrine\Common\Collections\Collection', get_class($results));
}
public function testSetMaxResultsAndSetFirstResultZero()
{
$qb = QueryBuilder::create($this->_em)
->select('u')
->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
->setMaxResults(10)
->setFirstResult(0);
$this->assertEquals('SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ OFFSET 0 LIMIT 10', $qb->getQuery()->getSql());
}
public function testSetMaxResultsAndSetFirstResult()
{
$qb = QueryBuilder::create($this->_em)
->select('u')
->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
->setMaxResults(10)
->setFirstResult(10);
$this->assertEquals('SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ OFFSET 10 LIMIT 10', $qb->getQuery()->getSql());
}
public function testRemoveSetMaxResultsAndSetFirstResult()
{
$qb = QueryBuilder::create($this->_em)
->select('u')
->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
->setMaxResults(10)
->setFirstResult(0)
->setMaxResults(null)
->setFirstResult(null);
$this->assertEquals('SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_', $qb->getQuery()->getSql());
}
public function testOnlyFirstResult()
{
$qb = QueryBuilder::create($this->_em)
->select('u')
->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
->setMaxResults(10);
$this->assertEquals('SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ LIMIT 10', $qb->getQuery()->getSql());
}
}
\ 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