Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix query binding with two colons in query #5117

Merged
merged 4 commits into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions system/Database/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,17 +269,19 @@ public function getOriginalQuery(): string
/**
* Escapes and inserts any binds into the finalQueryString object.
*
* @see https://regex101.com/r/EUEhay/4
* @see https://regex101.com/r/EUEhay/5
*/
protected function compileBinds()
{
$sql = $this->finalQueryString;

$hasNamedBinds = preg_match('/:((?!=).+):/', $sql) === 1;
$hasBinds = strpos($sql, $this->bindMarker) !== false;
$hasNamedBinds = ! $hasBinds
&& preg_match('/:(?!=).+:/', $sql) === 1;
samsonasik marked this conversation as resolved.
Show resolved Hide resolved

if (empty($this->binds)
|| empty($this->bindMarker)
|| (! $hasNamedBinds && strpos($sql, $this->bindMarker) === false)
|| (! $hasNamedBinds && ! $hasBinds)
) {
return;
}
Expand Down
17 changes: 17 additions & 0 deletions tests/system/Database/BaseQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,23 @@ public function testBindingAutoEscapesParameters()
$this->assertSame($expected, $query->getQuery());
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5114
*/
public function testBindingWithTwoColons()
{
$query = new Query($this->db);

$query->setQuery(
"SELECT mytable.id, DATE_FORMAT(mytable.created_at,'%d/%m/%Y %H:%i:%s') AS created_at_uk FROM mytable WHERE mytable.id = ?",
[1]
);

$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";

$this->assertSame($expected, $query->getQuery());
}

public function testNamedBinds()
{
$query = new Query($this->db);
Expand Down