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
......@@ -53,6 +53,21 @@ class Expr
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)
{
return new Expr\OrderBy($sort, $order);
......
<?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
return $this;
}
return $this->add('from', $delete . ' ' . $alias);
return $this->add('from', Expr::from($delete, $alias));
}
public function update($update = null, $alias = null)
......@@ -244,7 +244,7 @@ class QueryBuilder
return $this;
}
return $this->add('from', $update . ' ' . $alias);
return $this->add('from', Expr::from($update, $alias));
}
public function set($key, $value)
......@@ -257,20 +257,22 @@ class QueryBuilder
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);
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);
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)
......@@ -464,4 +466,9 @@ class QueryBuilder
{
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
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()
{
$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()
{
$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()
{
$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()
{
$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()
......@@ -252,6 +267,12 @@ class ExprTest extends \Doctrine\Tests\OrmTestCase
$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()
{
$selectExpr = Expr::select();
......
......@@ -121,17 +121,30 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase
$qb = $this->_em->createQueryBuilder()
->select('u', 'a')
->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');
}
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()
{
$qb = $this->_em->createQueryBuilder()
->select('u', 'a')
->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');
}
......
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