Commit cf77048e authored by guilhermeblanco's avatar guilhermeblanco

[2.0] Fixed issue with functions accepting subqueries. Implemented missing...

[2.0] Fixed issue with functions accepting subqueries. Implemented missing Expr\Join class. Added Expr::*join helper functions.
parent b0202f29
...@@ -52,6 +52,21 @@ class Expr ...@@ -52,6 +52,21 @@ class Expr
{ {
return new Expr\From($from, $alias); return new Expr\From($from, $alias);
} }
public static function join($joinType, $join, $alias = null, $conditionType = null, $condition = null)
{
return new Expr\Join($joinType, $join, $alias, $conditionType, $condition);
}
public static function leftJoin($join, $alias = null, $conditionType = null, $condition = null)
{
return new Expr\Join(Expr\Join::LEFT_JOIN, $join, $alias, $conditionType, $condition);
}
public static function innerJoin($join, $alias = null, $conditionType = null, $condition = null)
{
return new Expr\Join(Expr\Join::INNER_JOIN, $join, $alias, $conditionType, $condition);
}
public static function orderBy($sort = null, $order = null) public static function orderBy($sort = null, $order = null)
{ {
......
<?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 DQL from
*
* @author Jonathan H. Wage <jonwage@gmail.com>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://www.phpdoctrine.org
* @since 2.0
* @version $Revision$
*/
class Join
{
const INNER_JOIN = 'INNER';
const LEFT_JOIN = 'LEFT';
const ON = 'ON';
const WITH = 'WITH';
private $_joinType;
private $_join;
private $_alias;
private $_conditionType;
private $_condition;
public function __construct($joinType, $join, $alias = null, $conditionType = null, $condition = null)
{
$this->_joinType = $joinType;
$this->_join = $join;
$this->_alias = $alias;
$this->_conditionType = $conditionType;
$this->_condition = $condition;
}
public function __toString()
{
return strtoupper($this->_joinType) . ' JOIN ' . $this->_join
. ($this->_alias ? ' ' . $this->_alias : '')
. ($this->_condition ? ' ' . strtoupper($this->_conditionType) . ' ' . $this->_condition : '');
}
}
\ No newline at end of file
...@@ -233,7 +233,7 @@ class QueryBuilder ...@@ -233,7 +233,7 @@ class QueryBuilder
return $this; return $this;
} }
return $this->add('from', $delete . ' ' . $alias); return $this->add('from', Expr::from($delete, $alias));
} }
public function update($update = null, $alias = null) public function update($update = null, $alias = null)
...@@ -244,7 +244,7 @@ class QueryBuilder ...@@ -244,7 +244,7 @@ class QueryBuilder
return $this; return $this;
} }
return $this->add('from', $update . ' ' . $alias); return $this->add('from', Expr::from($update, $alias));
} }
public function set($key, $value) public function set($key, $value)
...@@ -256,21 +256,23 @@ class QueryBuilder ...@@ -256,21 +256,23 @@ class QueryBuilder
{ {
return $this->add('from', Expr::from($from, $alias), true); return $this->add('from', Expr::from($from, $alias), true);
} }
public function innerJoin($parentAlias, $join, $alias, $condition = null) public function innerJoin($join, $alias = null, $conditionType = null, $condition = null)
{ {
$join = 'INNER JOIN ' . $parentAlias . '.' . $join . ' ' /*$join = 'INNER JOIN ' . $parentAlias . '.' . $join . ' '
. $alias . (isset($condition) ? ' ' . $condition : null); . $alias . (isset($condition) ? ' ' . $condition : null);
return $this->add('from', $join, true); return $this->add('from', $join, true);*/
return $this->add('from', Expr::innerJoin($join, $alias, $conditionType, $condition), true);
} }
public function leftJoin($parentAlias, $join, $alias, $condition = null) public function leftJoin($join, $alias = null, $conditionType = null, $condition = null)
{ {
$join = 'LEFT JOIN ' . $parentAlias . '.' . $join . ' ' /*$join = 'LEFT JOIN ' . $parentAlias . '.' . $join . ' '
. $alias . (isset($condition) ? ' ' . $condition : null); . $alias . (isset($condition) ? ' ' . $condition : null);
return $this->add('from', $join, true); return $this->add('from', $join, true);*/
return $this->add('from', Expr::leftJoin($join, $alias, $conditionType, $condition), true);
} }
public function where($where) public function where($where)
...@@ -464,4 +466,9 @@ class QueryBuilder ...@@ -464,4 +466,9 @@ class QueryBuilder
{ {
return $this->_dqlParts[$queryPartName]; return $this->_dqlParts[$queryPartName];
} }
public function __toString()
{
return $this->getDql();
}
} }
\ No newline at end of file
...@@ -72,27 +72,42 @@ class ExprTest extends \Doctrine\Tests\OrmTestCase ...@@ -72,27 +72,42 @@ class ExprTest extends \Doctrine\Tests\OrmTestCase
public function testExistsExpr() public function testExistsExpr()
{ {
$this->assertEquals('EXISTS(SUBQUERY)', (string) Expr::exists('SUBQUERY')); $qb = $this->_em->createQueryBuilder();
$qb->select('u')->from('User', 'u')->where('u.name = ?1');
$this->assertEquals('EXISTS(SELECT u FROM User u WHERE (u.name = ?1))', (string) Expr::exists($qb));
} }
public function testAllExpr() public function testAllExpr()
{ {
$this->assertEquals('ALL(SUBQUERY)', (string) Expr::all('SUBQUERY')); $qb = $this->_em->createQueryBuilder();
$qb->select('u')->from('User', 'u')->where('u.name = ?1');
$this->assertEquals('ALL(SELECT u FROM User u WHERE (u.name = ?1))', (string) Expr::all($qb));
} }
public function testSomeExpr() public function testSomeExpr()
{ {
$this->assertEquals('SOME(SUBQUERY)', (string) Expr::some('SUBQUERY')); $qb = $this->_em->createQueryBuilder();
$qb->select('u')->from('User', 'u')->where('u.name = ?1');
$this->assertEquals('SOME(SELECT u FROM User u WHERE (u.name = ?1))', (string) Expr::some($qb));
} }
public function testAnyExpr() public function testAnyExpr()
{ {
$this->assertEquals('ANY(SUBQUERY)', (string) Expr::any('SUBQUERY')); $qb = $this->_em->createQueryBuilder();
$qb->select('u')->from('User', 'u')->where('u.name = ?1');
$this->assertEquals('ANY(SELECT u FROM User u WHERE (u.name = ?1))', (string) Expr::any($qb));
} }
public function testNotExpr() public function testNotExpr()
{ {
$this->assertEquals('NOT(SUBQUERY)', (string) Expr::not('SUBQUERY')); $qb = $this->_em->createQueryBuilder();
$qb->select('u')->from('User', 'u')->where('u.name = ?1');
$this->assertEquals('NOT(SELECT u FROM User u WHERE (u.name = ?1))', (string) Expr::not($qb));
} }
public function testAndExpr() public function testAndExpr()
...@@ -251,6 +266,12 @@ class ExprTest extends \Doctrine\Tests\OrmTestCase ...@@ -251,6 +266,12 @@ class ExprTest extends \Doctrine\Tests\OrmTestCase
$this->assertEquals('u.id, u.username', (string) $selectExpr); $this->assertEquals('u.id, u.username', (string) $selectExpr);
} }
public function testFromExpr()
{
$this->assertEquals('User', (string) Expr::from('User'));
$this->assertEquals('User u', (string) Expr::from('User', 'u'));
}
public function testExprBaseCount() public function testExprBaseCount()
{ {
......
...@@ -121,17 +121,30 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase ...@@ -121,17 +121,30 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase
$qb = $this->_em->createQueryBuilder() $qb = $this->_em->createQueryBuilder()
->select('u', 'a') ->select('u', 'a')
->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
->innerJoin('u', 'articles', 'a'); ->innerJoin('u.articles', 'a');
$this->assertValidQueryBuilder($qb, 'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.articles a'); $this->assertValidQueryBuilder($qb, 'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.articles a');
} }
public function testComplexInnerJoin()
{
$qb = $this->_em->createQueryBuilder()
->select('u', 'a')
->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
->innerJoin('u.articles', 'a', 'ON', 'u.id = a.author_id');
$this->assertValidQueryBuilder(
$qb,
'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.articles a ON u.id = a.author_id'
);
}
public function testLeftJoin() public function testLeftJoin()
{ {
$qb = $this->_em->createQueryBuilder() $qb = $this->_em->createQueryBuilder()
->select('u', 'a') ->select('u', 'a')
->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
->leftJoin('u', 'articles', 'a'); ->leftJoin('u.articles', 'a');
$this->assertValidQueryBuilder($qb, 'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a'); $this->assertValidQueryBuilder($qb, 'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a');
} }
......
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