Skip to content

Commit 4048ff4

Browse files
webmailcontatosbarryvdh
authored andcommitted
Bug fix repead params sql (php-debugbar#387)
* Bug fix repead params sql * Fix array * Fix array
1 parent 7702b86 commit 4048ff4

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/DebugBar/DataCollector/PDO/TracedStatement.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@ public function getSqlWithParams($quotationChar = '<>')
123123
}
124124

125125
$matchRule = "/({$marker}(?!\w))(?=(?:[^$quotationChar]|[$quotationChar][^$quotationChar]*[$quotationChar])*$)/";
126-
127-
$sql = preg_replace($matchRule, $v, $sql, 1);
126+
for ($i = 0; $i <= mb_substr_count($sql, $k); $i++) {
127+
$sql = preg_replace($matchRule, $v, $sql, 1);
128+
}
128129
}
129130

130131
$sql = strtr($sql, array_flip($cleanBackRefCharMap));

tests/DebugBar/Tests/TracedStatementTest.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function testReplacementParamsContainingBackReferenceSyntaxGeneratesCorre
5959
public function testReplacementParamsContainingPotentialAdditionalQuestionMarkPlaceholderGeneratesCorrectString()
6060
{
6161
$hasQuestionMark = "Asking a question?";
62-
$string = "Asking for a friend";
62+
$string = "Asking for a friend";
6363

6464
$sql = "INSERT INTO questions SET question = ?, detail = ?";
6565

@@ -89,7 +89,7 @@ public function testReplacementParamsContainingPotentialAdditionalQuestionMarkPl
8989
public function testReplacementParamsContainingPotentialAdditionalNamedPlaceholderGeneratesCorrectString()
9090
{
9191
$hasQuestionMark = "Asking a question with a :string inside";
92-
$string = "Asking for a friend";
92+
$string = "Asking for a friend";
9393

9494
$sql = "INSERT INTO questions SET question = :question, detail = :string";
9595

@@ -118,4 +118,36 @@ public function testReplacementParamsContainingPotentialAdditionalNamedPlacehold
118118

119119
$this->assertEquals($expected, $result);
120120
}
121+
122+
/**
123+
* Check if query parameters are being replaced in the correct way
124+
* @bugFix Before fix it : select *
125+
* from geral.person p
126+
* left join geral.contract c
127+
* on c.id_person = p.id_person
128+
* where c.status = <1> and
129+
* p.status <> :status;
130+
* @return void
131+
*/
132+
public function testRepeadParamsQuery()
133+
{
134+
$sql = 'select *
135+
from geral.person p
136+
left join geral.contract c
137+
on c.id_person = p.id_person
138+
where c.status = :status and
139+
p.status <> :status';
140+
$params = array(
141+
':status' => 1
142+
);
143+
$traced = new TracedStatement($sql, $params);
144+
$expected = 'select *
145+
from geral.person p
146+
left join geral.contract c
147+
on c.id_person = p.id_person
148+
where c.status = <1> and
149+
p.status <> <1>';
150+
$result = $traced->getSqlWithParams();
151+
$this->assertEquals($expected, $result);
152+
}
121153
}

0 commit comments

Comments
 (0)