From 842267c11c43610ded3f4c1085720cad8b085bbe Mon Sep 17 00:00:00 2001 From: guilhermeblanco Date: Thu, 20 Aug 2009 02:59:42 +0000 Subject: [PATCH] [2.0] Refactored pieces of QueryBuilder, it does not use string concatenation anymore. Splitted from into from and join, allowing update and delete DQLs to be correctly built. Added missing entry set. Removed Expr\Having because it was useless. Updated docs. --- lib/Doctrine/ORM/Mapping/ClassMetadata.php | 2 +- .../ORM/Mapping/ClassMetadataFactory.php | 2 +- lib/Doctrine/ORM/Query/Expr.php | 17 +- lib/Doctrine/ORM/Query/Expr/Andx.php | 2 +- lib/Doctrine/ORM/Query/Expr/Base.php | 9 + lib/Doctrine/ORM/Query/Expr/Comparison.php | 7 + lib/Doctrine/ORM/Query/Expr/Having.php | 43 ----- lib/Doctrine/ORM/Query/Expr/Orx.php | 4 +- lib/Doctrine/ORM/QueryBuilder.php | 182 ++++++++++-------- lib/Doctrine/ORM/Tools/SchemaTool.php | 2 + tests/Doctrine/Tests/ORM/Query/ExprTest.php | 18 +- tests/Doctrine/Tests/ORM/QueryBuilderTest.php | 18 +- 12 files changed, 151 insertions(+), 155 deletions(-) diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadata.php b/lib/Doctrine/ORM/Mapping/ClassMetadata.php index 059abcd76..38102e475 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadata.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadata.php @@ -384,7 +384,7 @@ final class ClassMetadata * * @var array */ - //private $_tableGeneratorDefinition; + public $tableGeneratorDefinition; /** * The policy used for change-tracking on entities of this class. diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php index 93ec7fab1..cb17434b8 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php @@ -194,7 +194,7 @@ class ClassMetadataFactory $class->getTableGeneratorDefinition($parent->getTableGeneratorDefinition()); } $class->setIdGeneratorType($parent->generatorType); - $class->setidGenerator($parent->getIdGenerator()); + $class->setIdGenerator($parent->getIdGenerator()); } else { $this->_completeIdGeneratorMapping($class); } diff --git a/lib/Doctrine/ORM/Query/Expr.php b/lib/Doctrine/ORM/Query/Expr.php index ca94ecb51..08025e69a 100644 --- a/lib/Doctrine/ORM/Query/Expr.php +++ b/lib/Doctrine/ORM/Query/Expr.php @@ -73,39 +73,34 @@ class Expr return new Expr\GroupBy(func_get_args()); } - public static function having($having = null) - { - return new Expr\Having(func_get_args()); - } - public static function eq($x, $y) { - return new Expr\Comparison($x, '=', $y); + return new Expr\Comparison($x, Expr\Comparison::EQ, $y); } public static function neq($x, $y) { - return new Expr\Comparison($x, '<>', $y); + return new Expr\Comparison($x, Expr\Comparison::NEQ, $y); } public static function lt($x, $y) { - return new Expr\Comparison($x, '<', $y); + return new Expr\Comparison($x, Expr\Comparison::LT, $y); } public static function lte($x, $y) { - return new Expr\Comparison($x, '<=', $y); + return new Expr\Comparison($x, Expr\Comparison::LTE, $y); } public static function gt($x, $y) { - return new Expr\Comparison($x, '>', $y); + return new Expr\Comparison($x, Expr\Comparison::GT, $y); } public static function gte($x, $y) { - return new Expr\Comparison($x, '>=', $y); + return new Expr\Comparison($x, Expr\Comparison::GTE, $y); } public static function avg($x) diff --git a/lib/Doctrine/ORM/Query/Expr/Andx.php b/lib/Doctrine/ORM/Query/Expr/Andx.php index 08f8fbddb..7f8dda84b 100644 --- a/lib/Doctrine/ORM/Query/Expr/Andx.php +++ b/lib/Doctrine/ORM/Query/Expr/Andx.php @@ -36,7 +36,7 @@ class Andx extends Base protected $_separator = ') AND ('; protected $_allowedClasses = array( 'Doctrine\ORM\Query\Expr\Comparison', + 'Doctrine\ORM\Query\Expr\Func', 'Doctrine\ORM\Query\Expr\Orx', - 'Doctrine\ORM\Query\Expr\Func' ); } \ No newline at end of file diff --git a/lib/Doctrine/ORM/Query/Expr/Base.php b/lib/Doctrine/ORM/Query/Expr/Base.php index 7fd563ac0..e96ed6c18 100644 --- a/lib/Doctrine/ORM/Query/Expr/Base.php +++ b/lib/Doctrine/ORM/Query/Expr/Base.php @@ -41,6 +41,11 @@ abstract class Base private $_parts = array(); public function __construct($args = array()) + { + $this->addMultiple($args); + } + + public function addMultiple($args = array()) { foreach ((array) $args as $arg) { $this->add($arg); @@ -70,6 +75,10 @@ abstract class Base public function __toString() { + if ($this->count() == 1) { + return (string) $this->_parts[0]; + } + return $this->_preSeparator . implode($this->_separator, $this->_parts) . $this->_postSeparator; } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/Query/Expr/Comparison.php b/lib/Doctrine/ORM/Query/Expr/Comparison.php index 2bc75ae75..2a9b599bf 100644 --- a/lib/Doctrine/ORM/Query/Expr/Comparison.php +++ b/lib/Doctrine/ORM/Query/Expr/Comparison.php @@ -32,6 +32,13 @@ namespace Doctrine\ORM\Query\Expr; */ class Comparison { + const EQ = '='; + const NEQ = '<>'; + const LT = '<'; + const LTE = '<='; + const GT = '>'; + const GTE = '>='; + private $_leftExpr; private $_operator; private $_rightExpr; diff --git a/lib/Doctrine/ORM/Query/Expr/Having.php b/lib/Doctrine/ORM/Query/Expr/Having.php index 0eb0f727a..e69de29bb 100644 --- a/lib/Doctrine/ORM/Query/Expr/Having.php +++ b/lib/Doctrine/ORM/Query/Expr/Having.php @@ -1,43 +0,0 @@ -. - */ - -namespace Doctrine\ORM\Query\Expr; - -/** - * Expression class for building DQL Having parts - * - * @author Jonathan H. Wage - * @author Guilherme Blanco - * @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 diff --git a/lib/Doctrine/ORM/Query/Expr/Orx.php b/lib/Doctrine/ORM/Query/Expr/Orx.php index b4a8ef743..b1aad153c 100644 --- a/lib/Doctrine/ORM/Query/Expr/Orx.php +++ b/lib/Doctrine/ORM/Query/Expr/Orx.php @@ -35,8 +35,8 @@ class Orx extends Base { protected $_separator = ') OR ('; protected $_allowedClasses = array( - 'Doctrine\ORM\Query\Expr\Comparison', 'Doctrine\ORM\Query\Expr\Andx', - 'Doctrine\ORM\Query\Expr\Func' + 'Doctrine\ORM\Query\Expr\Comparison', + 'Doctrine\ORM\Query\Expr\Func', ); } \ No newline at end of file diff --git a/lib/Doctrine/ORM/QueryBuilder.php b/lib/Doctrine/ORM/QueryBuilder.php index b7520cc0e..ee874b475 100644 --- a/lib/Doctrine/ORM/QueryBuilder.php +++ b/lib/Doctrine/ORM/QueryBuilder.php @@ -55,11 +55,13 @@ class QueryBuilder * @var array $dqlParts The array of DQL parts collected. */ private $_dqlParts = array( - 'select' => array(), - 'from' => array(), - 'where' => array(), + 'select' => array(), + 'from' => null, + 'join' => array(), + 'set' => array(), + 'where' => null, 'groupBy' => array(), - 'having' => array(), + 'having' => null, 'orderBy' => array() ); @@ -201,10 +203,12 @@ class QueryBuilder */ public function add($dqlPartName, $dqlPart, $append = false) { - if ($append) { + $isMultiple = is_array($this->_dqlParts[$dqlPartName]); + + if ($append && $isMultiple) { $this->_dqlParts[$dqlPartName][] = $dqlPart; } else { - $this->_dqlParts[$dqlPartName] = array($dqlPart); + $this->_dqlParts[$dqlPartName] = ($isMultiple) ? array($dqlPart) : $dqlPart; } $this->_state = self::STATE_DIRTY; @@ -214,15 +218,14 @@ class QueryBuilder public function select($select = null) { - $selects = func_get_args(); $this->_type = self::SELECT; + $selects = func_get_args(); if (empty($selects)) { return $this; } - - $select = call_user_func_array(array('Doctrine\ORM\Query\Expr', 'select'), $selects); - return $this->add('select', $select, true); + + return $this->add('select', new Expr\Select($selects), true); } public function delete($delete = null, $alias = null) @@ -233,7 +236,7 @@ class QueryBuilder return $this; } - return $this->add('from', Expr::from($delete, $alias)); + return $this->add('from', new Expr\From($delete, $alias)); } public function update($update = null, $alias = null) @@ -244,89 +247,90 @@ class QueryBuilder return $this; } - return $this->add('from', Expr::from($update, $alias)); - } - - public function set($key, $value) - { - return $this->add('set', Expr::eq($key, $value), true); + return $this->add('from', new Expr\From($update, $alias)); } public function from($from, $alias = null) { - return $this->add('from', Expr::from($from, $alias), true); + return $this->add('from', new Expr\From($from, $alias)); } public function innerJoin($join, $alias = null, $conditionType = null, $condition = null) { - return $this->add('from', Expr::innerJoin($join, $alias, $conditionType, $condition), true); + return $this->add('join', new Expr\Join( + Expr\Join::INNER_JOIN, $join, $alias, $conditionType, $condition + ), true); } public function leftJoin($join, $alias = null, $conditionType = null, $condition = null) { - return $this->add('from', Expr::leftJoin($join, $alias, $conditionType, $condition), true); + return $this->add('join', new Expr\Join( + Expr\Join::LEFT_JOIN, $join, $alias, $conditionType, $condition + ), true); + } + + public function set($key, $value) + { + return $this->add('set', new Expr\Comparison($key, Expr\Comparison::EQ, $value), true); } public function where($where) { - $where = call_user_func_array(array('Doctrine\ORM\Query\Expr', 'andx'), func_get_args()); - return $this->add('where', $where, false); + if ( ! (func_num_args() == 1 && ($where instanceof Expr\Andx || $where instanceof Expr\Orx))) { + $where = new Expr\Andx(func_get_args()); + } + + return $this->add('where', $where); } public function andWhere($where) { - if (count($this->_getDqlQueryPart('where')) > 0) { - $this->add('where', 'AND', true); + $where = $this->_getDqlQueryPart('where'); + $args = func_get_args(); + + if ($where instanceof Expr\Andx) { + $where->addMultiple($args); + } else { + array_unshift($args, $where); + $where = new Expr\Andx($args); } - - $where = call_user_func_array(array('Doctrine\ORM\Query\Expr', 'andx'), func_get_args()); - return $this->add('where', $where, true); + + return $this->add('where', $where); } public function orWhere($where) { - if (count($this->_getDqlQueryPart('where')) > 0) { - $this->add('where', 'OR', true); + $where = $this->_getDqlQueryPart('where'); + $args = func_get_args(); + + if ($where instanceof Expr\Orx) { + $where->addMultiple($args); + } else { + array_unshift($args, $where); + $where = new Expr\Orx($args); } - - $where = call_user_func_array(array('Doctrine\ORM\Query\Expr', 'orx'), func_get_args()); - return $this->add('where', $where, true); + + return $this->add('where', $where); } public function andWhereIn($expr, $params) { - if (count($this->_getDqlQueryPart('where')) > 0) { - $this->add('where', 'AND', true); - } - - return $this->add('where', Expr::in($expr, $params), true); + return $this->andWhere(Expr::in($expr, $params)); } public function orWhereIn($expr, $params = array()) { - if (count($this->_getDqlQueryPart('where')) > 0) { - $this->add('where', 'OR', true); - } - - return $this->add('where', Expr::in($expr, $params), true); + return $this->orWhere(Expr::in($expr, $params)); } public function andWhereNotIn($expr, $params = array()) { - if (count($this->_getDqlQueryPart('where')) > 0) { - $this->add('where', 'AND', true); - } - - return $this->add('where', Expr::notIn($expr, $params), true); + return $this->andWhere(Expr::notIn($expr, $params)); } public function orWhereNotIn($expr, $params = array()) { - if (count($this->_getDqlQueryPart('where')) > 0) { - $this->add('where', 'OR', true); - } - - return $this->add('where', Expr::notIn($expr, $params), true); + return $this->orWhere(Expr::notIn($expr, $params)); } public function groupBy($groupBy) @@ -341,25 +345,41 @@ class QueryBuilder public function having($having) { - return $this->add('having', Expr::having($having), false); + if ( ! (func_num_args() == 1 && ($having instanceof Expr\Andx || $having instanceof Expr\Orx))) { + $having = new Expr\Andx(func_get_args()); + } + + return $this->add('having', $having); } public function andHaving($having) { - if (count($this->_getDqlQueryPart('having')) > 0) { - $this->add('having', 'AND', true); + $having = $this->_getDqlQueryPart('having'); + $args = func_get_args(); + + if ($having instanceof Expr\Andx) { + $having->addMultiple($args); + } else { + array_unshift($args, $having); + $having = new Expr\Andx($args); } - - return $this->add('having', Expr::having($having), true); + + return $this->add('having', $having); } public function orHaving($having) { - if (count($this->_getDqlQueryPart('having')) > 0) { - $this->add('having', 'OR', true); + $having = $this->_getDqlQueryPart('having'); + $args = func_get_args(); + + if ($having instanceof Expr\Orx) { + $having->addMultiple($args); + } else { + array_unshift($args, $having); + $having = new Expr\Orx($args); } - - return $this->add('having', Expr::having($having), true); + + return $this->add('having', $having); } public function orderBy($sort, $order = null) @@ -374,8 +394,7 @@ class QueryBuilder /** * Get the DQL query string for DELETE queries - * - * BNF: + * EBNF: * * DeleteStatement = DeleteClause [WhereClause] [OrderByClause] [LimitClause] [OffsetClause] * DeleteClause = "DELETE" "FROM" RangeVariableDeclaration @@ -389,15 +408,14 @@ class QueryBuilder private function _getDqlForDelete() { return 'DELETE' - . $this->_getReducedDqlQueryPart('from', array('pre' => ' ', 'separator' => ' ')) - . $this->_getReducedDqlQueryPart('where', array('pre' => ' WHERE ', 'separator' => ' ')) + . $this->_getReducedDqlQueryPart('from', array('pre' => ' ')) + . $this->_getReducedDqlQueryPart('where', array('pre' => ' WHERE ')) . $this->_getReducedDqlQueryPart('orderBy', array('pre' => ' ORDER BY ', 'separator' => ', ')); } /** * Get the DQL query string for UPDATE queries - * - * BNF: + * EBNF: * * UpdateStatement = UpdateClause [WhereClause] [OrderByClause] * UpdateClause = "UPDATE" RangeVariableDeclaration "SET" UpdateItem {"," UpdateItem} @@ -409,16 +427,15 @@ class QueryBuilder private function _getDqlForUpdate() { return 'UPDATE' - . $this->_getReducedDqlQueryPart('from', array('pre' => ' ', 'separator' => ' ')) + . $this->_getReducedDqlQueryPart('from', array('pre' => ' ')) . $this->_getReducedDqlQueryPart('set', array('pre' => ' SET ', 'separator' => ', ')) - . $this->_getReducedDqlQueryPart('where', array('pre' => ' WHERE ', 'separator' => ' ')) + . $this->_getReducedDqlQueryPart('where', array('pre' => ' WHERE ')) . $this->_getReducedDqlQueryPart('orderBy', array('pre' => ' ORDER BY ', 'separator' => ', ')); } /** * Get the DQL query string for SELECT queries - * - * BNF: + * EBNF: * * SelectStatement = [SelectClause] FromClause [WhereClause] [GroupByClause] [HavingClause] [OrderByClause] * SelectClause = "SELECT" ["ALL" | "DISTINCT"] SelectExpression {"," SelectExpression} @@ -432,26 +449,27 @@ class QueryBuilder */ private function _getDqlForSelect() { - return 'SELECT' + return 'SELECT' . $this->_getReducedDqlQueryPart('select', array('pre' => ' ', 'separator' => ', ')) - . $this->_getReducedDqlQueryPart('from', array('pre' => ' FROM ', 'separator' => ' ')) - . $this->_getReducedDqlQueryPart('where', array('pre' => ' WHERE ', 'separator' => ' ')) + . $this->_getReducedDqlQueryPart('from', array('pre' => ' FROM ')) + . $this->_getReducedDqlQueryPart('join', array('pre' => ' ', 'separator' => ' ')) + . $this->_getReducedDqlQueryPart('where', array('pre' => ' WHERE ')) . $this->_getReducedDqlQueryPart('groupBy', array('pre' => ' GROUP BY ', 'separator' => ', ')) - . $this->_getReducedDqlQueryPart('having', array('pre' => ' HAVING ', 'separator' => ' ')) + . $this->_getReducedDqlQueryPart('having', array('pre' => ' HAVING ')) . $this->_getReducedDqlQueryPart('orderBy', array('pre' => ' ORDER BY ', 'separator' => ', ')); } private function _getReducedDqlQueryPart($queryPartName, $options = array()) { - if (empty($this->_dqlParts[$queryPartName])) { + $queryPart = $this->_getDqlQueryPart($queryPartName); + + if (empty($queryPart)) { return (isset($options['empty']) ? $options['empty'] : ''); } - - $str = (isset($options['pre']) ? $options['pre'] : ''); - $str .= implode($options['separator'], $this->_getDqlQueryPart($queryPartName)); - $str .= (isset($options['post']) ? $options['post'] : ''); - - return $str; + + return (isset($options['pre']) ? $options['pre'] : '') + . (is_array($queryPart) ? implode($options['separator'], $queryPart) : $queryPart) + . (isset($options['post']) ? $options['post'] : ''); } private function _getDqlQueryPart($queryPartName) diff --git a/lib/Doctrine/ORM/Tools/SchemaTool.php b/lib/Doctrine/ORM/Tools/SchemaTool.php index 4dc17ff5c..d6554554a 100644 --- a/lib/Doctrine/ORM/Tools/SchemaTool.php +++ b/lib/Doctrine/ORM/Tools/SchemaTool.php @@ -150,6 +150,8 @@ class SchemaTool $class->getQuotedTableName($this->_platform), $columns, $options)); $processedClasses[$class->name] = true; + // TODO if we're reusing the sequence previously defined (in another model), + // it should not attempt to create a new sequence. if ($class->isIdGeneratorSequence() && $class->name == $class->rootEntityName) { $seqDef = $class->getSequenceGeneratorDefinition(); $sequences[] = $this->_platform->getCreateSequenceSql( diff --git a/tests/Doctrine/Tests/ORM/Query/ExprTest.php b/tests/Doctrine/Tests/ORM/Query/ExprTest.php index 41acb1cbe..b2f0f09ab 100644 --- a/tests/Doctrine/Tests/ORM/Query/ExprTest.php +++ b/tests/Doctrine/Tests/ORM/Query/ExprTest.php @@ -75,7 +75,7 @@ class ExprTest extends \Doctrine\Tests\OrmTestCase $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)); + $this->assertEquals('EXISTS(SELECT u FROM User u WHERE u.name = ?1)', (string) Expr::exists($qb)); } public function testAllExpr() @@ -83,7 +83,7 @@ class ExprTest extends \Doctrine\Tests\OrmTestCase $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)); + $this->assertEquals('ALL(SELECT u FROM User u WHERE u.name = ?1)', (string) Expr::all($qb)); } public function testSomeExpr() @@ -91,7 +91,7 @@ class ExprTest extends \Doctrine\Tests\OrmTestCase $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)); + $this->assertEquals('SOME(SELECT u FROM User u WHERE u.name = ?1)', (string) Expr::some($qb)); } public function testAnyExpr() @@ -99,7 +99,7 @@ class ExprTest extends \Doctrine\Tests\OrmTestCase $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)); + $this->assertEquals('ANY(SELECT u FROM User u WHERE u.name = ?1)', (string) Expr::any($qb)); } public function testNotExpr() @@ -107,13 +107,21 @@ class ExprTest extends \Doctrine\Tests\OrmTestCase $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)); + $this->assertEquals('NOT(SELECT u FROM User u WHERE u.name = ?1)', (string) Expr::not($qb)); } public function testAndExpr() { $this->assertEquals('(1 = 1) AND (2 = 2)', (string) Expr::andx((string) Expr::eq(1, 1), (string) Expr::eq(2, 2))); } + + public function testIntelligentParenthesisPreventionAndExpr() + { + $this->assertEquals( + '(1 = 1) AND (2 = 2)', + (string) Expr::andx(Expr::orx(Expr::andx(Expr::eq(1, 1))), (string) Expr::eq(2, 2)) + ); + } public function testOrExpr() { diff --git a/tests/Doctrine/Tests/ORM/QueryBuilderTest.php b/tests/Doctrine/Tests/ORM/QueryBuilderTest.php index 54cef6f86..d5e6e28e9 100644 --- a/tests/Doctrine/Tests/ORM/QueryBuilderTest.php +++ b/tests/Doctrine/Tests/ORM/QueryBuilderTest.php @@ -156,7 +156,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->where('u.id = :uid'); - $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE (u.id = :uid)'); + $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid'); } public function testAndWhere() @@ -189,7 +189,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase ->where('u.id = :uid') ->andWhereIn('u.id', array(1, 2, 3)); - $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE (u.id = :uid) AND u.id IN(1, 2, 3)'); + $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE (u.id = :uid) AND (u.id IN(1, 2, 3))'); } public function testOrWhereIn() @@ -200,7 +200,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase ->where('u.id = :uid') ->orWhereIn('u.id', array(1, 2, 3)); - $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE (u.id = :uid) OR u.id IN(1, 2, 3)'); + $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE (u.id = :uid) OR (u.id IN(1, 2, 3))'); } public function testAndWhereNotIn() @@ -211,7 +211,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase ->where('u.id = :uid') ->andWhereNotIn('u.id', array(1, 2, 3)); - $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE (u.id = :uid) AND u.id NOT IN(1, 2, 3)'); + $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE (u.id = :uid) AND (u.id NOT IN(1, 2, 3))'); } public function testOrWhereNotIn() @@ -222,7 +222,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase ->where('u.id = :uid') ->OrWhereNotIn('u.id', array(1, 2, 3)); - $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE (u.id = :uid) OR u.id NOT IN(1, 2, 3)'); + $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE (u.id = :uid) OR (u.id NOT IN(1, 2, 3))'); } public function testGroupBy() @@ -256,7 +256,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase ->having('COUNT(u.id) > 1') ->andHaving('COUNT(u.id) < 1'); - $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id HAVING COUNT(u.id) > 1 AND COUNT(u.id) < 1'); + $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id HAVING (COUNT(u.id) > 1) AND (COUNT(u.id) < 1)'); } public function testOrHaving() @@ -269,7 +269,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase ->andHaving('COUNT(u.id) < 1') ->orHaving('COUNT(u.id) > 1'); - $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id HAVING COUNT(u.id) > 1 AND COUNT(u.id) < 1 OR COUNT(u.id) > 1'); + $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id HAVING ((COUNT(u.id) > 1) AND (COUNT(u.id) < 1)) OR (COUNT(u.id) > 1)'); } public function testOrderBy() @@ -319,7 +319,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase $qb = $this->_em->createQueryBuilder() ->select('u') ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') - ->where('u.username = :username OR u.username = :username2'); + ->where(Expr::orx('u.username = :username', 'u.username = :username2')); $qb->setParameters(array('username' => 'jwage', 'username2' => 'jonwage')); @@ -390,7 +390,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') ->where($orExpr); - $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE ((u.id = :uid3) OR (u.id IN(1)))'); + $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE (u.id = :uid3) OR (u.id IN(1))'); } public function testGetEntityManager()