Skip to content

Commit acbdcca

Browse files
Merge pull request #232 from iBiryukov/master
Bug fix for SQL parameter replacing function.
2 parents ef0f6f1 + 79dbe12 commit acbdcca

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

Tests/Twig/DoctrineExtensionTest.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/*
3+
* This file is part of the Doctrine Bundle
4+
*
5+
* The code was originally distributed inside the Symfony framework.
6+
*
7+
* (c) Fabien Potencier <fabien@symfony.com>
8+
* (c) Doctrine Project, Benjamin Eberlei <kontakt@beberlei.de>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Doctrine\Bundle\DoctrineBundle\Tests\Twig;
15+
16+
use Doctrine\Bundle\DoctrineBundle\Twig\DoctrineExtension;
17+
18+
class DoctrineExtensionTest extends \PHPUnit_Framework_TestCase
19+
{
20+
public function testReplaceQueryParametersWithPostgresCasting()
21+
{
22+
$extension = new DoctrineExtension();
23+
$query = "a=? OR (1)::string OR b=?";
24+
$parameters = array(1, 2);
25+
26+
$result = $extension->replaceQueryParameters($query, $parameters, false);
27+
$this->assertEquals("a=1 OR (1)::string OR b=2", $result);
28+
}
29+
}

Twig/DoctrineExtension.php

+8-5
Original file line numberDiff line numberDiff line change
@@ -278,21 +278,22 @@ public static function escapeFunction($parameter)
278278

279279
return $result;
280280
}
281-
281+
282282
/**
283283
* Return a query with the parameters replaced
284284
*
285285
* @param string $query
286286
* @param array $parameters
287+
* @param bool $highlight
287288
*
288289
* @return string
289290
*/
290-
public function replaceQueryParameters($query, $parameters)
291+
public function replaceQueryParameters($query, $parameters, $highlight = true)
291292
{
292293
$i = 0;
293294

294295
$result = preg_replace_callback(
295-
'/\?|(:[a-z0-9_]+)/i',
296+
'/\?|((?<!:):[a-z0-9_]+)/i',
296297
function ($matches) use ($parameters, &$i) {
297298
$key = substr($matches[0], 1);
298299
if (!array_key_exists($i, $parameters) && !array_key_exists($key, $parameters)) {
@@ -308,8 +309,10 @@ function ($matches) use ($parameters, &$i) {
308309
$query
309310
);
310311

311-
$result = \SqlFormatter::highlight($result);
312-
$result = str_replace(array("<pre ", "</pre>"), array("<span ", "</span>"), $result);
312+
if ($highlight) {
313+
$result = \SqlFormatter::highlight($result);
314+
$result = str_replace(array("<pre ", "</pre>"), array("<span ", "</span>"), $result);
315+
}
313316

314317
return $result;
315318
}

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"require-dev": {
3030
"doctrine/orm": "~2.3",
3131
"symfony/yaml": "~2.2",
32-
"symfony/validator": "~2.2"
32+
"symfony/validator": "~2.2",
33+
"twig/twig" : "~1"
3334
},
3435
"suggest": {
3536
"symfony/web-profiler-bundle": "to use the data collector",

0 commit comments

Comments
 (0)