Skip to content

Commit

Permalink
Query\Expr::_quoteLiteral properly quotes numeric strings
Browse files Browse the repository at this point in the history
  • Loading branch information
hobodave committed May 25, 2010
1 parent 2db8ad3 commit b6a5402
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
24 changes: 12 additions & 12 deletions lib/Doctrine/ORM/Query/Expr.php
Expand Up @@ -37,7 +37,7 @@ class Expr
{
/**
* Creates a conjunction of the given boolean expressions.
*
*
* Example:
*
* [php]
Expand All @@ -55,7 +55,7 @@ public function andX($x = null)

/**
* Creates a disjunction of the given boolean expressions.
*
*
* Example:
*
* [php]
Expand All @@ -70,21 +70,21 @@ public function orX($x = null)
{
return new Expr\Orx(func_get_args());
}

/**
* Creates an ASCending order expression.
*
*
* @param $sort
* @return OrderBy
*/
public function asc($expr)
{
return new Expr\OrderBy($expr, 'ASC');
}

/**
* Creates a DESCending order expression.
*
*
* @param $sort
* @return OrderBy
*/
Expand All @@ -95,7 +95,7 @@ public function desc($expr)

/**
* Creates an equality comparison expression with the given arguments.
*
*
* First argument is considered the left expression and the second is the right expression.
* When converted to string, it will generated a <left expr> = <right expr>. Example:
*
Expand Down Expand Up @@ -325,7 +325,7 @@ public function abs($x)

/**
* Creates a product mathematical expression with the given arguments.
*
*
* First argument is considered the left expression and the second is the right expression.
* When converted to string, it will generated a <left expr> * <right expr>. Example:
*
Expand Down Expand Up @@ -518,16 +518,16 @@ public function literal($literal)
{
return new Expr\Literal($this->_quoteLiteral($literal));
}

/**
* Quotes a literal value, if necessary, according to the DQL syntax.
*
*
* @param mixed $literal The literal value.
* @return string
*/
private function _quoteLiteral($literal)
{
if (is_numeric($literal)) {
if (is_numeric($literal) && !is_string($literal)) {
return (string) $literal;
} else {
return "'" . str_replace("'", "''", $literal) . "'";
Expand Down Expand Up @@ -557,4 +557,4 @@ public function trim($x)
{
return new Expr\Func('TRIM', $x);
}
}
}
29 changes: 19 additions & 10 deletions tests/Doctrine/Tests/ORM/Query/ExprTest.php
Expand Up @@ -27,7 +27,7 @@
require_once __DIR__ . '/../../TestInit.php';

/**
* Test case for the DQL Expr class used for generating DQL snippets through
* Test case for the DQL Expr class used for generating DQL snippets through
* a programmatic interface
*
* @author Jonathan H. Wage <jonwage@gmail.com>
Expand Down Expand Up @@ -75,51 +75,51 @@ public function testExistsExpr()
{
$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) $this->_expr->exists($qb));
}

public function testAllExpr()
{
$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) $this->_expr->all($qb));
}

public function testSomeExpr()
{
$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) $this->_expr->some($qb));
}

public function testAnyExpr()
{
$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) $this->_expr->any($qb));
}

public function testNotExpr()
{
$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) $this->_expr->not($qb));
}

public function testAndExpr()
{
$this->assertEquals('(1 = 1) AND (2 = 2)', (string) $this->_expr->andx((string) $this->_expr->eq(1, 1), (string) $this->_expr->eq(2, 2)));
}

public function testIntelligentParenthesisPreventionAndExpr()
{
$this->assertEquals(
'(1 = 1) AND (2 = 2)',
'(1 = 1) AND (2 = 2)',
(string) $this->_expr->andx($this->_expr->orx($this->_expr->andx($this->_expr->eq(1, 1))), (string) $this->_expr->eq(2, 2))
);
}
Expand Down Expand Up @@ -153,7 +153,7 @@ public function testQuotientExpr()
{
$this->assertEquals('10 / 2', (string) $this->_expr->quot(10, 2));
}

public function testScopeInArithmeticExpr()
{
$this->assertEquals('(100 - 20) / 2', (string) $this->_expr->quot($this->_expr->diff(100, 20), 2));
Expand Down Expand Up @@ -220,6 +220,15 @@ public function testNumericLiteralExpr()
$this->assertEquals(5, (string) $this->_expr->literal(5));
}

/**
* @group regression
* @group DDC-610
*/
public function testLiteralExprProperlyQuotesStrings()
{
$this->assertEquals("'00010001'", (string) $this->_expr->literal('00010001'));
}

public function testGreaterThanOrEqualToExpr()
{
$this->assertEquals('5 >= 2', (string) $this->_expr->gte(5, 2));
Expand Down Expand Up @@ -266,7 +275,7 @@ public function testOrxExpr()

$this->assertEquals('(1 = 1) OR (1 < 5)', (string) $orExpr);
}

public function testOrderByCountExpr()
{
$orderExpr = $this->_expr->desc('u.username');
Expand Down

0 comments on commit b6a5402

Please sign in to comment.