From cf77048ee490f9d27f51bf0ec4fec276cadd878a Mon Sep 17 00:00:00 2001 From: guilhermeblanco Date: Fri, 14 Aug 2009 22:50:36 +0000 Subject: [PATCH] [2.0] Fixed issue with functions accepting subqueries. Implemented missing Expr\Join class. Added Expr::*join helper functions. --- lib/Doctrine/ORM/Query/Expr.php | 15 +++++ lib/Doctrine/ORM/Query/Expr/Join.php | 62 +++++++++++++++++++ lib/Doctrine/ORM/QueryBuilder.php | 25 +++++--- tests/Doctrine/Tests/ORM/Query/ExprTest.php | 31 ++++++++-- tests/Doctrine/Tests/ORM/QueryBuilderTest.php | 17 ++++- 5 files changed, 134 insertions(+), 16 deletions(-) create mode 100644 lib/Doctrine/ORM/Query/Expr/Join.php diff --git a/lib/Doctrine/ORM/Query/Expr.php b/lib/Doctrine/ORM/Query/Expr.php index 645761b39..963963f61 100644 --- a/lib/Doctrine/ORM/Query/Expr.php +++ b/lib/Doctrine/ORM/Query/Expr.php @@ -52,6 +52,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) { diff --git a/lib/Doctrine/ORM/Query/Expr/Join.php b/lib/Doctrine/ORM/Query/Expr/Join.php new file mode 100644 index 000000000..b15cde48f --- /dev/null +++ b/lib/Doctrine/ORM/Query/Expr/Join.php @@ -0,0 +1,62 @@ +. + */ + +namespace Doctrine\ORM\Query\Expr; + +/** + * Expression class for DQL from + * + * @author Jonathan H. Wage + * @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 diff --git a/lib/Doctrine/ORM/QueryBuilder.php b/lib/Doctrine/ORM/QueryBuilder.php index efbdfafe9..a9c7018da 100644 --- a/lib/Doctrine/ORM/QueryBuilder.php +++ b/lib/Doctrine/ORM/QueryBuilder.php @@ -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) @@ -256,21 +256,23 @@ 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 diff --git a/tests/Doctrine/Tests/ORM/Query/ExprTest.php b/tests/Doctrine/Tests/ORM/Query/ExprTest.php index 6fc3b7ea6..41acb1cbe 100644 --- a/tests/Doctrine/Tests/ORM/Query/ExprTest.php +++ b/tests/Doctrine/Tests/ORM/Query/ExprTest.php @@ -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() @@ -251,6 +266,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() { diff --git a/tests/Doctrine/Tests/ORM/QueryBuilderTest.php b/tests/Doctrine/Tests/ORM/QueryBuilderTest.php index 671480c65..54cef6f86 100644 --- a/tests/Doctrine/Tests/ORM/QueryBuilderTest.php +++ b/tests/Doctrine/Tests/ORM/QueryBuilderTest.php @@ -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'); }