Skip to content

Commit fd43dfb

Browse files
authored
Fix random instances of SQL queries having params repeated (php-debugbar#505)
* Fix random instances of SQL queries having params repeated * Add test
1 parent 4458e2b commit fd43dfb

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/DebugBar/DataCollector/PDO/TracedStatement.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,11 @@ public function getSqlWithParams($quotationChar = '<>')
123123
}
124124

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

tests/DebugBar/Tests/TracedStatementTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,25 @@ public function testRepeadParamsQuery()
150150
$result = $traced->getSqlWithParams();
151151
$this->assertEquals($expected, $result);
152152
}
153+
154+
/**
155+
* Check that query parameters are being replaced only once
156+
* @bugFix Before fix it: select * from
157+
* `my_table` where `my_field` between
158+
* <2018-01-01> and <2018-01-01>
159+
* @return void
160+
*/
161+
public function testParametersAreNotRepeated()
162+
{
163+
$query = 'select * from `my_table` where `my_field` between ? and ?';
164+
$bindings = [
165+
'2018-01-01',
166+
'2020-09-01',
167+
];
168+
169+
$this->assertEquals(
170+
'select * from `my_table` where `my_field` between <2018-01-01> and <2020-09-01>',
171+
(new TracedStatement($query, $bindings))->getSqlWithParams()
172+
);
173+
}
153174
}

0 commit comments

Comments
 (0)