Skip to content

Commit

Permalink
Merge pull request #345 from hason/DDC-1802
Browse files Browse the repository at this point in the history
Fixed DDC-1802
  • Loading branch information
guilhermeblanco committed May 4, 2012
2 parents f566b79 + 3d85239 commit 6103db0
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Query/AST/Functions/ModFunction.php
Expand Up @@ -53,7 +53,7 @@ public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
*/
public function parse(\Doctrine\ORM\Query\Parser $parser)
{
$parser->match(Lexer::T_MOD);
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);

$this->firstSimpleArithmeticExpression = $parser->SimpleArithmeticExpression();
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Query/AST/Functions/SizeFunction.php
Expand Up @@ -109,7 +109,7 @@ public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
*/
public function parse(\Doctrine\ORM\Query\Parser $parser)
{
$parser->match(Lexer::T_SIZE);
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);

$this->collectionPathExpression = $parser->CollectionValuedPathExpression();
Expand Down
24 changes: 11 additions & 13 deletions lib/Doctrine/ORM/Query/Lexer.php
Expand Up @@ -98,18 +98,16 @@ class Lexer extends \Doctrine\Common\Lexer
const T_OUTER = 144;
const T_SELECT = 145;
const T_SET = 146;
const T_SIZE = 147;
const T_SOME = 148;
const T_SUM = 149;
const T_THEN = 150;
const T_TRAILING = 151;
const T_TRUE = 152;
const T_UPDATE = 153;
const T_WHEN = 154;
const T_WHERE = 155;
const T_WITH = 156;
const T_PARTIAL = 157;
const T_MOD = 158;
const T_SOME = 147;
const T_SUM = 148;
const T_THEN = 149;
const T_TRAILING = 150;
const T_TRUE = 151;
const T_UPDATE = 152;
const T_WHEN = 153;
const T_WHERE = 154;
const T_WITH = 155;
const T_PARTIAL = 156;

/**
* Creates a new query scanner object.
Expand Down Expand Up @@ -205,4 +203,4 @@ protected function getType(&$value)

return $type;
}
}
}
21 changes: 15 additions & 6 deletions lib/Doctrine/ORM/Query/Parser.php
Expand Up @@ -416,9 +416,10 @@ public function semanticalError($message = '', $token = null)
/**
* Peek beyond the matched closing parenthesis and return the first token after that one.
*
* @param boolean $resetPeek Reset peek after finding the closing parenthesis
* @return array
*/
private function _peekBeyondClosingParenthesis()
private function _peekBeyondClosingParenthesis($resetPeek = true)
{
$token = $this->_lexer->peek();
$numUnmatched = 1;
Expand All @@ -440,7 +441,9 @@ private function _peekBeyondClosingParenthesis()
$token = $this->_lexer->peek();
}

$this->_lexer->resetPeek();
if ($resetPeek) {
$this->_lexer->resetPeek();
}

return $token;
}
Expand Down Expand Up @@ -541,7 +544,7 @@ private function _processDeferredPartialObjectExpressions()

foreach ($expr->partialFieldSet as $field) {
if (isset($class->fieldMappings[$field])) {
continue;
continue;
}

$this->semanticalError(
Expand Down Expand Up @@ -1335,7 +1338,7 @@ public function OrderByItem()
break;
case ($glimpse['type'] === Lexer::T_DOT):
$expr = $this->SingleValuedPathExpression();

break;
case ($this->_lexer->peek() && $this->_isMathOperator($this->_peekBeyondClosingParenthesis())):
$expr = $this->ScalarExpression();
Expand Down Expand Up @@ -2206,7 +2209,13 @@ public function SimpleConditionalExpression()
if ($peek['value'] == '(') {
// Peek beyond the matching closing paranthesis ')'
$this->_lexer->peek();
$token = $this->_peekBeyondClosingParenthesis();
$token = $this->_peekBeyondClosingParenthesis(false);

if ($token['type'] === Lexer::T_NOT) {
$token = $this->_lexer->peek();
}

$this->_lexer->resetPeek();
} else {
// Peek beyond the PathExpression (or InputParameter)
$peek = $this->_lexer->peek();
Expand Down Expand Up @@ -2658,7 +2667,7 @@ public function AggregateExpression()
? $this->SingleValuedPathExpression()
: $this->SimpleArithmeticExpression();

$this->match(Lexer::T_CLOSE_PARENTHESIS);
$this->match(Lexer::T_CLOSE_PARENTHESIS);

return new AST\AggregateExpression($functionName, $pathExp, $isDistinct);
}
Expand Down
42 changes: 42 additions & 0 deletions tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php
Expand Up @@ -374,6 +374,17 @@ public function testSupportsBetweenClauseWithPositionalParameters()
);
}

/**
* @group DDC-1802
*/
public function testSupportsNotBetweenForSizeFunction()
{
$this->assertSqlGeneration(
"SELECT m.name FROM Doctrine\Tests\Models\StockExchange\Market m WHERE SIZE(m.stocks) NOT BETWEEN ?1 AND ?2",
"SELECT e0_.name AS name0 FROM exchange_markets e0_ WHERE (SELECT COUNT(*) FROM exchange_stocks e1_ WHERE e1_.market_id = e0_.id) NOT BETWEEN ? AND ?"
);
}

public function testSupportsFunctionalExpressionsInWherePart()
{
$this->assertSqlGeneration(
Expand Down Expand Up @@ -472,6 +483,17 @@ public function testSupportsNotInExpressionInWherePart()
);
}

/**
* @group DDC-1802
*/
public function testSupportsNotInExpressionForModFunction()
{
$this->assertSqlGeneration(
"SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE MOD(u.id, 5) NOT IN(1,3,4)",
"SELECT c0_.name AS name0 FROM cms_users c0_ WHERE MOD(c0_.id, 5) NOT IN (1, 3, 4)"
);
}

public function testInExpressionWithSingleValuedAssociationPathExpressionInWherePart()
{
$this->assertSqlGeneration(
Expand Down Expand Up @@ -854,6 +876,26 @@ public function testStringFunctionLikeExpression()
);
}

/**
* @group DDC-1802
*/
public function testStringFunctionNotLikeExpression()
{
$this->assertSqlGeneration(
"SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE LOWER(u.name) NOT LIKE '%foo OR bar%'",
"SELECT c0_.name AS name0 FROM cms_users c0_ WHERE LOWER(c0_.name) NOT LIKE '%foo OR bar%'"
);

$this->assertSqlGeneration(
"SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE UPPER(LOWER(u.name)) NOT LIKE UPPER(LOWER(:str))",
"SELECT c0_.name AS name0 FROM cms_users c0_ WHERE UPPER(LOWER(c0_.name)) NOT LIKE UPPER(LOWER(?))"
);
$this->assertSqlGeneration(
"SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a WITH a.topic NOT LIKE u.name",
"SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ LEFT JOIN cms_articles c1_ ON c0_.id = c1_.user_id AND (c1_.topic NOT LIKE c0_.name)"
);
}

/**
* @group DDC-338
*/
Expand Down

0 comments on commit 6103db0

Please sign in to comment.