Permalink
Browse files

DDC-1696 Allowed Strings to be used inside of Case expressions. It se…

…ems this fixes other misterious use cases not yet identified by us.
  • Loading branch information...
1 parent 0a78f7b commit 795438670517cd7a4e76bca6dd33fdc9bbb5d988 @guilhermeblanco guilhermeblanco committed Mar 12, 2012
@@ -30,6 +30,7 @@
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
* @since 2.0
+ * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
class DateAddFunction extends FunctionNode
@@ -40,19 +41,23 @@ class DateAddFunction extends FunctionNode
public function getSql(SqlWalker $sqlWalker)
{
- $unit = strtolower($this->unit);
- if ($unit == "day") {
- return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddDaysExpression(
- $this->firstDateExpression->dispatch($sqlWalker),
- $this->intervalExpression->dispatch($sqlWalker)
- );
- } else if ($unit == "month") {
- return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddMonthExpression(
- $this->firstDateExpression->dispatch($sqlWalker),
- $this->intervalExpression->dispatch($sqlWalker)
- );
- } else {
- throw QueryException::semanticalError('DATE_ADD() only supports units of type day and month.');
+ switch (strtolower($this->unit->value)) {
+ case 'day':
+ return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddDaysExpression(
+ $this->firstDateExpression->dispatch($sqlWalker),
+ $this->intervalExpression->dispatch($sqlWalker)
+ );
+
+ case 'month':
+ return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddMonthExpression(
+ $this->firstDateExpression->dispatch($sqlWalker),
+ $this->intervalExpression->dispatch($sqlWalker)
+ );
+
+ default:
+ throw QueryException::semanticalError(
+ 'DATE_ADD() only supports units of type day and month.'
+ );
}
}
@@ -66,6 +71,7 @@ public function parse(Parser $parser)
$this->intervalExpression = $parser->ArithmeticPrimary();
$parser->match(Lexer::T_COMMA);
$this->unit = $parser->StringPrimary();
+
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
}
@@ -30,29 +30,30 @@
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
* @since 2.0
+ * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
class DateSubFunction extends DateAddFunction
{
- public $firstDateExpression = null;
- public $intervalExpression = null;
- public $unit = null;
-
public function getSql(SqlWalker $sqlWalker)
{
- $unit = strtolower($this->unit);
- if ($unit == "day") {
- return $sqlWalker->getConnection()->getDatabasePlatform()->getDateSubDaysExpression(
- $this->firstDateExpression->dispatch($sqlWalker),
- $this->intervalExpression->dispatch($sqlWalker)
- );
- } else if ($unit == "month") {
- return $sqlWalker->getConnection()->getDatabasePlatform()->getDateSubMonthExpression(
- $this->firstDateExpression->dispatch($sqlWalker),
- $this->intervalExpression->dispatch($sqlWalker)
- );
- } else {
- throw QueryException::semanticalError('DATE_SUB() only supports units of type day and month.');
+ switch (strtolower($this->unit->value)) {
+ case 'day':
+ return $sqlWalker->getConnection()->getDatabasePlatform()->getDateSubDaysExpression(
+ $this->firstDateExpression->dispatch($sqlWalker),
+ $this->intervalExpression->dispatch($sqlWalker)
+ );
+
+ case 'month':
+ return $sqlWalker->getConnection()->getDatabasePlatform()->getDateSubMonthExpression(
+ $this->firstDateExpression->dispatch($sqlWalker),
+ $this->intervalExpression->dispatch($sqlWalker)
+ );
+
+ default:
+ throw QueryException::semanticalError(
+ 'DATE_SUB() only supports units of type day and month.'
+ );
}
}
}
@@ -2574,7 +2574,7 @@ public function StringPrimary()
case Lexer::T_STRING:
$this->match(Lexer::T_STRING);
- return $this->_lexer->token['value'];
+ return new AST\Literal(AST\Literal::STRING, $this->_lexer->token['value']);
case Lexer::T_INPUT_PARAMETER:
return $this->InputParameter();
@@ -2854,7 +2854,8 @@ public function LikeExpression()
if ($this->_lexer->lookahead['type'] === Lexer::T_ESCAPE) {
$this->match(Lexer::T_ESCAPE);
$this->match(Lexer::T_STRING);
- $escapeChar = $this->_lexer->token['value'];
+
+ $escapeChar = new AST\Literal(AST\Literal::STRING, $this->_lexer->token['value']);
}
$likeExpr = new AST\LikeExpression($stringExpr, $stringPattern, $escapeChar);
@@ -43,7 +43,7 @@ class SqlWalker implements TreeWalker
* @var string
*/
const HINT_DISTINCT = 'doctrine.distinct';
-
+
/**
* @var ResultSetMapping
*/
@@ -1979,11 +1979,11 @@ public function walkLikeExpression($likeExpr)
} elseif ($likeExpr->stringPattern instanceof AST\PathExpression) {
$sql .= $this->walkPathExpression($likeExpr->stringPattern);
} else {
- $sql .= $this->_conn->quote($likeExpr->stringPattern);
+ $sql .= $this->walkLiteral($likeExpr->stringPattern);
}
if ($likeExpr->escapeChar) {
- $sql .= ' ESCAPE ' . $this->_conn->quote($likeExpr->escapeChar);
+ $sql .= ' ESCAPE ' . $this->walkLiteral($likeExpr->escapeChar);
}
return $sql;
@@ -1112,15 +1112,15 @@ public function testSimpleCaseWithSingleWhenClause()
{
$this->assertSqlGeneration(
"SELECT g FROM Doctrine\Tests\Models\CMS\CmsGroup g WHERE g.id = CASE g.name WHEN 'admin' THEN 1 ELSE 2 END",
- "SELECT c0_.id AS id0, c0_.name AS name1 FROM cms_groups c0_ WHERE c0_.id = CASE c0_.name WHEN admin THEN 1 ELSE 2 END"
+ "SELECT c0_.id AS id0, c0_.name AS name1 FROM cms_groups c0_ WHERE c0_.id = CASE c0_.name WHEN 'admin' THEN 1 ELSE 2 END"
);
}
public function testSimpleCaseWithMultipleWhenClause()
{
$this->assertSqlGeneration(
"SELECT g FROM Doctrine\Tests\Models\CMS\CmsGroup g WHERE g.id = (CASE g.name WHEN 'admin' THEN 1 WHEN 'moderator' THEN 2 ELSE 3 END)",
- "SELECT c0_.id AS id0, c0_.name AS name1 FROM cms_groups c0_ WHERE c0_.id = CASE c0_.name WHEN admin THEN 1 WHEN moderator THEN 2 ELSE 3 END"
+ "SELECT c0_.id AS id0, c0_.name AS name1 FROM cms_groups c0_ WHERE c0_.id = CASE c0_.name WHEN 'admin' THEN 1 WHEN 'moderator' THEN 2 ELSE 3 END"
);
}
@@ -1144,15 +1144,26 @@ public function testSimpleCaseWithSingleWhenClauseInSubselect()
{
$this->assertSqlGeneration(
"SELECT g FROM Doctrine\Tests\Models\CMS\CmsGroup g WHERE g.id IN (SELECT CASE g2.name WHEN 'admin' THEN 1 ELSE 2 END FROM Doctrine\Tests\Models\CMS\CmsGroup g2)",
- "SELECT c0_.id AS id0, c0_.name AS name1 FROM cms_groups c0_ WHERE c0_.id IN (SELECT CASE c1_.name WHEN admin THEN 1 ELSE 2 END AS sclr2 FROM cms_groups c1_)"
+ "SELECT c0_.id AS id0, c0_.name AS name1 FROM cms_groups c0_ WHERE c0_.id IN (SELECT CASE c1_.name WHEN 'admin' THEN 1 ELSE 2 END AS sclr2 FROM cms_groups c1_)"
);
}
public function testSimpleCaseWithMultipleWhenClauseInSubselect()
{
$this->assertSqlGeneration(
"SELECT g FROM Doctrine\Tests\Models\CMS\CmsGroup g WHERE g.id IN (SELECT CASE g2.name WHEN 'admin' THEN 1 WHEN 'moderator' THEN 2 ELSE 3 END FROM Doctrine\Tests\Models\CMS\CmsGroup g2)",
- "SELECT c0_.id AS id0, c0_.name AS name1 FROM cms_groups c0_ WHERE c0_.id IN (SELECT CASE c1_.name WHEN admin THEN 1 WHEN moderator THEN 2 ELSE 3 END AS sclr2 FROM cms_groups c1_)"
+ "SELECT c0_.id AS id0, c0_.name AS name1 FROM cms_groups c0_ WHERE c0_.id IN (SELECT CASE c1_.name WHEN 'admin' THEN 1 WHEN 'moderator' THEN 2 ELSE 3 END AS sclr2 FROM cms_groups c1_)"
+ );
+ }
+
+ /**
+ * @group DDC-1696
+ */
+ public function testSimpleCaseWithStringPrimary()
+ {
+ $this->assertSqlGeneration(
+ "SELECT g.id, CASE WHEN ((g.id / 2) > 18) THEN 'Foo' ELSE 'Bar' END AS test FROM Doctrine\Tests\Models\CMS\CmsGroup g",
+ "SELECT c0_.id AS id0, CASE WHEN (c0_.id / 2 > 18) THEN 'Foo' ELSE 'Bar' END AS sclr1 FROM cms_groups c0_"
);
}

0 comments on commit 7954386

Please sign in to comment.