Skip to content

Commit 194a484

Browse files
authored
Merge pull request #5117 from kenjis/fix-5114
Fix query binding with two colons in query
2 parents c29aaff + 2277e6d commit 194a484

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

system/Database/Query.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,17 +269,19 @@ public function getOriginalQuery(): string
269269
/**
270270
* Escapes and inserts any binds into the finalQueryString object.
271271
*
272-
* @see https://regex101.com/r/EUEhay/4
272+
* @see https://regex101.com/r/EUEhay/5
273273
*/
274274
protected function compileBinds()
275275
{
276276
$sql = $this->finalQueryString;
277277

278-
$hasNamedBinds = preg_match('/:((?!=).+):/', $sql) === 1;
278+
$hasBinds = strpos($sql, $this->bindMarker) !== false;
279+
$hasNamedBinds = ! $hasBinds
280+
&& preg_match('/:(?!=).+:/', $sql) === 1;
279281

280282
if (empty($this->binds)
281283
|| empty($this->bindMarker)
282-
|| (! $hasNamedBinds && strpos($sql, $this->bindMarker) === false)
284+
|| (! $hasNamedBinds && ! $hasBinds)
283285
) {
284286
return;
285287
}

tests/system/Database/BaseQueryTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,23 @@ public function testBindingAutoEscapesParameters()
238238
$this->assertSame($expected, $query->getQuery());
239239
}
240240

241+
/**
242+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5114
243+
*/
244+
public function testBindingWithTwoColons()
245+
{
246+
$query = new Query($this->db);
247+
248+
$query->setQuery(
249+
"SELECT mytable.id, DATE_FORMAT(mytable.created_at,'%d/%m/%Y %H:%i:%s') AS created_at_uk FROM mytable WHERE mytable.id = ?",
250+
[1]
251+
);
252+
253+
$expected = "SELECT mytable.id, DATE_FORMAT(mytable.created_at,'%d/%m/%Y %H:%i:%s') AS created_at_uk FROM mytable WHERE mytable.id = 1";
254+
255+
$this->assertSame($expected, $query->getQuery());
256+
}
257+
241258
public function testNamedBinds()
242259
{
243260
$query = new Query($this->db);

0 commit comments

Comments
 (0)