From 4048ff47eddb78d21bfd2da7dbeb975e25dcbfa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thiago=20Guimar=C3=A3es?= Date: Sat, 19 May 2018 14:41:13 -0300 Subject: [PATCH] Bug fix repead params sql (#387) * Bug fix repead params sql * Fix array * Fix array --- .../DataCollector/PDO/TracedStatement.php | 5 +-- tests/DebugBar/Tests/TracedStatementTest.php | 36 +++++++++++++++++-- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/DebugBar/DataCollector/PDO/TracedStatement.php b/src/DebugBar/DataCollector/PDO/TracedStatement.php index 8f71ba5..96070cc 100644 --- a/src/DebugBar/DataCollector/PDO/TracedStatement.php +++ b/src/DebugBar/DataCollector/PDO/TracedStatement.php @@ -123,8 +123,9 @@ class TracedStatement } $matchRule = "/({$marker}(?!\w))(?=(?:[^$quotationChar]|[$quotationChar][^$quotationChar]*[$quotationChar])*$)/"; - - $sql = preg_replace($matchRule, $v, $sql, 1); + for ($i = 0; $i <= mb_substr_count($sql, $k); $i++) { + $sql = preg_replace($matchRule, $v, $sql, 1); + } } $sql = strtr($sql, array_flip($cleanBackRefCharMap)); diff --git a/tests/DebugBar/Tests/TracedStatementTest.php b/tests/DebugBar/Tests/TracedStatementTest.php index 3c482bd..a4da7ef 100644 --- a/tests/DebugBar/Tests/TracedStatementTest.php +++ b/tests/DebugBar/Tests/TracedStatementTest.php @@ -59,7 +59,7 @@ class TracedStatementTest extends DebugBarTestCase public function testReplacementParamsContainingPotentialAdditionalQuestionMarkPlaceholderGeneratesCorrectString() { $hasQuestionMark = "Asking a question?"; - $string = "Asking for a friend"; + $string = "Asking for a friend"; $sql = "INSERT INTO questions SET question = ?, detail = ?"; @@ -89,7 +89,7 @@ class TracedStatementTest extends DebugBarTestCase public function testReplacementParamsContainingPotentialAdditionalNamedPlaceholderGeneratesCorrectString() { $hasQuestionMark = "Asking a question with a :string inside"; - $string = "Asking for a friend"; + $string = "Asking for a friend"; $sql = "INSERT INTO questions SET question = :question, detail = :string"; @@ -118,4 +118,36 @@ class TracedStatementTest extends DebugBarTestCase $this->assertEquals($expected, $result); } + + /** + * Check if query parameters are being replaced in the correct way + * @bugFix Before fix it : select * + * from geral.person p + * left join geral.contract c + * on c.id_person = p.id_person + * where c.status = <1> and + * p.status <> :status; + * @return void + */ + public function testRepeadParamsQuery() + { + $sql = 'select * + from geral.person p + left join geral.contract c + on c.id_person = p.id_person + where c.status = :status and + p.status <> :status'; + $params = array( + ':status' => 1 + ); + $traced = new TracedStatement($sql, $params); + $expected = 'select * + from geral.person p + left join geral.contract c + on c.id_person = p.id_person + where c.status = <1> and + p.status <> <1>'; + $result = $traced->getSqlWithParams(); + $this->assertEquals($expected, $result); + } }