1
0
mirror of synced 2025-02-21 14:43:14 +03:00

Merge branch 'DDC-610'

* DDC-610:
  Query\Expr::_quoteLiteral properly quotes numeric strings
This commit is contained in:
David Abdemoulaie 2010-05-25 13:42:05 -05:00
commit de30ce5922
2 changed files with 31 additions and 22 deletions

View File

@ -37,7 +37,7 @@ class Expr
{ {
/** /**
* Creates a conjunction of the given boolean expressions. * Creates a conjunction of the given boolean expressions.
* *
* Example: * Example:
* *
* [php] * [php]
@ -55,7 +55,7 @@ class Expr
/** /**
* Creates a disjunction of the given boolean expressions. * Creates a disjunction of the given boolean expressions.
* *
* Example: * Example:
* *
* [php] * [php]
@ -70,10 +70,10 @@ class Expr
{ {
return new Expr\Orx(func_get_args()); return new Expr\Orx(func_get_args());
} }
/** /**
* Creates an ASCending order expression. * Creates an ASCending order expression.
* *
* @param $sort * @param $sort
* @return OrderBy * @return OrderBy
*/ */
@ -81,10 +81,10 @@ class Expr
{ {
return new Expr\OrderBy($expr, 'ASC'); return new Expr\OrderBy($expr, 'ASC');
} }
/** /**
* Creates a DESCending order expression. * Creates a DESCending order expression.
* *
* @param $sort * @param $sort
* @return OrderBy * @return OrderBy
*/ */
@ -95,7 +95,7 @@ class Expr
/** /**
* Creates an equality comparison expression with the given arguments. * Creates an equality comparison expression with the given arguments.
* *
* First argument is considered the left expression and the second is the right expression. * 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: * When converted to string, it will generated a <left expr> = <right expr>. Example:
* *
@ -325,7 +325,7 @@ class Expr
/** /**
* Creates a product mathematical expression with the given arguments. * Creates a product mathematical expression with the given arguments.
* *
* First argument is considered the left expression and the second is the right expression. * 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: * When converted to string, it will generated a <left expr> * <right expr>. Example:
* *
@ -518,16 +518,16 @@ class Expr
{ {
return new Expr\Literal($this->_quoteLiteral($literal)); return new Expr\Literal($this->_quoteLiteral($literal));
} }
/** /**
* Quotes a literal value, if necessary, according to the DQL syntax. * Quotes a literal value, if necessary, according to the DQL syntax.
* *
* @param mixed $literal The literal value. * @param mixed $literal The literal value.
* @return string * @return string
*/ */
private function _quoteLiteral($literal) private function _quoteLiteral($literal)
{ {
if (is_numeric($literal)) { if (is_numeric($literal) && !is_string($literal)) {
return (string) $literal; return (string) $literal;
} else { } else {
return "'" . str_replace("'", "''", $literal) . "'"; return "'" . str_replace("'", "''", $literal) . "'";
@ -557,4 +557,4 @@ class Expr
{ {
return new Expr\Func('TRIM', $x); return new Expr\Func('TRIM', $x);
} }
} }

View File

@ -27,7 +27,7 @@ use Doctrine\ORM\Query;
require_once __DIR__ . '/../../TestInit.php'; 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 * a programmatic interface
* *
* @author Jonathan H. Wage <jonwage@gmail.com> * @author Jonathan H. Wage <jonwage@gmail.com>
@ -75,7 +75,7 @@ class ExprTest extends \Doctrine\Tests\OrmTestCase
{ {
$qb = $this->_em->createQueryBuilder(); $qb = $this->_em->createQueryBuilder();
$qb->select('u')->from('User', 'u')->where('u.name = ?1'); $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)); $this->assertEquals('EXISTS(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->_expr->exists($qb));
} }
@ -83,7 +83,7 @@ class ExprTest extends \Doctrine\Tests\OrmTestCase
{ {
$qb = $this->_em->createQueryBuilder(); $qb = $this->_em->createQueryBuilder();
$qb->select('u')->from('User', 'u')->where('u.name = ?1'); $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)); $this->assertEquals('ALL(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->_expr->all($qb));
} }
@ -91,7 +91,7 @@ class ExprTest extends \Doctrine\Tests\OrmTestCase
{ {
$qb = $this->_em->createQueryBuilder(); $qb = $this->_em->createQueryBuilder();
$qb->select('u')->from('User', 'u')->where('u.name = ?1'); $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)); $this->assertEquals('SOME(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->_expr->some($qb));
} }
@ -99,7 +99,7 @@ class ExprTest extends \Doctrine\Tests\OrmTestCase
{ {
$qb = $this->_em->createQueryBuilder(); $qb = $this->_em->createQueryBuilder();
$qb->select('u')->from('User', 'u')->where('u.name = ?1'); $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)); $this->assertEquals('ANY(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->_expr->any($qb));
} }
@ -107,7 +107,7 @@ class ExprTest extends \Doctrine\Tests\OrmTestCase
{ {
$qb = $this->_em->createQueryBuilder(); $qb = $this->_em->createQueryBuilder();
$qb->select('u')->from('User', 'u')->where('u.name = ?1'); $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)); $this->assertEquals('NOT(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->_expr->not($qb));
} }
@ -115,11 +115,11 @@ class ExprTest extends \Doctrine\Tests\OrmTestCase
{ {
$this->assertEquals('(1 = 1) AND (2 = 2)', (string) $this->_expr->andx((string) $this->_expr->eq(1, 1), (string) $this->_expr->eq(2, 2))); $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() public function testIntelligentParenthesisPreventionAndExpr()
{ {
$this->assertEquals( $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)) (string) $this->_expr->andx($this->_expr->orx($this->_expr->andx($this->_expr->eq(1, 1))), (string) $this->_expr->eq(2, 2))
); );
} }
@ -153,7 +153,7 @@ class ExprTest extends \Doctrine\Tests\OrmTestCase
{ {
$this->assertEquals('10 / 2', (string) $this->_expr->quot(10, 2)); $this->assertEquals('10 / 2', (string) $this->_expr->quot(10, 2));
} }
public function testScopeInArithmeticExpr() public function testScopeInArithmeticExpr()
{ {
$this->assertEquals('(100 - 20) / 2', (string) $this->_expr->quot($this->_expr->diff(100, 20), 2)); $this->assertEquals('(100 - 20) / 2', (string) $this->_expr->quot($this->_expr->diff(100, 20), 2));
@ -220,6 +220,15 @@ class ExprTest extends \Doctrine\Tests\OrmTestCase
$this->assertEquals(5, (string) $this->_expr->literal(5)); $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() public function testGreaterThanOrEqualToExpr()
{ {
$this->assertEquals('5 >= 2', (string) $this->_expr->gte(5, 2)); $this->assertEquals('5 >= 2', (string) $this->_expr->gte(5, 2));
@ -266,7 +275,7 @@ class ExprTest extends \Doctrine\Tests\OrmTestCase
$this->assertEquals('(1 = 1) OR (1 < 5)', (string) $orExpr); $this->assertEquals('(1 = 1) OR (1 < 5)', (string) $orExpr);
} }
public function testOrderByCountExpr() public function testOrderByCountExpr()
{ {
$orderExpr = $this->_expr->desc('u.username'); $orderExpr = $this->_expr->desc('u.username');