Skip to content

Commit

Permalink
Merge pull request #232 from iBiryukov/master
Browse files Browse the repository at this point in the history
Bug fix for SQL parameter replacing function.
  • Loading branch information
guilhermeblanco committed Jan 27, 2014
2 parents ef0f6f1 + 79dbe12 commit acbdcca
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
29 changes: 29 additions & 0 deletions Tests/Twig/DoctrineExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/*
* This file is part of the Doctrine Bundle
*
* The code was originally distributed inside the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
* (c) Doctrine Project, Benjamin Eberlei <kontakt@beberlei.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Doctrine\Bundle\DoctrineBundle\Tests\Twig;

use Doctrine\Bundle\DoctrineBundle\Twig\DoctrineExtension;

class DoctrineExtensionTest extends \PHPUnit_Framework_TestCase
{
public function testReplaceQueryParametersWithPostgresCasting()
{
$extension = new DoctrineExtension();
$query = "a=? OR (1)::string OR b=?";
$parameters = array(1, 2);

$result = $extension->replaceQueryParameters($query, $parameters, false);
$this->assertEquals("a=1 OR (1)::string OR b=2", $result);
}
}
13 changes: 8 additions & 5 deletions Twig/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,21 +278,22 @@ public static function escapeFunction($parameter)

return $result;
}

/**
* Return a query with the parameters replaced
*
* @param string $query
* @param array $parameters
* @param bool $highlight
*
* @return string
*/
public function replaceQueryParameters($query, $parameters)
public function replaceQueryParameters($query, $parameters, $highlight = true)
{
$i = 0;

$result = preg_replace_callback(
'/\?|(:[a-z0-9_]+)/i',
'/\?|((?<!:):[a-z0-9_]+)/i',
function ($matches) use ($parameters, &$i) {
$key = substr($matches[0], 1);
if (!array_key_exists($i, $parameters) && !array_key_exists($key, $parameters)) {
Expand All @@ -308,8 +309,10 @@ function ($matches) use ($parameters, &$i) {
$query
);

$result = \SqlFormatter::highlight($result);
$result = str_replace(array("<pre ", "</pre>"), array("<span ", "</span>"), $result);
if ($highlight) {
$result = \SqlFormatter::highlight($result);
$result = str_replace(array("<pre ", "</pre>"), array("<span ", "</span>"), $result);
}

return $result;
}
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"require-dev": {
"doctrine/orm": "~2.3",
"symfony/yaml": "~2.2",
"symfony/validator": "~2.2"
"symfony/validator": "~2.2",
"twig/twig" : "~1"
},
"suggest": {
"symfony/web-profiler-bundle": "to use the data collector",
Expand Down

0 comments on commit acbdcca

Please sign in to comment.